2

So I'm working on a project that is using RFECV for feature selection and then doing ridge regression with the selected variables.

The way the data set is structured I have a train_y = dependent variable, train_x = everything else in the data frame (variables are all fine in other models).

Here is the code I'm using:

# Variable Selection with RFECV
lr = LinearRegression()
rfecv = RFECV(estimator = lr, step = 1, cv=StratifiedKFold(train_y, 2), scoring='r2')
selector = rfecv.fit(train_x, train_y)
train_X_new = selector.transform(train_x)
train_Y_new = selector.transform(train_y)

param_grid = {'alpha': sp_rand()}
# create and fit a ridge regression model, testing random alpha values
model = Ridge()
rsearch = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_iter=100)
rsearch.fit(train_X_new, train_Y_new)
expected = train_X_new
predicted = model.predict(train_Y_new)
# summarize the fit of the model
mse = np.mean((predicted-expected)**2)
print "MSE and Model Score: "
print(mse)
print(model.score(train_X_new, train_Y_new))

The code errors out on this line:

train_Y_new = selector.transform(train_y)

with "ValueError: X has a different shape than during fitting." No idea what is causing the error.

Any help/insight is appreciated!

Thanks!

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
mswhitehead
  • 79
  • 1
  • 7

1 Answers1

2

The transform method is used to "Reduce X to the selected features." The intent of this method is to create a new X that includes only the variables relevant to fitting.

You are getting an error because transform only expects inputs whose shape match the X on which it was fit. Y does not have the correct shape, and shouldn't.

There should be no change in your target variable, Y. It does not make sense to call transform on Y, as eliminating features only means changing X.

  • That makes sense. We didn't get a lot of explanation on this one and I'm still flailing around on it. lol – mswhitehead May 02 '17 at 00:52
  • Yes, this was a small piece to it. The code had a considerable amount of more problems beyond this. But definitely answered that part. – mswhitehead May 03 '17 at 13:19
  • If you are satisfied with the answer, please mark it as the accepted answer. If you feel this does not completely answer the question, please let me know what is unclear/missing. – Gregory Toepperwein May 03 '17 at 16:09