2

Here are the two cases under consideration. (a)Scalars and (b) Numpy matrices. My query is about initialization and assignment.

1 Perfect

y = x = 0;
x = 7
print(x,y)
y = 8 
print(x,y)    
x=10
print( x,y)

output is perfectly fine. Effect of y = x = 0 is not seen.

7 0
7 8
10 8

2 Perfect

import numpy as np
n=3
#d11 = d =np.zeros(n)#.reshape(-1,1) ;
d11 = np.zeros(n); d =np.zeros(n)

for j in np.arange(0, n, 1):            
    d11[j] = j**3 
    d[j] = j**2

print (d11, d   )

output is perfectly fine, as d11 and d are initialized separately.

[ 0.  1.  8.] [ 0.  1.  4.]

3 Ambiguous

import numpy as np
n=3
d11 = d =np.zeros(n)#.reshape(-1,1) ;
#d11 = np.zeros(n); d =np.zeros(n)

for j in np.arange(0, n, 1):            
    d11[j] = j**3 
    d[j] = j**2

print (d11, d   )

output is not okay, as d11 and d are initialized and equated. Though I update d11 and d separately, there are being equated to eachother with the more recent update. Output goes like this:

[ 0.  1.  4.] [ 0.  1.  4.]

Queries:

Initializing multiple variables with a single line is working fine on scalars (y = x = 0) , but not on matrices (d11 = d =np.zeros(n))?

Is there something wrong with my coding or understanding ? This is not the case with Matlab. Why d11 = d is taking effect in case 3, though they are being updated at different stages/lines.

Case 3 can be considered as bad programming skill ?. I dont see any use of this kind of functionality from any programming language ?

Please enlighten me. I just moved from matlab to python. Hence I got this fundamental question.

Thanks in advance for your attention.

1

This is the difference between an immutable object (as Python numbers are) and a mutable object (as Numpy matrices are). When you perform an operation on an immutable object, a new copy is made, and your variable is updated to refer to the new copy:

x = 0
...
x = 7

First a '0' object is created, and 'x' is set to refer to it. Then a '7' is created, and 'x' is updated to refer to that. (Note: this is an oversimplification, but for the purposes of answering this question the details are unimportant)

However when you create a numpy matrix:

x = np.zeros(3)

this stores a reference in x to the created matrix, and later change it:

x[i] = 1

this doesn't create a new matrix, but changes the existing one. Also, if you use two variables, as you do:

x = y = np.zeros(3)

this only creates a single matrix and makes both x and y refer to it.

Allowing a single object to be stored in multiple variables is actually quite useful in larger systems, where for example you could share a large matrix (perhaps the weights of a neural network layer) between two separate sections of code (e.g. the forward propagation step and the backward propagation trainer step) and allow one of them (i.e. training in this example) to change it and influence the behaviour of the other.

  • Noted. Since the scalar is created using python, it is not behaving similar to array/matrix created by Numpy. I understand that: If we have to initialize 5 matrices, it is better to initialize separately. This way they are representing separate links to 5 separate matrices. – learner123 Jul 14 '16 at 13:37
  • Right, but note that it isn't a difference between python and numpy: other python objects (such as lists and dictionaries) behave the same way numpy matrices do. It's just that numbers, strings, and a handful of other standard types are specifically defined as immutable, so you don't have to worry about identity issues like this. See also a related question I happpen to have open in another tab... – Jules Jul 14 '16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.