1r_11 = LogisticRegression (C=C, solver=' liblinear' penalty="11", max_iter=5000). fit (x_ train, y_train) print ("Training accuracy of 11 logree with C=(:. 3r]: (:.2f]". format ( C, 1r_11. score (X_train, y_train))) print ("Test accuracy of 11 logreg with C=(:.3f]: (:. 2f)". format ( C, Ir_11. score (X_test, y_test))) plt. plot (lr_11. coef_. T, marker, label="C=(: 3f]". format (C)) plt. sticks (range (cancer. data. shape[1]), cancer. feature_names, rotation=90) xlims = plt. xlim( pit. hlines (0, xlims[0], xlims[1]) plt. xlim (xlims) plt. label ("Feature") plt. ylabel ("Coefficient magnitude") pit. ylim(-5, 5) plt. legend (10c=3)修改代码
时间: 2024-02-03 10:02:07 浏览: 188
以下是修改后的代码:
```
lr_11 = LogisticRegression(C=C, solver='liblinear', penalty="l1", max_iter=5000).fit(X_train, y_train)
print("Training accuracy of l1 logreg with C={:.3f}: {:.2f}".format(C, lr_11.score(X_train, y_train)))
print("Test accuracy of l1 logreg with C={:.3f}: {:.2f}".format(C, lr_11.score(X_test, y_test)))
plt.plot(lr_11.coef_.T, marker='o', label="C={:.3f}".format(C))
plt.xticks(range(cancer.data.shape[1]), cancer.feature_names, rotation=90)
xlims = plt.xlim()
plt.hlines(0, xlims[0], xlims[1])
plt.xlim(xlims)
plt.xlabel("Feature")
plt.ylabel("Coefficient magnitude")
plt.ylim(-5, 5)
plt.legend(loc='best')
```
主要修改包括:
1. 将 penalty 参数的取值改为 "l1",表示使用 L1 正则化。
2. 将 marker 参数的取值改为 'o',表示使用圆圈作为标记。
3. 将 plt.sticks 修改为 plt.xticks,因为 plt.sticks 不存在。
4. 将 plt.label 修改为 plt.xlabel,因为 plt.label 不存在。
5. 将 plt.legend 修改为 plt.legend(loc='best'),增加了定位参数。
阅读全文