我想乘的int16
陣列但float
陣列,具有自動四捨五入,但這種失敗:乘numpy的int和float數組
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: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'
看起來''numpy.multiply(A,B,out = A,cast ='unsafe')''是可能的,但語法要長很多! 默認情況下有沒有辦法在numpy中設置casting ='unsafe'? – Basj
查看https://github.com/numpy/numpy/pull/6499/files – Basj