coef1 = pd.DataFrame() for alpha in np.linspace(0.0001,0.2,20): lasso_clf = Lasso(alpha=alpha) lasso_clf.fit(X_train[features_without_ones],y_train) df = pd.DataFrame([lasso_clf.coef_],columns=X_train[features_without_ones].columns) df['alpha'] = alpha coef1 = pd.concat([coef1,df],ignore_index=True) coef1.head() plt.figure(figure=(9,6),dpi=600) for feature in X_train.columns[:-1]: plt.plot('alpha',feature,data=coef1) plt.legend(loc='upper right') plt.xlabel(r'$\alpha$',fontsize=15) plt.ylabel("coefficient",fontsize=15) plt.show() RuntimeError: Can not put single artist in more than one figure Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
时间: 2023-06-19 08:05:18 浏览: 132
LR_coef.zip_lr.coef_\_python_python特征选择_特征选择_逻辑回归 coef
这个错误是因为您尝试将单个艺术家(Artist)放置到多个Figure中。您需要在循环外面创建一个Figure,然后在循环中使用subplot来绘制每个特征的系数。以下是修改后的代码:
```
coef1 = pd.DataFrame()
fig, ax = plt.subplots(figsize=(9,6), dpi=600)
for feature in X_train.columns[:-1]:
for alpha in np.linspace(0.0001,0.2,20):
lasso_clf = Lasso(alpha=alpha)
lasso_clf.fit(X_train[features_without_ones],y_train)
df = pd.DataFrame([lasso_clf.coef_],columns=X_train[features_without_ones].columns)
df['alpha'] = alpha
coef1 = pd.concat([coef1,df],ignore_index=True)
ax.plot('alpha',feature,data=coef1)
ax.legend(loc='upper right')
ax.set_xlabel(r'$\alpha$',fontsize=15)
ax.set_ylabel("coefficient",fontsize=15)
plt.show()
```
这个修改后的代码将循环嵌套,首先对每个特征进行循环,然后在每个特征的循环中对alpha进行循环。在每个特征的循环中,我们使用subplot来绘制每个特征的系数。
阅读全文