Ritesh Singh
Ritesh Singh

Reputation: 73

How to use standard scaler model on dataset having less features than original dataset in which it was initially trained

I was using standard scalar model from sklearn.preprocessing. I fitted the standard scaler model on the dataset having 27 features in it. Is it possible to use same standard scalar model on a testing dataset having less than 27 features in it Code Snippet

from sklearn.preprocessing import StandardScaler()
sc=StandardScaler()
sc.fit_transform(x_train)

Till this point this is working fine.Problem is arising when I am trying to transform my test dataset. I know the problem why it is happening so. The test dataset has 24 features in it. But is it possible to transform the only 24 features and ignoring those columns which are not present in it.

sc.transform(x_test)

Thanks in advance!!

Upvotes: 2

Views: 430

Answers (1)

jezrael
jezrael

Reputation: 862741

If want select all features without first 3 features use DataFrame.iloc:

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()

x_train.iloc[:, 3:] = sc.fit_transform(x_train.iloc[:, 3:])
print (x_train)

If features are in list use subset:

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()

features = ['col1','col2',..., 'col24']
x_train[features] = sc.fit_transform(x_train[features])
print (x_train)

Upvotes: 2

Related Questions