0
$\begingroup$

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]})

df

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


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


transformer = ColumnTransformer(
    transformers=[
        ("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)



pipe.named_steps['onehotenc'].get_feature_names_out().tolist(), 

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

1 Answer 1

1
$\begingroup$

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'?

$\endgroup$

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.