将numpy int和float数组相乘 [英] Multiply numpy int and float arrays
本文介绍了将numpy int和float数组相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 int16
数组乘以 float
数组,并进行自动舍入,但这会失败:
I'd like to multiply an int16
array but a float
array, with auto rounding, but this fails :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
我得到:
TypeError :无法将dtype('float64')的ufunc乘法输出转换为dtype('int16'),具有强制转换规则'same_kind'
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
推荐答案
解决此问题的两种方法:
您可以通过替换解决此问题
You can solve this by replacing
A *= B
A = (A * B)
或与
numpy.multiply(A, B, out=A, casting='unsafe')
这篇关于将numpy int和float数组相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文