Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to sum an array by block's

I have a large dataset stored in a numpy array (A) I am trying to sum by block's using:

B=numpy.add.reduceat(numpy.add.reduceat(A, numpy.arange(0, A.shape[0], n),axis=0), numpy.arange(0, A.shape[1], n), axis=1)

it work's fine when i try it on a test array but with my data's I get the following message:

TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'

Does someone now how to handle this?

Thanks for the help.

like image 990
user3272910 Avatar asked Feb 10 '26 19:02

user3272910


2 Answers

I believe the problem you're encountering is because your block size, n, is a float rather than an integer:

>>> A = np.arange(10)

# float block size
>>> np.add.reduceat(A, np.arange(0, A.shape[0], 5.0), axis=0)
# TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
# according to the rule 'safe'

# integer block size
>>> np.add.reduceat(A, np.arange(0, A.shape[0], 5), axis=0)
# array([10, 35])

numpy.add.reduceat requires an array of integer indices, but numpy.arange(0, A.shape[0], 5.0) returns an array of float64 values.

like image 131
ali_m Avatar answered Feb 13 '26 09:02

ali_m


In case anyone else has a similar problem but the chosen answer doesn't solve it, one possibility could be that in Python3, some index or integer quantity fed into a np function is an expression using '/' for example n/2, which ought to be '//'.

like image 31
DarenW Avatar answered Feb 13 '26 10:02

DarenW