简体   繁体   中英

Error: TypeError: only integer scalar arrays can be converted to a scalar index

I am pretty new to python/numpy and is not completely aware of it.

I have been trying to implement an algorithm and getting stuck at a certain point where when trying to take the dot product of an array with its transpose. The error that I am getting is this

TypeError: only integer scalar arrays can be converted to a scalar index.

Below is my code for reference.

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))

Check out the docs for np.prod() :

 numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>) 

Return the product of array elements over a given axis.

The np.prod() function is not meant for two different arrays. For the dot product, you can use the np.dot() function or, equivalently, the ndarray.dot() method :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM