Reputation: 73
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
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