Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将大小为1934的数组重塑为形状(3,1)_Python_Numpy_Scatter Matrix - Fatal编程技术网

Python 无法将大小为1934的数组重塑为形状(3,1)

Python 无法将大小为1934的数组重塑为形状(3,1),python,numpy,scatter-matrix,Python,Numpy,Scatter Matrix,我想用python为具有(1934,32)形状的数据集构建自己的PCA。Numpy数组(二进制图像文件)。在PCA中,我需要计算散射矩阵。我有一个代码,可以很好地处理图像和大小(3,x)的数组。但对我的不起作用 我尝试将np.zero和整形方法整形为32和1934,但没有任何效果。下面是我现在使用的代码 for i in range(X.shape[1]): scatter_matrix += (X[:,i].reshape(3,1) - mean_vector).dot((X[:,i]

我想用python为具有(1934,32)形状的数据集构建自己的PCA。Numpy数组(二进制图像文件)。在PCA中,我需要计算散射矩阵。我有一个代码,可以很好地处理图像和大小(3,x)的数组。但对我的不起作用

我尝试将np.zero和整形方法整形为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)”

我通过添加一个尺寸为(19341934)而不是(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环境中,也会花费太多时间。我甚至无法在数据集上获得一次迭代的结果

是否有任何解决方案可以加快速度