I am trying to do a Random Forest in a dataset with numerical and categorical variables in order to obtain a categorical result (two possible classes, column name "predicción"). I am using the scikit-learn library in Jupyter notebook.

I have done the train-test split like this:

 X_train, X_test, y_train, y_test = train_test_split(datos.drop(columns = 'predicción'), datos['predicción'],random_state = 123)

then I made two lists with the column names that hold numerical or categorical values:

cat_cols = X_train.select_dtypes(include=['object', 'category']).columns.to_list()
numeric_cols = X_train.select_dtypes(include=['float64', 'int']).columns.to_list()

cat = list(np.array(cat_cols).reshape(1,9))

I then do the encoding using ColumnTransformer:

niveles = ['0', '1', '2']
encoder = ColumnTransformer([('ordinal', OrdinalEncoder(categories=[niveles]), cat)],remainder='passthrough')

So far so good, no errors up to this point. The error rises when I use the fit_transform:

X_train = encoder.fit_transform(X_train)
X_test  = encoder.fit_transform(X_test)

I have not been able to find a solution to this problem or any alternative. I am fairly new to machine learning if that can be an excuse. Any bit of help is welcome!

  • $\begingroup$ Remove the fit_transform from the test data set and just fit an transform on the test. That will fix it! $\endgroup$ Nov 5 '21 at 11:15

You are using the fit_transform method on both the training and test dataset which is incorrect. You should only use fit_transform on the training dataset (since the encoder should only fit/learn from data in the training dataset and not the test dataset as this would be new information when deploying your model) and then use transform to apply the encoder on the test dataset.

  • $\begingroup$ I've changed the code to: 'X_train_fit = encoder.fit(X_train) X_train_trans = X_train_fit.transform(X_train)' I still get the same error, i don't know if I'm using the methods right. Thank you for your quick answer! $\endgroup$
    – Jose Cle
    Nov 5 '21 at 11:56
  • $\begingroup$ Like I said, try using fit_transform for the training data and transform for the test data. In addition, check the data types of your X_train, cat variables and try changing categories=[niveles] to categories=niveles since niveles already is a list. $\endgroup$
    – Oxbowerce
    Nov 5 '21 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.