Kaggle competition: House Prices - Advanced Regression Techniques
import numpy as np
import random
import pandas as pd
import pydicom
import os
import matplotlib.pyplot as plt
from tqdm import tqdm
from PIL import Image
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold<import_modules>
X_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 X_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 X_train.drop(X_train[X_train.GrLivArea > 4500].index, inplace=True)
 y = np.log1p(X_train['SalePrice'])
 X_train.drop(columns=['SalePrice'], inplace=True )
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<define_variables>
cat_cols = [cname for cname in X_train.columns if X_train[cname].dtype == 'object']
num_cols = [cname for cname in X_train.columns if X_train[cname].dtype in ['int64', 'float64']]
num_cols_zeros = []
num_cols_median = []
cat_cols_freq = []
cat_cols_none = []
for column in num_cols:
if X_train[column].min() == 0:
num_cols_zeros.append(column)
 else:
num_cols_median.append(column)
 for column in cat_cols:
if X_train[column].isnull().sum() < 30:
cat_cols_freq.append(column)
 else:
cat_cols_none.append(column)
 zero_imputer = Pipeline([ 
('impute', SimpleImputer(strategy='constant', fill_value=0.0)) , 
('scale', StandardScaler())
 ])
 medi_imputer = Pipeline([ 
('impute', SimpleImputer(strategy='median')) , 
('scale', StandardScaler())
 ])
 none_imputer = Pipeline([ 
('impute', SimpleImputer(strategy='constant', fill_value='none')) , 
('OH_encode', OneHotEncoder(handle_unknown='ignore', sparse=False))
])
 freq_imputer = Pipeline([ 
('impute', SimpleImputer(strategy='most_frequent')) , 
('OH_encode', OneHotEncoder(handle_unknown='ignore', sparse=False))
])
 target_encoder = Pipeline([ 
('encode', ce.TargetEncoder(cols=cat_cols)) , 
('scale', StandardScaler())
 ])
 count_encoder = Pipeline([ 
('encode', ce.CountEncoder()), 
('scale', StandardScaler())
 ])
 preprocessor = make_column_transformer(
(zero_imputer, num_cols_zeros), 
(medi_imputer, num_cols_median), 
(none_imputer, cat_cols_none), 
(freq_imputer, cat_cols_freq), 
(count_encoder, cat_cols), 
(target_encoder, cat_cols)
)
 def full_area(X):
X_copy = X.copy()
X_copy['full_area'] = X['TotalBsmtSF'] + X['1stFlrSF'] + X['2ndFlrSF'] + X['GarageArea'] + X['MasVnrArea'] + X['WoodDeckSF'] + X['OpenPorchSF'] + X['ScreenPorch']
return X_copy
pre_pipe = Pipeline([ 
('features', FunctionTransformer(full_area)) , 
('preprocessing', preprocessor)
 ] )
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )<define_variables>
random_state=8
models = [
XGBRegressor(random_state=random_state),
SVR() ,
DecisionTreeRegressor(random_state=random_state),
AdaBoostRegressor(DecisionTreeRegressor(random_state=random_state)) ,
RandomForestRegressor(random_state=random_state),
ExtraTreesRegressor(random_state=random_state),
GradientBoostingRegressor(random_state=random_state),
MLPRegressor(random_state=random_state),
KNeighborsRegressor() ,
Ridge(random_state=random_state),
]
def evaluate_model(X, y, models, pre_pipe):
performance = {}
for model in models:
X_transformed = pre_pipe.fit_transform(X, y)
 score = cross_val_score(model, X_transformed, y, cv=5 ).mean()
print("Model: {}, mean cross validated score: {}".format(type(model ).__name__, score))
performance[type(model ).__name__] = score
return performance
model_scores = evaluate_model(X_train, y, models, pre_pipe)
 top_models = sorted(model_scores, key=model_scores.get, reverse=True)[:5]
print("The best models are: {}".format(top_models))
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
DESIRED_SIZE = 128<load_from_csv>
GBR_params = {'model__n_estimators' : np.arange(100,500,50),
'model__learning_rate': np.arange(0.01, 0.1, 0.01),
'model__max_depth': np.arange(2,10,1),
'model__min_samples_leaf': np.arange(100,200,10),
'model__alpha': np.arange(0.7,0.9,0.05)}
Ridge_params = {'model__alpha': np.arange(0.5, 1.5, 0.1)}
ExtraT_params = {'model__n_estimators' : np.arange(50,1000,50)}
Ada_params = {'model__n_estimators' : np.arange(50,1000,50),
'model__learning_rate': np.arange(0.01, 0.1, 0.01)}
XGB_params = {'model__n_estimators' : np.arange(100,2000,100),
'model__learning_rate': np.arange(0.01, 0.1, 0.01),
'model__max_depth': np.arange(1,10,1)}
SVR_params = {'model__epsilon': np.arange(0.1, 0.5, 0.1),
'model__C': np.arange(1, 20, 1)}
KNR_params = {'model__leaf_size': np.arange(5, 50, 5),
'model__n_neighbors': np.arange(3,10,1),
'model__p': np.arange(1,4,1)}
model_list = [GradientBoostingRegressor() , Ridge() , ExtraTreesRegressor() , AdaBoostRegressor() , XGBRegressor() ]
param_list = [GBR_params, Ridge_params, ExtraT_params, Ada_params, XGB_params]
iterations = 250
best_estimators = {}
for i in range(len(model_list)) :
model_name = type(model_list[i] ).__name__
full_pipeline = Pipeline([ 
('preprocessing', pre_pipe), 
('model', model_list[i])])
 fitted_model = RandomizedSearchCV(full_pipeline, param_distributions = param_list[i], n_iter=iterations, cv=5, scoring="neg_mean_squared_error", verbose=0)
 fitted_model.fit(X_train, y)
 print("Model: {}, best cross validated score: {}".format(model_name, -1*fitted_model.best_score_))
best_estimators[model_name] = fitted_model.best_estimator_
voting = VotingRegressor([ 
('GradientBoostingRegressor', best_estimators['GradientBoostingRegressor']), 
('Ridge', best_estimators['Ridge']), 
('ExtraTreesRegressor',best_estimators['ExtraTreesRegressor']), 
('AdaBoostRegressor', best_estimators['AdaBoostRegressor']), 
('XGBRegressor', best_estimators['XGBRegressor'])
 ])
 voting = voting.fit(X_train, y)
 preds = np.expm1(voting.predict(X_test))
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )<concatenate>
output = pd.DataFrame({'Id': X_test.index,
'SalePrice': preds})
 output.to_csv('submission.csv', index=False)