Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
%matplotlib inline
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
sns.set()
warnings.filterwarnings('ignore')
 gc.enable()
model1.fit(X, y)
 model2.fit(X, y)
 model3.fit(X, y)
 model4.fit(X, y)
 model5.fit(X, y)
 predictions1 = model1.predict(X_target)
 predictions2 = model2.predict(X_target)
 predictions3 = model3.predict(X_target)
 predictions4 = model4.predict(X_target)
 predictions5 = model5.predict(X_target)
 predictions = 0.2*predictions1+0.3*predictions2+0.1*predictions3+0.2*predictions4+0.2*predictions5<prepare_output>
def draw_missing_data_table(df):
total = df.isnull().sum().sort_values(ascending=False)
 percent =(df.isnull().sum() /df.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 return missing_data
predictions = np.expm1(predictions )<create_dataframe>
path_train = '.. /input/train_V2.csv'
path_test = '.. /input/test_V2.csv'
submission = pd.DataFrame({'Id': data.loc['test'].index, 'SalePrice': predictions} )<save_to_csv>
train_df_raw = pd.read_csv(path_train)
 train_df_raw.head()
submission.to_csv("submission.csv", index=False )<save_to_csv>
draw_missing_data_table(train_df_raw )
submission.to_csv("submission.csv", index=False )<set_options>
train_df_raw['matchType'].value_counts()
%matplotlib inline
warnings.filterwarnings("ignore")
 %matplotlib inline<load_from_csv>
def preprocess_data(df, with_categorical=False):
processed_df = df.drop(['Id', 'rankPoints'], axis=1)
 print('-'*5 + ' handling matchType column ' + '-'*5)
 new_matchType_cols = list()
if with_categorical:
for mtype in processed_df['matchType']:
if mtype in ['squad', 'squad-fpp', 'normal-squad-fpp', 'normal-squad', 'flarefpp', 'flaretpp']:
new_matchType_cols.append([3])
 elif mtype in ['solo', 'solo-fpp', 'normal-solo-fpp', 'normal-solo']:
new_matchType_cols.append([1])
 else:
new_matchType_cols.append([2])
 match_df = pd.DataFrame(new_matchType_cols, columns=['matchType'], index=processed_df.index)
 else:
for mtype in processed_df['matchType']:
if mtype in ['squad', 'squad-fpp', 'normal-squad-fpp', 'normal-squad', 'flarefpp', 'flaretpp']:
new_matchType_cols.append([1, 0, 0])
 elif mtype in ['solo', 'solo-fpp', 'normal-solo-fpp', 'normal-solo']:
new_matchType_cols.append([0, 0, 1])
 else:
new_matchType_cols.append([0, 1, 0])
 match_df = pd.DataFrame(new_matchType_cols, columns=['squad','duo', 'solo'], index=processed_df.index)
 processed_df = processed_df.drop(['matchType'], axis=1)
 print('-'*5 + ' create matchSize column ' + '-'*5)
 match_size = processed_df.groupby(['matchId'] ).size().reset_index(name='matchSize')
 processed_df = processed_df.merge(match_size, how='left', on=['matchId'])
 print('-'*5 + ' create teamSize column ' + '-'*5)
 processed_df['combinedId'] = processed_df['matchId'] + processed_df['groupId']
group_size = processed_df.groupby(['combinedId'] ).size().reset_index(name='teamSize')
 processed_df = processed_df.merge(group_size, how='left', on=['combinedId'])
 print('-'*5 + ' create totalDistance column ' + '-'*5)
 processed_df['totalDistance'] = processed_df['rideDistance'] + processed_df['walkDistance'] + processed_df['swimDistance']
processed_df = processed_df.drop(['combinedId', 'matchId', 'groupId'], axis=1)
 processed_df = processed_df.join(match_df)
 processed_df = processed_df.drop(['teamKills', 'vehicleDestroys', 'roadKills', 'swimDistance', 'headshotKills', 'solo', 'duo', 'squad'], 1)
 return processed_df
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>
train_df = preprocess_data(train_df_raw.dropna())
 X_train = train_df.drop('winPlacePerc', 1)
 y_train = train_df['winPlacePerc']
sc = StandardScaler()
X_train = pd.DataFrame(sc.fit_transform(X_train.values), index=X_train.index, columns=X_train.columns)
 X_train.head()
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>
test_df_raw = pd.read_csv(path_test)
 draw_missing_data_table(test_df_raw )
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>
X_test = preprocess_data(test_df_raw)
 X_test = pd.DataFrame(sc.fit_transform(X_test.values), index=X_test.index, columns=X_test.columns)
 X_test.head()
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>
params = {
'num_leaves': 2048,
'learning_rate': 0.01,
'n_estimators': 1000,
'min_data_in_leaf': 400,
'max_bin': 1000,
'random_state': 42,
'objective' : 'regression',
'metric': 'mae'
}
model = lgb.LGBMRegressor(**params, verbose=2, n_jobs=4, silent=False)
 model.fit(X_train, y_train, eval_metric= 'mae' )
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>
prediction = model.predict(X_test )
quick_predict(train_data.drop(['SalePrice'],axis=1),train_data.SalePrice )<categorify>
for i in range(len(test_df_raw)) :
winPlacePerc = prediction[i]
maxPlace = int(test_df_raw.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
prediction[i] = winPlacePerc
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>
result_df = test_df_raw.copy()
result_df['winPlacePerc'] = prediction
result_df.head()
result_df.to_csv('submission.csv', columns=['Id', 'winPlacePerc'], index=False )