I'm working on the Titanic Dataset as my first Kaggle Project. But I get this User Warning And I'm trying to find out how to get rid of it.
So I made two Preprocessing sub-pipelines:
num_pipeline = Pipeline([
('imputer', SimpleImputer( strategy='median')),
('scaler', StandardScaler()) ])
cat_pipeline = Pipeline([
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
('onehot', OneHotEncoder()) ])
My selected features for building the model are :
numeric_features = ['Age', 'SibSp', 'Parch', 'Fare']
categorical_features = ['Pclass', 'Sex', 'Embarked']
My Preprocessing Pipeline is :
preprocessor = ColumnTransformer(
transformers = [
('num', num_pipeline, numeric_features),
('cat', cat_pipeline, categorical_features)
])
In my final Pipeline I add a classifier:
clf = Pipeline([
('Preprocessor' , preprocessor),
('Classifier', DecisionTreeClassifier()) ])
Then i call cross_val_score to evaluate the model, and this is when I get the User Warning:
cross_val_score(clf, X_train, y_train, cv=3, scoring="accuracy")
ValueError: Found unknown categories ['Missing'] in column 2 during transform UserWarning,
array([ nan, 0.70403587, 0.74774775])
My guess is the cross_val_score gets the first fold WITHOUT the 'Missing' category and then test it on another WITH the 'Missing' category.Hence the Error. So I tried to drop the rows with the missing values in 'Embarked', but still get the error which is weird.