我正在开发一个kmeans集群。我已经在网络上的一些可用引用的帮助下写下了代码但是当我运行这段代码时它会触发错误:
from sklearn import cluster
import pandas as pd
df = [
[0.57,-0.845,-0.8277,-0.1585,-1.616],
[0.47,-0.14,-0.5277,-0.158,-1.716],
[0.17,-0.845,-0.5277,-0.158,-1.616],
[0.27,-0.14,-0.8277,-0.158,-1.716]]
df = pd.DataFrame(df,columns= ["a","b","c","d", "e"])
# df = pd.read_csv("cleaned_remove_cor.csv")
k = 3
kmeans = cluster.KMeans(n_clusters=k)
kmeans.fit(df)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
from matplotlib import pyplot
import numpy as np
for i in range(k):
# select only data observations with cluster label == i
ds = df[np.where(labels==i)]
# plot the data observations
pyplot.plot(ds[:,0],ds[:,1],'o')
# plot the centroids
lines = pyplot.plot(centroids[i,0],centroids[i,1],'kx')
# make the centroid x's bigger
pyplot.setp(lines,ms=15.0)
pyplot.setp(lines,mew=2.0)
pyplot.show()
尽管如此,许多以前的线程都可以使用相同的错误,但是没有可用的单一解决方案可以在我的程序中处理此错误。如何调试此错误?
我使用的代码:
^XA^POI^MNV^LL3000
我的DataFrame的形状是(8127x600)
答案 0 :(得分:3)
我试过,这对我有用,将pandas df转换为numpy矩阵:
df = df.as_matrix(columns= ["a","b","c","d", "e"])