0
点赞
收藏
分享

微信扫一扫

使用kmeans聚类算法进行预测


使用kmeans聚类算法进行预测

如何寻找K值, 可以通过轮廓系数 来筛选判断比较

# 라이브러리를 임포트합니다.
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

# 加载数据
iris = datasets.load_iris()
features = iris.data

# 标准化
scaler = StandardScaler()
features_std = scaler.fit_transform(features)

# 创建KMeans 对象
cluster = KMeans(n_clusters=3, random_state=0, n_jobs=-1)

# 你和训练
model = cluster.fit(features_std)
# 查看预测的类
model.labels_
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0,
2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 2, 2, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0,
0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2])
# 查看真实的类
iris.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
# 创建样本
# new_observation = [[0.8, 0.8, 0.8, 0.8]]
new_observation = [[0.8, 0.8, 0.8, 0.8]]

# 预测
model.predict(new_observation)
array([0])
预测样本的中心点
# 查看预测样本的中心点中心点
model.cluster_centers_
array([[ 1.13597027, 0.08842168, 0.99615451, 1.01752612],
[-1.01457897, 0.85326268, -1.30498732, -1.25489349],
[-0.05021989, -0.88337647, 0.34773781, 0.2815273 ]])


举报

相关推荐

0 条评论