- vs -= operators with numpy

前端 未结 3 1106
闹比i
闹比i 2021-01-04 02:55

I\'m having some strange behavior in my python code related to - and -=. I\'m writing a QR decomposition using numpy, and have the following line o

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 03:05

    You could get different results from x - y and x -= y if the data types of x and y differ.

    For example:

    import numpy as np
    
    x = np.array(range(0,6))
    y = np.array(np.arange(0,3,0.5))
    
    print x - y
    x -= y
    print x
    

    This prints out:

    [ 0.   0.5  1.   1.5  2.   2.5]
    [0 0 1 1 2 2]
    

    It may be worth making sure your arrays' dtypes are exactly as you expect (e.g. you're not inadvertently using integer or float32 arrays instead of float64), paying particular attention to arrays used on the left-hand side of -=.

提交回复
热议问题