【问题标题】:ValueError: shapes (2,2) and (4,6) not aligned: 2 (dim 1) != 4 (dim 0)ValueError:形状(2,2)和(4,6)未对齐:2(dim 1)!= 4(dim 0)
【发布时间】:2017-02-11 22:12:11
【问题描述】:

抱怨这一行:

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

【问题讨论】:

    标签: python scikit-learn pca sklearn-pandas


    【解决方案1】:

    问题是pca.inverse_transform()不应该将clusters作为参数。

    确实,如果您查看documentation,它应该将从PCA 获得的数据应用于您的原始数据,而 > 使用 KMeans 获得的质心

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-21
      • 2021-04-25
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 2019-06-08
      相关资源
      最近更新 更多