開始使用免費開始

顯示係數與截距

當邏輯斯迴歸模型就緒後,查看各係數可以幫助你判斷模型是否合理。

對於已擬合完成的邏輯斯迴歸模型 logreg,你可以用屬性 coef_ 取得係數。係數出現的順序與你將變數餵入模型的順序相同。截距可以用屬性 intercept_ 取得。

你在前面練習中建立的邏輯斯迴歸模型已替你加入並擬合到 logreg

本練習屬於課程

Python 預測分析入門

檢視課程

練習說明

  • 將邏輯斯迴歸模型的係數指定給清單 coef
  • 將邏輯斯迴歸模型的截距指定給變數 intercept

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Construct a logistic regression model that predicts the target using age, gender_F and time_since_last gift
predictors = ["age","gender_F","time_since_last_gift"]
X = basetable[predictors]
y = basetable[["target"]]
logreg = linear_model.LogisticRegression()
logreg.fit(X, y)

# Assign the coefficients to a list coef
coef = ____.____
for p,c in zip(predictors,list(coef[0])):
    print(p + '\t' + str(c))
    
# Assign the intercept to the variable intercept
intercept = ____.____
print(intercept)
編輯並執行程式碼