Kaggle competition: Tracy Regression
<SOS> metric: rmsle Kaggle data source: tracy-regression<drop_column>
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
traindata.drop(traindata[traindata['killsWithoutMoving'] == True].index, inplace=True )<drop_column>
print(os.listdir(".. /input"))
local = 0
if(local):
train = pd.read_csv("input/train.csv")
 test = pd.read_csv("input/test.csv")
 else:
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
traindata.drop(traindata[traindata['roadKills'] > 10].index, inplace=True )<drop_column>
train_clean = train.drop(['ID','price'],1)
 test_clean = test.drop('ID',1)
 test_clean.head()
train_clean.shape
traindata.drop(traindata[traindata['kills'] > 30].index, inplace=True )<drop_column>
all_data = pd.concat(( train_clean[:],test_clean[:]))
all_data["caret_sqroot"] = np.sqrt(all_data["carat"])
 all_data["caret_cubtroot"] = all_data.carat **(1/3)
 all_data.shape
traindata.drop(traindata[traindata['longestKill'] >= 1000].index, inplace=True)
 traindata.drop(traindata[traindata['walkDistance'] >= 10000].index, inplace=True)
 traindata.drop(traindata[traindata['rideDistance'] >= 20000].index, inplace=True)
 traindata.drop(traindata[traindata['swimDistance'] >= 2000].index, inplace=True)
 traindata.drop(traindata[traindata['weaponsAcquired'] >= 80].index, inplace=True)
 traindata.drop(traindata[traindata['heals'] >= 40].index, inplace=True )<prepare_x_and_y>
train["logprice"] = np.log1p(train["price"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
print(numeric_feats )
df = df_sample.drop(columns = ['winPlacePerc'])
 y = df_sample['winPlacePerc']<split>
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.12, random_state=42)
<compute_train_metric>
all_data = pd.get_dummies(all_data)
 all_data.head()
def print_score(m : RandomForestRegressor):
res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train),
'mae val: ', mean_absolute_error(m.predict(X_test), y_test)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<train_model>
all_data = all_data.fillna(all_data.mean() )
m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m1.fit(X_train, y_train)
 print_score(m1 )<features_selection>
print(train.shape[0])
 print(all_data.shape[0])
 X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
train_price = train["logprice"]
print(train_price.head())
 y = train_price
to_keep = fi[fi.imp>0.005].cols
print('Significant features: ', len(to_keep))
to_keep<split>
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse)
 def rmse(ypred, yval):
rmseval = np.sqrt(( yval - ypred)^2)
 return(rmseval )
df_keep = df[to_keep].copy()
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.12, random_state=42)
<train_model>
lm = LinearRegression()
lm_fit = lm.fit(X_train,y)
 lm_pred = np.array(lm_fit.predict(X_train))
print(lm_fit)
 print(round(rmse_cv(lm_fit ).mean() ,4))
m2 = RandomForestRegressor(n_estimators=80, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m2.fit(X_train, y_train)
 print_score(m2 )<set_options>
predp = pd.DataFrame(lm_pred )
corr = np.round(scipy.stats.spearmanr(df_keep ).correlation, 4)
 corr_condensed = hc.distance.squareform(1-corr)
 z = hc.linkage(corr_condensed, method='average')
 fig = plt.figure(figsize=(14,10))
dendrogram = hc.dendrogram(z, labels=df_keep.columns, orientation='left', leaf_font_size=16)
 plt.plot()<split>
predp.columns = ['x']
predp.set_index('x')
val_perc_full = 0.12
n_valid_full = int(val_perc_full * len(traindata))
n_trn_full = len(traindata)-n_valid_full
df_full = traindata.drop(columns = ['winPlacePerc'])
 y = traindata['winPlacePerc']
df_full = df_full[to_keep]
X_train, X_test, y_train, y_test = train_test_split(df_full, y, test_size=0.12, random_state=42)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_test.shape )<train_model>
cdf = predp.sort_values('x' ).reset_index()
cdf['p'] = cdf.index / float(len(cdf)- 1)
 interp = interp1d(cdf['x'], cdf['p'])
 print(cdf.head())
 print(predp['x'].quantile(0.57))
print(interp(8.0403611))
print(interp(predp['x'].quantile(0.43)))
m3 = RandomForestRegressor(n_estimators=70, min_samples_leaf=3, max_features=0.5,
n_jobs=-1)
 m3.fit(X_train, y_train)
 print_score(m3 )<feature_engineering>
sz = len(lm_pred)
 sz
testdata['totalDistance'] = testdata['walkDistance'] + testdata['rideDistance'] + testdata['swimDistance']
testdata['headshotRate'] = testdata['headshotKills']/testdata['kills']
testdata['headshotRate'] = testdata['headshotRate'].fillna(0)
 testdata['playersJoined'] = testdata.groupby('matchId')['matchId'].transform('count')
 testdata['killsNorm'] = testdata['kills']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['damageDealtNorm'] = testdata['damageDealt']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['maxPlaceNorm'] = testdata['maxPlace']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['matchDurationNorm'] = testdata['matchDuration']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['healsandboosts'] = testdata['heals'] + testdata['boosts']
testdata['killsWithoutMoving'] =(( testdata['kills'] > 0)&(testdata['totalDistance'] == 0))
testdata['groupId'] = testdata['groupId'].astype('category')
 testdata['matchId'] = testdata['matchId'].astype('category')
 testdata['groupId_cat'] = testdata['groupId'].cat.codes
testdata['matchId_cat'] = testdata['matchId'].cat.codes
test_pred = testdata[to_keep].copy()
test_pred.fillna(0, inplace=True)
 test_pred.head() 
<save_to_csv>
lm_predict = lm.predict(X_train)
 print(r2_score(lm_predict,y))
predictions = np.clip(a = m3.predict(test_pred), a_min = 0.0, a_max = 1.0)
 pred_df = pd.DataFrame({'Id' : testdata['Id'], 'winPlacePerc' : predictions})
 pred_df.to_csv("submission.csv", index=False )<load_from_csv>
model_ridge = Ridge()
print(os.listdir(".. /input/"))
df_sub = pd.read_csv(".. /input/lightgbm-baseline/submission_adjusted.csv")
 df_sub2 = pd.read_csv(".. /input/pubg-lgb-ensamble-lvl-1/submission_v8.csv")
 df_sub3 = pd.read_csv(".. /input/pubg-nn-ensamble-lvl-1/submission_nn_ensamble_v5.csv")
 df_test = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 df_sub["winPlacePerc"] =(df_sub2["winPlacePerc"] + df_sub3["winPlacePerc"])/ 2
df_sub = df_sub[["Id", "winPlacePerc"]]
df_test = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 df_sub = df_sub.merge(df_test[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 df_sub_group = df_sub.groupby(["matchId", "groupId"] ).first().reset_index()
df_sub_group["rank"] = df_sub_group.groupby(["matchId"])["winPlacePerc"].rank()
df_sub_group = df_sub_group.merge(
 df_sub_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 df_sub_group["adjusted_perc"] =(df_sub_group["rank"] - 1)/(df_sub_group["numGroups"] - 1)
 df_sub = df_sub.merge(df_sub_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 df_sub["winPlacePerc"] = df_sub["adjusted_perc"]
df_sub.loc[df_sub.maxPlace == 0, "winPlacePerc"] = 0
df_sub.loc[df_sub.maxPlace == 1, "winPlacePerc"] = 1
subset = df_sub.loc[df_sub.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
df_sub.loc[df_sub.maxPlace > 1, "winPlacePerc"] = new_perc
df_sub.loc[(df_sub.maxPlace > 1)&(df_sub.numGroups == 1), "winPlacePerc"] = 0
assert df_sub["winPlacePerc"].isnull().sum() == 0
df_sub["winPlacePerc"] = df_sub["winPlacePerc"]
df_sub[["Id", "winPlacePerc"]].to_csv("submission_ensemble_lvl2_v4.csv", index=False )<concatenate>
alphas = [0.00001,0.0001, 0.001, 0.005, 0.01, 0.03, 0.05, 0.08, 0.1,.25,.5, 1]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean()
for alpha in alphas]
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))<set_options>
model_lasso = LassoCV(alphas = [10, 5,1, 0.1, 0.001, 0.0005],tol = 0.001 ).fit(X_train, y )
sns.set_style('darkgrid')
 sns.set_palette('bone')
 pd.options.display.float_format = '{:,.3f}'.format
print(os.listdir(".. /input")) 
<load_from_csv>
rmse_cv(model_lasso ).mean()
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )<count_missing_values>
print("Lasso picked " + str(sum(coef != 0)) + " variables and eliminated the other " + str(sum(coef == 0)) + " variables" )
null_cnt = train.isnull().sum().sort_values()
print('null count:', null_cnt[null_cnt > 0])
 train.dropna(inplace=True )<count_unique_values>
imp_coef = pd.concat([coef.sort_values().head(12),
coef.sort_values().tail(11)] )
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )<count_values>
import xgboost as xgb
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )<drop_column>
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=1000, early_stopping_rounds=100 )
print(group['players in group'].nlargest(5))
del match,group<count_unique_values>
model_xgb = xgb.XGBRegressor(n_estimators=1000, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )
( train.groupby('matchId')['matchDuration'].nunique() > 1 ).any()<count_unique_values>
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))
<count_unique_values>
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
sub = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(sub.groupby('matchId')))
 print(' - unique winPlacePerc:', sub['winPlacePerc'].unique() )<concatenate>
preds = 0.7*lasso_preds + 0.3*xgb_preds
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )<set_options>
solution = pd.DataFrame({"ID":test.ID, "price":preds})
 solution.to_csv("ridge_sol.csv", index = False )
grpSolo.nlargest(5, 'kills' )<count_unique_values>
from keras.layers import Dense
from keras.models import Sequential
from keras.regularizers import l1
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
grpTeam.nlargest(5, 'kills' )<drop_column>
X_train = StandardScaler().fit_transform(X_train )
del grpSolo, grpTeam<filter>
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y, random_state = 3 )
def printMatchStats(matchIds):
for mid in matchIds:
subMatch = train[train['matchId'] == mid]
print('matchType:', subMatch['matchType'].values[0])
 grp1st = subMatch[subMatch['winPlacePerc'] == 1]
grpOther = subMatch[subMatch['winPlacePerc'] != 1]
print('players'.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(len(subMatch), len(grp1st), len(grpOther)))
 for c in ['kills','teamKills','roadKills','DBNOs','revives','assists']:
print(c.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(subMatch[c].sum() , grp1st[c].sum() , grpOther[c].sum()))
print('-' * 30 )<count_unique_values>
model = Sequential()
model.add(Dense(1, input_dim = X_train.shape[1], W_regularizer=l1(0.001)))
 model.compile(loss = "mse", optimizer = "adam" )
<groupby><EOS> 
hist = model.fit(X_tr, y_tr, validation_data =(X_val, y_val))