Reputation: 5444
I am trying to perform feature selection in python using several techniques. The first technique that I am trying to apply is selecting features using only the varience of the features. My code is the following:
def feature_selection_technique(train, test, lbls, technique):
if technique == "variance":
sel = VarianceThreshold(threshold=(0.00010 * (1 - .15)))
model1 = sel1.fit_transform(face_train)
new_train = model1.transform(train)
new_test = model1.transform(test)
return new_train, new_test
Actually I want to calculate the selected features using the train dataset and then to apply it to the test dataset. It seems that the transform method cannot be aoolied in that case. What can I do in that case?
Upvotes: 1
Views: 801
Reputation: 19634
I think that there is a problem in the syntax you are using. See documentation and example here. The correct syntax is as follows:
def feature_selection_technique(train, test, lbls, technique):
if technique == "variance":
sel = VarianceThreshold(threshold=(0.00010 * (1 - .15)))
new_train=sel.fit_transform(train)
new_test = sel.transform(test)
return new_train, new_test
That is, you should initialize sel
, then fit it to the training data and transform it, and then transform the test data.
Upvotes: 1