Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
selected_data_one_hot = pd.get_dummies(selected_data,
columns = one_hot_features)
 rescaling_features = ['Age', 'Fare']
std_scaler = StandardScaler()
for f in rescaling_features:
selected_data_one_hot[f] = std_scaler.fit_transform(selected_data_one_hot[f].values.reshape(-1, 1))
train_x = selected_data[:train.shape[0]]
test_x = selected_data[train.shape[0]:]
train_x_one_hot = selected_data_one_hot[:train.shape[0]]
test_x_one_hot = selected_data_one_hot[train.shape[0]:]
train_y = data[:train.shape[0]].Survived<train_on_grid>
df_train = pd.read_csv(".. /input/train_V2.csv")
 df_test = pd.read_csv(".. /input/test_V2.csv" )
parameters = {'n_estimators': [10,50,100,200],
'learning_rate': [0.05, 0.1],
'max_depth': [2,3,4],
'min_samples_leaf': [2,3],
'verbose': [0]}
grid_obj = GridSearchCV(GradientBoostingClassifier() , parameters, scoring = 'roc_auc', cv = 4, n_jobs = 4, verbose = 1)
 grid_obj = grid_obj.fit(train_x, train_y)
 gb = grid_obj.best_estimator_
gb<compute_train_metric>
df_train.isnull().sum()
model = gb.fit(train_x, train_y)
 pred_y = gb.predict(train_x)
 f1 = f1_score(train_y, pred_y)
 acc = accuracy_score(train_y, pred_y )<predict_on_test>
df_test.isnull().sum()
test_y = pd.Series(gb.predict(test_x), name="Survived", dtype='int32')
 results = pd.concat([data[train.shape[0]:].PassengerId, test_y], axis=1 )<save_to_csv>
df_train[df_train.winPlacePerc.isnull() ]
results.to_csv("gbdt_csv_to_submit.csv",index=False )<define_search_space>
match_type = pd.get_dummies(df_train["matchType"])
 df_train["playersInMatch"] = df_train.groupby("matchId")["Id"].transform("count")
 df_train["playersInGroup"] = df_train.groupby("groupId")["Id"].transform("count" )
training_config = {
'gbdt': {
'clf': GradientBoostingClassifier() ,
'parameters': {
'n_estimators': [10,50,100,200],
'learning_rate': [0.05, 0.1],
'max_depth': [2,3,4],
'min_samples_leaf': [2,3],
},
'n_jobs': 4,
'one_hot': False
},
'logit' : {
'clf': LogisticRegression() ,
'parameters': {
'penalty': ['l1', 'l2'],
'C': list(np.arange(0.5, 8.0, 0.1))
}
},
'svm': {
'clf': LinearSVC() ,
'parameters': {
'penalty': ['l2'],
'loss': ['hinge', 'squared_hinge'],
'C': list(np.arange(0.5, 8.0, 0.1))
}
},
'rf': {
'clf': RandomForestClassifier() ,
'parameters': {
'n_estimators': [10,50,100,200],
'criterion': ['gini', 'entropy'],
'max_depth': [2,3,4],
'min_samples_leaf': [2,3],
},
'n_jobs': 4,
'one_hot': False
},
'ada': {
'clf': AdaBoostClassifier() ,
'parameters': {
'n_estimators': [10,50,100,200],
'learning_rate': [0.05, 0.1, 0.5, 1.0, 2.0],
},
'n_jobs': 4,
'one_hot': False
}
}
exp_to_run = training_config.keys()<train_on_grid>
df_train = pd.concat([df_train,match_type],sort=False, axis = 1)
 df_train.drop(["matchType","matchId","groupId"],axis=1,inplace=True )
results = { 'name': [], 'f1': [], 'accuracy': [] }
train_pred = {}
test_pred = {}
for name in exp_to_run:
conf = training_config[name]
clf = conf['clf']
parameters = conf['parameters']
n_jobs = conf.get('n_jobs', 1)
 one_hot = conf.get('one_hot', True)
 print('=' * 20)
 print('Starting training:', name)
 grid_obj = GridSearchCV(clf, parameters, scoring = 'roc_auc', cv = 4, n_jobs = n_jobs, verbose = 1)
 train_X = train_x_one_hot if one_hot else train_x
print('Number of Features:', train_X.columns.shape[0])
 grid_obj = grid_obj.fit(train_X, train_y)
 best_clf = grid_obj.best_estimator_
print('Best classifier:', repr(best_clf))
model = best_clf.fit(train_X, train_y)
 pred_y = best_clf.predict(train_X)
 train_pred[name] = pred_y
f1 = f1_score(train_y, pred_y)
 acc = accuracy_score(train_y, pred_y)
 results['name'].append(name)
 results['f1'].append(f1)
 results['accuracy'].append(acc)
 test_X = test_x_one_hot if one_hot else test_x
test_y = pd.Series(best_clf.predict(test_X), name="Survived", dtype='int32')
 test_pred[name] = test_y
output = pd.concat([test.PassengerId, test_y], axis=1)
 output_filename = name + "_csv_to_submit.csv"
print('Writing submission file:', output_filename)
 output.to_csv(output_filename, index=False)
<save_to_csv>
threshold = 0.9
high_corr_pair = {}
i = -1
for key,rows in corr.iterrows() :
i += 1
for j,vals in enumerate(rows):
if i != j:
if abs(vals)> threshold:
high_corr_pair["_".join([key,corr.columns[j]])] = vals
high_corr_pair
pred_y = pd.DataFrame.from_dict(train_pred ).mean(axis=1)> 0.5
f1 = f1_score(train_y, pred_y)
 acc = accuracy_score(train_y, pred_y)
 results['name'].append('voting')
 results['f1'].append(f1)
 results['accuracy'].append(acc)
 test_y = pd.Series(pd.DataFrame.from_dict(test_pred ).mean(axis=1)> 0.5,
name="Survived",
dtype='int32')
 output = pd.concat([test.PassengerId, test_y], axis=1)
 output_filename = 'voting_csv_to_submit.csv'
print('Writing submission file:', output_filename)
 output.to_csv(output_filename, index=False )<create_dataframe>
toRemoved = set()
for pair in high_corr_pair:
a,b = pair.split("_")
 if corrTarget[a] <= corrTarget[b]:
toRemoved.add(a)
 else:
toRemoved.add(b)
 toRemoved
pd.DataFrame.from_dict(results )<set_options>
df_train.drop(["killPoints","maxPlace","winPoints"], axis = 1, inplace = True )
%matplotlib inline
warnings.filterwarnings("ignore")
 %matplotlib inline<load_from_csv>
df_train["walkDistance"].quantile(0.99 )
train_data=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test_data=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test_data['SalePrice']=np.nan
test_data['tmpPrice']=np.nan<compute_test_metric>
def cramers_v(x, y):
confusion_matrix = pd.crosstab(x,y)
 chi2 = scipy.stats.chi2_contingency(confusion_matrix)[0]
n = confusion_matrix.sum().sum()
phi2 = chi2/n
r,k = confusion_matrix.shape
phi2corr = max(0, phi2-(( k-1)*(r-1)) /(n-1))
rcorr = r-(( r-1)**2)/(n-1)
 kcorr = k-(( k-1)**2)/(n-1)
 return np.sqrt(phi2corr/min(( kcorr-1),(rcorr-1)) )<compute_test_metric>
def eta_squared(x,y):
moyenne_y = y.mean()
classes = []
for classe in x.unique() :
yi_classe = y[x==classe]
classes.append({'ni': len(yi_classe),
'moyenne_classe': yi_classe.mean() })
 SCT = sum([(yj-moyenne_y)**2 for yj in y])
 SCE = sum([c['ni']*(c['moyenne_classe']-moyenne_y)**2 for c in classes])
 return SCE/SCT<compute_test_metric>
match_type = pd.get_dummies(df_test["matchType"])
 df_test["playersInMatch"] = df_test.groupby("matchId")["Id"].transform("count")
 df_test["playersInGroup"] = df_test.groupby("groupId")["Id"].transform("count")
 df_test = pd.concat([df_test,match_type],sort=False, axis = 1)
 df_test.drop(["matchType","matchId","groupId"],axis=1,inplace=True)
 df_test.drop(["killPoints","maxPlace","winPoints"], axis = 1, inplace = True )
def adaptative_correlation(x,y):
x = x.reset_index(drop=True)
 y = y.reset_index(drop=True)
 non_na = np.logical_and(~x.isna() ,~y.isna())
 x=x[non_na]
y=y[non_na]
if(x.dtype.name=='object' or x.dtype.name=='category')and(y.dtype.name=='object' or y.dtype.name=='category'):
return cramers_v(x,y)
 elif(x.dtype.name=='object' or x.dtype.name=='category'):
return abs(eta_squared(x,y))
elif(y.dtype.name=='object' or y.dtype.name=='category'):
return abs(eta_squared(y,x))
else:
return abs(x.corr(y))<find_best_model_class>
df_train.set_index("Id",inplace=True )
imputer = KNNImputer(n_neighbors=3)
 n_folds = 5
model = RandomForestRegressor(n_jobs=-1)
 def quick_predict(X,y):
X = pd.get_dummies(X)
 X = pd.DataFrame(imputer.fit_transform(X), columns = X.columns)
 kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(X)
 rmse= np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv = kf))
print("Quality score: {:.4f} standard deviation({:.4f})
 ".format(rmse.mean() , rmse.std()))<predict_on_test>
X = df_train.drop(["winPlacePerc"], axis=1 ).values
Y = df_train["winPlacePerc"].values
quick_predict(train_data.drop(['SalePrice'],axis=1),train_data.SalePrice )<categorify>
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.2 )
def mean_encoder(df,columns):
for col in columns:
if col not in df.columns:
print("Error, ",col," not in list of columns")
 else:
mean_encode = df.groupby(col)['tmpPrice'].mean()
mean_encode =(mean_encode - mean_encode.min())/(mean_encode.max() - mean_encode.min())
 new_col_name = col + '_enc'
df.loc[:,new_col_name] = df[col].map(mean_encode )<filter>
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test = sc.transform(X_test )
len(train_data[train_data.SalePrice>600000] )<filter>
train_data = train_data[train_data.SalePrice<600000]<set_options>
%%time
lgb_train = lgb.Dataset(X_train, Y_train)
 lgb_eval = lgb.Dataset(X_test, Y_test, reference=lgb_train)
 lgbm_params = {'objective': 'regression','metric': 'rmse'}
m_lgb = lgb.train(lgbm_params, lgb_train, valid_sets=lgb_eval,
num_boost_round=2000,
early_stopping_rounds=5 )
scipy.stats.skew(train_data.SalePrice.dropna() )<feature_engineering>
Y_pred = m_lgb.predict(X_test, num_iteration=m_lgb.best_iteration)
 r2_score(Y_test,Y_pred )
train_data['tmpPrice']=np.log1p(train_data.SalePrice )<feature_engineering>
X_for_pred = df_test.values[:,1:]
<predict_on_test><EOS> 
X_for_pred = sc.transform(X_for_pred)
 Y_test_pred = m_lgb.predict(X_for_pred, num_iteration=m_lgb.best_iteration)
 for_submission = pd.DataFrame({"Id":df_test.Id,"winPlacePerc":Y_test_pred})
 for_submission.to_csv('submission.csv', index=False )