Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<train_on_grid>
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.formula.api as sm
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn.ensemble import RandomForestRegressor
from sklearn import preprocessing
from scipy import stats
import lightgbm as lgb
import gc
alphas = np.linspace(0.00001,1,1000)
 lasso = LassoCV(alphas = alphas, max_iter = 50000, cv = 10)
 lasso.fit(X_train, y_train)
 alpha = lasso.alpha_
print("Best alpha :", alpha)
 print("Try again for more precision with alphas centered around " + str(alpha))
lasso = LassoCV(alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8,
alpha *.85, alpha *.9, alpha *.95, alpha, alpha * 1.05,
alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3, alpha * 1.35,
alpha * 1.4],
max_iter = 50000, cv = 10)
 lasso.fit(X_train, y_train)
 alpha = lasso.alpha_
print("Best alpha :", alpha)
 print("LASSO picked " + str(sum(lasso.coef_ != 0)) + " features and eliminated the other " + \
str(sum(lasso.coef_ == 0)) + " features" )<compute_train_metric>
train_init= pd.read_csv('.. /input/train.csv' )
y_train_las = lasso.predict(X_train)
 y_test_las = lasso.predict(X_test)
 print("Lasso RMSE on Training set :", rmsle(y_train, y_train_las ).mean())
 print("Lasso RMSE on Validation set :", rmsle(y_test, y_test_las ).mean() )<train_on_grid>
list_largest_corr = train_init.drop(['matchId','groupId','teamKills',u'winPlacePerc'],axis=1 ).columns
alphas = np.linspace(0.00001,1,1000)
 lasso = LassoCV(alphas = alphas, max_iter = 50000, cv = 10)
 lasso.fit(train, y)
 alpha = lasso.alpha_
print("Best alpha :", alpha)
 print("Try again for more precision with alphas centered around " + str(alpha))
lasso = LassoCV(alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8,
alpha *.85, alpha *.9, alpha *.95, alpha, alpha * 1.05,
alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3, alpha * 1.35,
alpha * 1.4],
max_iter = 50000, cv = 10)
 lasso.fit(train, y)
 alpha = lasso.alpha_
print("Best alpha :", alpha)
 print("LASSO picked " + str(sum(lasso.coef_ != 0)) + " features and eliminated the other " + \
str(sum(lasso.coef_ == 0)) + " features")
 y_lasso = lasso.predict(train)
 print("Lasso RMSE on Training set :", rmsle(y, y_lasso ).mean())
 lasso_submission_price = lasso.predict(test )<prepare_output>
train_without_out = train_init[(np.abs(stats.zscore(train_init)) < 6 ).all(axis=1)]
del train_init
gc.collect()
def add_means(train_without_out,list_largest_corr,isSampleTest=False):
agg = train_without_out.groupby(['matchId','groupId'])[list_largest_corr].agg('mean')
 agg_rank = agg.groupby('matchId')[list_largest_corr].rank(pct=True ).reset_index()
train_without_out = train_without_out.merge(agg_rank, suffixes=["", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 del agg
del agg_rank
gc.collect()
agg = train_without_out.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 train_without_out = train_without_out.merge(agg, how='left', on=['matchId', 'groupId'])
 del agg
gc.collect()
agg = train_without_out.groupby(['matchId'] ).size().reset_index(name='match_size')
 train_without_out = train_without_out.merge(agg, how='left', on=['matchId'])
 del agg
gc.collect()
agg = train_without_out.groupby(['matchId'])[list_largest_corr].agg('mean' ).reset_index()
train_without_out = train_without_out.merge(agg,suffixes=["", "_mean_match"], how='left', on=['matchId'])
 del agg
gc.collect()
if isSampleTest == False:
x = train_without_out.drop('winPlacePerc',axis=1)
 else:
x = train_without_out
scaler = preprocessing.MinMaxScaler(feature_range=(0, 1), copy=False ).fit(x)
 scaler.transform(x)
 return x
x = add_means(train_without_out,list_largest_corr)
 y = train_without_out['winPlacePerc']
del train_without_out
gc.collect()
ridge_submission_price = np.expm1(ridge_submission_price )<save_to_csv>
d_train = lgb.Dataset(x, y)
 iterations = 2000
watchlist = [d_train]
params = {
'learning_rate': 0.1,
'max_depth': -1,
'num_leaves': 30,
'feature_fraction': 0.9,
'min_data_in_leaf': 100,
'lambda_l2': 4,
'objective': 'regression_l2',
'metric': 'mae',
'seed': 123}
model = lgb.train(params, train_set=d_train, num_boost_round=iterations,
valid_sets=watchlist, early_stopping_rounds=20, verbose_eval=200 )
solution = pd.DataFrame({"Id":test_ID, "SalePrice":ridge_submission_price})
 solution.to_csv("ridge_sol.csv", index = False )<set_options>
test = pd.read_csv('.. /input/test.csv')
 x_test = add_means(test,list_largest_corr,True)
 predict = model.predict(x_test )
<load_from_csv><EOS> 
test['winPlacePerc'] = predict
test[['Id','winPlacePerc']].to_csv("submission.csv", index = False )