提取 Logistic Regression 的参数
现在,您将练习提取逻辑回归模型中的一个重要参数。逻辑回归还有一些其他参数,此处不展开,您可以在 scikit-learn.org 上 LogisticRegression() 模块的 "Attributes" 中查看。
这个参数有助于理解各变量对目标的影响方向和影响力度。
在本练习中,我们将提取系数参数(位于 coef_ 属性),把它与原始列名配对,然后查看哪些变量对目标变量具有最大的正向影响。
您将可以使用:
- 名为
log_reg_clf的逻辑回归模型对象 X_trainDataFrame
已为您导入 sklearn 和 pandas。
本练习是课程的一部分
Python 中的超参数调优
练习说明
- 创建训练用 DataFrame 的原始列名列表。
- 提取逻辑回归估计器的系数。
- 创建包含系数与变量名的 DataFrame,并查看它。
- 根据系数大小打印出正向影响最大的 3 个变量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a list of original variable names from the training DataFrame
original_variables = ____
# Extract the coefficients of the logistic regression estimator
model_coefficients = ____.____[____]
# Create a dataframe of the variables and coefficients & print it out
coefficient_df = pd.DataFrame({"Variable" : ____, "Coefficient": ____})
print(coefficient_df)
# Print out the top 3 positive variables
top_three_df = coefficient_df.sort_values(by=____, axis=0, ascending=____)[0:____]
print(top_three_df)