An error occurred while trying to sum the array by blocks

I have a large dataset stored in a numpy (A) array. I am trying to sum by block 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 works fine when I try to use it in a test array, but with my data, I get the following message:

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

Does anyone handle this now?

Thanks for the help.

+3
source share
2 answers

I suppose the problem you are facing is that the size of your block nis a float, not 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.reduceatan array of integer indices is required, but numpy.arange(0, A.shape[0], 5.0)returns an array of float64 values.

+3
source

, - , , , Python3 , np, , '/', n/2, "//'.

+1

All Articles