Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
print(f'matplotlib: {matplotlib.__version__}')
 print(f'sklearn : {sklearn.__version__}' )
df_test['winPlacePerc'] = y_pred_test
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<load_from_csv>
print(f'pandas version: {pd.__version__}')
 test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv" )
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' )<drop_column>
def format_data(data):
data = pd.get_dummies(data, columns=['Sex','Embarked'])
 data = data.drop(['Name','Ticket','Cabin'], axis=1)
 data.fillna(data.mean() , inplace=True)
 if 'Survived' in data.columns:
data_y = data['Survived']
data_x = data.drop(['Survived'], axis=1)
 return data_x, data_y
else:
return data
train_x, train_y = format_data(train)
 test_x = format_data(test)
 train_x.describe()
train.drop(2744604,inplace=True )<concatenate>
model = RandomForestClassifier(random_state=1)
 model.fit(train_x, train_y);
all_data = pd.concat([train, test] )<groupby>
X_train, X_test, y_train, y_test = train_test_split(train_x, train_y,
test_size=0.2,
random_state=42)
 model.fit(X_train, y_train)
 def summary_stats(x,y):
pred = model.predict(x)
 f1 = f1_score(pred, y)
 acc = model.score(x, y)
 print(f" F1 score: {f1}")
 print(f" Accuracy: {acc}")
 print(f"Training:")
 summary_stats(X_train, y_train)
 print(f"Testing:")
 summary_stats(X_test, y_test )
all_data['playersJoined'] = all_data.groupby('matchId')['matchId'].transform('count' )<drop_column>
search_pars = {
'n_estimators': [10, 30, 100, 300, 1000],
'max_features': [0.25, 0.5, 0.75, 1.0],
'criterion' : ['gini', 'entropy']
}
all_data.drop(columns = ['Id','groupId','matchId'],inplace=True )<categorify>
rf_model = RandomForestClassifier(random_state=1)
 clf = GridSearchCV(rf_model, search_pars)
 clf.fit(X_train, y_train);
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']<split>
model = clf.best_estimator_
model.fit(train_x, train_y);
train = all_data.iloc[:4446965]
test = all_data.iloc[4446965:]<drop_column>
print("Final model training results:")
 summary_stats(train_x, train_y )
train.drop(train[(train['kills']>1)&(train['totalDistance']==0)].index,inplace=True )<drop_column>
pred_test = model.predict(test_x )
train.drop(train[train['kills']>20].index,inplace=True )<prepare_x_and_y>
submission.to_csv('submission.csv', index=False )
X = train.drop(columns=['winPlacePerc'])
 Y = train['winPlacePerc']<prepare_x_and_y>
submission.to_csv('submission.csv', index=False )