0

I have an assignment where they slice the data we get like a_list[:,:-1], a_list[:,-1]

The data in the csv file looks like this (first two lines):

6.7240e-02,0.0000e+00,3.2400e+00,0.0000e+00,4.6000e-01,6.3330e+00,1.7200e+01,5.2146e+00,4.0000e+00,4.3000e+02,1.6900e+01,3.7521e+02,7.3400e+00,2.2600e+01
9.2323e+00,0.0000e+00,1.8100e+01,0.0000e+00,6.3100e-01,6.2160e+00,1.0000e+02,1.1691e+00,2.4000e+01,6.6600e+02,2.0200e+01,3.6615e+02,9.5300e+00,5.0000e+01

Code looks like this:

train_data = numpy.loadtxt("data.csv", 
delimiter=",")
X_train, t_train = train_data[:,:-1], train_data[:,-1]

When printing X_train and t_train respectively

enter image description here

Still not quite sure [:,:-1] and [:,-1] does

Lubbi
  • 91
  • 1
  • 6
  • 1
    `[:]` means all rows, `[ : , -1]` means last element in all rows, `[ : , :-1 ]` means all rows with all elements in rows except last one. – furas Nov 20 '19 at 15:58
  • 2
    get some small array always with the same values (not random) and test slices. You will see what it gives you. – furas Nov 20 '19 at 15:59
  • Thanks furas! And yeah makes sense, will do next time! – Lubbi Nov 20 '19 at 16:04

2 Answers2

4

Get small array with small values ie.

import numpy as np

arr = np.array([ 
    [1,2,3], 
    [4,5,6], 
    [7,8,9] 
])

and test it.


print( arr[ : , -1 ] )

array([3, 6, 9])

It gives last column - last element in row ([-1]) in all rows ([:])


print( arr[ : , :-1 ] )

array([[1, 2],
       [4, 5],
       [7, 8]])

It gives all except last column - all elements in row except last one ([:-1]) in all rows ([:]).

furas
  • 134,197
  • 12
  • 106
  • 148
0

[:,:-1]: This will take all rows and all but the last column.

[:,-1] : This will take all rows and only the last column.

: denotes "all", and -1 in indexing means the last row/column.

However, ":-1" in [:,:-1] does arithmetic operation of indexes - which here means 'total no of columns - 1'.

a_r
  • 488
  • 6
  • 12
  • 1
    I think there is an error here. "[:,-1]: This will take all rows and all columns." --> I think it should be all rows and the last column only. – MuneshSingh May 20 '22 at 16:33
  • Thanks for pointing it out. I have made the changes. – a_r May 22 '22 at 16:44