I was trying to solve the 2024 Kaggle Playground Series and I had to handle some columns with missing values. To do this tried to perform an imputation inside the pipeline. But the imputation isn't working, I have checked it multiple times but cannot figure out why it is happening. Can you please help me in this?
import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OrdinalEncoder, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
data = pd.read_csv('/kaggle/input/playground-series-s4e8/train.csv')
data.drop([
'stem-root',
'veil-type',
'veil-color',
'spore-print-color',
], axis = 1, inplace = True) #more than 80% null
length = data.shape[0]
acceptable_cols = [col for col in data.columns
if data[col].isnull().sum()/data.shape[0]*100 <= 1]
trainSet = data[acceptable_cols].copy().sample(frac = 0.1, random_state = 0)
X = trainSet.drop(['class', 'id'], axis = 1)
y = trainSet['class']
cat_cols = X.select_dtypes('object').columns
num_cols = list(set(X.columns) - set(cat_cols)) ###################Selection of number columns
transformations = ColumnTransformer(transformers = [
('oe', OrdinalEncoder(), cat_cols),
('impute & scale', Pipeline([
('impute', SimpleImputer()), ############################# The imputer
('scaler', StandardScaler())
]), num_cols)
])
pipeline = Pipeline([
('transformations', transformations),
('gbr', GradientBoostingClassifier(verbose = True))
])
X_train, X_val, y_train, y_val = train_test_split(X, y)
pipeline.fit(X_train, y_train) ###########################the line where it shows the error
pipeline.score(X_val, y_val)
It always shows the error:
Input X contains NaN.
GradientBoostingClassifier does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-values
Thanks in advance for helping me.
pipeline.fit
orpipeline.score
? Can you identify which columns appear to still have NaN, by runningtransformations.transform(X_train
)`? $\endgroup$