plt.xticks(range(X.shape[1]), names, rotation=70)
时间: 2023-09-16 22:14:28 浏览: 43
这段代码的作用是在 Matplotlib 中设置 x 轴的刻度和标签。其中,range(X.shape[1]) 表示 x 轴的刻度位置,X.shape[1] 表示 X 数组的列数,即 x 轴上的数据点个数。names 是一个字符串数组,表示 x 轴上每个数据点对应的标签。rotation=70 表示将 x 轴标签旋转 70 度,以便更好地展示长标签。
相关问题
plt.plot(logreg.coef_.T, 'o', label "c-1") plt.plot(logreg100.coef_.T. labe1=“C=100“) pltplot(logreg001.coefT,'v, label=“C=0.001”) plt.xticks(range(cancer.data.shape[1]).cancer.feature_names,rotation=90) xlims =plt.xl1m0 plt.hlines(0,xlins[0], x1ins[1]] plt.xlim(xlims) p1t.ylim(-5, 5) plt.xlabel(Feature plt.ylabel(“Coefficient magnitude”) plt.legend()修改代码
以下是修改后的代码:
```python
plt.plot(logreg.coef_.T, 'o', label="C=1")
plt.plot(logreg100.coef_.T, 'x', label="C=100")
plt.plot(logreg001.coef_.T, 'v', label="C=0.001")
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.ylim(-5, 5)
plt.xlabel("Feature")
plt.ylabel("Coefficient magnitude")
plt.legend()
```
修改的内容包括:
- 在第一行代码中,将`label "c-1"`修改为`label="C=1"`,使标签更具有可读性。
- 在第二行、第三行和第四行中,将`labe1`修改为`label`,并分别将符号改为`x`和`v`,使得不同的线条更加容易区分。
- 在第五行中,将`cancer.feature_names`放到`range`函数前面,使得特征名称能够正确显示在x轴上。
- 在第六行中,将`x1ins`修改为`xlims`,使得`xlims`变量能够正确使用。
- 在第七行中,将`p1t`修改为`plt`,使得`plt.ylim`能够正确调整y轴范围。
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)修改代码
以下是修改后的代码:
```
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'),增加了定位参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)