Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv" )
%%time
lr = LogisticRegression(C=0.012, max_iter=10000)
 lr.fit(X_train_7, y_train )<predict_on_test>
%matplotlib inline
train[num_features].hist(bins=8, figsize=(10,10))
val_preds_7 = lr.predict_proba(X_val_7)[:,1]
test_preds_7 = lr.predict_proba(test_EB7)[:,1]<compute_test_metric>
num_passengers_in_train_set = 891
num_pass_without_child_or_parent = train.loc[train.Parch == 0]['Parch'].count()
percent_pass_without_child_or_parent = 100 * num_pass_without_child_or_parent / num_passengers_in_train_set
print("%2d passengers(%2d%%)travelled without a parent or child" %(num_pass_without_child_or_parent, percent_pass_without_child_or_parent))
num_pass_without_sib_or_spouse = train.loc[train.SibSp == 0]['SibSp'].count()
percent_pass_without_sib_or_spouse = 100 * num_pass_without_sib_or_spouse / num_passengers_in_train_set
print("%2d passengers(%2d%%)travelled without a sibling or spouse" %(num_pass_without_sib_or_spouse, percent_pass_without_sib_or_spouse))
roc_auc_score(y_val, 0.6*val_preds_6+0.4*val_preds_5 )<compute_test_metric>
train.loc[train.Cabin == 'C23 C25 C27'][['Name', 'Ticket']].values
log_loss(y_val, 0.6*val_preds_6+0.4*val_preds_5 )<compute_test_metric>
train.loc[train.Ticket == '347082'][['Name', 'Ticket', 'Cabin', 'Fare', 'Pclass']].values
roc_auc_score(y_val, 0.55*val_preds_7+0.33*val_preds_6+0.12*val_preds_5 )<compute_test_metric>
train_children = train.loc[train.Age <= 12].copy()
num_children = train_children.shape[0]
num_children_survived = train_children[train_children.Survived == 1].shape[0]
percent_children_survived = 100 * num_children_survived / num_children
num_children_in_pc1 = train_children.loc[train_children.Pclass == 1].shape[0]
num_children_in_pc2 = train_children.loc[train_children.Pclass == 2].shape[0]
num_children_in_pc3 = train_children.loc[train_children.Pclass == 3].shape[0]
print('%d(%d%%)of the %d children(<=12 years)survived' %(num_children_survived, percent_children_survived, num_children))
print('%d, %d and %d children travelled in 1st, 2nd and 3rd class respectively' %(num_children_in_pc1,num_children_in_pc2, num_children_in_pc3))
train_children[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Pclass', ascending=True )
log_loss(y_val, 0.55*val_preds_7+0.33*val_preds_6+0.12*val_preds_5 )<save_to_csv>
train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
sample_submission['id'] = ids
sample_submission['label'] = 0.6*test_preds_6+0.4*test_preds_5
sample_submission.to_csv('submission_1.csv', index=False)
 sample_submission.head()<save_to_csv>
train.loc[pd.isnull(train.Embarked)]
sample_submission['id'] = ids
sample_submission['label'] = 0.55*test_preds_7+0.33*test_preds_6+0.12*test_preds_5
sample_submission.to_csv('submission_2.csv', index=False)
 sample_submission.head()<import_modules>
train.loc[train.Cabin == 'B28']
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
train.loc[train.Ticket == '113572']
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
test_passenger_ids = test['PassengerId']
for data in [train, test]:
data.drop(['Ticket', 'Cabin', 'PassengerId'], axis=1, inplace=True)
 for data in [train, test]:
imputer = SimpleImputer(strategy='median')
 data_trans = pd.DataFrame(imputer.fit_transform(data[['Fare']]), columns=['Fare'], index=data.index)
 data.update(data_trans)
 for data in [train, test]:
imputer = SimpleImputer(strategy='most_frequent')
 data_trans = pd.DataFrame(imputer.fit_transform(data[['Embarked']]), columns=['Embarked'], index=data.index)
 data.update(data_trans )
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
train_dummies = pd.get_dummies(train, columns=['Embarked'], prefix=['Embarked'])
 train = train_dummies
test_dummies = pd.get_dummies(test, columns=['Embarked'], prefix=['Embarked'])
 test = test_dummies
train['Sex'] = train['Sex'].apply(lambda x: 1 if x == 'male' else 0)
 test['Sex'] = test['Sex'].apply(lambda x: 1 if x == 'male' else 0)
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
for data in [train, test]:
data['Title'] = data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'] )
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
for data in [train, test]:
data['Title'].replace('Mlle', 'Miss', inplace=True)
 data['Title'].replace('Ms', 'Miss', inplace=True)
 data['Title'].replace('Mme', 'Mrs', inplace=True)
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).count()
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
for data in [train, test]:
data['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other', inplace=True)
 print(train[['Title', 'Survived']].groupby(['Title'], as_index=False ).count() )
train_EB5 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB5.npy')
 test_EB5 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB5.npy' )<split>
train_dummies = pd.get_dummies(train, columns=['Title'], prefix=['Title'])
 train = train_dummies
test_dummies = pd.get_dummies(test, columns=['Title'], prefix=['Title'])
 test = test_dummies
for data in [test, train]:
data.drop(['Name'], axis=1, inplace=True )
X_train, X_val, y_train, y_val = train_test_split(train_EB5, target, test_size=0.1, random_state=42 )<train_model>
for data in [test, train]:
iterative_regressor = IterativeImputer(random_state=0, max_iter=100, estimator=KNeighborsRegressor(n_neighbors=15))
iterative_regressor.fit(data[['Age', 'Sex', 'Pclass', 'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Other']])
 data_imp = iterative_regressor.transform(data[['Age', 'Sex', 'Pclass', 'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Other']])
 data_temp = pd.DataFrame(data_imp, columns=['Age', 'Sex', 'Pclass', 'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Other'], index=data.index)
 data['Age'] = data_temp['Age']
%%time
lr = LogisticRegression(C=0.012, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
train['FamilySize'] = 0
test['FamilySize'] = 0
for data in [train, test]:
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = data['FamilySize'].apply(lambda x: 1 if x == 1 else 0)
 data['PassengerFare'] = data.Fare/data.FamilySize
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
pd.DataFrame(abs(train.corr() ['Survived'] ).sort_values(ascending = False))
roc_auc_score(y_val, val_preds )<compute_test_metric>
for data in [train, test]:
data.drop(['PassengerFare','Embarked_Q'], axis=1, inplace=True )
log_loss(y_val, val_preds )<predict_on_test>
model = RandomForestClassifier(random_state=1, n_estimators=100)
 rfc_rfe = RFECV(model, step = 1, scoring = 'accuracy', cv = 10)
 rfc_rfe.fit(train.drop(['Survived'], axis=1), train['Survived'].copy())
 print('The optimal number of features :', rfc_rfe.n_features_)
 print('The optimal features are:',(train.drop(['Survived'], axis=1)).columns[rfc_rfe.support_] )
test_preds = lr.predict_proba(test_EB5)[:,1]<save_to_csv>
train_survived = train['Survived'].copy()
train.drop(['Survived'], axis=1, inplace=True)
 train = pd.DataFrame(train.values[:,rfc_rfe.support_], columns=train.columns[rfc_rfe.support_], index=train.index)
 train['Survived'] = train_survived
test = pd.DataFrame(test.values[:,rfc_rfe.support_], columns=test.columns[rfc_rfe.support_], index=test.index)
 train.head()
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
train_X = train.drop(['Survived'], axis=1)
 train_Y = train['Survived'].copy()
test_X = test
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
standard_scaler = StandardScaler()
train_X = standard_scaler.fit_transform(train_X)
 test_X = standard_scaler.transform(test_X )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
pp = pprint.PrettyPrinter(indent=4)
 models = {}
cv_split = ShuffleSplit(n_splits = 10, test_size =.3, train_size =.6, random_state = 0)
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
model_name = 'lrc_model'
models[model_name] = {}
model = LogisticRegressionCV(random_state=1, cv=10, max_iter=1000)
 model.fit(train_X, train_Y)
 models[model_name]['model'] = model
models[model_name]['crossval_score'] = model.score(train_X, train_Y, sample_weight=None)
 pp.pprint(models[model_name] )
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
model_name = 'dtc_model'
models[model_name] = {}
model = DecisionTreeClassifier(random_state=1)
 model.fit(train_X, train_Y)
 models[model_name]['model'] = model
models[model_name]['score'] = model.score(train_X, train_Y, sample_weight=None)
 models[model_name]['cross_val_score'] = cross_val_score(model, train_X, \
train_Y, scoring="accuracy", cv=10 ).mean()
pp.pprint(models[model_name] )
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
model_name = 'rfc_model'
models[model_name] = {}
model = RandomForestClassifier(random_state=1)
 model.fit(train_X, train_Y)
 models[model_name]['model'] = model
models[model_name]['score'] = model.score(train_X, train_Y, sample_weight=None)
 models[model_name]['cross_val_score'] = cross_val_score(model, train_X, \
train_Y, scoring="accuracy", cv=10 ).mean()
pp.pprint(models[model_name] )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
model_name = 'lrc_model'
models[model_name]['param_grid'] = [
{'max_iter':[1000]}]
grid_search = GridSearchCV(models[model_name]['model'], models[model_name]['param_grid'], cv=10,
scoring='accuracy', return_train_score=True)
 grid_search.fit(train_X, train_Y)
 models[model_name]['best_params'] = grid_search.best_params_
models[model_name]['best_estimator'] = grid_search.best_estimator_
models[model_name]['best_score'] = grid_search.best_score_
pp.pprint(models )
train_EB1 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB1.npy')
 test_EB1 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB1.npy' )<split>
model_name = 'dtc_model'
models[model_name]['param_grid'] = [
{'max_features': ['auto'], 'max_depth': [7,8,9], 'criterion': ['gini', 'entropy']}
]
grid_search = GridSearchCV(models[model_name]['model'], models[model_name]['param_grid'], cv=cv_split,
scoring='accuracy', return_train_score=True)
 grid_search.fit(train_X, train_Y)
 models[model_name]['best_params'] = grid_search.best_params_
models[model_name]['best_estimator'] = grid_search.best_estimator_
models[model_name]['best_score'] = grid_search.best_score_
pp.pprint(models[model_name] )
X_train, X_val, y_train, y_val = train_test_split(train_EB1, target, test_size=0.1, random_state=42 )<train_model>
model_name = 'rfc_model'
models[model_name]['param_grid'] = [
{'n_estimators': [100], 'oob_score': [True], 'max_features': ['auto'], 'max_depth': [7,8,9], 'criterion': ['gini', 'entropy']}
]
grid_search = GridSearchCV(models[model_name]['model'], models[model_name]['param_grid'], cv=cv_split,
scoring='roc_auc', return_train_score=True)
 grid_search.fit(train_X, train_Y)
 models[model_name]['best_params'] = grid_search.best_params_
models[model_name]['best_estimator'] = grid_search.best_estimator_
models[model_name]['best_score'] = grid_search.best_score_
pp.pprint(models[model_name] )
%%time
lr = LogisticRegression(C=0.005, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
today = datetime.datetime.now()
file_name = today.strftime("%Y%m%d%H%M%S")
 best_score = 0
for key, value in models.items() :
if(value['best_score'] > best_score):
best_score = value['best_score']
best_model = key
best_estimator = value['best_estimator']
print("The best model is",best_model, "with a score of", best_score)
 predictions = best_estimator.predict(test_X)
 output_predictions = pd.DataFrame({'PassengerId': test_passenger_ids, 'Survived': predictions})
 output_predictions.to_csv(file_name+"prediction.csv", index=False)
 f = open(file_name+"models.txt","w")
 pp = pprint.PrettyPrinter(indent=4, stream=f)
 pp.pprint(models)
 f.close()