'Slicing: What does [:,:-1] and [:,-1] mean?
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
Still not quite sure [:,:-1] and [:,-1] does
Solution 1:[1]
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 ([:]).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | furas |

