Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
df.pivot_table(values = 'Age', index = ['Pclass', 'SibSp', 'Parch'], aggfunc = 'median' )<count_missing_values>
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv' )
df.Age.isnull().sum()<feature_engineering>
train.drop(2744604,inplace=True )
age_null = df.Age.isnull()
group_med_age = df.pivot_table(values = 'Age', index = ['Pclass', 'SibSp'], aggfunc = 'median')
 df.loc[age_null, 'Age'] = df.loc[age_null, ['Pclass', 'SibSp']].apply(lambda x: group_med_age.loc[(group_med_age.index.get_level_values('Pclass')== x.Pclass)&(group_med_age.index.get_level_values('SibSp')== x.SibSp)].Age.values[0], axis = 1 )<count_missing_values>
all_data = pd.concat([train, test] )
df.Age.isnull().sum()<count_missing_values>
all_data['playersJoined'] = all_data.groupby('matchId')['matchId'].transform('count' )
print("Cabin has", df.Cabin.isnull().sum() , "missing values out of", len(df))<correct_missing_values>
all_data.drop(columns = ['Id','groupId','matchId'],inplace=True )
df.Cabin.fillna('O', inplace = True )<count_missing_values>
all_data['killsNorm'] = all_data['kills']*(( 100-all_data['playersJoined'])/100+1)
 all_data['damageDealtNorm'] = all_data['damageDealt']*(( 100-all_data['playersJoined'])/100+1)
 all_data['totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']
all_data['headshot_rate'] = all_data['headshotKills']/all_data['kills']
all_data['headshot_rate'] = all_data['headshot_rate'].fillna(0)
 all_data = pd.get_dummies(all_data,columns=['matchType'])
 all_data['BoostAndHeal'] = all_data['boosts'] + all_data['heals']
all_data['skill'] = all_data['roadKills'] +all_data['headshotKills']
all_data['teamwork'] = all_data['revives'] +all_data['assists']
df.isnull().sum()<feature_engineering>
train = all_data.iloc[:4446965]
test = all_data.iloc[4446965:]
df['Fare_log'] = df.Fare.map(lambda i: np.log(i)if i > 0 else 0 )<feature_engineering>
train.drop(train[(train['kills']>1)&(train['totalDistance']==0)].index,inplace=True )
df['Family_size'] = 1 + df.Parch + df.SibSp
df['Alone'] = np.where(df.Family_size == 1, 1, 0 )<count_values>
train.drop(train[train['kills']>20].index,inplace=True )
print(df.Family_size.value_counts())
 print(df.Alone.value_counts() )<feature_engineering>
X = train.drop(columns=['winPlacePerc'])
 Y = train['winPlacePerc']
df.loc[df['Family_size'] == 1, 'Family_size_bin'] = 0
df.loc[(df['Family_size'] >= 2)&(df['Family_size'] <= 4), 'Family_size_bin'] = 1
df.loc[df['Family_size'] >=5, 'Family_size_bin'] = 2<count_values>
df['Title'] = df.Name.str.split(", ", expand = True)[1].str.split(".", expand = True)[0]
df.Title.value_counts()<count_values>
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from IPython.display import display
from sklearn import metrics
import lightgbm as lgb
from sklearn.metrics import accuracy_score
minor_titles = df.Title.value_counts() <= 4
df['Title'] = df.Title.apply(lambda x: 'Others' if minor_titles.loc[x] == True else x)
 df.Title.value_counts()<data_type_conversions>
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed )
df['Fare_bin'] = pd.qcut(df.Fare, 4, labels = [0,1,2,3] ).astype(int)
 df['Age_bin'] = pd.cut(df.Age.astype(int), 5, labels = [0,1,2,3,4] ).astype(int )<categorify>
d_train = lgb.Dataset(X_train, label=y_train)
 params = {}
params['objective'] = 'regression'
params['metric'] = 'mae'
label = LabelEncoder()
df['Title'] = label.fit_transform(df.Title)
 df['Sex'] = label.fit_transform(df.Sex)
 df['Embarked'] = label.fit_transform(df.Embarked)
 df['Cabin'] = label.fit_transform(df.Cabin )<drop_column>
model = lgb.train(params, d_train )
corr_columns = list(df.drop(['Name', 'PassengerId', 'Ticket', 'label'], axis = 1 ).columns )<feature_engineering>
y_pred=model.predict(X_test )
df['Ticket'] = df.Ticket.map(lambda x: re.sub(r'\W+', '', x))<feature_engineering>
mae = mean_absolute_error(y_test, y_pred)
 print("MAE: {}".format(mae))
Ticket = []
for i in list(df.Ticket):
if not i.isdigit() :
Ticket.append(i[:2])
 else:
Ticket.append("X")
 df['Ticket'] = Ticket<categorify>
d_train_full = lgb.Dataset(X, label=Y)
 params = {}
params['objective'] = 'regression'
params['metric'] = 'mae'
df = pd.get_dummies(df, columns = ['Ticket'], prefix = 'T' )<define_variables>
model_full = lgb.train(params, d_train_full )
cat_variables = [x for x in df.columns if df.dtypes[x] == 'object']
cat_variables<drop_column>
X_submit = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv' )
df.drop(['Name', 'PassengerId'], axis = 1, inplace = True )<drop_column>
test.drop(columns=['winPlacePerc'], inplace = True )
train = df.loc[df.label == 'train'].drop('label', axis = 1)
 test = df.loc[df.label == 'test'].drop(['label', 'Survived'], axis = 1 )<prepare_x_and_y>
y_pred_submit=model_full.predict(test )
X_train = train.drop(['Survived'], axis = 1)
 y_train = train['Survived'].astype(int)
 X_test = test<choose_model_class>
submission = pd.concat([X_submit,pd.Series(y_pred_submit, name='winPlacePerc')], axis=1 )
kfold = StratifiedKFold(n_splits = 5 )<choose_model_class>
submission['pred_winPlacePerc'] = submission.iloc[:,-1]
def adjust_pred(x):
space = 1/(x.maxPlace-1)
 return round(x.pred_winPlacePerc / space)* space
submission['adj_winPlacePerc'] = adjust_pred(submission)
 submission.head()
classifiers = []
classifiers.append(KNeighborsClassifier())
 classifiers.append(LinearDiscriminantAnalysis())
 classifiers.append(LogisticRegression(random_state = 0))
classifiers.append(LinearSVC(random_state = 0))
classifiers.append(SVC(random_state = 0))
classifiers.append(RandomForestClassifier(random_state = 0))
classifiers.append(ExtraTreesClassifier(random_state = 0))
classifiers.append(XGBClassifier(random_state = 0))
classifiers.append(LGBMClassifier(random_state = 0))
classifiers.append(MLPClassifier())
 cv_results = []
for classifier in classifiers:
cv_results.append(cross_val_score(classifier, X_train, y_train, scoring = 'accuracy', cv = kfold, n_jobs = -1))
cv_means = []
cv_std = []
for cv_result in cv_results:
cv_means.append(cv_result.mean())
 cv_std.append(cv_result.std())
 cv_res = pd.DataFrame({'CV_means':cv_means, 'CV_std':cv_std, 'Algorithm':['KNN', 'LinearDiscriminantAnalysis', 'LogisticRegression', 'LinearSVC', 'SVC', 'RandomForest', 'ExtraTrees', 'XGB', 'LGB', 'MLP']})
<train_model>
submission = submission.loc[:,['Id','adj_winPlacePerc']]
submission.columns = ['Id','winPlacePerc']
submission.head()
LDA_best = LinearDiscriminantAnalysis().fit(X_train, y_train )<train_on_grid>
RF = RandomForestClassifier(random_state = 0)
 RF_params = {'n_estimators' : [10,50,100],
'criterion' : ['gini', 'entropy'],
'max_depth' : [5,8,None],
'min_samples_split' : [2,5,8],
'min_samples_leaf' : [1,3,5],
'max_features' : ['auto', 'log2', None]}
GS_RF = GridSearchCV(RF, param_grid = RF_params, cv = kfold, scoring = 'accuracy', n_jobs = -1, verbose = 1)
 GS_RF.fit(X_train, y_train)
 RF_best = GS_RF.best_estimator_
print("Best parameters :", RF_best)
 print("Best score :", GS_RF.best_score_ )<train_on_grid>
ET = ExtraTreesClassifier(random_state = 0)
 ET_params = {'n_estimators' : [10,50,100],
'criterion' : ['gini', 'entropy'],
'max_depth' : [5,8,None],
'min_samples_split' : [2,5,8],
'min_samples_leaf' : [1,3,5],
'max_features' : ['auto', 'log2', None]}
GS_ET = GridSearchCV(ET, param_grid = ET_params, cv= kfold, scoring = 'accuracy', n_jobs = -1, verbose = 1)
 GS_ET.fit(X_train, y_train)
 ET_best = GS_ET.best_estimator_
print('Best parameters :', ET_best)
 print('Best score :', GS_ET.best_score_ )<set_options>
submission.to_csv('submission.csv', index=False )
<train_on_grid><EOS> 
submission.to_csv('submission.csv', index=False )