text_plain_output_1.png
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/30-days-of-ml/train.csv', index_col='id')
test = pd.read_csv('../input/30-days-of-ml/test.csv', index_col='id')
full_df = train.copy()
fulltest_df = test.copy()
preprocessing1 = 'none'
preprocessing2 = 'none'
y = train.target
train.drop(['target'], axis=1, inplace=True)

preprocessing1 = 'ordinal encoding'
Cfeatures = [col for col in useful_features if 'cat' in col]
ordinal_encoder = OrdinalEncoder()
train[Cfeatures] = ordinal_encoder.fit_transform(train[Cfeatures])
test[Cfeatures] = ordinal_encoder.transform(test[Cfeatures])

preprocessing1 = 'ordinal & ione-hot encoding'
Nfeatures = [cname for cname in train.columns if train[cname].dtype in ['int64', 'float64']]
Cfeatures = [col for col in useful_features if 'cat' in col]
low_card_columns = [cname for cname in train.columns if train[cname].nunique() < 10 and train[cname].dtype == 'object']
high_card_columns = [cname for cname in train.columns if train[cname].nunique() >= 10 and train[cname].dtype == 'object']
Xtrain = train.copy()
Xtest = test.copy()
ordinal_encoder = OrdinalEncoder()
train[high_card_columns] = ordinal_encoder.fit_transform(train[high_card_columns])
test[high_card_columns] = ordinal_encoder.transform(test[high_card_columns])
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(train[low_card_columns]))
OH_cols_test = pd.DataFrame(OH_encoder.transform(test[low_card_columns]))
OH_cols_train.index = train.index
OH_cols_test.index = test.index
all_cols = OH_cols_train.columns
new_cols = [i for i in all_cols if isinstance(i, (int, float))]
OH_cols_train = OH_cols_train[new_cols].add_prefix('cat_encode_')
OH_cols_test = OH_cols_test[new_cols].add_prefix('cat_encode_')
num_X_train = train.drop(low_card_columns, axis=1)
num_X_test = test.drop(low_card_columns, axis=1)
train = pd.concat([num_X_train, OH_cols_train], axis=1)
test = pd.concat([num_X_test, OH_cols_test], axis=1)

PreprocessPerformanced_df = pd.DataFrame(columns=['modelname', 'preprocessing1', 'preprocessing2', 'datashape', 'trainsize', 'mean_squared_error'])
modelname = 'XGBRegressor'
preprocessing1 = 'none'
preprocessing2 = 'none'
print('Dataframe created')