DEV Community

Stefano Mariani
Stefano Mariani

Posted on

NaN giving ValueError in OneHotEncoder in scikit-learn

Here is my code

import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder

train = pd.DataFrame({
        'users':['John Johnson','John Smith','Mary Williams']
})
test = pd.DataFrame({
        'users':[None,np.nan,'John Smith','Mary Williams']
})

ohe = OneHotEncoder(sparse=False,handle_unknown='ignore')
ohe.fit(train)
train_transformed = ohe.fit_transform(train)

test_transformed = ohe.transform(test)
print(test_transformed)

I expected the OneHotEncoder to be able to…

Top comments (1)

Collapse
 
smarianimore profile image
Stefano Mariani

Anyone willing to answer this?