Lesson 09_tabular: ValueError: Number of features of the model must match the input. Model n_features is 15 and input n_features is 14

Hi,

I am running into an error while running 09_tabular with the latest version of the code and fastai 2.1.7 and encountered an error in the 6th code cell of the Using a Neural Network part. Here is the cell:

xs_filt2 = xs_filt.drop(‘fiModelDescriptor’, axis=1)
valid_xs_time2 = valid_xs_time.drop(‘fiModelDescriptor’, axis=1)
m2 = rf(xs_filt2, y_filt)
m_rmse(m, xs_filt2, y_filt), m_rmse(m2, valid_xs_time2, valid_y)

And here is the error:


ValueError Traceback (most recent call last)
in
2 valid_xs_time2 = valid_xs_time.drop(‘fiModelDescriptor’, axis=1)
3 m2 = rf(xs_filt2, y_filt)
----> 4 m_rmse(m, xs_filt2, y_filt), m_rmse(m2, valid_xs_time2, valid_y)

in m_rmse(m, xs, y)
1 def r_mse(pred,y): return round(math.sqrt(((pred-y)**2).mean()), 6) #jdm: the last arg here (6) is the number of decimal points
----> 2 def m_rmse(m, xs, y): return r_mse(m.predict(xs), y)

/opt/conda/envs/fastai/lib/python3.8/site-packages/sklearn/ensemble/_forest.py in predict(self, X)
781 check_is_fitted(self)
782 # Check data
–> 783 X = self._validate_X_predict(X)
784
785 # Assign chunk of trees to jobs

/opt/conda/envs/fastai/lib/python3.8/site-packages/sklearn/ensemble/_forest.py in validate_X_predict(self, X)
419 check_is_fitted(self)
420
–> 421 return self.estimators
[0]._validate_X_predict(X, check_input=True)
422
423 @property

/opt/conda/envs/fastai/lib/python3.8/site-packages/sklearn/tree/_classes.py in validate_X_predict(self, X, check_input)
394 n_features = X.shape[1]
395 if self.n_features
!= n_features:
–> 396 raise ValueError("Number of features of the model must "
397 "match the input. Model n_features is %s and "
398 "input n_features is %s "

ValueError: Number of features of the model must match the input. Model n_features is 15 and input n_features is 14

Any ideas?

PS: for the time being I just ignored it since it does not impact the rest of the program…

Thanks,

1 Like

There is a typo. You need to pass the new model you just trained, m2 to m_rmse. Instead you are pasing just m, which is a model trained before dropping fiModelDescriptor feature.

1 Like

Thanks!