text_html_output_1.png
from sklearn.preprocessing import OneHotEncoder

import pandas as pd

train = pd.read_csv('../input/30-days-of-ml/train.csv')
train

train.drop('id', axis=1, inplace=True)

na_count = pd.Series([train[col].isna().sum() for col in train.columns], index=train.columns, name='NA Count')
na_count

y = train['target']
X = train.drop('target', axis=1)

num_cols = [col for col in X.columns if X[col].dtype in ['int64', 'float64']]
cat_cols = [col for col in X.columns if X[col].dtype == 'object' and X[col].nunique() < 10]

cardinality = pd.Series([X[col].nunique() for col in cat_cols], index=cat_cols, name='Cardinality')
cardinality

from sklearn.preprocessing import OneHotEncoder
OHE = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train = pd.DataFrame(OHE.fit_transform(X_train[cat_cols]))
OH_cols_valid = pd.DataFrame(OHE.transform(X_valid[cat_cols]))
OH_cols_train.index = X_train.index
OH_cols_valid.index = X_valid.index
num_X_train = X_train.drop(cat_cols, axis=1)
num_X_valid = X_valid.drop(cat_cols, axis=1)
OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)

test = pd.read_csv('../input/30-days-of-ml/test.csv')
test