Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need np.array() or np.copy()?

Tags:

python

numpy

A real numpy newbie question here.

I have an numpy array called 'image'. Doing this:

image2 = image
image2[image < minval] = minval
image2[image > maxval] = maxval

...changes the contents of 'image'.

I gather that's because variables in Python are really references, so 'image2' is just another way of referring to 'image'. So I'm supposed to use "image2 = np.copy(image)". Fine.

But, then, why doesn't 'a' change when I do this:

a = 5
b = a
b = 7

Isn't 'b' just another way of referring to 'a'? If so, why doesn't a==7 at the end of this?

I want to know if there's some mental model that makes this seem consistent. Because it doesn't.

like image 369
nerdfever.com Avatar asked Dec 18 '14 02:12

nerdfever.com


People also ask

Why do we use NP array?

Why use NumPy? NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types.

What does NP copy do in Python?

copy() function in Python is used to return a copy of an array of the given object.

What is the difference between copy and view in NumPy?

The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array. The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.

How do you copy a NumPy array?

Python Numpy – Duplicate or Copy Array Copying array means, a new instance is created, and the contents of the original array is copied into this array. To copy array data to another using Python Numpy library, you can use numpy. ndarray. copy() function.


1 Answers

The answer really lies in the way direct assignments like b=a and b=7 work. b=a creates a new reference to the object also referenced by a, and associates that new reference with the name b. The subsequent b=7 then removes the reference that was attached to the name b, and makes a different association with the name b. This will be true whether a is an immutable type (like an integer) or a mutable type (like a numpy array). In neither case will the content of a be modified.

By contrast, image2[image < minval] = minval is not a reassignment. Via its use of [] it calls a method (__setitem__) of the object image2. This method changes parts of the underlying data structure without reassigning anything to image2.

Python's most fundamental types are "immutable". This means that nothing you can do will change them (for example, if image2 were of the immutable type tuple, trying to change one of its elements with [] indexing would cause an exception to be raised).

As a result, very very loosely, if you're accustomed to a C/C++ mindset, it can sometimes help to think of immutable types as being passed by value and mutable types being passed by reference when you're looking at a function or method prototype. As pointed out in the comments, though, this is not really what happens: everything is a reference, but some references (the immutable types) are automatically treated the way C/C++ would treat const references.

The immutable types include:

  • basic numeric types: bool, int, long, float and complex
  • basic string types: str, unicode (python 2.x only) and bytes (python 3.x only)
  • tuple (but not list)
like image 106
jez Avatar answered Sep 18 '22 14:09

jez