<jupyter_start><jupyter_text>Load the data<jupyter_code>processed_data_path = os.path.join(os.path.pardir, 'data', 'processed')
train_path = os.path.join(processed_data_path, 'train.csv')
test_path = os.path.join(processed_data_path, 'test.csv')
train_data = pd.read_csv(train_path, index_col = 'PassengerId')
test_data = pd.read_csv(test_path, index_col = 'PassengerId')
train_data.info()
test_data.info()
X = train_data.loc[:,'Age':].values.astype('float')
y = train_data['Survived'].ravel()
print(X.shape, y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
print('main survival in train: {0:.3f}'.format(np.mean(y_train)))
print('main survival in test: {0:.3f}'.format(np.mean(y_test)))<jupyter_output>main survival in train: 0.383
main survival in test: 0.385<jupyter_text>Check Scklear pacakege<jupyter_code>import sklearn
print(sklearn.__version__)<jupyter_output>0.20.3<jupyter_text>Basaline model<jupyter_code>from sklearn.dummy import DummyClassifier
# create model
model_dummy = DummyClassifier(strategy='most_frequent',random_state=0)
model_dummy.fit(X_train, y_train)
print('Score baseline model: {0:.2f}'.format(model_dummy.score(X_test, y_test)))
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
# acuracy
print('Accuracy for baseline model: {0:.2f}'.format(accuracy_score(y_test, model_dummy.predict(X_test))))
# confusion matrix
print('Confusion matrix for baseline model: \n {0}'.format(confusion_matrix(y_test, model_dummy.predict(X_test))))
# precission and recall
print('Precission for for baseline model: {0:.2f}'.format(precision_score(y_test, model_dummy.predict(X_test))))
print('Precission for for baseline model: {0:.2f}'.format(recall_score(y_test, model_dummy.predict(X_test))))<jupyter_output>Precission for for baseline model: 0.00
Precission for for baseline model: 0.00<jupyter_text>First Kaggle submission<jupyter_code>test_X=test_data.values.astype('float')
# Predict
predictions= model_dummy.predict(test_X)
df_submission = pd.DataFrame({'PassengerId': test_data.index, 'Survived': predictions})
df_submission.head()
submission_data_path = os.path.join(os.path.pardir, 'data', 'external')
submission_file_path = os.path.join(submission_data_path, '01_dummy.csv')
df_submission.to_csv(submission_file_path, index=False)
def get_submission_file(model, filename):
    test_X=test_data.values.astype('float')
    predictions= model.predict(test_X)
    df_submission = pd.DataFrame({'PassengerId': test_data.index, 'Survived': predictions})
    submission_data_path = os.path.join(os.path.pardir, 'data', 'external')
    submission_file_path = os.path.join(submission_data_path, filename)
    df_submission.to_csv(submission_file_path, index=False)
get_submission_file(model_dummy, '01_dummy.csv')<jupyter_output><empty_output><jupyter_text>Linear Regression Model<jupyter_code>from sklearn.linear_model import LogisticRegression
model_lr_1 = LogisticRegression(random_state=0)
model_lr_1.fit(X_train, y_train)
print('Score baseline model: {0:.2f}'.format(model_lr_1.score(X_test, y_test)))
# acuracy
print('Accuracy for baseline model: {0:.2f}'.format(accuracy_score(y_test, model_lr_1.predict(X_test))))
# confusion matrix
print('Confusion matrix for baseline model: \n {0}'.format(confusion_matrix(y_test, model_lr_1.predict(X_test))))
# precission and recall
print('Precission for for baseline model: {0:.2f}'.format(precision_score(y_test, model_lr_1.predict(X_test))))
print('Precission for for baseline model: {0:.2f}'.format(recall_score(y_test, model_lr_1.predict(X_test))))
get_submission_file(model_lr_1, '02_linear.csv')<jupyter_output><empty_output><jupyter_text>Hyperparameter Optimization<jupyter_code># base model
model_lr = LogisticRegression(random_state=0)
from sklearn.model_selection import GridSearchCV
parameters = {'C':[1.0,10.0,50.0,100.0,1000.0], 'penalty': ['l1', 'l2']}
clf = GridSearchCV(model_lr,param_grid=parameters, cv=3)
clf.fit(X_train, y_train)
clf.best_params_<jupyter_output><empty_output><jupyter_text>Making Third Submission<jupyter_code>get_submission_file(clf, '03_linear.csv')<jupyter_output><empty_output><jupyter_text>Model persistance<jupyter_code>import pickle
# file paths
model_file_path = os.path.join(os.path.pardir, 'models', 'lr_model.pkl')
model_file_pickle = open(model_file_path,'wb')
pickle.dump(clf, model_file_pickle)
model_file_pickle.close()<jupyter_output><empty_output><jupyter_text>loading saved model<jupyter_code>model_file_pickle = open(model_file_path,'rb' )
clf_loaded = pickle.load(model_file_pickle)
clf_loaded<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13443948096149755, 'fraction_numerical': 0.01744309721335886, 'mean_word_length': 9.662131519274377, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '652013', 'license': 'MIT', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1734, 'n_words': 264, 'path': 'notebooks/predictive_model.ipynb', 'repo_name': 'oscarv17/titanic-disaster-project', 'size': '4701'}