Today I have noticed strange behavior of Numpy/Scipy arrays. It looks like adding array cell with integer inside to float can have two different results, depending on the variable to which the result is assigned. Instead of long explanations, below I present the code:
import scipy as sp
array_int = sp.array([[0], [0]])
float_operand = 0.1
print array_int[0, 0] + float_operand #gives 0.1
But
import scipy as sp
array_int = sp.array([[0], [0]])
float_operand = 0.1
array_int[0, 0] = array_int[0, 0] + float_operand
print array_int[0, 0] #gives 0
I could understand if this kind of behavior was inherited from Python, but:
In contrary to behavior of "bare" Python (2.7):
integer_for_sure = int(0)
integer_for_sure = integer_for_sure + 0.1
print integer_for_sure #gives 0.1 as expected
Is this kind of feature somwhere documented? Has anybody encounterd it before?
zeros((2, 1))instead of constructing a 2x1 list of lists of zeros to pass toarray(especially when you get a lot bigger than 2x1).import numpy as np(and use thenpprefix) instead ofimport scipy as sp. Thescipynamespace includes thenumpynamespace for historical reasons, but it is more explicit (and removes a dependency on scipy if all you really need is numpy) to import the numpy names directly from numpy.