I'm trying to fit a dataframe with SkLearn DecisionTree with the following code. But I get a error Length of feature_names, 9 does not match number of features, 8. The DecisionTree seems to have only fitted categorical features after transformed by onehotencoding, not the numerical feature. How can I include the numerical feature in the decisiontree model?

import pandas as pd
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn import tree
from matplotlib import pyplot as plt
import graphviz 
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder,StandardScaler
from sklearn.compose import ColumnTransformer, make_column_transformer
from sklearn.linear_model import LinearRegression

df = pd.DataFrame({'brand'      : ['aaaa', 'asdfasdf', 'sadfds', 'NaN'],
                   'category'   : ['asdf','asfa','asdfas','as'], 
                   'num1'       : [1, 1, 0, 0] ,
                   'target'     : [1,0,0,1]})


dfeatures=df.drop('target', axis=1)

num = dfeatures.select_dtypes(include=["int64"]).columns.tolist()
cat = dfeatures.select_dtypes(include=["object"]).columns.tolist()

transformer = ColumnTransformer(
        ("cat", OneHotEncoder(),  cat),

clf= DecisionTreeClassifier(criterion="entropy", max_depth = 5)

pipe = Pipeline(steps=[
                ('onehotenc', transformer),
                ('decisiontree', clf)

#Fit the training data to the pipeline
pipe.fit(dfeatures, dtarget)


dot_data= tree.export_graphviz(clf,
                     feature_names = num + pipe.named_steps['onehotenc'].get_feature_names_out().tolist(), 
                     class_names= ['1', '0'],
                     filled = True)

1 Answer 1


It's probably due to ColumnTransformer 'remainder' parameter. If you see on the docs, ColumnTransformer will drop the features that wasn't specified.

remainder{‘drop’, ‘passthrough’} or estimator, default=’drop’ By default, only the specified columns in transformers are transformed and combined in the output, and the non-specified columns are dropped. (default of 'drop'). By specifying remainder='passthrough', all remaining columns that were not specified in transformers will be automatically passed through.

In your case, since you only mentioned the categorical columns, the remaining columns might be dropped. Perhaps try setting remainder='passthrough'?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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