Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<groupby>
%matplotlib inline
rcParams['figure.figsize'] = 20,5
rcParams['xtick.labelsize'] = 9
rcParams['ytick.labelsize'] = 9
rcParams['axes.labelsize'] = 10
groups = test_data_df.groupby(['matchId', 'groupId'])['groupId'].agg('mean' ).values<drop_column>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 df = pd.concat([train,test] )
features = features.drop(['matchId'], axis=1 )<normalization>
df.Survived.value_counts()
test_features = features.values
test_features = scaler.transform(test_features )<predict_on_test>
eda, df_test = train_test_split(train, test_size=0.25, random_state=42)
 eda.head()
predictions = model.predict(test_features )<groupby>
df_fill = df.copy()
df_fill['Fare'].fillna(df_fill['Fare'].median() , inplace = True)
 df_fill['Embarked'].fillna(df_fill['Embarked'].mode().iloc[0], inplace = True)
 df_fill.head()
features['winPlacePercPred'] = predictions
features['matchId'] = matches
features['groupId'] = groups
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )<sort_values>
df_index = df_fill.set_index('PassengerId')
 df_index.head()
test_data_df = test_data_df.sort_values(['matchId', 'groupId'] )<define_variables>
df_cut = df_split_name.copy()
df_cut['FareBin'] = pd.qcut(df_cut.Fare, 5)
 label = LabelEncoder()
df_cut['FareBin_Code'] = label.fit_transform(df_cut['FareBin'])
 df_cut.drop(['FareBin'], 1, inplace=True )
dictionary = dict(zip(features['groupId'].values, group_preds.values))<prepare_output>
df_cut['AgeBin'] = pd.qcut(df_cut.Age, 5)
 label = LabelEncoder()
df_cut['AgeBin_code'] = label.fit_transform(df_cut['AgeBin'])
 df_cut.drop(['Age','AgeBin'], 1, inplace=True)
 df_cut.head()
new_ranking_preds = []
for i in test_data_df['groupId'].values:
new_ranking_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = new_ranking_preds<prepare_output>
df_comb = df_cut.copy()
df_comb['Family_members_aboard'] = df_comb['SibSp'] + df_comb['Parch']
df_comb.drop(['SibSp','Parch'], axis=1, inplace=True)
 df_comb.head()
predictions = pd.DataFrame(np.transpose(np.array([test_data_df.loc[:, 'Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
predictions['Id'] = np.int32(predictions['Id'])
 predictions = predictions.sort_values(by=['Id'])
 predictions.head(20 )<sort_values>
df_extr_family = df_comb.copy()
df_extr_family.insert(2,'Surname',df_extr_family['Name'].str.extract('([A-Za-z]+)\,', expand=True)[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
df_extr_family['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
df_extr_family.reset_index(inplace=True)
 for surname, sur_group in df_extr_family[df_extr_family['Family_members_aboard'] > 0].groupby(['Surname','Fare']):
for ind, row in sur_group.iterrows() :
smax = sur_group.drop(ind ).Survived.max()
smin = sur_group.drop(ind ).Survived.min()
passID = row['PassengerId']
if smax == 1:
df_extr_family.loc[df_extr_family['PassengerId'] == passID, 'Family_Survival'] = 1
elif smin == 0:
df_extr_family.loc[df_extr_family['PassengerId'] == passID, 'Family_Survival'] = 0
df_extr_family[df_extr_family.Family_Survival != 0.5]
maxPlaces = test_data_df.sort_values(by=['Id'])['maxPlace'].values
numGroups = test_data_df.sort_values(by=['Id'])['numGroups'].values
new_predictions = predictions['winPlacePerc'].values
for i in range(0, len(test_data_df)) :
gap = 1.0 /(maxPlaces[i] - 1.0)
 new_predictions[i] = round(new_predictions[i]/gap)*gap<prepare_output>
df_enc = df_extr_family.copy()
label = LabelEncoder()
df_enc['Embarked_code'] = label.fit_transform(df_enc['Embarked'])
 label = LabelEncoder()
df_enc['Sex_code'] = label.fit_transform(df_enc['Sex'])
 df_enc.drop(['Sex', 'Embarked'], 1, inplace=True)
 df_enc.head()
predictions['winPlacePerc'] = new_predictions<save_to_csv>
def drop_cols(cols):
return df_enc.drop(cols, axis=1)
 attr_to_drop = ['Title', 'Surname', 'Name', 'Ticket', 'Cabin', 'Fare']
df_prepared = drop_cols(attr_to_drop)
 df_prepared.set_index('PassengerId',inplace=True)
 train_ready = df_prepared[:891]
submission = df_prepared[891:]
train_ready
predictions.to_csv('PUBG_preds.csv', index=False )<import_modules>
X = train_ready.drop('Survived',1)
 y = train_ready['Survived']
X_submission = submission.drop('Survived',1 )
<categorify>
std_scaler = StandardScaler()
X = std_scaler.fit_transform(X)
 X_submission = std_scaler.transform(X_submission )
train = pd.get_dummies(train,columns=['matchType'] )<correct_missing_values>
def compare_clf(classifiers):
rows = []
for clf in classifiers:
start = time.time()
score_arr = cross_val_score(clf,X,y,cv=5,scoring='roc_auc')
 end = time.time()
for i, score in enumerate(score_arr):
score_dict = {
'fold':i+1,
'Classifier':clf.__class__.__name__,
'Score':score,
'Time(sec)':end-start
}
rows.append(score_dict)
 return pd.DataFrame(rows)
 classifiers = [
SGDClassifier() ,
LogisticRegression() ,
LinearDiscriminantAnalysis() ,
GaussianNB() ,
DecisionTreeClassifier() ,
RandomForestClassifier() ,
SVC() ,
KNeighborsClassifier()
]
train =train.dropna()<categorify>
compare_clf(classifiers ).groupby('Classifier' ).agg({'mean','median','std'} ).drop('fold',1 ).sort_values(( 'Score','mean'),ascending=False )
test = pd.get_dummies(test,columns=['matchType'] )<prepare_x_and_y>
n_neighbors = [6,7,8,9,10,11,12,14,16,18,20,22]
algorithm = ['auto']
weights = ['uniform', 'distance']
leaf_size = list(range(1,50,5))
hyperparams = {'algorithm': algorithm, 'weights': weights, 'leaf_size': leaf_size,
'n_neighbors': n_neighbors}
gd=GridSearchCV(estimator = KNeighborsClassifier() , param_grid = hyperparams, verbose=True,
cv=5, scoring = "roc_auc")
 gd.fit(X, y)
 print(gd.best_score_)
 print(gd.best_estimator_ )
y_train =train['winPlacePerc']
x_train =train.drop(['Id','groupId','matchId','winPlacePerc'],axis=1 )<prepare_x_and_y>
gd.best_estimator_.fit(X, y)
 y_pred = gd.best_estimator_.predict(X_submission )
X_train = x_train.values
Y_train = y_train.values<choose_model_class>
submit=pd.DataFrame(data=y_pred, index=submission.index, columns=['Survived'], dtype='int')
 submit.to_csv('submission.csv' )