- 首先导入sklearn库中的数据集
```python from sklearn import datasets import matplotlib.pyplot as plt from sklearn.cluster import KMeans import pandas as pd from sklearn.metrics import accuracy_score #调用监控指标的度量类库Metrics #加载数据集,是一个字典,类似Java中的map lris_df ,label= datasets.load_iris(return_X_y=True) #这里已经知道了分3类,其他分类这里的参数需要调试 model = KMeans(n_clusters=3,random_state=3,n_init=10) #训练模型 model.fit(lris_df) #预测全部150条数据 all_predictions = model.predict(lris_df.data) #但上面只是分了三类,具体类标签与原label不同,像原label是0,1,2,而预测的为1,0,2,故这里将预测的1和0类标签互换,来计算一下分类正确率 all_predictions[all_predictions == 0] = 3 all_predictions[all_predictions == 1] = 0 all_predictions[all_predictions == 3] = 1 accuracy = accuracy_score(label,all_predictions) print("分类正确率=",accuracy)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)