Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<sort_values>
%matplotlib inline
warnings.filterwarnings('ignore' )
df_raw = df_raw.sort_values(['matchId'] )<prepare_x_and_y>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.describe(include="all" ).T
train_cats(df_raw)
 df, y, nas = proc_df(df_raw, 'winPlacePerc', max_n_cat=5 )<split>
print(pd.isnull(train ).sum() )
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = 444647
n_trn = len(df)-n_valid<compute_test_metric>
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1)
 print('Feature Dropped!!' )
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )<define_variables>
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1)
 print('Feature Dropped!!' )
set_rf_samples(100000 )<define_variables>
train = train.fillna({"Embarked": "S"})
 train.head()
drop_cols = ['totalDistance', 'walkDistance', 'groupId', 'matchId', 'Id']<drop_column>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
df = df.drop(drop_cols, axis=1 )<split>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()
X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train.shape<train_model>
mr_age = train[train["Title"] == 1]["AgeGroup"].mode()
miss_age = train[train["Title"] == 2]["AgeGroup"].mode()
mrs_age = train[train["Title"] == 3]["AgeGroup"].mode()
master_age = train[train["Title"] == 4]["AgeGroup"].mode()
royal_age = train[train["Title"] == 5]["AgeGroup"].mode()
rare_age = train[train["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train["AgeGroup"])) :
if train["AgeGroup"][x] == "Unknown":
train["AgeGroup"][x] = age_title_mapping[train["Title"][x]]
for x in range(len(test["AgeGroup"])) :
if test["AgeGroup"][x] == "Unknown":
test["AgeGroup"][x] = age_title_mapping[test["Title"][x]]
m = RandomForestRegressor(n_estimators=100, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 %time m.fit(X_train, y_train)
 print_score(m )<compute_test_metric>
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1)
 print('Dropped!!' )
fi = rf_feat_importance(m, df); fi[:]<features_selection>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1)
 print('Dropped!!' )
to_keep = fi[fi.imp>0.0025].cols; len(to_keep )<split>
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn )<save_model>
test = test.fillna({"Embarked": "S"})
 embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()
np.save('keep_cols.npy', np.array(df_keep.columns))<load_pretrained>
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )
keep_cols = np.load('keep_cols.npy')
 df_keep = df[keep_cols]<split>
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2 )
X_train, X_valid = split_vals(df_keep, n_trn )<train_model>
print('gaussian accuracy score is:',acc_gaussian )
m = RandomForestRegressor(n_estimators=70, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )<load_from_csv>
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('logreg accuracy score is:',acc_logreg )
df_test = pd.read_csv('.. /input/test_V2.csv' )<sort_values>
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('svc accuracy score is:',acc_svc )
df_test['totalDistance'] = df_test['rideDistance'] + df_test['walkDistance'] + df_test['swimDistance']
ranking_cats = ['killPlace', 'damageDealt', 'kills', 'walkDistance', 'rankPoints', 'weaponsAcquired', 'totalDistance']
for c in ranking_cats: df_test[c+'_ranking'] = df_test.groupby('matchId')[c].rank(ascending=False)
 df_test.head()<concatenate>
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('linear svc accuracy score is:',acc_linear_svc )
train_cats(df_test )<compute_test_metric>
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('perceptron accuracy score is:',acc_perceptron )
df_test, y, nas = proc_df(df=df_test, y_fld=None )<load_from_csv>
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('decision tree accuracy score is:',acc_decisiontree )
df_submit = df_test[keep_cols]
a = m.predict(df_submit)
 a = pd.Series(a)
 submission = pd.Series(( pd.read_csv('.. /input/test_V2.csv', low_memory=False)) ['Id'])
 submission = pd.concat([submission, a], axis=1)
 submission = submission.rename(columns={0:'winPlacePerc'} )<save_to_csv>
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('random forest accuracy score is:',acc_randomforest )
submission.to_csv('submission.csv', index=False )<save_to_csv>
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('knn accuracy score is:',acc_knn )
submission.to_csv('submission.csv', index=False )<set_options>
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('stochastic gradient descent accuracy score is:',acc_sgd )
%matplotlib inline<load_from_csv>
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print('gradient boosting accuracy score is:',acc_gbk )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )<count_missing_values>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk]})
 models.sort_values(by='Score', ascending=False )
<count_missing_values><EOS> 
ids = test['PassengerId']
predictions = gbk.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )