In Python how do I make a copy of a numpy Matrix column such that any further operations to the copy does not affect the original matrix?
<p>I would normally copy a whole matrix as follows:</p>

<pre><code>from copy import copy, deepcopy
b=np.array([[2,3],[1,2]])
a = np.empty_like (b)
a[:] = b
</code></pre>

<p>(Note a and b are not what I am using in my code and are just made up for this example). But how do I copy just the first column (or any selected column) of a matrix so that when operated on it does not affect the original column?</p>

<p>PS. I am new so sorry If I am making a really stupid error but I really have searched for the solution a long time</p>

<p>Just use indexing to slice a column then use <code>copy()</code> attribute of array object to create a copy:</p>

<pre><code>&gt;&gt;&gt; b=np.array([[2,3],[1,2]])
&gt;&gt;&gt; b
array([[2, 3],
       [1, 2]])
&gt;&gt;&gt; a = b[:,0].copy()
&gt;&gt;&gt; a
array([2, 1])
&gt;&gt;&gt; a += 2
&gt;&gt;&gt; a
array([4, 3])
&gt;&gt;&gt; b
array([[2, 3],
       [1, 2]])
</code></pre>

python arrays numpy matrix copy