0

Thank you for your time. We have already seen a comprehensive discussion of slicing multi-dimension arrays which were of this structure:

newvariable1 = myvariable1[start:stop]
newvariable2 = myvariable2[start:stop:step]

These were discussed comprehensively at Understanding slice notation.

The "start","stop" and "step" may be positive or negative integers or a list of numbers.

I am interested in a further comprehensive version of slicing which involves the slicing operator ":", the comma operator ":", and variables before and/or after the comma operator.

newvariable1 = myarray1[var1,var2:var3,var4]
newvariable2 = myarray2[var1,var2:var3,var4:var5,var6:var7,var8]

Note that not all variables are present in the operation. It is to show that variables be they integers or lists before and after a comma "," AND before and after the slicing operator have a significant result.

I am looking for a systematic method of including commas and the effect of vars before and after a comma operator ",".

I have even attempted myself a combination of slicing and selection at the bottom of this page under my nom de plume "Anthony The Koala" at https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/.

Here is an example, with a 3x3 matrix called "doo". I did do my own work in playing around with slicing and using indexes.

doo; # We could also say doo[:]
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]],

       [[25, 26, 27, 28],
        [29, 30, 31, 32],
        [33, 34, 35, 36]]])

Here is a sample of selection and slicing

#Specific columns of all submatrices 3x3 -please relate this to the 3x4x3 matrix
doo[:,:,0] #First column of all submatrices
array([[ 1,  5,  9],
       [13, 17, 21],
       [25, 29, 33]])

doo[:,:,1] #Second column of all submatrices
array([[ 2,  6, 10],
       [14, 18, 22],
       [26, 30, 34]])

Also

#Specific columns of all submatrices 3x3x3
doo[:,:,[0,1,3]]; #First, second and fourth cols of all submatrices
array([[[ 1,  2,  4],
        [ 5,  6,  8],
        [ 9, 10, 12]],

       [[13, 14, 16],
        [17, 18, 20],
        [21, 22, 24]],

       [[25, 26, 28],
        [29, 30, 32],
        [33, 34, 36]]])

If those commas were not present the result is different.

Another example is at http://ataspinar.com/2018/12/21/a-guide-for-using-the-wavelet-transform-in-machine-learning/ I am not after a tutorial on wavelet transformation. The example is to demonstrate the use of slicing, commas and integers.

test_data_cwt = np.ndarray(shape=(test_size, 127, 127, 9))
for ii in range(0,test_size):
    if ii % 100 == 0:
        print(ii)
    for jj in range(0,9):
        signal = uci_har_signals_test[ii, :, jj]; # ii before ",", jj before ","
        coeff, freq = pywt.cwt(signal, scales, waveletname, 1)
        coeff_ = coeff[:,:127]; # The purpose of the ","
        test_data_cwt[ii, :, :, jj] = coeff_ ;# ii before ",", jj before ","

Note the slicing operation involving integers using variables before and after the comma operator "," and before and after the slicing operator ":".

I am asking for a systematic/comprehensive application of the significance of the position of the variable before and after the comma:

signal = uci_har_signals_test[ii, :, jj]
What is "ii" and "jj" in respect of slicing and what happens if ii were after the comma and jj were before the comma.

coeff_ = coeff[:,:127]
What is the meaning of the ","?

test_data_cwt[ii, :, :, jj] = coeff_

Again, I am interested in the meaning of the structure of arrays/matrices when wanting to obtain a subset of the original matrix as I ask the original question.

newvariable1 = myarray1[var1,var2:var3,var4]
newvariable2 = myarray2[var1,var2:var3,var4:var5,var6:var7,var8]

In summary:

While I know about slicing, and having been able to work out how to get a subset of a 3x3 matrix using slicing and a limited amount of use of the comma "," operator and integers and/or lists, I still need help to understand more fully the concept of using numbers/lists before and after a comma operator "," and before and after the slicing operator ":".

Thank you, Anthony of Sydney

  • Commas are used in numpy arrays, not list slices. – Barmar Nov 21 '19 at 12:27
  • This is a very vague question. Could you clarify exactly what you don't understand? There's nothing special about how variables are used, they just get replaced with their values. – Barmar Nov 21 '19 at 12:29
  • Thank you for the reply. However, I am looking for a comprehensive answer to using commas and slicing. Whether you use commas or slicing, the aim is to extract a subset of the original data matrix, whether it is 3x3 or 2x2 or any other size. I have seen applications of commas and slices used together and the difference between the position before and after the comma in a slicing operation makes a difference. So I am asking for a comprehensive application of using commas and slicing operators. I did have a go at another site. BUT want something comprehensive. Please address first part. Thanks. – Anthony from Sydney Nov 23 '19 at 03:23
  • Just to give you an example of the application of using commas and slicing at http://ataspinar.com/2018/12/21/a-guide-for-using-the-wavelet-transform-in-machine-learning/ where we see examples of coeff[:,:127], train_data_cwt[ii, :, :, jj] = coeff_ . Notice combination of using variables and commas and the slicing operator. Even in my earlier mentioned contribution, at as "Anthony the Koala" https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ I had doo[:,:,[0,1]] and doo[0:,1]. That is why I want a systematic elaboration of my first part. Thanks – Anthony from Sydney Nov 24 '19 at 05:51

0 Answers0