Extract of matrix subpart using a deep copy - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Extract of matrix subpart using a deep copy (/thread-17976.html) |
Extract of matrix subpart using a deep copy - paul18fr - May-01-2019 Hi, I remember that in Python "A = B" corresponds to a "shallow" copy; in other word if I change any cell in B, then the same cell is modified in A. I'm trying to extract rows in B using a deep copy at the same time; between the 2 following trials, the structure of the 2 arrays is different (1 more dimension in the trial 1): why such behaviour? What's the correct syntax? Thanks Paul import numpy as np A = np.random.randint(10, size = (100,10), dtype = np.int); index = np.where(A[:,0] == 1); Extract_trial1 = np.copy(A[index,:]); del index; Extract_trial2 = np.copy(A); index = np.where(A[:,0] != 1); Extract_trial2 = np.delete(Extract_trial2,index,axis=0);First matrix: dimension here (1,12,10) Quote:array([[[1, 6, 1, 5, 0, 4, 8, 2, 4, 3], Second matrix: dimension here (12,10) Quote:array([[1, 6, 1, 5, 0, 4, 8, 2, 4, 3], RE: Extract of matrix subpart using a deep copy - scidam - May-02-2019 In case of Extract_trial1 , when you invoke A[index, :] it triggers advanced indexing of Numpy.You can read about advanced indexing [here](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html). Adv. indexing always returns a copy of the data, so using np.copy is redundant here. Advanced indexingis triggered because you pass an array of integers to A[...] . From official docs: You can inspect this by printing shape of the index variable (it is randomly changed between runs):index = np.where(A[:,0] == 1) print(np.array(index).shape)Lets look at the advanced indexing broadcasting formula: result[i_1, ..., i_M] == x[ind_1[i_1, ..., i_M], ind_2[i_1, ..., i_M], ..., ind_N[i_1, ..., i_M]] ind_1 is your index variable, (ind_2 = ':' in your case, that is simple indexing); ind_1 has shape (1, small random integer) , so result shape will be (1, small_random_integer, 10) . This is what you are having regarding Extract_trial1 .You can try the following examples: A[[1,2,3], :] => shape = (3, 10) A[[[1,2,3],], :] => shape (1, 3, 10) A[[[[1,2,3],]], :] => shape (1, 1, 3, 10)To fix this behavior you need to pass 1d array of indices to A[...] , i.e. A[index[0], :] .
RE: Extract of matrix subpart using a deep copy - paul18fr - May-02-2019 Thanks scidam for the detailled explanations and the link; let me digging into it. Paul |