Python:K-Means调用Sklearn的写法

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from numpy import random

data = np.loadtxt('E:\data set\K-means_data')
# plt.scatter(data[:,0],data[:,1],c='red',marker='o',label='see')
# plt.show()
k=7
estimator = KMeans(n_clusters=k)
estimator.fit(data)
label_pred = estimator.labels_

Color = 'rbgyckm'
for i in range(k):
    clusterData = data[label_pred == i]
    plt.scatter(clusterData[:,0],clusterData[:,1],color=Color[i])
plt.show()

画图部分仅适用于二维数据。实际中的多维数据不使用该画图内容!

你可能感兴趣的:(Python学习)