错误:TypeError:只能将整数标量数组转换为标量索引

我对python / numpy很新,并没有完全意识到它 .

我一直试图实现一个算法,并在试图通过其转置获取数组的点积时陷入某一点 . 我得到的错误就是这个

TypeError:只能将整数标量数组转换为标量索引 .

以下是我的代码供参考 .

import pandas as pd
import numpy as np

dataset=pd.read_csv('SCLC_study_output_filtered_2.csv',header=0,delimiter=",")

#forming the first class
class_1 = dataset.iloc[0:20,1:20].values

#forming the second class
class_2 = dataset.iloc[20:41,1:20].values

mean_c1 = np.mean(class_1, axis=0)

#Taking mean of class 2 
mean_c2 = np.mean(class_2, axis=0)

mean_classes =[mean_c1,mean_c2]

#Calculating S-within for class-1
scatter_within_c1 = np.zeros((19,19))
for i in range(0,20):
    for col in class_1:
        col, m = col.reshape(19,1), mean_c1.reshape(19,1)
        sub = np.subtract(col,m)
        scatter_within_c1 += np.prod(sub,np.transpose(sub))

回答(1)

2 years ago

看看docs for np.prod()

numpy.prod(a,axis = None,dtype = None,out = None,keepdims = <class numpy._globals._NoValue>)
返回给定轴上的数组元素的乘积 .

np.prod() 函数不适用于两个不同的数组 . 对于点积,您可以使用np.dot() function或等效的ndarray.dot() method

>>> A = np.array([1, 2, 3, 4, 5])
>>> np.dot(A, A)
55
>>> A.dot(A)
55