【问题标题】:cannot reshape array of size 1934 into shape (3,1)无法将大小为 1934 的数组重塑为形状 (3,1)
【发布时间】:2020-02-18 06:19:23
【问题描述】:

我想在 python 中为形状为 (1934,32) 的数据集构建自己的 PCA。 Numpy 数组(二进制图像文件)。在 PCA 中,我需要计算散点矩阵。我有一个代码,它适用于图像和大小数组(3,x)。但不适用于我的。

我尝试将 np.zeros 和 reshape 方法重塑为 32 和 1934,但没有任何效果。这是我现在正在使用的代码一瞥

for i in range(X.shape[1]):
    scatter_matrix += (X[:,i].reshape(3,1) - mean_vector).dot((X[:,i].reshape(3,1) - mean_vector).T)
print('Scatter Matrix:\n', scatter_matrix)

错误是“无法将大小为 1934 的数组转换为形状 (3,1)”

【问题讨论】:

    标签: python numpy scatter-matrix


    【解决方案1】:

    我通过添加维度为 (1934,1934) 而不是 (3,1) 的散布矩阵找到了解决方案。它现在工作正常。代码如下所示

    scatter_matrix = np.zeros((1934,1934))
    for i in range(X.shape[1]):
      print('first',i)
        A = X[:,i].reshape(1934,1) - mean
        #print(A)
        B = (X[:,i].reshape(1934,1) - mean).T
        #print(B)
        sb = A.dot(B)
        print(sb)
        #scatter_matrix += (A).dot(B)
        #print(i)
    print('Scatter Matrix:\n', scatter_matrix)
    

    但是,现在我被上述代码中的点积计算困住了。 即使在 Kaggle GPU 环境中也需要花费太多时间。我什至无法获得对数据集进行单次迭代的结果。

    是否有任何解决方案可以使其更快?

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 2019-08-23
      • 2020-04-14
      • 2020-10-05
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多