Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<drop_column>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1 )<count_values>
def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
Train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 Train = Train.set_index('Id')
 Test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 Test = Test.set_index('Id')
 preprocessing_path = '.. /input/house-price-eda'
train_scale = pd.read_csv(f'{preprocessing_path}/train_scaled.csv')
 train_scale = train_scale.set_index('Id')
 test_scale = pd.read_csv(f'{preprocessing_path}/test_scaled.csv')
 try:
test_scale = test_scale.rename(columns={'Unnamed: 0':'Id'})
 except:
pass
test_scale = test_scale.set_index('Id')
 Train = Train.loc[train_scale.index]
listing = glob.glob(f'{preprocessing_path}/*_one_hot_pickle')
 for model_file in listing:
col = model_file.split('_one_')[0]
col = col.split('/')[-1]
enc = pickle.load(open(model_file, 'rb'))
new_cols = pd.DataFrame(enc.transform(Train[[col]] ).toarray() ,
columns=f'{col}_' + enc.categories_[0])
 new_cols.index = Train.index
Train = pd.concat([Train, new_cols], axis = 1)
 Train = Train.drop(col, axis = 1)
 for log_col in train_scale.columns[train_scale.columns.str.endswith('log')]:
col = log_col.split('_log')[0]
Train[log_col] = np.log(Train[col])
 Train = Train.drop(col, axis = 1)
 Train = Train[train_scale.columns]
for model_file in listing:
col = model_file.split('_one_')[0]
col = col.split('/')[-1]
enc = pickle.load(open(model_file, 'rb'))
new_cols = pd.DataFrame(enc.transform(Test[[col]] ).toarray() ,
columns=f'{col}_' + enc.categories_[0])
 new_cols.index = Test.index
Test = pd.concat([Test, new_cols], axis = 1)
 Test = Test.drop(col, axis = 1)
 for log_col in test_scale.columns[test_scale.columns.str.endswith('log')]:
col = log_col.split('_log')[0]
Test[log_col] = np.log(Test[col])
 Test = Test.drop(col, axis = 1)
 Test = Test[test_scale.columns]
train_data['Color'].value_counts()<count_values>
test_data['Color'].value_counts()<categorify>
train_data['Color'].fillna(value='Unknown',inplace=True)
 test_data['Color'].fillna(value='Unknown',inplace=True )<count_values>
def getFoldValidation(length, K = 4):
diff = int(length/K)
 fraction = length % K
batchValidation = [[k*diff,(k + 1)*diff] for k in range(K)]
batchValidation[-1][1] = batchValidation[-1][1] + fraction - 1
return batchValidation
batchValidation = np.array(getFoldValidation(len(Train)) )
train_data['Transmission'].value_counts()<count_values>
def getTrainTestBatch() :
fold = 0
while fold < batchValidation.shape[0]:
train_batch = pd.concat([Train.iloc[batchValidation[i][0]:batchValidation[i][1]]
for i in range(batchValidation.shape[0])if i != fold])
 test_batch = Train.iloc[batchValidation[fold][0]:batchValidation[fold][1]]
yield train_batch, test_batch, fold
fold += 1
train_batch, test_batch, fold = next(getTrainTestBatch())
test_data['Transmission'].value_counts()<filter>
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb
train_data[train_data['Transmission']=='Manual']<rename_columns>
def mse_loss(y_true, y_pred):
return K.sqrt(K.mean(( y_true - y_pred)**2))
def e_loss(y_true, y_pred):
return K.sqrt(K.mean(( np.e**y_true - np.e**y_pred)**2))
train_data['Transmission'].replace('Manual','MANUAL',inplace=True )<count_values>
genarator = getTrainTestBatch()
train_data['Transmission'].value_counts()<data_type_conversions>
lasso = make_pipeline(RobustScaler()
,Lasso(alpha =0.0005, random_state=1))
train_data['Transmission'].fillna(value='TranUnknown',inplace=True)
 test_data['Transmission'].fillna(value='TranUnknown',inplace=True )<count_values>
ENet = make_pipeline(RobustScaler() ,
ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
train_data['WheelTypeID'].value_counts()<drop_column>
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5 )
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )<count_values>
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )
train_data['WheelType'].value_counts()<count_values>
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )
train_data['WheelType'].value_counts()<data_type_conversions>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 )
train_data['Nationality'].fillna(value='NatUnknown',inplace=True)
 test_data['Nationality'].fillna(value='NatUnknown',inplace=True )<data_type_conversions>
X_Train = Train.drop('SalePrice_log', axis = 1)
 X_Train
train_data['Size'].fillna(value='SizeUnknown',inplace=True)
 test_data['Size'].fillna(value='SizeUnknown',inplace=True )<data_type_conversions>
def rmsle_cv(model, n_folds = 5):
X_Train = Train.drop('SalePrice_log', axis = 1 ).values
Y_Train = Train[['SalePrice_log']].values
kf =(KFold(n_folds, shuffle=True,
random_state=42
).
 get_n_splits(X_Train))
rmse= np.sqrt(-cross_val_score(model, X_Train, Y_Train, scoring="neg_mean_squared_error", cv = kf))
return(rmse)
 score = rmsle_cv(lasso)
 print("
Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
".format(score.mean() , score.std()))
train_data['TopThreeAmericanName'].fillna(value='T3AUnknown',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='T3AUnknown',inplace=True )<data_type_conversions>
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1)
 averaged_models = AveragingModels(models = [ENet, GBoost, KRR, lasso])
 score = rmsle_cv(averaged_models)
 print(" Averaged base models score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
train_data['PRIMEUNIT'].fillna(value='PrimeUnknown',inplace=True)
 test_data['PRIMEUNIT'].fillna(value='PrimeUnknown',inplace=True )<count_values>
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred.flatten()
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features)
 stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = LinearRegression())
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))
train_data['AUCGUART'].value_counts()<count_values>
X_Train = Train.drop('SalePrice_log', axis = 1)
 Y_Train = Train[['SalePrice_log']]
stacked_averaged_models.fit(X_Train.values, Y_Train.values )
test_data['AUCGUART'].value_counts()<data_type_conversions>
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))
train_data['AUCGUART'].fillna(value='AucguartUnknown',inplace=True)
 test_data['AUCGUART'].fillna(value='AucguartUnknown',inplace=True )<drop_column>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice']
,inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice']
,inplace=True,axis=1 )<drop_column>
stacked_averaged_models.fit(Train.drop('SalePrice_log', axis = 1 ).values, Train[['SalePrice_log']].values)
 stacked_train_pred = stacked_averaged_models.predict(Train.drop('SalePrice_log', axis = 1 ).values )
train_data.drop('PurchDate',inplace=True,axis=1)
 test_data.drop('PurchDate',inplace=True,axis=1 )<drop_column>
print(rmsle(Train[['SalePrice_log']].values, stacked_train_pred))
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'<drop_column>
predict = stacked_averaged_models.predict(Test.values )
non_cat=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']<feature_engineering>
predict = pd.DataFrame(predict)
 predict.index = Test.index
predict.columns = ['SalePrice_log']
predict['SalePrice'] = np.e**(predict['SalePrice_log'])
 predict = predict.drop('SalePrice_log', axis = 1)
 predict
for i in non_cat:
maxi=np.max(train_data[i])
 train_data[i]/=maxi
maxi_test=np.max(test_data[i])
 test_data[i]/=maxi_test<drop_column>
predict.to_csv("submission.csv" )