2017-03-19 5 views
2

TruncatedSVDのPython:プロットクラスタKは、意味の後、私は私のデータセットでクラスタリングを実行した結果をプロットしようとしているが、私はエラーを取得しています

File "cluster.py", line 93, in <module> 
    Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()]) 
    File "/usr/local/lib/python2.7/dist-packages/sklearn/cluster/k_means_.py", line 957, in predict 
    X = self._check_test_data(X) 
    File "/usr/local/lib/python2.7/dist-packages/sklearn/cluster/k_means_.py", line 867, in _check_test_data 
    n_features, expected_n_features)) 
ValueError: Incorrect number of features. Got 2 features, expected 73122 
fit()への私の呼び出しが正常に動作します

が、プロットはどこが間違っているかです。ここで

は私のコードです:

reduced_data = TruncatedSVD(n_components=2).fit_transform(X) 

kmeans = KMeans(n_clusters=4, init='k-means++', max_iter=100, n_init=1, verbose=False) 
kmeans.fit(X) 

h = .02  # point in the mesh [x_min, x_max]x[y_min, y_max]. 

# Plot the decision boundary. For that, we will assign a color to each 
x_min, x_max = reduced_data[:, 0].min() - 1, reduced_data[:, 0].max() + 1 
y_min, y_max = reduced_data[:, 1].min() - 1, reduced_data[:, 1].max() + 1 
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) 

# Obtain labels for each point in mesh. Use last trained model. 
Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()]) 

# Put the result into a color plot 
Z = Z.reshape(xx.shape) 
plt.figure(1) 
plt.clf() 
plt.imshow(Z, interpolation='nearest', 
      extent=(xx.min(), xx.max(), yy.min(), yy.max()), 
      cmap=plt.cm.Paired, 
      aspect='auto', origin='lower') 

plt.plot(reduced_data[:, 0], reduced_data[:, 1], 'k.', markersize=2) 
# Plot the centroids as a white X 
centroids = kmeans.cluster_centers_ 
plt.scatter(centroids[:, 0], centroids[:, 1], 
      marker='x', s=169, linewidths=3, 
      color='w', zorder=10) 
plt.title('K-means clustering on the digits dataset (PCA-reduced data)\n' 
      'Centroids are marked with white cross') 
plt.xlim(x_min, x_max) 
plt.ylim(y_min, y_max) 
plt.xticks(()) 
plt.yticks(()) 
plt.show() 

は、誰もが、私は、クラスタの図を得るために、私のコードを変更する方法を提案することはできますか?トレースバックがあなたを語っている

答えて

2

問題が何であるか:

ValueError: Incorrect number of features. Got 2 features, expected 73122

kmeans分類器は73122次元列車のサンプルとフィットし、したがって、あなたは2次元試験サンプルの予測を行うためにkmeansを使用することはできません。

kmeans.fit(X)kmeans.fit(reduced_data)に変更するだけでコードを修正できます。

関連する問題