我有一个存储在numpy数组中的大型数据集(A)我试图通过块的总和使用:
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)
当我在测试阵列上尝试它时它工作正常但是我的数据是我得到以下消息:
TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'
现在有人如何处理这个问题?
感谢您的帮助。
答案 0 :(得分:3)
我相信您遇到的问题是因为您的块大小n
是浮点而不是整数:
>>> 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
需要一个整数索引数组,但numpy.arange(0, A.shape[0], 5.0)
返回一个float64值数组。
答案 1 :(得分:1)
如果其他人有类似的问题,但所选择的答案没有解决它,一种可能性是,在Python3中,输入到np函数的一些索引或整数量是使用'/'的表达式,例如n / 2,应该是'//'。