Mailing List Archive

Why can't numpy array be restored to saved value?
Why isn't the final value of the numpy array npary in the following code the
same as the initial value before some but not all elements of the array were
changed to a new value?

I know I am missing something basic here. I thought I understood the
concepts of immutable vs mutable values but obviously I missed something.

My environment is Win10-64, Python 3.8.5, numpy 1.19.2.

Code and output follows. TIA for any help you can provide to cure my
ignorance.

Peter

--- nptest.py ---
import numpy as np
import sys

if len(sys.argv) > 0:
try:
asz = int(sys.argv[1]) + 0
except:
asz = 4

npary = np.full([asz, asz, asz], 0, dtype=np.int32)
print("Array before change=\n{}".format(npary))
svary = npary[:, :, :]
npary[1:-1, 1:-1, 1:-1] = 1
print("Array after change=\n{}".format(npary))
npary = svary[:, :, :]
print("Array after restore=\n{}".format(npary))
--- nptest.py ---

--- output ---
Array before change=
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
Array after change=
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 0 0]]

[[0 0 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
Array after restore=
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 0 0]]

[[0 0 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
--- output ---


--
https://mail.python.org/mailman/listinfo/python-list
RE: Why can't numpy array be restored to saved value? [ In reply to ]
Never mind, I found the numpy.copy function does what I need. Revised code
below works.

Sorry for wasting bandwidth.

Peter

--- nptest.py ---
import numpy as np
import sys

if len(sys.argv) > 0:
try:
asz = int(sys.argv[1]) + 0
except:
asz = 4

npary = np.full([asz, asz, asz], 0, dtype=np.int32)
print("Array before change=\n{}".format(npary))
svary = np.copy(npary, order='C')
npary[1:-1, 1:-1, 1:-1] = 1
print("Array after change=\n{}".format(npary))
npary = svary
print("Array after restore=\n{}".format(npary))
--- nptest.py ---

> -----Original Message-----
> From: pjfarley3@earthlink.net <pjfarley3@earthlink.net>
> Sent: Wednesday, November 25, 2020 1:48 AM
> To: 'python-list@python.org' <python-list@python.org>
> Subject: Why can't numpy array be restored to saved value?
>
> Why isn't the final value of the numpy array npary in the following code
the
> same as the initial value before some but not all elements of the array
were
> changed to a new value?
>
> I know I am missing something basic here. I thought I understood the
concepts
> of immutable vs mutable values but obviously I missed something.
>
> My environment is Win10-64, Python 3.8.5, numpy 1.19.2.
>
> Code and output follows. TIA for any help you can provide to cure my
> ignorance.
>
> Peter
>
> --- nptest.py ---
> import numpy as np
> import sys
>
> if len(sys.argv) > 0:
> try:
> asz = int(sys.argv[1]) + 0
> except:
> asz = 4
>
> npary = np.full([asz, asz, asz], 0, dtype=np.int32)
> print("Array before change=\n{}".format(npary))
> svary = npary[:, :, :]
> npary[1:-1, 1:-1, 1:-1] = 1
> print("Array after change=\n{}".format(npary))
> npary = svary[:, :, :]
> print("Array after restore=\n{}".format(npary))
> --- nptest.py ---
>
> --- output ---
> Array before change=
> [[[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]]
> Array after change=
> [[[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 1 1 0]
> [0 1 1 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 1 1 0]
> [0 1 1 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]]
> Array after restore=
> [[[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 1 1 0]
> [0 1 1 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 1 1 0]
> [0 1 1 0]
> [0 0 0 0]]
>
> [[0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]
> [0 0 0 0]]]
> --- output ---


--
https://mail.python.org/mailman/listinfo/python-list
Re: Why can't numpy array be restored to saved value? [ In reply to ]
On 25/11/20 7:47 pm, pjfarley3@earthlink.net wrote:
> Why isn't the final value of the numpy array npary in the following code the
> same as the initial value before some but not all elements of the array were
> changed to a new value?

Slicing a numpy array doesn't copy anything, it just
gives you another view of the underlying data.

This is a trap you need to watch out for, since it's
different from the way sequences normally behave in
Python.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list
Re: Why can't numpy array be restored to saved value? [ In reply to ]
Am 25.11.20 um 07:47 schrieb pjfarley3@earthlink.net:
> Why isn't the final value of the numpy array npary in the following code the
> same as the initial value before some but not all elements of the array were
> changed to a new value?
>
> I know I am missing something basic here. I thought I understood the
> concepts of immutable vs mutable values but obviously I missed something.
>
> My environment is Win10-64, Python 3.8.5, numpy 1.19.2.
>
> Code and output follows. TIA for any help you can provide to cure my
> ignorance.
>
> Peter
>
> --- nptest.py ---
> import numpy as np
> import sys
>
> if len(sys.argv) > 0:
> try:
> asz = int(sys.argv[1]) + 0
> except:
> asz = 4
>
> npary = np.full([asz, asz, asz], 0, dtype=np.int32)
> print("Array before change=\n{}".format(npary))
> svary = npary[:, :, :]

Because this does not copy the array, rather it creates a view into the
original array. This is an optimization to avoid copying. If you want a
copy, do svary = npary.copy()

Christian
--
https://mail.python.org/mailman/listinfo/python-list
RE: Why can't numpy array be restored to saved value? [ In reply to ]
> -----Original Message-----
> From: Greg Ewing <greg.ewing@canterbury.ac.nz>
> Sent: Thursday, November 26, 2020 12:01 AM
> To: python-list@python.org
> Subject: Re: Why can't numpy array be restored to saved value?
>
> On 25/11/20 7:47 pm, pjfarley3@earthlink.net wrote:
> > Why isn't the final value of the numpy array npary in the following code the
> > same as the initial value before some but not all elements of the array were
> > changed to a new value?
>
> Slicing a numpy array doesn't copy anything, it just
> gives you another view of the underlying data.
>
> This is a trap you need to watch out for, since it's
> different from the way sequences normally behave in
> Python.
>
> --
> Greg

Thank you for that explanation. I will certainly watch out for it in the future.

Peter

--
https://mail.python.org/mailman/listinfo/python-list
RE: Why can't numpy array be restored to saved value? [ In reply to ]
> -----Original Message-----
> From: Christian Gollwitzer <auriocus@gmx.de>
> Sent: Thursday, November 26, 2020 3:26 AM
> To: python-list@python.org
> Subject: Re: Why can't numpy array be restored to saved value?
>
> Am 25.11.20 um 07:47 schrieb pjfarley3@earthlink.net:
> > Why isn't the final value of the numpy array npary in the following code the
> > same as the initial value before some but not all elements of the array were
> > changed to a new value?
> >
> > I know I am missing something basic here. I thought I understood the
> > concepts of immutable vs mutable values but obviously I missed something.
> >
<Snipped>
>
> Because this does not copy the array, rather it creates a view into the
> original array. This is an optimization to avoid copying. If you want a
> copy, do svary = npary.copy()

Thank you for the explanation. I did indeed find that using the copy() function did what I needed to do.

Peter

--
https://mail.python.org/mailman/listinfo/python-list