Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<load_from_csv>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
train_data_df = pd.read_csv('.. /input/train_V2.csv')
 test_data_df = pd.read_csv('.. /input/test_V2.csv' )<feature_engineering>
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
train_data_df.insert(loc=28, column='totalDistance', value=train_data_df['swimDistance'] + train_data_df['walkDistance'] + train_data_df['rideDistance'])
 test_data_df.insert(loc=28, column='totalDistance', value=test_data_df['swimDistance'] + test_data_df['walkDistance'] + test_data_df['rideDistance'] )<categorify>
model = XGBClassifier()
train_data_df = train_data_df.replace({'matchType' : {'crashfpp':0, 'crashtpp':1, 'duo':2, 'duo-fpp':3, 'flarefpp':4, 'flaretpp':5, 'normal-duo':6, 'normal-duo-fpp':7, 'normal-solo':8, 'normal-solo-fpp':9, 'normal-squad':10, 'normal-squad-fpp':11, 'solo':12, 'solo-fpp':13, 'squad':14, 'squad-fpp':15}})
 test_data_df = test_data_df.replace({'matchType' : {'crashfpp':0, 'crashtpp':1, 'duo':2, 'duo-fpp':3, 'flarefpp':4, 'flaretpp':5, 'normal-duo':6, 'normal-duo-fpp':7, 'normal-solo':8, 'normal-solo-fpp':9, 'normal-squad':10, 'normal-squad-fpp':11, 'solo':12, 'solo-fpp':13, 'squad':14, 'squad-fpp':15}} )<normalization>
train_data["Sex"] = train_data["Sex"].fillna("NA")
 train_data["Embarked"] = train_data["Embarked"].fillna("C")
 test_data["Sex"] = test_data["Sex"].fillna("NA")
 test_data["Embarked"] = test_data["Embarked"].fillna("C")
 train_data[['Pclass', 'Age', 'SibSp', 'Fare']] = train_data[['Pclass', 'Age', 'SibSp', 'Fare']].fillna(0)
 test_data[['Pclass', 'Age', 'SibSp', 'Fare']] = test_data[['Pclass', 'Age', 'SibSp', 'Fare']].fillna(0 )
train_data_df = reduce_mem_usage(train_data_df)
 test_data_df = reduce_mem_usage(test_data_df )<merge>
genders = {'male': 0, 'female': 1, 'NA': 2}
embarks = {'C': 0, 'Q': 1, 'S': 2,}
train_data['Sex'] = train_data['Sex'].apply(lambda x: genders[x])
 train_data['Embarked'] = train_data['Embarked'].apply(lambda x: embarks[x])
 test_data['Sex'] = test_data['Sex'].apply(lambda x: genders[x])
 test_data['Embarked'] = test_data['Embarked'].apply(lambda x: embarks[x] )
groupedGroups = train_data_df.groupby(['matchId','groupId'])[columns]
print("Add group mean features")
 group_features = groupedGroups.agg('mean')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(group_features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['_group_mean', '_group_mean_rank'])
 print("Add group max features")
 group_features = groupedGroups.agg('max')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max_rank'])
 print("Add group min features")
 group_features = groupedGroups.agg('min')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min_rank'])
 print("Add group size features")
 group_features = train_data_df.groupby(['matchId','groupId'] ).size()
group_features.columns = ['matchId', 'groupId', 'group_size']
group_features = pd.DataFrame(group_features)
 features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', ''] )<merge>
X = train_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Embarked', 'Fare']]
Y = train_data['Survived']
groupedMatches = train_data_df.groupby(['matchId'])[columns]
print("Add match mean features")
 match_features = groupedMatches.agg('mean')[columns].reset_index()
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', '_match_mean'])
 print("Add match size features")
 match_features = train_data_df.groupby(['matchId'] ).size().reset_index()
match_features.columns = ['matchId', 'match_size']
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', ''] )<correct_missing_values>
sc = StandardScaler()
X = sc.fit_transform(X )
features = features.fillna(0.0 )<groupby>
trainX, testX, trainY, testY = train_test_split(X, Y, test_size=0.2, random_state=42 )
train_targets = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']<prepare_output>
model.fit(trainX, trainY )
train_targets = train_targets.values<import_modules>
predict = model.predict(testX )
import sklearn<normalization>
from sklearn.metrics import accuracy_score
train_features = features.drop(['matchId', 'groupId'], axis=1 ).values
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(train_features)
 train_features = scaler.transform(train_features )<correct_missing_values>
accuracy_score(predict, testY )
train_features = np.delete(train_features, 266069, 0)
 train_targets = np.delete(train_targets, 266069, 0 )<import_modules>
from sklearn.ensemble import RandomForestClassifier
import ultimate
from ultimate.mlp import MLP<train_model>
model_f = RandomForestClassifier()
epoch_train = 15
model = MLP(layer_size=[train_features.shape[1], 32, 32, 32, 1], regularization=1, output_shrink=0.1, output_range=[0,1], loss_type="hardmse")
 model.train(train_features, train_targets, iteration_log=20000, rate_init=0.08, rate_decay=0.8, epoch_train=epoch_train, epoch_decay=1 )<merge>
model_f.fit(trainX, trainY )
groupedGroups = test_data_df.groupby(['matchId','groupId'])[columns]
print("Add group mean features")
 group_features = groupedGroups.agg('mean')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(group_features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['_group_mean', '_group_mean_rank'])
 print("Add group max features")
 group_features = groupedGroups.agg('max')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_max_rank'])
 print("Add group min features")
 group_features = groupedGroups.agg('min')
 group_rank_features = group_features.groupby(['matchId'])[columns].rank(pct=True ).reset_index()
features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min'])
 features = pd.merge(features, group_rank_features, on=['matchId', 'groupId'], how='right', suffixes=['', '_group_min_rank'])
 print("Add group size features")
 group_features = test_data_df.groupby(['matchId','groupId'] ).size()
group_features.columns = ['matchId', 'groupId', 'group_size']
group_features = pd.DataFrame(group_features)
 features = pd.merge(features, group_features, on=['matchId', 'groupId'], how='right', suffixes=['', ''] )<merge>
predict_f = model_f.predict(testX )
groupedMatches = test_data_df.groupby(['matchId'])[columns]
print("Add match mean features")
 match_features = groupedMatches.agg('mean')[columns].reset_index()
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', '_match_mean'])
 print("Add match size features")
 match_features = test_data_df.groupby(['matchId'] ).size().reset_index()
match_features.columns = ['matchId', 'match_size']
features = pd.merge(features, match_features, on=['matchId'], how='right', suffixes=['', ''] )<normalization>
accuracy_score(predict_f, testY )
test_features = features.drop(['matchId', 'groupId'], axis=1 ).values
test_features = scaler.transform(test_features )<predict_on_test>
from sklearn.svm import SVC
predictions = model.predict(test_features )<groupby>
model_svm = SVC(gamma='scale', decision_function_shape='ovo' )
features['winPlacePercPred'] = predictions
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )<define_variables>
model_svm.fit(trainX, trainY )
dictionary = dict(zip(features['groupId'].values, group_preds.values))<prepare_output>
predict_svm = model_svm.predict(testX )
individual_preds = []
for i in test_data_df['groupId'].values:
individual_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = individual_preds<create_dataframe>
accuracy_score(predict_f, testY )
predictions = pd.DataFrame(np.transpose(np.array([test_data_df['Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']<prepare_output>
from sklearn.neural_network import MLPClassifier
maxPlaces = test_data_df['maxPlace'].values
new_predictions = predictions['winPlacePerc'].values
for i in range(0, len(test_data_df)) :
if maxPlaces[i] == 0:
new_predictions[i] = 0.0
if maxPlaces[i] == 1:
new_predictions[i] = 1.0
else:
gap = 1.0 /(maxPlaces[i] - 1.0)
 new_predictions[i] = round(new_predictions[i]/gap)*gap<prepare_output>
model_nn = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1 )
predictions['winPlacePerc'] = new_predictions<save_to_csv>
model_nn.fit(trainX, trainY )
predictions.to_csv('PUBG_preds.csv', index=False )<define_variables>
predict_nn = model_nn.predict(testX )
def get_sample(df,n):

 idxs = sorted(np.random.permutation(len(df)) [:n])
 return df.iloc[idxs].copy()
def proc_df(df, y_fld, skip_flds=None, do_scale=False, na_dict=None,
preproc_fn=None, max_n_cat=None, subset=None, mapper=None):

 if not skip_flds: skip_flds=[]
if subset: df = get_sample(df,subset)
 df = df.copy()
if preproc_fn: preproc_fn(df)
 y = df[y_fld].values
df.drop(skip_flds+[y_fld], axis=1, inplace=True)
 if na_dict is None: na_dict = {}
for n,c in df.items() : na_dict = fix_missing(df, c, n, na_dict)
 if do_scale: mapper = scale_vars(df, mapper)
 for n,c in df.items() : numericalize(df, c, n, max_n_cat)
 res = [pd.get_dummies(df, dummy_na=True), y, na_dict]
if do_scale: res = res + [mapper]
return res
def rf_feat_importance(m, df):
return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}
).sort_values('imp', ascending=False)
 def set_rf_samples(n):

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n))
def reset_rf_samples() :

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n_samples))<load_from_csv>
accuracy_score(predict_nn, testY )
train = pd.read_csv(KAGGLE_DIR + 'train_V2.csv')
 test = pd.read_csv(KAGGLE_DIR + 'test_V2.csv' )<train_model>
test_df = test_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Embarked', 'Fare']]
test_df = sc.transform(test_df)
 final_predictions = model_svm.predict(test_df )
<filter><EOS> 
submission = pd.DataFrame({
"PassengerId": test_data["PassengerId"],
"Survived": final_predictions
})
 submission.to_csv('submission.csv', index=False )