尝试使用以下代码绘制逻辑回归的 L2 正则化路径(正则化路径示例可在 ML 教科书 Elements of Statistical Learning https://web.stanford.edu/~hastie/Papers/ESLII的第 65 页找到.pdf)。有一种感觉,我正在以愚蠢的方式做这件事 - 认为有一种更简单,更优雅的编码方式 - 非常感谢建议。
counter = 0
for c in np.arange(-10, 2, dtype=np.float):
lr = LogisticRegression(C = 10**c,
fit_intercept=True,
solver = 'liblinear',
penalty = 'l2',
tol = 0.0001,
n_jobs = -1,
verbose = -1,
random_state = 0
)
model=lr.fit(X_train_z, y_train)
coeff_list=model.coef_.ravel()
if counter == 0:
coeff_table = pd.DataFrame(pd.Series(coeff_list,index=X_train.columns),columns=[10**c])
else:
temp_table = pd.DataFrame(pd.Series(coeff_list,index=X_train.columns),columns=[10**c])
coeff_table = coeff_table.join(temp_table,how='left')
counter += 1
plt.rcParams["figure.figsize"] = (20,10)
coeff_table.transpose().iloc[:,:10].plot()
plt.ylabel('weight coefficient')
plt.xlabel('C')
plt.legend(loc='right')
plt.xscale('log')
plt.show()