Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Extract of matrix subpart using a deep copy paul18fr Programmer named Tim Posts: 16 Threads: 5 Joined: Apr 2019 Reputation: 1 Likes received: 1 #1 May-01-2019, 09:17 PM 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], [1, 7, 7, 3, 8, 9, 2, 8, 7, 8], [1, 3, 7, 9, 3, 1, 2, 2, 8, 8], [1, 9, 5, 7, 9, 5, 2, 3, 2, 5], [1, 2, 0, 8, 9, 4, 7, 2, 0, 1], [1, 3, 4, 4, 4, 6, 6, 1, 0, 4], [1, 8, 1, 4, 2, 7, 0, 6, 9, 0], [1, 1, 6, 3, 1, 4, 3, 2, 2, 4], [1, 8, 0, 4, 2, 7, 1, 3, 7, 5], [1, 4, 7, 9, 0, 2, 1, 4, 5, 2], [1, 5, 6, 8, 9, 2, 0, 4, 6, 6], [1, 6, 2, 4, 4, 2, 3, 2, 1, 2]]]) Second matrix: dimension here (12,10) Quote:array([[1, 6, 1, 5, 0, 4, 8, 2, 4, 3], [1, 7, 7, 3, 8, 9, 2, 8, 7, 8], [1, 3, 7, 9, 3, 1, 2, 2, 8, 8], [1, 9, 5, 7, 9, 5, 2, 3, 2, 5], [1, 2, 0, 8, 9, 4, 7, 2, 0, 1], [1, 3, 4, 4, 4, 6, 6, 1, 0, 4], [1, 8, 1, 4, 2, 7, 0, 6, 9, 0], [1, 1, 6, 3, 1, 4, 3, 2, 2, 4], [1, 8, 0, 4, 2, 7, 1, 3, 7, 5], [1, 4, 7, 9, 0, 2, 1, 4, 5, 2], [1, 5, 6, 8, 9, 2, 0, 4, 6, 6], [1, 6, 2, 4, 4, 2, 3, 2, 1, 2]]) scidam Posts: 422 Threads: 0 Joined: Mar 2018 Reputation: 59 Likes received: 61 #2 May-02-2019, 01:19 AM 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/referen...exing.html). Adv. indexing always returns a copy of the data, so using `np.copy` is redundant here. Advanced indexing is triggered because you pass an array of integers to `A[...]`. From official docs: ``Output:Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool).``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], :]`. paul18fr Programmer named Tim Posts: 16 Threads: 5 Joined: Apr 2019 Reputation: 1 Likes received: 1 #3 May-02-2019, 06:49 AM Thanks scidam for the detailled explanations and the link; let me digging into it. Paul « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post How to extract different data groups from multiple CSV files using python Rafiz 3 234 Jun-04-2019, 05:20 PM Last Post: jefsummers comparing each rows of two matrix PhysChem 10 923 May-17-2019, 06:50 AM Last Post: Gribouillis Inverse of singular matrix Divanova94 4 657 May-11-2019, 02:40 AM Last Post: scidam matrix by vector mcgrim 8 290 May-02-2019, 10:39 AM Last Post: ichabod801 Help in adding confusion matrix Aashish 5 489 Apr-15-2019, 11:45 PM Last Post: scidam counting the occurence of a specified number in a numpy-matrix PhysChem 1 217 Apr-03-2019, 01:37 PM Last Post: PhysChem function matrix mcgrim 14 684 Mar-19-2019, 09:28 PM Last Post: mcgrim PyCM 1.8 released: Machine learning library for confusion matrix statistical analysis sepandhaghighi 0 299 Jan-05-2019, 12:36 PM Last Post: sepandhaghighi Creating matrix counting words in list of strings jazmad 2 433 Dec-23-2018, 05:47 PM Last Post: jazmad Clustering based on a variable and on a distance matrix flucoe 2 571 Dec-16-2018, 09:57 PM Last Post: flucoe

Forum Jump:

Users browsing this thread: 1 Guest(s)