Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<save_to_csv>
import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor
env.write_submission_file()<set_options>
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )
pd.set_option('display.max_columns', 50)
 pd.set_option('display.max_rows', 150 )<import_modules>
train['totalSF'] =(train['TotalBsmtSF'] + train['1stFlrSF'] + train['2ndFlrSF'])
 train['total_bathrooms'] =(train['BsmtFullBath'] + 0.5*train['BsmtHalfBath'] + train['FullBath'] + 0.5*train['HalfBath'])
 train['ageHouse'] =(train['YrSold'] - train['YearBuilt'])
 test['totalSF'] =(test['TotalBsmtSF'] + test['1stFlrSF'] + test['2ndFlrSF'])
 test['total_bathrooms'] =(test['BsmtFullBath'] + 0.5*test['BsmtHalfBath'] + test['FullBath'] + 0.5*test['HalfBath'])
 test['ageHouse'] =(test['YrSold'] - test['YearBuilt'] )
from keras.layers import Dense
from keras.models import Sequential
from keras.callbacks import Callback, EarlyStopping
from keras.layers import Dropout, PReLU, BatchNormalization, ELU, GaussianNoise, Activation
from keras.optimizers import Adam<load_from_csv>
train.drop(['Id','Utilities','PoolQC','MiscFeature','Alley','Fence','GarageYrBlt'] , axis=1 , inplace=True)
 train.drop(['TotalBsmtSF' , '1stFlrSF' ,'2ndFlrSF'] , axis = 1 , inplace =True)
 train.drop(['BsmtFullBath' , 'BsmtHalfBath' , 'FullBath' , 'HalfBath'] , axis=1 , inplace=True)
 train = train.drop(columns = [ 'YearRemodAdd', 'GarageQual', 'GarageCond','YrSold','MoSold', 'YearBuilt', 'Heating'], axis = 1)
 Y = train['SalePrice']
train = train.drop(columns = ['SalePrice'], axis = 1)
 Id = test['Id']
test.drop(['Id','Utilities','PoolQC','MiscFeature','Alley','Fence','GarageYrBlt'] , axis=1 , inplace=True)
 test.drop(['TotalBsmtSF' , '1stFlrSF' ,'2ndFlrSF'] , axis = 1 , inplace =True)
 test.drop(['BsmtFullBath' , 'BsmtHalfBath' , 'FullBath' , 'HalfBath'] , axis=1 , inplace=True)
 test = test.drop(columns = [ 'YearRemodAdd', 'GarageQual', 'GarageCond','YrSold','MoSold', 'YearBuilt', 'Heating'], axis = 1 )
train = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2020/train.csv', usecols=['GameId', 'PlayId', 'Team', 'X', 'Y', 'S', 'A', 'Dis',
'Orientation', 'Dir', 'NflId', 'DisplayName', 'YardLine',
'Quarter', 'GameClock', 'PossessionTeam', 'Down', 'Distance',
'FieldPosition', 'NflIdRusher', 'OffenseFormation',
'OffensePersonnel', 'DefendersInTheBox', 'DefensePersonnel',
'PlayDirection', 'TimeHandoff', 'TimeSnap', 'Yards',
'WindSpeed', 'Temperature', 'Humidity'] )<remove_duplicates>
mode = ['MasVnrArea' , 'Electrical' , 'MSZoning' , 'SaleType','Exterior1st','Exterior2nd','KitchenQual']
for col in mode:
train[col] = train[col].fillna(train[col].mode() [0])
 for col in mode:
test[col] = test[col].fillna(test[col].mode() [0])
 No = ['GarageType','GarageFinish',
'BsmtExposure','BsmtFinType2','BsmtFinType1','BsmtCond','BsmtQual',
'FireplaceQu','MasVnrType']
for col in No:
train[col]=train[col].fillna('None')
 for col in No:
test[col]=test[col].fillna('None')
 train['Functional'] = train['Functional'].fillna('Typ')
 train['LotFrontage'] = train['LotFrontage'].fillna(train['LotFrontage'].median())
 test['Functional'] = test['Functional'].fillna('Typ')
 test['LotFrontage'] = test['LotFrontage'].fillna(test['LotFrontage'].median())
 zero = ['total_bathrooms','totalSF','BsmtFinSF1','BsmtFinSF2','BsmtUnfSF','GarageArea','GarageCars' ]
for col in zero:
train[col] = train[col].fillna(0)
 for col in zero:
test[col] = test[col].fillna(0)
 train.head()
outcomes = train[['GameId','PlayId','Yards']].drop_duplicates()<prepare_x_and_y>
train.dropna(inplace = True)
 test.dropna(inplace = True )
def create_features(df, deploy=False):
def new_X(x_coordinate, play_direction):
if play_direction == 'left':
return 120.0 - x_coordinate
else:
return x_coordinate
def new_line(rush_team, field_position, yardline):
if rush_team == field_position:
return 10.0 + yardline
else:
return 60.0 +(50 - yardline)
 def new_orientation(angle, play_direction):
if play_direction == 'left':
new_angle = 360.0 - angle
if new_angle == 360.0:
new_angle = 0.0
return new_angle
else:
return angle
def euclidean_distance(x1,y1,x2,y2):
x_diff =(x1-x2)**2
y_diff =(y1-y2)**2
return np.sqrt(x_diff + y_diff)
 def back_direction(orientation):
if orientation > 180.0:
return 1
else:
return 0
def update_yardline(df):
new_yardline = df[df['NflId'] == df['NflIdRusher']]
new_yardline['YardLine'] = new_yardline[['PossessionTeam','FieldPosition','YardLine']].apply(lambda x: new_line(x[0],x[1],x[2]), axis=1)
 new_yardline = new_yardline[['GameId','PlayId','YardLine']]
return new_yardline
def update_orientation(df, yardline):
df['X'] = df[['X','PlayDirection']].apply(lambda x: new_X(x[0],x[1]), axis=1)
 df['Orientation'] = df[['Orientation','PlayDirection']].apply(lambda x: new_orientation(x[0],x[1]), axis=1)
 df['Dir'] = df[['Dir','PlayDirection']].apply(lambda x: new_orientation(x[0],x[1]), axis=1)
 df = df.drop('YardLine', axis=1)
 df = pd.merge(df, yardline, on=['GameId','PlayId'], how='inner')
 return df
def projection_features(df):
rad = 2 * np.pi *(90 - df[['Orientation']])/ 360
v_0 = df['S'].values * np.cos(rad ).values.reshape(-1)
 v_1 = np.sin(rad ).values.reshape(-1)
 a_0 = df['A'].values * np.cos(rad ).values.reshape(-1)
 a_1 = np.sin(rad)
 df['v_0'] = v_0
df['v_1'] = v_1
df['a_0'] = a_0
df['a_1'] = a_1
return df
def back_features(df):
carriers = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','NflIdRusher','X','Y','Orientation','Dir','YardLine']]
carriers['back_from_scrimmage'] = carriers['YardLine'] - carriers['X']
carriers['back_oriented_down_field'] = carriers['Orientation'].apply(lambda x: back_direction(x))
carriers['back_moving_down_field'] = carriers['Dir'].apply(lambda x: back_direction(x))
carriers = carriers.rename(columns={'X':'back_X',
'Y':'back_Y'})
 carriers = carriers[['GameId','PlayId','NflIdRusher','back_X','back_Y','back_from_scrimmage','back_oriented_down_field','back_moving_down_field']]
return carriers
def features_relative_to_back(df, carriers):
player_distance = df[['GameId','PlayId','NflId','X','Y']]
player_distance = pd.merge(player_distance, carriers, on=['GameId','PlayId'], how='inner')
 player_distance = player_distance[player_distance['NflId'] != player_distance['NflIdRusher']]
player_distance['dist_to_back'] = player_distance[['X','Y','back_X','back_Y']].apply(lambda x: euclidean_distance(x[0],x[1],x[2],x[3]), axis=1)
 player_distance = player_distance.groupby(['GameId','PlayId','back_from_scrimmage','back_oriented_down_field','back_moving_down_field'])\ 
.agg({'dist_to_back':['min','max','mean','std']})\ 
.reset_index()
player_distance.columns = ['GameId','PlayId','back_from_scrimmage','back_oriented_down_field','back_moving_down_field',
'min_dist','max_dist','mean_dist','std_dist']
return player_distance
def defense_features(df):
rusher = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','Team','X','Y']]
rusher.columns = ['GameId','PlayId','RusherTeam','RusherX','RusherY']
defense = pd.merge(df,rusher,on=['GameId','PlayId'],how='inner')
 defense = defense[defense['Team'] != defense['RusherTeam']][['GameId','PlayId','X','Y','RusherX','RusherY']]
defense['def_dist_to_back'] = defense[['X','Y','RusherX','RusherY']].apply(lambda x: euclidean_distance(x[0],x[1],x[2],x[3]), axis=1)
 defense = defense.groupby(['GameId','PlayId'])\ 
.agg({'def_dist_to_back':['min','max','mean','std']})\ 
.reset_index()
defense.columns = ['GameId','PlayId','def_min_dist','def_max_dist','def_mean_dist','def_std_dist']
return defense
def static_features(df):
static_features = df[df['NflId'] == df['NflIdRusher']][['GameId','PlayId','X','Y','S','A','Dis','Orientation','Dir',
'YardLine','Quarter','Down','Distance','DefendersInTheBox']].drop_duplicates()
static_features['DefendersInTheBox'] = static_features['DefendersInTheBox'].fillna(np.mean(static_features['DefendersInTheBox']))
return static_features
def combine_features(relative_to_back, defense, static, speed_frame, deploy=deploy):
df = pd.merge(relative_to_back,defense,on=['GameId','PlayId'],how='inner')
 df = pd.merge(df,static,on=['GameId','PlayId'],how='inner')
 df = pd.merge(df, speed_frame, on=['GameId','PlayId'],how='inner')
 if not deploy:
df = pd.merge(df, outcomes, on=['GameId','PlayId'], how='inner')
 return df
df = projection_features(df)
 speed_frame= df[['GameId','PlayId',
'a_0','a_1','v_0','v_1', 'Temperature', 'Humidity']]
yardline = update_yardline(df)
 df = update_orientation(df, yardline)
 back_feats = back_features(df)
 rel_back = features_relative_to_back(df, back_feats)
 def_feats = defense_features(df)
 static_feats = static_features(df)
 basetable = combine_features(rel_back, def_feats, static_feats, speed_frame, deploy=deploy)
 basetable[['a_0','a_1','v_0','v_1']] = basetable[['a_0','a_1','v_0','v_1']].fillna(0)
 basetable['Temperature'] = basetable['Temperature'].fillna(60.436442)
 basetable['Humidity'] = basetable['Humidity'].fillna(59.0)
 return basetable<feature_engineering>
Label = ['MSZoning', 'Street', 'LotShape', 'LandContour', 'LotConfig', 'LandSlope', 'Condition1', 'RoofStyle', 'RoofMatl', 'MasVnrType', 'ExterCond', 'Electrical', 'Functional', 'PavedDrive', 'SaleType', 'Exterior1st', 'HeatingQC', 'BsmtCond', 'Foundation', 'SaleCondition', 'CentralAir', 'ExterQual' ,'BsmtQual', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'Neighborhood', 'OverallQual', 'FireplaceQu', 'KitchenQual' , 'Condition2', 'Exterior2nd', 'GarageType', 'GarageFinish', 'BldgType', 'HouseStyle']
for feature in Label:
ohe = pd.get_dummies(train[feature], prefix=feature)
 train = pd.concat([train, ohe], axis=1)
 train = train.drop(columns = Label, axis = 1)
 for feature in Label:
ohe = pd.get_dummies(test[feature], prefix=feature)
 test = pd.concat([test, ohe], axis=1)
 test = test.drop(columns = Label, axis = 1 )
%time train_basetable = create_features(train, False )<prepare_x_and_y>
train = train.drop(columns = ['HouseStyle_2.5Fin','RoofMatl_Metal', 'RoofMatl_Roll', 'RoofMatl_Membran', 'RoofMatl_ClyTile', 'Electrical_Mix', 'Exterior1st_Stone', 'Exterior1st_ImStucc', 'Condition2_RRNn', 'Condition2_RRAn', 'Condition2_RRAe', 'Exterior2nd_Other'], axis = 1 )
X = train_basetable.copy()
yards = X.Yards
y = np.zeros(( yards.shape[0], 1))
_y = np.zeros(( yards.shape[0], 199))
for idx, target in enumerate(list(yards)) :
y[idx] = target
_y[idx][99 + target] = 1
X.drop(['GameId','PlayId','Yards'], axis=1, inplace=True)
 scaler = StandardScaler()
X = scaler.fit_transform(X )<choose_model_class>
model = 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)
 model.fit(X = train, y = Y )
<import_modules><EOS> 
test['SalePrice'] = model.predict(test)
 test['Id'] = Id
test[['Id', 'SalePrice']].to_csv('price_submission.csv', index=False )