Kaggle competition: Titanic - Machine Learning from Disaster
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))<load_pretrained>
df = pd.read_csv('.. /input/train.csv')
 %matplotlib inline
df.head()
df.isnull().sum()
df['Fare'] = df['Fare'].fillna(df['Fare'].median())
 df['Age'] = df['Age'].fillna(df['Age'].median())
 df['Embarked'] = df['Embarked'].fillna('S')
 df['Sex'] = df['Sex'].apply(lambda x: 1 if x == 'male' else 0)
 df['Embarked'] = df['Embarked'].map({'S':0, 'C':1, 'Q':2} ).astype(int)
 df = df.drop(['Cabin','Name','PassengerId','Ticket'],axis = 1)
 train_X = df.drop('Survived',axis = 1)
 train_y = df.Survived 
(train_X, test_X, train_y, test_y)= train_test_split(train_X, train_y, test_size = 0.3, random_state = 0)
 df.isnull().sum()
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv' )<import_modules>
clf = DecisionTreeClassifier(random_state = 0)
 clf.fit(train_X, train_y)
import numpy as np
import pandas as pd
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss<load_from_csv>
parameters = {
"max_depth":[2,3,None],
"n_estimators":[50,100,200,300,400,500],
"max_features":[1,3,7],
"min_samples_split":[2,3,10],
"min_samples_leaf":[1,3,10],
"bootstrap":[True,False],
"criterion":["gini","entropy"]
}
clf = GridSearchCV(
 estimator = RandomForestClassifier() ,
param_grid = parameters,
scoring = "accuracy",
n_jobs = -1,
cv = 5 
)
 clf.fit(train_X , train_y)
season_result = pd.read_csv('.. /input/RegularSeasonCompactResults.csv')
 tourney_result = pd.read_csv('.. /input/NCAATourneyCompactResults.csv')
 sample_submission = pd.read_csv('.. /input/SampleSubmissionStage2.csv' )<feature_engineering>
MLPClassifier?
team_list = sorted(set(season_result.WTeamID)| set(season_result.LTeamID))
n_teams = len(team_list)
 team_to_int = {t: i for i, t in enumerate(team_list)}
season_result['team_a'] = season_result.WTeamID.apply(team_to_int.get)
 season_result['team_b'] = season_result.LTeamID.apply(team_to_int.get)
 season_result['log_ratio'] = np.log(season_result.WScore / season_result.LScore)
 tourney_result['team_a'] = tourney_result.WTeamID.apply(team_to_int.get)
 tourney_result['team_b'] = tourney_result.LTeamID.apply(team_to_int.get)
 sample_submission['team_a'] = sample_submission.ID.apply(lambda a: team_to_int[int(a.split('_')[1])])
 sample_submission['team_b'] = sample_submission.ID.apply(lambda a: team_to_int[int(a.split('_')[2])] )<categorify>
parameters={
'solver':['lbfgs', 'sgd', 'adam'],
'activation':['identity', 'logistic', 'tanh', 'relu'],
'hidden_layer_sizes':[(100,),(100, 10),(100, 100, 10),(100, 100, 100, 10)],
'learning_rate':['constant', 'invscaling', 'adaptive'],
'early_stopping':[True],
}
clf = GridSearchCV(
 estimator = MLPClassifier() ,
param_grid = parameters,
n_jobs = -1,
cv = 5 
)
 clf.fit(train_X , train_y)
 print('clf.best_params_ : ', clf.best_params_)
 print('clf.best_estimator_ : ', clf.best_estimator_)
 print('clf.best_score_ : ', clf.best_score_)
def encode_team(d):
n_samples = d.shape[0]
a = np.arange(n_samples)
 x = sp.sparse.lil_matrix(( n_samples, n_teams))
x[a, d.team_a] = 1
x[a, d.team_b] = -1
return x<categorify>
clf.score(train_X , train_y )
def cv_w_c(year_list, w_list, c_list):
loss_array = np.zeros(( len(year_list), len(w_list), len(c_list)))
 for i, year in enumerate(year_list):
sr = season_result[season_result.Season == year]
x_sr = encode_team(sr)
 y_sr = np.ones(x_sr.shape[0], dtype=np.int64)
 x_train = sp.sparse.vstack([x_sr, -x_sr])
 y_train = np.concatenate([y_sr, -y_sr])
 log_ratio = np.concatenate([sr.log_ratio, sr.log_ratio])
 tr = tourney_result[tourney_result.Season == year]
x_tr = encode_team(tr)
 y_tr = np.ones(x_tr.shape[0], dtype=np.int64)
 x_test = sp.sparse.vstack([x_tr, -x_tr])
 y_test = np.concatenate([y_tr, -y_tr])
 for j, w in enumerate(w_list):
if w is None:
w_train = None
else:
w_train = w * log_ratio.min() + log_ratio
for k, c in enumerate(c_list):
cls = LogisticRegression(C=c)
 cls.fit(x_train, y_train, w_train)
 col = list(cls.classes_ ).index(1)
 y_pred = cls.predict_proba(x_test)[:, col]
loss = log_loss(y_test, y_pred)
 loss_array[i, j, k] = loss
return loss_array.mean(0 )<define_variables>
pred = clf.predict(test_X)
 fpr, tpr , thresholds = roc_curve(test_y,pred,pos_label = 1)
 auc(fpr,tpr)
 accuracy_score(pred,test_y )
<find_best_params><EOS> 
fin = pd.read_csv('.. /input/test.csv')
 fin.head()
passsengerid = fin['PassengerId']
fin.isnull().sum()
fin['Fare'] = fin['Fare'].fillna(fin['Fare'].median())
 fin['Age'] = fin['Age'].fillna(fin['Age'].median())
 fin['Embarked'] = fin['Embarked'].fillna('S')
 fin['Sex'] = fin['Sex'].apply(lambda x: 1 if x == 'male' else 0)
 fin['Embarked'] = fin['Embarked'].map({'S':0 , 'C':1 , 'Q':2} ).astype(int)
 fin= fin.drop(['Cabin','Name','Ticket','PassengerId'],axis =1)
 predictions = clf.predict(fin)
 submission = pd.DataFrame({'PassengerId':passsengerid, 'Survived':predictions})
 submission.to_csv('submission.csv' , index = False)