Numpy Cannot cast ufunc multiply output from dtype

后端 未结 3 1647
無奈伤痛
無奈伤痛 2021-01-01 14:45

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         


        
3条回答
  •  情深已故
    2021-01-01 15:23

    You could use broadcasting to multiply the two arrays and take only the integer part as follows:

    In [2]: (A*B).astype(int)
    Out[2]: array([ 0,  4,  9, 16])
    

    Timing Constraints:

    In [8]: %timeit (A*B).astype(int)
    1000000 loops, best of 3: 1.65 µs per loop
    
    In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
    100000 loops, best of 3: 2.01 µs per loop
    

提交回复
热议问题