text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.impute import SimpleImputer

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import train_test_split

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder

from xgboost import XGBRegressor

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

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

train.shape

target = train.target
X_trainfull, X_validfull, y_train, y_valid = train_test_split(train, target, train_size=0.8, test_size=0.2, random_state=42)

categorical_cols = [cname for cname in X_trainfull.columns if X_trainfull[cname].dtype == 'object']
numerical_cols = [cname for cname in X_trainfull.columns if X_trainfull[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_trainfull[my_cols].copy()
X_valid = X_validfull[my_cols].copy()
X_test = test[my_cols].copy()

numeric = SimpleImputer(strategy='constant')
cat = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant')), ('onehot', OneHotEncoder(handle_unknown='ignore'))])
preprocessor = ColumnTransformer(transformers=[('num', numeric, numerical_cols), ('cat', cat, categorical_cols)])

model = XGBRegressor()
clf = Pipeline(steps=[('preprocessor', preprocessor), ('model', model)])

clf.fit(X_train, y_train)

predictions = clf.predict(X_valid)

from sklearn.model_selection import cross_val_score
scores = -1 * cross_val_score(clf, train, target, cv=5, scoring='neg_mean_absolute_error')
print('MAE scores:\n', scores)