可视化k表示聚类python

时间:2018-10-10 19:33:52

标签: python pandas matplotlib k-means

我有一个包含4295个条目的csv文件:

    ,name,product,ship_from,score,shops,class
0,tom,22,0.3,0.893818566,2,0
1,jer,2,0.3,0.910212895,2,0
2,ed.,6,1,0.195939375,1,0
3,paul,16,0.3,0.56267631,2,0
4,min,3,0.3,0.01069298,1,0

我使用K表示将我的数据分为3个类(类代表相应的类)。

    import pandas as pd 
from sklearn import datasets
from sklearn import datasets

#Load dataset


df=pd.read_csv('book4.csv')

from sklearn.model_selection import train_test_split

X=df[['product', 'ship_from', 'score', 'shops']]
y=df['class']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7)

from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=3)

kmeans = kmeans.fit(X_train)

labels = kmeans.predict(X_train)

centroids = kmeans.cluster_centers_


print(labels)
print(centroids)


import matplotlib.pyplot as plt

    labels.ravel()

plt.scatter(X.values[:, 0], X.values[:, 1], c=labels[:,0])

centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], s=200, alpha=0.5);

我想可视化k个均值的簇以及我的数据如何分散在中心附近。到目前为止,我还没有这样做。这是我第一次尝试使用matplotlib进行可视化(通常使用Gephi)。我不断收到错误消息:

ValueError: c of shape (1288,) not acceptable as a color sequence for x with size 4295, y with size 4295

问题是我不理解该错误。另外,我不确定是否要以正确的方式进行可视化处理。有帮助吗?

0 个答案:

没有答案