Py学习  »  Python

可视化k表示集群python

Gerasimos • 5 年前 • 503 次点击  

我有一个包含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

问题是我不明白这个错误。另外,我不确定我是否以正确的方式接近可视化。有什么帮助吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43167
 
503 次点击