Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<drop_column>
train_data_df = pd.read_csv('.. /input/train.csv')
 test_data_df = pd.read_csv('.. /input/test.csv' )
related_people.drop(columns={"Ticket","Last_Name","Name","SibSp","Parch","total_related"})
 related_people.set_index("PassengerId",inplace=True)
 train_df["RGroup"]=""
train_df.set_index("PassengerId",inplace=True)
 train_df.update(related_people)
 train_df.reset_index(inplace=True)
 related_people.reset_index(inplace=True)
<prepare_output>
new_train_data_df = train_data_df.groupby(['matchId','groupId'])['assists', 'boosts', 'damageDealt', 'DBNOs','headshotKills', 'heals', 'killPlace', 'killPoints', 'kills','killStreaks', 'longestKill', 'maxPlace', 'numGroups', 'revives','rideDistance', 'roadKills', 'swimDistance', 'teamKills','vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'winPoints'].agg('mean' ).reset_index()
new_train_data_df['winPlacePerc'] = train_data_df['winPlacePerc']
new_train_data_df['winPlacePerc'] = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']
related_people.set_index("PassengerId",inplace=True)
 test_df["RGroup"]=""
test_df.set_index("PassengerId",inplace=True)
 test_df.update(related_people)
 test_df.reset_index(inplace=True)
 related_people.reset_index(inplace=True )<categorify>
new_test_data_df = test_data_df.groupby(['matchId','groupId'])['assists', 'boosts', 'damageDealt', 'DBNOs','headshotKills', 'heals', 'killPlace', 'killPoints', 'kills','killStreaks', 'longestKill', 'maxPlace', 'numGroups', 'revives','rideDistance', 'roadKills', 'swimDistance', 'teamKills','vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'winPoints'].agg('mean' ).reset_index()
train_df["Sex"]=train_df["Sex"].astype("category")
 train_df["Pclass"]=train_df["Pclass"].astype("category")
 train_df["Embarked"]=train_df["Embarked"].astype("category")
 train_df["RGroup"]=train_df["RGroup"].astype("category")
 train_df.drop(columns={"Name","Ticket","PassengerId"},inplace=True)
 train_df=pd.get_dummies(train_df,columns=["Pclass"])
 train_df=pd.get_dummies(train_df,columns=["Sex"])
 train_df=pd.get_dummies(train_df,columns=["Embarked"])
 train_df.rename(columns={"Pclass_1.0":"Pclass_1","Pclass_2.0":"Pclass_2","Pclass_3.0":"Pclass_3"},inplace=True)
<categorify>
import numpy as np
test_df["Sex"]=test_df["Sex"].astype("category")
 test_df["Pclass"]=test_df["Pclass"].astype("category")
 test_df["Embarked"]=test_df["Embarked"].astype("category")
 test_df["RGroup"]=test_df["RGroup"].astype("category")
 test_passengerId=test_df["PassengerId"].copy()
test_df.drop(columns={"Name","Cabin","Ticket","PassengerId"},inplace=True)
 test_df=pd.get_dummies(test_df,columns=["Pclass"])
 test_df=pd.get_dummies(test_df,columns=["Sex"])
 test_df=pd.get_dummies(test_df,columns=["Embarked"])
<define_variables>
train_data = train_data_df.values
test_data = test_data_df.values
train_features = new_train_data_df.values[:, 2:24][0:np.int32(0.8*len(new_train_data_df)) ]
train_targets = new_train_data_df.values[:, 24][0:np.int32(0.8*len(new_train_data_df)) ]
val_features = new_train_data_df.values[:, 2:24][np.int32(0.8*len(new_train_data_df)) :len(new_train_data_df)]
val_targets = new_train_data_df.values[:, 24][np.int32(0.8*len(new_train_data_df)) :len(new_train_data_df)]
test_features = new_test_data_df.values[:, 2:24]
test_data = test_data[:, 0:3]
train_df_dec=train_df.copy()
train_df_dec = train_df_dec.sample(frac=1 ).reset_index(drop=True)
 row_count = train_df_dec.shape[0]
split_point = int(row_count*0.30)
 cv_data, train_data = train_df_dec[:split_point].copy() , train_df_dec[split_point:].copy()<drop_column>
import sklearn
train_df_dec
R=train_data.groupby(["RGroup"])["Survived"].agg(["count","sum"])
 R.reset_index(inplace=True)
 R["percentage"]=R["sum"]/R["count"]
R.drop(columns={"count","sum"},inplace=True )<data_type_conversions>
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(train_features)
 train_features = scaler.transform(train_features)
 val_features = scaler.transform(val_features)
 test_features = scaler.transform(test_features )
R.set_index("RGroup",inplace=True)
 train_data["percentage"]=np.nan
train_data.set_index("RGroup",inplace=True)
 train_data.update(R)
 train_data.reset_index(inplace=True)
 R.reset_index(inplace=True)
<feature_engineering>
model = CatBoostRegressor(iterations=20000, learning_rate=0.1, eval_metric='MAE')
 model.fit(train_features, train_targets, eval_set=(val_features, val_targets))
R.set_index("RGroup",inplace=True)
 cv_data["percentage"]=np.nan
cv_data.set_index("RGroup",inplace=True)
 cv_data.update(R)
 cv_data.reset_index(inplace=True)
 R.reset_index(inplace=True)
 cv_data.loc[(cv_data["percentage"].isnull()),"percentage"]=0.5<feature_engineering>
predictions = model.predict(test_features )
R.set_index("RGroup",inplace=True)
 test_df["percentage"]=np.nan
test_df.set_index("RGroup",inplace=True)
 test_df.update(R)
 test_df.reset_index(inplace=True)
 R.reset_index(inplace=True)
 test_df.loc[(test_df["percentage"].isnull()),"percentage"]=0.5 
<prepare_x_and_y>
new_test_data_df['winPlacePercPred'] = predictions
Y_train=train_data["Survived"].copy()
X_train=train_data.drop(columns={"Survived"})
 Y_cv=cv_data["Survived"].copy()
X_cv=cv_data.drop(columns={"Survived"})
 X_train.drop(columns={"RGroup"},inplace=True)
 X_cv.drop(columns={"RGroup"},inplace=True)
<find_best_params>
group_preds = new_test_data_df.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
scores=[]
for x in range(1,7):
clf=tree.DecisionTreeClassifier(criterion='entropy',max_depth=x)
 clf=clf.fit(X_train,Y_train)
 Y_pred=clf.predict(X_cv)
 scores.append(metrics.accuracy_score(Y_cv,Y_pred))
print(scores )<find_best_params>
group_preds = group_preds['winPlacePercPred']
X_train.drop(columns={"percentage"},inplace=True)
 X_cv.drop(columns={"percentage"},inplace=True)
 test_df.drop(columns={"percentage"},inplace=True)
 test_df.drop(columns={"RGroup"},inplace=True)
 scores=[]
for x in range(1,18):
clf=tree.DecisionTreeClassifier(criterion='entropy',max_depth=x)
 clf=clf.fit(X_train,Y_train)
 Y_pred=clf.predict(X_cv)
 scores.append(metrics.accuracy_score(Y_cv,Y_pred))
print(scores )<prepare_x_and_y>
dictionary = dict(zip(new_test_data_df['groupId'].values, group_preds))
train_df_dec.drop(columns={"RGroup"},inplace=True)
 Target_df=train_df_dec["Survived"]
train_df_dec.drop(columns={"Survived"},inplace=True )<compute_train_metric>
new_preds = []
for i in test_data_df['groupId'].values:
new_preds.append(dictionary[i] )
clf=tree.DecisionTreeClassifier(criterion='entropy',max_depth=4)
 scores_cv = cross_val_score(clf, train_df_dec,Target_df, cv=10)
 scores_cv<train_model>
test_data_df['winPlacePercPred'] = new_preds
clf=tree.DecisionTreeClassifier(criterion='entropy',max_depth=4)
 clf=clf.fit(X_train,Y_train)
 dot_data = tree.export_graphviz(clf, out_file=None,feature_names=X_train.columns.values.tolist())
 graph = graphviz.Source(dot_data)
 graph<save_to_csv>
test_data_df.sort_values(by=['matchId', 'groupId'] )
Y_pred=clf.predict(test_df)
 my_submission = pd.DataFrame({'PassengerId': test_passengerId, 'Survived': Y_pred})
 my_submission.to_csv('submission.csv', index=False)
<set_options>
import numpy as np
pd.options.mode.chained_assignment = None<load_from_csv>
predictions = pd.DataFrame(np.transpose(np.array([test_data[:, 0], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
train_set = pd.read_csv('.. /input/train.csv')
 test_set = pd.read_csv('.. /input/test.csv' )<count_missing_values>
predictions['Id'] = np.int32(predictions['Id'] )
train_set.isnull().sum()<count_missing_values>
predictions.to_csv('PUBG_preds3.csv', index=False )