<jupyter_start><jupyter_text>Titanic  0 - Collect The Titanic dataset can be download in the [kaggle website](https://www.kaggle.com/c/titanic/data)  1 - Prepare<jupyter_code>import pandas as pd

train = pd.read_csv('./titanic_train.csv')
test = pd.read_csv('./titanic_test.csv')<jupyter_output><empty_output><jupyter_text>2 - Visualize<jupyter_code>import pandas_profiling

train.profile_report().to_file('./titanic_report.html')<jupyter_output><empty_output><jupyter_text>3 - Selection<jupyter_code>train = train.dropna(subset=['Age'])
test = test.dropna(subset=['Age'])

train = pd.get_dummies(train, columns=['Sex'])
test = pd.get_dummies(test, columns=['Sex'])

feature_cols = ['Pclass', 'Parch', 'Age', 'Sex_female', 'Sex_male']

X = train[feature_cols]
y = train.Survived<jupyter_output><empty_output><jupyter_text>4 - Training<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression(solver='liblinear')
logreg.fit(X_train,y_train)
logreg_y_pred_target = logreg.predict(X_test)
from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train,y_train)
rfc_y_pred_target = rfc.predict(X_test)<jupyter_output><empty_output><jupyter_text>5 - Evaluation<jupyter_code>from sklearn import metrics

def evaluate(y_test, y_pred_target):

    return dict(accuracy='{:.2f}'.format(metrics.accuracy_score(y_test, y_pred_target)),
                precision='{:.2f}'.format(metrics.precision_score(y_test, y_pred_target)),
                recall='{:.2f}'.format(metrics.recall_score(y_test, y_pred_target)))

print('logreg', evaluate(y_test, logreg_y_pred_target))
print('rfc', evaluate(y_test, rfc_y_pred_target))<jupyter_output>logreg {'accuracy': '0.82', 'precision': '0.80', 'recall': '0.78'}
rfc {'accuracy': '0.77', 'precision': '0.75', 'recall': '0.70'}<jupyter_text>6 - Prediction<jupyter_code>sample = X_test.sample(1)
print(sample)
print('logreg = %s' % logreg.predict(sample)[0])
print('rfc = %s' % rfc.predict(sample)[0])<jupyter_output>Pclass  Parch   Age  Sex_female  Sex_male
706       2      0  45.0           1         0
logreg = 1
rfc = 0<jupyter_text>7 - Export<jupyter_code>import joblib

features = list(X.columns)
model = logreg

joblib.dump(features, '../fastapi/features.pkl')
joblib.dump(model,'../fastapi/logreg_model.pkl', compress=9)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14908073541167066, 'fraction_numerical': 0.01958433253397282, 'mean_word_length': 6.60790273556231, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '652584', 'license': 'MIT', 'n_tokens_mistral': 1004, 'n_tokens_neox': 963, 'n_words': 140, 'path': 'notebook/titanic_notebook.ipynb', 'repo_name': 'rodrigodelmonte/ml-titanic', 'size': '2502'}