1

I am trying to manipulate a copy of a matrix, as follows.

import numpy as np

A = np.matrix([[4, 1, -1, 1], [1, 4, -1, -1], [-1, -1, 5, 1], [1, -1, 1, 3]])

B = A

B[0, 0] = 0

This would change 00-entry of A to 0 as well, but I don't want this to happen. I tried it with np.array() as well, but the same thing happens again. I think this is because A and B are pointing to the same spot in the memory.

I'd appreciate it if anyone let's me know how to prevent A from changing, while B is changed.

1
  • 1
    B = A never creates a copy in python no matter what objects A and B refer to Commented Oct 11, 2018 at 6:00

5 Answers 5

1

You are completely right, A and B point to the same memory allocation in your case.
numpy has a copy function for that what you want to achieve:

B = np.copy(A)
Sign up to request clarification or add additional context in comments.

Comments

0

When you do B = A, B is just a reference to the already existing array A. NumPy doesn't copy straight ahead for efficiency reasons.

So, you should use the arr.copy() to copy the contents of the array as in:

In [9]: B = A.copy()                                                                                                                                                                                               

In [10]: A                                                                                                                                                                                                         
Out[10]: 
matrix([[ 4,  1, -1,  1],
        [ 1,  4, -1, -1],
        [-1, -1,  5,  1],
        [ 1, -1,  1,  3]])

# update an entry in array `B`
In [11]: B[0, 0] = 0                                                                                                                                                                                               

# A is unaffected
In [12]: A                                                                                                                                                                                                         
Out[12]: 
matrix([[ 4,  1, -1,  1],
        [ 1,  4, -1, -1],
        [-1, -1,  5,  1],
        [ 1, -1,  1,  3]])

# change happens only in array B
In [13]: B                                                                                                                                                                                                         
Out[13]: 
matrix([[ 0,  1, -1,  1],
        [ 1,  4, -1, -1],
        [-1, -1,  5,  1],
        [ 1, -1,  1,  3]])

Comments

0

Try this:

  B[:] = A

This will make a deep copy

Comments

0

You need to create the copy of the object. You can do this with numpy.copy since you are working with a numpy object.

So, your code will be like so:

B = np.copy(A)

There are two kinds of copy used with python.

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

They work independent of object type.

from copy import copy, deepcopy

# Shallow copy
b= copy(a)

# Deep copy of object
b= deepcopy(a)

Comments

0

You are correct that right now your B and A are referencing the same spot in the memory. For your problem I would recommend

 numpy.matrix.copy

check out the documentation here: https://docs.scipy.org/doc/numpy-1.10.4/reference/generated/numpy.matrix.copy.html

There is an example there with exactly what you are looking for:

x = np.array([[1,2,3],[4,5,6]], order='F')
y = x.copy()

x.fill(0)

x =  array([[0, 0, 0],[0, 0, 0]])
y = array([[1, 2, 3],[4, 5, 6]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.