StandardScaler in Python

307 Views Asked by At

I want to standardize 'x_train'.

The first 'x_train' in the picture is the original data set, and the next 'x_train' below the previous one is standardized.

I just want to standardize the first six columns, so I wrote x_train[:,0:6] during standardization.

However, the result of standardization is obviously unreasonable. Moreover, when I use the mean and standard deviation of 'x_train' to standardize x_test, the result went right. It's weird. I have no idea what's wrong with my code.

Below is my code for standardizing.

enter image description here

1

There are 1 best solutions below

0
Agnij On

Try -

scaler = preprocessing.StandardScaler().fit(x_train.iloc[:, 0:6])

#returning the scaled values to a new variable
X_train_first_six = scaler.transform(x_train.iloc[:, 0:6])
X_test_first_six = scaler.transform(x_test.iloc[:, 0:6])

ref. pandas iloc