[SciPy-User] problems with numpy array assignment

Eric Hermes ehermes at chem.wisc.edu
Tue Mar 4 15:53:34 EST 2014


On Tuesday, March 04, 2014 2:52:04 PM, Eric Hermes wrote:
>
> On 3/4/2014 2:46 PM, Gabriele Brambilla wrote:
>> thanks,
>> I think I have understand:
>> It pass the references only when I do something like A=B.
>> if I do A=c() I create a new variable.
>>
>> But why Python works in this way? which utility has this behaviour?
>>
>> Gabriele
>>
> I am only speaking about numpy arrays.  For example:
>
> B = np.array([...])
> A = B
>
> makes A a reference to B.  Modifications to A affect B, and vice
> versa.  If, however, you do:
>
> B = np.array([...])
> A = B.copy()
>
> A will instead be a copy of A, and they can be changed independently.
Correction: A will instead by a copy of *B*
>
> This is not true of python datatypes in general. For example:
>
> B = [...]
> A = B
>
> In this case A and B can be independently modifed without affecting
> each other.  This is because python lists are passed by value, unlike
> numpy arrays which are passed by reference.  That is, saying "A = B"
> means something different depending on whether B is a list or a numpy
> array.
>
> I hope this cleared some things up,
> Eric
>>
>> 2014-03-04 15:25 GMT-05:00 Eric Hermes <ehermes at chem.wisc.edu
>> <mailto:ehermes at chem.wisc.edu>>:
>>
>>     On 3/4/2014 2:18 PM, Gabriele Brambilla wrote:
>>>     So if I do:
>>>
>>>     B2 = PAS1
>>>     Dx2 = ndimage.gaussian_filter(B2, sigma=[nsmopha, nsmoene])
>>>     B2 = Dx2
>>>
>>>     B1 = PAS1
>>>     Dx1 = ndimage.gaussian_filter(B1, sigma=[nsmopha, nsmoene])
>>>     B1 = Dx1
>>>
>>>     Am I applying the smooth two times?
>>>
>>     No. Based on the code snippets you've posted so far, this is what
>>     is happening, with comments:
>>
>>     PAS = MYMAP[:,hhh,:]      # PAS now refers to a slice of array MYMAP
>>     PAS1 = PAS              # PAS1 now refers to a slice of array MYMAP
>>     <some things happen to PAS1>
>>     B2 = PAS1              # B2 now refers to a slice of array MYMAP
>>     Dx2 = ndimage.gaussian_filter(B2, sigma=[nsmopha, nsmoene]) # Dx2
>>     is a NEW array
>>     B2 = Dx2            # B2 now refers to Dx2.  PAS1 *still* refers
>>     to a slice of array MYMAP
>>     B1 = PAS1           # B1 now refers to a slice of array MYMAP
>>     Dx1 = ndimage.gaussian_filter(B1, sigma=[nsmopha, nsmoene])# Dx1
>>     is a NEW array
>>     B1 = Dx1             # B1 now refers to Dx1
>>
>>     Eric
>>>     2014-03-04 15:13 GMT-05:00 Gabriele Brambilla
>>>     <gb.gabrielebrambilla at gmail.com
>>>     <mailto:gb.gabrielebrambilla at gmail.com>>:
>>>
>>>         But the changes I make on  PAS=MYMAP[:, hhh. :] will be
>>>         applied to G=MYMAP[:, :, :]?
>>>
>>>         Gabriele
>>>
>>>
>>>         2014-03-04 15:07 GMT-05:00 Gabriele Brambilla
>>>         <gb.gabrielebrambilla at gmail.com
>>>         <mailto:gb.gabrielebrambilla at gmail.com>>:
>>>
>>>             thanks.
>>>
>>>             Gabriele
>>>
>>>
>>>             2014-03-04 15:04 GMT-05:00 Eric Hermes
>>>             <ehermes at chem.wisc.edu <mailto:ehermes at chem.wisc.edu>>:
>>>
>>>                 On 3/4/2014 2:00 PM, Gabriele Brambilla wrote:
>>>>                 Hi,
>>>>
>>>>                 Something strange happens in my code: I have added
>>>>                 the lines after #...
>>>>
>>>>                 MYMAP is a 3d numpy array
>>>>                 PAS = MYMAP[:, hhh, :]
>>>>
>>>>                 #multiplying per energy bin width
>>>>                 PAS1 = PAS
>>>>                 for l in nIph:
>>>>                  PAS1[l,:] = PAS1[l,:]*zz
>>>>
>>>>                 But this affect the result I get when I elaborate
>>>>                 the data from:
>>>>
>>>>                 EXCT = MYMAP[:, hhh, :]
>>>>
>>>>                 is python continuing to modify the original matrix??
>>>>
>>>>                 Thanks
>>>>
>>>>                 Gabriele
>>>>
>>>>
>>>>                 _______________________________________________
>>>>                 SciPy-User mailing list
>>>>                 SciPy-User at scipy.org  <mailto:SciPy-User at scipy.org>
>>>>                 http://mail.scipy.org/mailman/listinfo/scipy-user
>>>                 Gabriele,
>>>
>>>                 Numpy arrays are passed as references, so in your
>>>                 code when you modify PAS1 (which inherits the
>>>                 reference to MYMAP from PAS), the changes propogates
>>>                 upwards into MYMAP.  If you want to avoid this, you
>>>                 can explicitly create a copy of the array, either by
>>>                 doing:
>>>
>>>                 PAS = MYMAP[:,hhh,:].copy()
>>>
>>>                 or:
>>>
>>>                 PAS1 = PAS.copy()
>>>
>>>                 Eric
>>>
>>>                 --
>>>                 Eric Hermes
>>>                 J.R. Schmidt Group
>>>                 Chemistry Department
>>>                 University of Wisconsin - Madison
>>>
>>>
>>>                 _______________________________________________
>>>                 SciPy-User mailing list
>>>                 SciPy-User at scipy.org <mailto:SciPy-User at scipy.org>
>>>                 http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>>
>>>
>>>
>>>
>>>     _______________________________________________
>>>     SciPy-User mailing list
>>>     SciPy-User at scipy.org  <mailto:SciPy-User at scipy.org>
>>>     http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>     --
>>     Eric Hermes
>>     J.R. Schmidt Group
>>     Chemistry Department
>>     University of Wisconsin - Madison
>>
>>
>>     _______________________________________________
>>     SciPy-User mailing list
>>     SciPy-User at scipy.org <mailto:SciPy-User at scipy.org>
>>     http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
> --
> Eric Hermes
> J.R. Schmidt Group
> Chemistry Department
> University of Wisconsin - Madison

--
Eric Hermes
J.R. Schmidt Group
Chemistry Department
University of Wisconsin - Madison



More information about the SciPy-User mailing list