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.
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.
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 '//'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With