Kaggle competition: House Prices - Advanced Regression Techniques
train['New1']=train['Sex']*train['Pclass']
test['New1']=test['Sex']*test['Pclass']<drop_column>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
import sklearn.metrics as metrics
import math
train.drop(['Name','Ticket','PassengerId','Parch','SibSp'],axis=1,inplace=True)
 test.drop(['Name','Ticket','PassengerId','Parch','SibSp'],axis=1,inplace=True )<prepare_x_and_y>
sample_submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 c_test = test.copy()
c_train = train.copy()
steps=list()
steps.append(( 'scaler',MinMaxScaler()))
steps.append(( 'model',GradientBoostingClassifier()))
pipeline=Pipeline(steps=steps)
 x=train.drop('Survived',axis=1)
 y=train['Survived']<compute_train_metric>
c_train['train'] = 1
c_test['train'] = 0
df = pd.concat([c_train, c_test], axis=0,sort=False )
cv=RepeatedStratifiedKFold(n_splits=10,n_repeats=10,random_state=1)
 score=cross_val_score(pipeline,x,y,scoring='accuracy',cv=cv,n_jobs=-1)
 print('Accuracy: %.3f(%.3f)' %(np.mean(score)*100, np.std(score)*100))<choose_model_class>
NAN = [(c, df[c].isna().mean() *100)for c in df]
NAN = pd.DataFrame(NAN, columns=["column_name", "percentage"] )
model=GradientBoostingClassifier()
model.fit(x,y)
 pred=model.predict(test)
 submit['Survived']=pred<save_to_csv>
NAN = NAN[NAN.percentage > 50]
NAN.sort_values("percentage", ascending=False )
submit.to_csv('ver1.csv',index=False )<load_from_csv>
df = df.drop(['Alley','PoolQC','Fence','MiscFeature'],axis=1 )
train_data = pd.read_csv(".. /input/titanic/train.csv")
 train_data.head()<count_missing_values>
null_counts = object_columns_df.isnull().sum()
print("Number of null values in each column:
{}".format(null_counts))
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column > 0] )<prepare_x_and_y>
columns_None = ['BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','GarageType','GarageFinish','GarageQual','FireplaceQu','GarageCond']
object_columns_df[columns_None]= object_columns_df[columns_None].fillna('None' )
features = ['Pclass', 'Sex','SibSp','Age','Parch','Fare','Embarked']
X = pd.get_dummies(train_data[features], drop_first=True)
 y = train_data["Survived"]<normalization>
columns_with_lowNA = ['MSZoning','Utilities','Exterior1st','Exterior2nd','MasVnrType','Electrical','KitchenQual','Functional','SaleType']
object_columns_df[columns_with_lowNA] = object_columns_df[columns_with_lowNA].fillna(object_columns_df.mode().iloc[0] )
my_imputer = SimpleImputer()
imputed_X = my_imputer.fit_transform(X )<train_model>
null_counts = numerical_columns_df.isnull().sum()
print("Number of null values in each column:
{}".format(null_counts))
forest = RandomForestClassifier(oob_score=True)
 forest.fit(imputed_X,y)
 forest.oob_score_<compute_test_metric>
numerical_columns_df['GarageYrBlt'] = numerical_columns_df['GarageYrBlt'].fillna(numerical_columns_df['YrSold']-35)
 numerical_columns_df['LotFrontage'] = numerical_columns_df['LotFrontage'].fillna(68 )
for name, score in zip(X.columns, forest.feature_importances_):
print(score, name )<choose_model_class>
object_columns_df = object_columns_df.drop(['Heating','RoofMatl','Condition2','Street','Utilities'],axis=1)
smart_forest = RandomForestClassifier(n_estimators = 100, max_depth=5, max_leaf_nodes=12, min_impurity_decrease=.0048,oob_score=True)
 smart_forest.fit(imputed_X,y)
 smart_forest.oob_score_<compute_test_metric>
Negatif = numerical_columns_df[numerical_columns_df['Age_House'] < 0]
Negatif
for name, score in zip(X.columns, smart_forest.feature_importances_):
print(score, name )<train_model>
numerical_columns_df['TotalBsmtBath'] = numerical_columns_df['BsmtFullBath'] + numerical_columns_df['BsmtFullBath']*0.5
numerical_columns_df['TotalBath'] = numerical_columns_df['FullBath'] + numerical_columns_df['HalfBath']*0.5
numerical_columns_df['TotalSA']=numerical_columns_df['TotalBsmtSF'] + numerical_columns_df['1stFlrSF'] + numerical_columns_df['2ndFlrSF']
bag = BaggingClassifier(
 DecisionTreeClassifier(splitter="random"),
n_estimators = 100, max_samples=1.0, oob_score=True 
)
 bag.fit(imputed_X,y)
 bag.oob_score_<train_model>
bin_map = {'TA':2,'Gd':3, 'Fa':1,'Ex':4,'Po':1,'None':0,'Y':1,'N':0,'Reg':3,'IR1':2,'IR2':1,'IR3':0,"None" : 0,
"No" : 2, "Mn" : 2, "Av": 3,"Gd" : 4,"Unf" : 1, "LwQ": 2, "Rec" : 3,"BLQ" : 4, "ALQ" : 5, "GLQ" : 6
}
object_columns_df['ExterQual'] = object_columns_df['ExterQual'].map(bin_map)
 object_columns_df['ExterCond'] = object_columns_df['ExterCond'].map(bin_map)
 object_columns_df['BsmtCond'] = object_columns_df['BsmtCond'].map(bin_map)
 object_columns_df['BsmtQual'] = object_columns_df['BsmtQual'].map(bin_map)
 object_columns_df['HeatingQC'] = object_columns_df['HeatingQC'].map(bin_map)
 object_columns_df['KitchenQual'] = object_columns_df['KitchenQual'].map(bin_map)
 object_columns_df['FireplaceQu'] = object_columns_df['FireplaceQu'].map(bin_map)
 object_columns_df['GarageQual'] = object_columns_df['GarageQual'].map(bin_map)
 object_columns_df['GarageCond'] = object_columns_df['GarageCond'].map(bin_map)
 object_columns_df['CentralAir'] = object_columns_df['CentralAir'].map(bin_map)
 object_columns_df['LotShape'] = object_columns_df['LotShape'].map(bin_map)
 object_columns_df['BsmtExposure'] = object_columns_df['BsmtExposure'].map(bin_map)
 object_columns_df['BsmtFinType1'] = object_columns_df['BsmtFinType1'].map(bin_map)
 object_columns_df['BsmtFinType2'] = object_columns_df['BsmtFinType2'].map(bin_map)
 PavedDrive = {"N" : 0, "P" : 1, "Y" : 2}
object_columns_df['PavedDrive'] = object_columns_df['PavedDrive'].map(PavedDrive)
extra_trees=ExtraTreesClassifier(n_estimators = 50, max_depth=5, max_leaf_nodes=24, bootstrap=True, oob_score=True)
 extra_trees.fit(imputed_X,y)
 extra_trees.oob_score_<compute_test_metric>
rest_object_columns = object_columns_df.select_dtypes(include=['object'])
 object_columns_df = pd.get_dummies(object_columns_df, columns=rest_object_columns.columns )
for name, score in zip(X.columns, extra_trees.feature_importances_):
print(score, name )<load_from_csv>
df_final = pd.concat([object_columns_df, numerical_columns_df], axis=1,sort=False)
 df_final.head()
test_data = pd.read_csv(".. /input/titanic/test.csv")
 X_test=pd.get_dummies(test_data[features], drop_first=True)
 imputed_test=my_imputer.fit_transform(X_test )<save_to_csv>
df_final = df_final.drop(['Id',],axis=1)
 df_train = df_final[df_final['train'] == 1]
df_train = df_train.drop(['train',],axis=1)
 df_test = df_final[df_final['train'] == 0]
df_test = df_test.drop(['SalePrice'],axis=1)
 df_test = df_test.drop(['train',],axis=1 )
predictions=smart_forest.predict(imputed_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<define_variables>
target= df_train['SalePrice']
df_train = df_train.drop(['SalePrice'],axis=1 )
N_FEATURES = 22
THRESHOLD = 73<load_from_csv>
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )
train_df = pd.read_csv(".. /input/train.csv" )<data_type_conversions>
xgb =XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1)
 lgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=12000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.4,
)
train_df[train_df.columns[1:]] = train_df[train_df.columns[1:]].astype(np.float32 )<filter>
xgb.fit(x_train, y_train)
 lgbm.fit(x_train, y_train,eval_metric='rmse')
good_ids = set(train_df.loc[train_df['Ref'].notna() , 'Id'])
 train_df = train_df[train_df['Id'].isin(good_ids)]
train_df.shape<correct_missing_values>
predict1 = xgb.predict(x_test)
 predict = lgbm.predict(x_test )
train_df.fillna(0.0, inplace=True)
 train_df.reset_index(drop=True, inplace=True)
 train_df.head()<filter>
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict1))))
print('Root Mean Square Error test = ' + str(math.sqrt(metrics.mean_squared_error(y_test, predict))))
train_df = train_df[train_df['Expected'] < THRESHOLD]<groupby>
xgb.fit(df_train, target)
 lgbm.fit(df_train, target,eval_metric='rmse' )
train_groups = train_df.groupby("Id")
 train_size = len(train_groups )<prepare_x_and_y>
predict4 = lgbm.predict(df_test)
 predict3 = xgb.predict(df_test)
 predict_y =(predict3*0.45 + predict4 * 0.55 )
<load_from_csv><EOS> 
submission = pd.DataFrame({
"Id": test["Id"],
"SalePrice": predict_y
})
 submission.to_csv('submission.csv', index=False )