Kaggle competition: House Sales
<SOS> metric: rmse Kaggle data source: eic-house-sales<drop_column>
import pandas as pd
import numpy as np
import sys
import os
import random
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn import metrics
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor
import lightgbm as lgb
train.drop(train.query(query ).index, inplace=True )<drop_column>
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )
train.drop(train.query(query ).index, inplace=True )<drop_column>
print("Train: rows:{} cols:{}".format(train_df.shape[0], train_df.shape[1]))
print("Test: rows:{} cols:{}".format(test_df.shape[0], test_df.shape[1]))
train = train.drop('totalDistance', axis=1 )<concatenate>
def missing_data(data):
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() /data.isnull().count() *100 ).sort_values(ascending = False)
 return np.transpose(pd.concat([total, percent], axis=1, keys=['Total', 'Percent']))
data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()<feature_engineering>
missing_data(train_df )
match = data.groupby('matchId')
 data['_killsPerc'] = match['kills'].rank(pct=True ).values
data['_damageDealtPerc'] = match['damageDealt'].rank(pct=True ).values
data['_walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
data['_walkPerc_killsPerc'] = data['_walkDistancePerc'] / data['_killsPerc']
data['_totalDistance'] = data['walkDistance'] + data['rideDistance'] + data['swimDistance']
data['_items'] = data['heals'] + data['boosts']
data['_damageDealtAndWalkDistance'] = data['damageDealt'] + data['walkDistance']
data['_headshotRate'] = data['headshotKills'] / data['kills']
data['_killPlace_maxPlace'] = data['killPlace'] / data['maxPlace']
fillInf(data, 0 )<count_missing_values>
missing_data(test_df )
nullCnt = data.isnull().sum()
nullCnt<sort_values>
print("There are {} unique zipcodes.".format(train_df['zipcode'].nunique()))
corr = data.corr()
corr['winPlacePerc'].sort_values(ascending=False)
 del corr
gc.collect()
depCols = ['rideDistance', 'swimDistance', 'heals', 'boosts', 'headshotKills', 'roadKills', 'vehicleDestroys', 'killStreaks', 'DBNOs', 'killPoints', 'rankPoints', 'winPoints', 'matchDuration']
data = data.drop(depCols, axis=1 )<merge>
for df in [train_df, test_df]:
df['date'] = pd.to_datetime(df['date'])
 df['dayofweek'] = df['date'].dt.dayofweek
df['weekofyear'] = df['date'].dt.weekofyear
df['dayofyear'] = df['date'].dt.dayofyear
df['quarter'] = df['date'].dt.quarter
df['is_month_start'] = pd.to_numeric(df['date'].dt.is_month_start)
 df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['is_weekend'] = pd.to_numeric(df['dayofweek']>=5 )
minKills = data.sort_values(['matchId', 'groupId', 'kills', 'killPlace'] ).groupby(['matchId', 'groupId', 'kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
matchData = pd.merge(matchData, minKills[nKills][['matchId','groupId',c]], how='left')
 matchData = reduce_mem_usage(matchData)
 del minKills, nKills<merge>
for df in [train_df, test_df]:
df['med_lat'] = np.round(df['lat'],1)
 df['med_long'] = np.round(df['long'],1)
 df['build_old'] = 2019 - df['yr_built']
df['sqft_living_diff'] = df['sqft_living'] - df['sqft_living15']
df['sqft_lot_diff'] = df['sqft_lot'] - df['sqft_lot15']
df['bedroom_bathroom_ratio'] = df['bedrooms'] / df['bathrooms']
data = pd.merge(data, matchData)
 del matchData
gc.collect()<feature_engineering>
features = ['bedrooms','bathrooms','floors',
'waterfront','view','condition','grade',
'sqft_living','sqft_lot','sqft_above','sqft_basement','sqft_living15','sqft_lot15',
'yr_built','yr_renovated',
'lat', 'long','zipcode',
'date', 'dayofweek', 'weekofyear', 'dayofyear', 'quarter',
'is_month_start', 'month', 'year', 'is_weekend',
'price']
mask = np.zeros_like(train_df[features].corr() , dtype=np.bool)
 mask[np.triu_indices_from(mask)] = True
f, ax = plt.subplots(figsize=(18,18))
plt.title('Pearson Correlation Matrix',fontsize=25)
 sns.heatmap(train_df[features].corr() ,linewidths=0.25,vmax=1.0,square=True,cmap="Blues",
linecolor='w',annot=True,mask=mask,cbar_kws={"shrink":.75});
data['enemy.players'] = data['m.players'] - data['players']
for c in sum_col:
data['p.max_msum.' + c] = data['max.' + c] / data['m.sum.' + c]
data['p.max_mmax.' + c] = data['max.' + c] / data['m.max.' + c]
data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(data, 0 )<groupby>
VALID_SIZE = 0.2
RANDOM_STATE = 2019
train, valid = train_test_split(train_df, test_size=VALID_SIZE, random_state=RANDOM_STATE, shuffle=True )
match = data.groupby('matchId')
 matchRank = match[numCols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 data = reduce_mem_usage(pd.concat([data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()<merge>
predictors = ['sqft_living', 'grade']
target = 'price'
match = data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
data = pd.merge(data, matchRank)
 for c in numCols:
data['rank.' + c] = data['rank.' + c] / data['max.rank.' + c]
data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()<count_unique_values>
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values
constant_columns = [col for col in data.columns if data[col].nunique() == 1]
print(constant_columns)
 data.drop(constant_columns, axis=1, inplace=True )<categorify>
RFC_METRIC = 'mse'
NUM_ESTIMATORS = 100
NO_JOBS = 4
data['matchType'] = data['matchType'].apply(mapper)
 data = pd.concat([data, pd.get_dummies(data['matchType'])], axis=1)
 data.drop(['matchType'], axis=1, inplace=True)
 data['matchId'] = data['matchId'].apply(lambda x: int(x, 16))
data['groupId'] = data['groupId'].apply(lambda x: int(x, 16))<sort_values>
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False )
nullCnt = data.isnull().sum().sort_values()
print(nullCnt[nullCnt > 0] )<categorify>
model.fit(train_X, train_Y )
cols = [col for col in data.columns if col not in ['Id', 'matchId', 'groupId']]
for i, t in data.loc[:, cols].dtypes.iteritems() :
if t == object:
data[i] = pd.factorize(data[i])[0]
data = reduce_mem_usage(data)
 data.head()<prepare_x_and_y>
preds = model.predict(valid_X )
X_train = data[data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = data[data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del data
gc.collect()<drop_column>
print("RF Model score: ", model.score(train_X, train_Y))
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId', 'groupId']].copy()
train_matchId = X_train['matchId']
X_train.drop(['matchId', 'groupId'], axis=1, inplace=True)
 X_test.drop(['matchId', 'groupId'], axis=1, inplace=True)
 print(X_train.shape, X_test.shape )<create_dataframe>
def rmse(preds, y):
return np.sqrt(mean_squared_error(preds, y))
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )<train_model>
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))
params={'learning_rate': 0.1,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )<concatenate>
predictors = ['sqft_living', 'grade', 'sqft_above']
target = 'price'
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False)
 model.fit(train_X, train_Y)
 preds = model.predict(valid_X )
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )<feature_engineering>
print("RF Model score: ", model.score(train_X, train_Y))
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
X_test['winPlacePerc'] = X_test['winPlacePerc'].clip(lower=0,upper=1 )<feature_engineering>
predictors = ['sqft_living', 'sqft_lot',
'sqft_above', 'sqft_living15',
'waterfront', 'view', 'condition', 'grade',
'bedrooms', 'bathrooms', 'floors',
'zipcode',
'month', 'dayofweek',
'med_lat', 'med_long',
'build_old', 'sqft_living_diff', 'sqft_lot_diff',
]
target = 'price'
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False)
 model.fit(train_X, train_Y)
 preds = model.predict(valid_X)
 plot_feature_importance()
print("RF Model score: ", model.score(train_X, train_Y))
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0
X_test['winPlacePerc'].describe()<save_to_csv>
test_X = test_df[predictors]
predictions_RF = model.predict(test_X)
 submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_RF
submission.to_csv('submission.csv', index=False )
test = pd.read_csv('.. /input/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )<import_modules>
param = {'num_leaves': 51,
'min_data_in_leaf': 30,
'objective':'regression',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"metric": 'rmse',
"verbosity": -1,
"nthread": 4,
"random_state": 42}
<prepare_x_and_y>
predictors = ['sqft_living', 'sqft_lot',
'sqft_above', 'sqft_living15',
'waterfront', 'view', 'condition', 'grade',
'bedrooms', 'bathrooms', 'floors',
'zipcode',
'month', 'dayofweek',
'med_lat', 'med_long',
'build_old', 'sqft_living_diff', 'sqft_lot_diff',
]
target = 'price'
<feature_engineering>
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 oof = np.zeros(len(train_df))
predictions_lgb_cv = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
<import_modules>
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df,train_df['price'].values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 trn_data = lgb.Dataset(train_df.iloc[trn_idx][predictors], label=train_df.iloc[trn_idx][target])
 val_data = lgb.Dataset(train_df.iloc[val_idx][predictors], label=train_df.iloc[val_idx][target])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 100)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][predictors], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = predictors
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions_lgb_cv += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)/ folds.n_splits
strRMSE = "RMSE: {}".format(rmse(oof, train_df[target]))
print(strRMSE )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_lgb_cv
submission.to_csv('submission_cv.csv', index=False )
train = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 test = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv' )<drop_column>
predictions_blending = predictions_RF * 0.55 + predictions_lgb_cv * 0.45
<feature_engineering><EOS> 
submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_blending
submission.to_csv('submission_blending.csv', index=False )