【发布时间】:2016-06-12 05:11:51
【问题描述】:
我正在尝试使用 numpy 和 sklearn 在 Python 中执行 kmeans 聚类。 我有一个包含 45 列和 645 行的 txt 文件。第一行是 Y,其余 644 行是 X。
我的 Python 代码是:
import numpy as np
import matplotlib.pyplot as plt
import csv
from sklearn.cluster import KMeans
#The following code reads the first row and terminates the loop
with open('trainDataXY.txt','r') as f:
read = csv.reader(f)
for first_row in read:
y = list(first_row)
break
#The following code skips the first row and reads rest of the rows
firstLine = True
with open('trainDataXY.txt','r') as f1:
readY = csv.reader(f1)
for rows in readY:
if firstLine:
firstLine=False
continue
x = list(readY)
X = np.array((x,y), dtype=object)
kmean = KMeans(n_clusters=2)
kmean.fit(X)
我在这一行遇到错误:kmean.fit(X)
我得到的错误是:
Traceback (most recent call last):
File "D:\file_path\kmeans.py", line 25, in <module> kmean.fit(X)
File "C:\Anaconda2\lib\site-packages\sklearn\cluster\k_means_.py",
line 812, in fit X = self._check_fit_data(X)
File "C:\Anaconda2\lib\site-packages\sklearn\cluster\k_means_.py",
line 786, in _check_fit_data X = check_array(X, accept_sparse='csr',
dtype=np.float64)
File "C:\Anaconda2\lib\site-packages\sklearn\utils\validation.py",
line 373, in check_array array = np.array(array, dtype=dtype,
order=order, copy=copy) ValueError: setting an array element with a
sequence.`
trainDataXY.txt
1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3 ,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5 47,64,508,1116,1116,1116,1116,11161610101161011610101161615,1161010101015101515,11616151015151515,11615
47,65,42 ,11,1116,116,1116,116,1116,1116,1116.116,111.11.111161011618,111.116015,1165,201015,116515,1165.11151015,1165.11515,1165.1115,1165
49,205,205,111110108,1111101010110110101011010110108,1115.11011010105105,115.1115,11510510451051085.11151051085.5855.11151510815.1175
51,78,52,46,56,74,50,28,38,38,39,38,38,37,40,39,39,41,96,101,99,104,97,101,111,101,104,115,116,116,119,110,112,119,116,116,135,130,129,135,120,108,133,120,125 P>
55,79,53,65,52,102,55,28,36,39,40,38,39,37,40,39,40,42,79,86,84,105,84,57,110,85,76,117,118,115,110 ,66,86,117,117,118,123,130,130,129,106,93,130,113,114
48,80,59,81,50,120,63,26,31,39,40,39,40,38,42,37,41,42,53,73,77,90,47,34,76 ,52,63,106,102,97,80,33,68,105,105,113,115,130,124,111,83,91,128,105,110
45,95,56,86,38,137,60,27,27,39,40,38,40,37,41,52,38,41,24,44,44,79,40,32,48 ,26,28,63,52,59,42,30,62,79,67,77,116,121,122,114,96,90,126,93,103
45,93,47,86,35,144,60,26,27,39,40,45,39,38,43,87,46,58,33,21,26,62,42,49,49 ,37,24,33,41,56,29,28,68,79,58,74,115,111,115,119,117,104,132,92,97
48,85,50,83,37,142,62,25,29,57,47,77,43,64,61,115,70,101,41,28,28,48,39,46,42,38,37 ,47,43,74,32,28,64,86,80,81,127,113,99,130,140,112,139,92,97
48,94,78,77,30,138,57,28,29,91,66,94,61,94,103,129,89,140,38,34,32,38,33,43,38,36,39,50 ,39,75,31,33,65,89,82,84,127,112,100,133,141,107,136,95,97
45,108,158,77,30,140,67,29,26,104,97,113,92,106,141,137,116,151,33,32,32,43,44,40,37,34,37,54,86,77,55,48,77,112,1313,109,125,120, ,98,129,89,99
48,115,250105,115010101010101501501551551581555,1155.11851855.115515855.11551585.1155.11515855.1155
131,157,111111111111111111111111111111111111111111111181181851818158185.1188185.1188185.111815815
160,160,1111,1111111111161165,1111111111111111111111115115.1111511511511510115115.1115.111511511511511515115115115115115115115.11165.1116151151151151151151165.1115151151151151165.11151151151165.11165.11165.1115115.111615
【问题讨论】:
-
请显示您的数据文件的摘录。还要确保创建一个真实的数字数据矩阵,而不是
dtype=object。更喜欢使用 numpy 或 pandas 阅读器阅读您的数据。
标签: python numpy scikit-learn k-means