This is an archival dump of old wiki content --- see scipy.org for current material

This page describes characteristics of numpy that are by design but may be surprising to some users.

assigment creates a view, not a copy

This is actually standard Python, but is repeated here because it is often a source of confusion for new users. The following snippet illustrates the correct behavior.

>>> a=numpy.arange(10)
>>> print a
[0 1 2 3 4 5 6 7 8 9]
>>> b=a # creates a second reference to the same memory
>>> print b
[0 1 2 3 4 5 6 7 8 9]
>>> c=numpy.array(a,copy=True) # copies the memory
>>> print c
[0 1 2 3 4 5 6 7 8 9]
>>> a[3]=1234
>>> print a
[   0    1    2 1234    4    5    6    7    8    9]
>>> print b
[   0    1    2 1234    4    5    6    7    8    9]
>>> print c
[0 1 2 3 4 5 6 7 8 9]

allclose() does not ensure shape similarity

Like most of numpy, allclose() uses the broadcasting rules when performing its operation. This leads to the following behavior:

>>> a=32
>>> b=numpy.array([])
>>> numpy.allclose(a,b)
True

Upon closer inspection, we can see that the broadcasting rules cause a to become an empty array like b. The default truth value of an empty array is True, so the following holds and illustrates how the above result is consistent with numpy's rules.

>>> a==b
array([], dtype=bool)
>>> numpy.all(a==b)
True

See this thread for further information.

SciPy: numpy_warts_and_gotchas (last edited 2015-10-24 17:48:23 by anonymous)