显示系数和截距
当逻辑回归模型准备就绪后,查看各系数有助于判断模型是否合理。
对于已拟合的逻辑回归模型 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)