Kaggle competition: HEROZ Internal Competition
print(sc.normaltest(train['winPlacePerc']))
print('Skew: ', sc.skew(train['winPlacePerc']))<count_unique_values>
!pip install category_encoders
print("Number of match in train dataset:",train['matchId'].nunique() )<groupby>
train = pd.read_csv("/kaggle/input/heroz-internal-competition/train.csv")
 test = pd.read_csv("/kaggle/input/heroz-internal-competition/test_features.csv")
 ans = pd.read_csv("/kaggle/input/heroz-internal-competition/sample_submission.csv")
 print("Train shape : ", train.shape)
 print("Test shape : ", test.shape)
 print("ans shape : ", ans.shape)
 train
def _min(x):
return x.value_counts().values.min()
def _max(x):
return x.value_counts().values.max()
def _avg(x):
return x.value_counts().values.mean()
def _med(x):
return np.median(x.value_counts().values)
 def _num(x):
return x.nunique()
infostat = train.groupby('matchType' ).agg({
"matchId": [np.size, _num, _min,_med,_max],
"groupId": [_min,_med,_max],
"matchDuration": [min,np.median, max],
"maxPlace": [min,np.median,max],
"numGroups":[min,np.median,max]
} ).sort_values(by=('matchId','size'),ascending=False)
 display(infostat )<import_modules>
train.isnull().sum()
import lightgbm as lgb<prepare_x_and_y>
test.isnull().sum()
def initial_preparing(df, Debug):
if Debug:
df = df[df['matchId'].isin(df['matchId'].unique() [:2000])]
df.drop(columns=['killPoints','rankPoints','winPoints','matchType','maxPlace','Id'],inplace=True)
 X = df.groupby(['matchId','groupId'] ).agg(np.mean)
 X = reduce_mem_usage(X)
 y = X['winPlacePerc']
X.drop(columns=['winPlacePerc'],inplace=True)
 X_ranked = X.groupby('matchId' ).rank(pct=True)
 X = X.reset_index() [['matchId','groupId']].merge(X_ranked, how='left', on=['matchId', 'groupId'])
 X.drop(['matchId','groupId'],axis=1, inplace=True)
 X = reduce_mem_usage(X)
 return X, y<prepare_x_and_y>
train['reviews_per_month'] = train['reviews_per_month'].fillna(0)
 test['reviews_per_month'] = test['reviews_per_month'].fillna(0 )
X_train, y = initial_preparing(train.copy() ,False )<split>
train['last_review'] = train['last_review'].fillna('2019-12-31')
 test['last_review'] = test['last_review'].fillna('2019-12-31' )
X_train, X_holdout, y_train, y_holdout = train_test_split(X_train, y, test_size=0.2, random_state=666 )<import_modules>
train['last_review'] = pd.to_datetime(train['last_review'])
 test['last_review'] = pd.to_datetime(test['last_review'])
 train['year'] = train['last_review'].dt.year-2010
test['year'] = test['last_review'].dt.year-2010
train['month'] = train['last_review'].dt.month-1
test['month'] = test['last_review'].dt.month-1
from sklearn.model_selection import cross_val_score
import sklearn.metrics
from sklearn.model_selection import GridSearchCV<compute_train_metric>
train['latitude'] = train['latitude']-40
train['longitude'] = train['longitude']+75
test['latitude'] = test['latitude']-40
test['longitude'] = test['longitude']+75
train['waru'] = train['latitude'] / train['longitude']
train['kakeru'] = train['latitude'] * train['longitude']
test['waru'] = test['latitude'] / test['longitude']
test['kakeru'] = test['latitude'] * test['longitude']
%%time
lgtrain = lgb.Dataset(X_train, label=y_train.reset_index(drop=True))
res = lgb.cv({'metric': 'mae'},lgtrain, nfold=5,stratified=False,seed=666)
 print("Mean score:",res['l1-mean'][-1])
 gc.collect()<define_variables>
train['minimum_nights_log'] = np.log1p(train['minimum_nights'])
 test['minimum_nights_log'] = np.log1p(test['minimum_nights'])
 train['calculated_host_listings_count_log'] = np.log1p(train['calculated_host_listings_count'])
 test['calculated_host_listings_count_log'] = np.log1p(test['calculated_host_listings_count'])
 train['availability_365_log'] = np.log1p(train['availability_365'])
 test['availability_365_log'] = np.log1p(test['availability_365'])
 train['number_of_reviews_log'] = np.log1p(train['number_of_reviews'])
 test['number_of_reviews_log'] = np.log1p(test['number_of_reviews'])
 train['reviews_per_month_log'] = np.log1p(train['reviews_per_month'])
 test['reviews_per_month_log'] = np.log1p(test['reviews_per_month'] )
team_features = {
'assists': [sum, np.mean, np.size],
'boosts' : [sum, np.var, np.mean],
'heals': [sum, np.var, np.mean],
'damageDealt': [np.var,min,max,np.mean],
'DBNOs': [np.var,max,np.mean],
'headshotKills': [max,np.mean],
'killPlaceScall':[sum, min,max, np.var, np.mean],
'kills': [ sum, max, np.var,np.mean],
'killStreaks': [max,np.var,np.mean],
'longestKill': [max, np.mean, np.var],
'revives': sum,
'rideDistance': [sum, np.mean,np.var],
'swimDistance': [np.var],
'teamKills': sum,
'vehicleDestroys': sum,
'walkDistance': [np.var,np.mean],
'weaponsAcquired': [np.mean],
'damageRate': [np.var,min,max,np.mean],
'headshotRate': [np.var,max,np.mean],
'killStreakRate': [np.var,np.max, np.mean],
'healthItems': [np.var, np.mean],
'healsOnKill': [ np.var, np.mean],
'sniper': [ np.var, np.mean],
'totalDistance': [sum, np.var, np.mean],
'totalDistancePen': [ sum ,np.var, np.mean],
'killsPerRoadDist': [np.mean],
'killsPerWalkDist': [np.mean],
'killsPerDist': [np.mean],
'distance_over_weapons': [np.mean],
'walkDistance_over_heals': [np.mean],
'skill': [np.var,np.mean]
}<feature_engineering>
target = 'price'
list_cols = ['neighbourhood_group', 'neighbourhood', 'room_type',]
target_columns = ['neighbourhood_group', 'neighbourhood', 'latitude', 'longitude', 'room_type',
'minimum_nights', 'number_of_reviews', 'reviews_per_month', 'calculated_host_listings_count', 'availability_365', 'waru', 'kakeru', 'minimum_nights_log',
'calculated_host_listings_count_log', 'availability_365_log',
'number_of_reviews_log', 'reviews_per_month_log', 'year', 'month']
ce_ohe = ce.OneHotEncoder(cols=list_cols)
 train_onehot = ce_ohe.fit_transform(train[target_columns] )
def featuring(df, isTrain, Debug):
y=None
if Debug:
df = df[df['matchId'].isin(df['matchId'].unique() [:2000])]
nplayers = df.groupby('matchId')['matchId'].transform('count')
 df['killPlaceScall'] = df['killPlace'] / nplayers
df['damageRate'] = df['kills']/(0.01*df['damageDealt'])
 df['headshotRate'] = df['headshotKills']/df['kills']
df['killStreakRate'] = df['killStreaks']/df['kills']
df['healthItems'] = df['heals'] + df['boosts']
df['healsOnKill'] = df['healthItems']/df['kills']
df['sniper'] = df['longestKill']/100*df['weaponsAcquired']
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
df['totalDistancePen'] = df['rideDistance']/5 + df["walkDistance"] + df["swimDistance"]*10
df['killsPerRoadDist'] = df['roadKills'] /(df['rideDistance']+1)
 df['killsPerWalkDist'] =(df['kills']-df['roadKills'])/(df['walkDistance']+1)
 df['killsPerDist'] = df['kills']/(df['totalDistance']+1)
 df['distance_over_weapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistance_over_heals'] = df['walkDistance']/100/df['heals']
df["skill"] = df["headshotKills"] + df["roadKills"] - df['teamKills']
df.fillna(0,inplace=True)
 df.replace(np.inf, 0, inplace=True)
 ids = df[['matchId','groupId','Id']]
df.drop(columns=['killPlace','killPoints','rankPoints','winPoints','matchType','maxPlace','Id'],inplace=True)
 tfeatures = team_features.copy()
if isTrain:
tfeatures['winPlacePerc'] = max
X = df.groupby(['matchId','groupId'] ).agg(tfeatures)
 X.fillna(0,inplace=True)
 X.replace(np.inf, 1000000, inplace=True)
 X = reduce_mem_usage(X)
 if isTrain:
y = X[('winPlacePerc','max')]
X.drop(columns=[('winPlacePerc','max')],inplace=True)
 X_ranked = X.groupby('matchId' ).rank(pct=True)
 X = X.reset_index() [['matchId','groupId']].merge(X_ranked, suffixes=["", "_rank"], how='left', on=['matchId', 'groupId'])
 ids_after = X[['matchId','groupId']]
ids_after.columns = ['matchId','groupId']
X = X.drop(['matchId','groupId'],axis=1)
 X.columns = [a+"_"+b for a,b in X.columns]
X = reduce_mem_usage(X)
 return X, y, ids,ids_after<categorify>
train_onehot.isnull().sum()
%%time
X_train, y, _,_ = featuring(train,True,False)
 X_test, _,ids_init,ids_after = featuring(test,False,False )<split>
test_onehot = ce_ohe.transform(test[target_columns])
X_train, X_holdout, y_train, y_holdout = train_test_split(X_train, y, test_size=0.2, random_state=666 )<compute_train_metric>
pca2 = PCA(n_components=5)
 pca2_results = pca2.fit_transform(train_onehot)
 train_onehot['pca0']=pca2_results[:,0]
train_onehot['pca1']=pca2_results[:,1]
train_onehot['pca2']=pca2_results[:,2]
train_onehot['pca3']=pca2_results[:,3]
train_onehot['pca4']=pca2_results[:,4]
pca2_results = pca2.transform(test_onehot)
 test_onehot['pca0']=pca2_results[:,0]
test_onehot['pca1']=pca2_results[:,1]
test_onehot['pca2']=pca2_results[:,2]
test_onehot['pca3']=pca2_results[:,3]
test_onehot['pca4']=pca2_results[:,4]
%%time
lgtrain = lgb.Dataset(X_train, label=y_train.reset_index(drop=True))
res = lgb.cv({'metric': 'mae'},lgtrain, nfold=5,stratified=False,seed=666)
 print("Mean score:",res['l1-mean'][-1] )<define_search_model>
y_train = np.log1p(train["price"] )
gridParams = {
'num_leaves': [30,50,100], 'max_depth': [-1,8,15],
'min_data_in_leaf': [100,300,500], 'max_bin': [250,500],
'lambda_l1': [0.01], 'num_iterations': [5],
'nthread': [4], 'seed': [666],
'learning_rate': [0.05], 'metric': ['mae'],
"bagging_fraction" : [0.7], "bagging_seed" : [0], "colsample_bytree" : [0.7]
}
model = lgb.LGBMRegressor()
grid = GridSearchCV(model, gridParams,
verbose=1,
cv=5 )<train_model>
kf = KFold(n_splits=5, shuffle=True, random_state=1)
 s = list(kf.split(train_onehot, y_train))
%%time
grid.fit(X_train.iloc[:500000,:], y_train.iloc[:500000] )<find_best_params>
gbk_models = []
for train_i, val_i in s:
gbk = GradientBoostingRegressor()
gbk.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = gbk.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 gbk_models.append(gbk )
print("Best params:", grid.best_params_)
 print("
Best score:", grid.best_score_)
 params = grid.best_params_<train_model>
mlp_models = []
for train_i, val_i in s:
mlp = MLPRegressor(max_iter=100, hidden_layer_sizes=(100,100),
activation='relu', learning_rate_init=0.01)
 mlp.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = mlp.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 mlp_models.append(mlp )
%%time
lgtrain = lgb.Dataset(X_train, label=y_train)
 lgval = lgb.Dataset(X_holdout, label=y_holdout)
 params['num_iterations'] = 5000
model = lgb.train(params, lgtrain, valid_sets=[lgtrain, lgval], early_stopping_rounds=200, verbose_eval=1000 )<save_to_csv>
rfc_models = []
for train_i, val_i in s:
rfc = RandomForestRegressor()
rfc.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = rfc.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 rfc_models.append(rfc )
pred_test = model.predict(X_test, num_iteration=model.best_iteration)
 ids_after['winPlacePerc'] = pred_test
predict = ids_init.merge(ids_after, how='left', on=['groupId',"matchId"])['winPlacePerc']
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 df_sub['winPlacePerc'] = predict
df_sub[["Id", "winPlacePerc"]].to_csv("submission.csv", index=False )<set_options>
models = gbk_models + rfc_models
preds = np.array([np.expm1(model.predict(test_onehot)) for model in models])
 preds = preds.mean(axis=0)
 preds = np.where(preds < 0 , 0, preds )
gc.enable()<load_from_csv>
ans["price"] = preds
ans.to_csv("heroz_nakai.csv", index=False )