Numpy无法强制转换dtype的ufunc乘法输出 [英] Numpy Cannot cast ufunc multiply output from dtype
本文介绍了Numpy无法强制转换dtype的ufunc乘法输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用自动舍入将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:无法使用强制转换规则"same_kind"将ufunc乘法输出从dtype('float64')转换为dtype('int16')
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无法强制转换dtype的ufunc乘法输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文