Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Extract of matrix subpart using a deep copy
#1
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]])
Quote
#2
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], :].
Quote
#3
Thanks scidam for the detailled explanations and the link; let me digging into it.

Paul
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  h5py: deep dataset access paul18fr 2 218 Nov-28-2019, 03:43 PM
Last Post: paul18fr
  Free ebook "Deep Learning with PyTorch" ThomasL 0 239 Nov-22-2019, 02:50 PM
Last Post: ThomasL
  3D Object Recognition using Deep Learning chandininair 0 680 Aug-08-2018, 11:29 PM
Last Post: chandininair

Forum Jump:


Users browsing this thread: 1 Guest(s)