Q4: Error in CrossValidate : Input contains NaN

Hello,
I have been going around in circles for hours on this error in question 4: Inputs contains NaN.
I tried with just one fold with a fit method it’s the same problem.
I reread the forum questions but … I loop.

Here is an extract

categorical_transformer = OrdinalEncoder()
numerical_transformer = SimpleImputer()

preprocessor = ColumnTransformer(transformers = [
    ('ordinal-encoder', categorical_transformer, categorical_columns),
    ('standard-scaler', numerical_transformer, numerical_columns)])
model_global = make_pipeline(preprocessor, DecisionTreeRegressor(random_state=0))
cv_results_global = cross_validate(model_global, data, target, cv = 10,
                         return_train_score=True,error_score="raise")

Thank you for help.

By passing error_score="raise" should raise an error. If you could provide this error, it would be great.
If you could also extend your full code snippet, it could be better. With the current information, I have too little to be able to reproduce or debug what is wrong :slight_smile:

1 Like

Hi,

I got the same error. After spending several time, I solved it by making a SimpleImputer before the ordinal encoder in the transformer for the categorical columns. My explanation : the NaN values will “disturb” the model.

I put these lines :

categorical_processor =  make_pipeline(SimpleImputer(strategy="most_frequent"), 
                                       OrdinalEncoder(handle_unknown="use_encoded_value",
                                                      unknown_value=-1))
numerical_processor = make_pipeline(StandardScaler(), SimpleImputer())

I don’t know if @glemaitre58 will agree with such explanation?

I hope that my post will help you :wink:

1 Like

I agree if the error mentions that the category " Holand-Netherland" is unknown at predict :slight_smile: But I just want to be sure that this is indeed the same error.

Thank you metssye, that’s great !!!

Thank you for answering, as metssie says, it’s necessary to value the Nan with sth…