探索逻辑回归系数
现在,您将探索逻辑回归的系数,以了解哪些因素会推动流失率上升或下降。在本练习中,您将从已拟合的模型中提取逻辑回归系数,并计算其指数,使结果更易解释。
已拟合的逻辑回归实例加载为 logreg,标准化后的特征作为名为 train_X 的 pandas DataFrame 加载。numpy 和 pandas 库分别以 np 和 pd 的名称加载。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 将特征名和系数合并为一个
pandasDataFrame。 - 计算逻辑回归系数的指数。
- 移除等于 0 的系数,并按指数系数排序后打印。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Combine feature names and coefficients into pandas DataFrame
feature_names = pd.DataFrame(___.columns, columns = ['Feature'])
log_coef = pd.DataFrame(np.transpose(logreg.coef_), columns = ['Coefficient'])
coefficients = pd.concat([feature_names, ___], axis = 1)
# Calculate exponent of the logistic regression coefficients
coefficients['Exp_Coefficient'] = np.___(coefficients['Coefficient'])
# Remove coefficients that are equal to zero
coefficients = coefficients[coefficients['Coefficient']!=___]
# Print the values sorted by the exponent coefficient
print(coefficients.sort_values(by=['___']))