[英]ValueError: shapes (10,) and (2,2) not aligned: 10 (dim 0) != 2 (dim 0)
[英]ValueError: shapes (2,2) and (4,6) not aligned: 2 (dim 1) != 4 (dim 0)
抱怨這條線:
log_centers = pca.inverse_transform(centers)
碼:
# TODO: Apply your clustering algorithm of choice to the reduced data
clusterer = KMeans(n_clusters=2, random_state=0).fit(reduced_data)
# TODO: Predict the cluster for each data point
preds = clusterer.predict(reduced_data)
# TODO: Find the cluster centers
centers = clusterer.cluster_centers_
log_centers = pca.inverse_transform(centers)
數據:
log_data = np.log(data)
good_data = log_data.drop(log_data.index[outliers]).reset_index(drop = True)
pca = PCA(n_components=2)
pca = pca.fit(good_data)
reduced_data = pca.transform(good_data)
reduced_data = pd.DataFrame(reduced_data, columns = ['Dimension 1', 'Dimension 2'])
數據是csv; 標頭看起來像:
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
0 14755 899 1382 1765 56 749
1 1838 6380 2824 1218 1216 295
2 22096 3575 7041 11422 343 2564
問題在於pca.inverse_transform()
不應將clusters
作為參數。
實際上,如果您查看文檔 ,則應將從PCA獲得的數據 應用於原始數據,而不是從KMeans獲得的質心 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.