Data read from text

In the dataset the Last column is Y_data and other remaining columns are x_train_data
While slicing and converting it into tensor

Question: What does xy[:, 0:-1] or xy[:, [-1]] mean?

from torch import nn, optim, from_numpy
import numpy as np

xy = np.loadtxt(’./data/diabetes.csv.gz’, delimiter=’,’, dtype=np.float32)
x_data = from_numpy(xy[:, 0:-1])
y_data = from_numpy(xy[:, [-1]])
print(f’X’s shape: {x_data.shape} | Y’s shape: {y_data.shape}’)

It’s just NumPy array slicing. You get a slice of NumPy array by passing different values arr[v1, v2, v3, …] according to the dimension of your data. Here colon means all values across that dimensions. Consider rows as first dimension and columns as the second dimension in your data:

  1. xy[:, 0:-1]: will get you all rows (colon) and all columns except the last column (0:-1).
  2. xy[:, [-1]]: will get you all rows (colon) and last columns ([-1]).

I hope it helps.

1 Like