mglearn.discrete_scatter用法
时间: 2023-05-17 12:07:54 浏览: 709
mglearn.discrete_scatter是一个用于绘制离散散点图的函数,它可以将不同类别的数据点用不同颜色和标记绘制出来。使用该函数需要传入三个参数:x、y和labels。其中,x和y是数据点的坐标,labels是每个数据点所属的类别标签。例如,可以使用以下代码绘制两个类别的离散散点图:
import matplotlib.pyplot as plt
import mglearn
X, y = mglearn.datasets.make_forge()
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.legend(["Class 0", "Class 1"], loc=4)
plt.xlabel("First feature")
plt.ylabel("Second feature")
print("mglearn.discrete_scatter用法示例:")
注意:以上示例代码中的make_forge()函数是mglearn中的一个数据集生成函数,用于生成一个二分类数据集。
相关问题
from sklearn.datasets import load_iris data,target=load_iris(return_X_y=True) print('feature_value:',data.shape) print('target:',target) from sklearn.cluster import KMeans import numpy as np for i in range(0,30): kmeans=KMeans(n_clusters=3,max_iter=30,tol=0.0001, random_state=i).fit(data) label=kmeans.labels_ #print('label:',label) center=kmeans.cluster_centers_ #print('center:',center) predict=kmeans.predict(data) print('i=',i) print('predict:',predict) accuracy=np.mean(predict==target)*100 print('accuracy',accuracy) #模型预测 predict=kmeans.predict(data) print('predict:',predict) import numpy as np accurancy=np.mean(predict==target)*100 print('i=',i) print('accurancy',accurancy) import matplotlib.pyplot as plt import mglearn plt.figure(figsize=(10,8)) plt.subplot(221) mglearn.discrete_scatter(data[:,0],data[:,1],target,markers='^') plt.xlabel('data') plt.ylabel('origin') plt.subplot(222) mglearn.discrete_scatter(data[:,0],data[:,1],y_predict,markers='^') mglearn.discrete_scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],[0,1,2],markers='o',markeredgewidth=2) plt.xlabel('data') plt.ylabel('y_predict') plt.show()写一下注释,要非常详细
注释如下:
```python
# 导入数据集
from sklearn.datasets import load_iris
data, target = load_iris(return_X_y=True)
print('feature_value:',data.shape)
print('target:',target)
# 导入KMeans聚类模型,对数据进行聚类
from sklearn.cluster import KMeans
import numpy as np
# 通过循环实现多次聚类,记录每次聚类的预测结果和准确率
for i in range(0,30):
kmeans = KMeans(n_clusters=3, max_iter=30, tol=0.0001, random_state=i).fit(data)
label = kmeans.labels_
center = kmeans.cluster_centers_
predict = kmeans.predict(data)
print('i=',i)
print('predict:',predict)
accuracy=np.mean(predict==target)*100
print('accuracy',accuracy)
# 对聚类模型进行预测
predict=kmeans.predict(data)
print('predict:',predict)
import numpy as np
accuracy=np.mean(predict==target)*100
print('i=',i)
print('accuracy',accuracy)
# 绘制数据散点图和聚类结果散点图
import matplotlib.pyplot as plt
import mglearn
plt.figure(figsize=(10,8))
plt.subplot(221)
mglearn.discrete_scatter(data[:,0],data[:,1],target,markers='^')
plt.xlabel('data')
plt.ylabel('origin')
plt.subplot(222)
mglearn.discrete_scatter(data[:,0],data[:,1],y_predict,markers='^')
mglearn.discrete_scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],[0,1,2],markers='o',markeredgewidth=2)
plt.xlabel('data')
plt.ylabel('y_predict')
plt.show()
```
其中,注释解释了代码的主要功能和实现方法,如导入数据集、导入KMeans聚类模型、多次聚类、预测、绘制数据散点图和聚类结果散点图等。
from sklearn.datasets import make_blobs X, y = make_blobs(random_state=42) mglearn.discrete_scatter(X[:, 0], X[:, 1], y) plt.xlabel("Feature 0") plt.ylabel("Feature 1") plt.legend("Class 0","Class 1","Class 2")修改代码
修改后的代码如下:
```python
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import mglearn
X, y = make_blobs(random_state=42)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
plt.legend(["Class 0","Class 1","Class 2"])
```
主要做了两个修改:
1. 在导入 `matplotlib.pyplot` 后,需要使用 `plt` 来指代它。
2. 在调用 `legend()` 函数时,需要将每个类别标签放在一个列表中,作为参数传入函数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)