# 导入库from sklearndatasets import load_irisfrom sklearncluster import KMeansfrom sklearnmodel_selection import train_test_splitimport numpy as np# 加载鸢尾花数据集iris = load_irisx = irisdatay = iristarget# 将数
导入库
from sklearn.datasets import load_iris from sklearn.cluster import KMeans from sklearn.model_selection import train_test_split import numpy as np
加载鸢尾花数据集
iris = load_iris() x = iris['data'] y = iris['target']
将数据集分为训练集和测试集
np.random.seed(0) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)
任务1:创建 KMeans 对象,令 n_clusters=4
########## Begin ########## kmeans = KMeans(n_clusters=4) ########## End ##########
任务2:调用 fit 函数执行训练过程
########## Begin ########## kmeans.fit(x_train) ########## End ##########
任务3:调用 predict 函数进行预测
########## Begin ########## y_pred = kmeans.predict(x_test) ########## End ##########
打印结果
print("真实结果:\n", y_test) print("预测结果:\n", y_pred)
计算 a,b,c,d
a = b = c = d = 0 m = 15 for j in range(m): for i in range(j): if y_test[i]==y_test[j] and y_pred[i]==y_pred[j]: a = a + 1 elif y_test[i]==y_test[j] and y_pred[i]!=y_pred[j]: b = b + 1 elif y_test[i]!=y_test[j] and y_pred[i]==y_pred[j]: c = c + 1 else: d = d + 1
根据公式计算 Jaccard 系数
JC = a / (a + b + c)
根据公式计算 FM 指数
FM = np.sqrt(a ** 2 / ((a + b) * (a + c)))
根据公式计算 Rand 指数
RI = 2 * (a + b) / (m * (m - 1))
打印结果
print("Jaccard 系数为{},FM 指数为{},Rand 指数为{}。".format(JC, FM, RI)
原文地址: http://www.cveoy.top/t/topic/fpFW 著作权归作者所有。请勿转载和采集!