# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/chavamoon/MachineLearningExamples/blob/main/Python/Classification/RandomForest_Classifier.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="5mfPt49RObNc"
import pandas as pd
import numpy as np
import sklearn.model_selection as skm
import matplotlib.pyplot as plt

# + id="yIqilSJ9Pc4A"
np.random.seed(200804)

# + [markdown] id="d5RirZknPpRF"
# #EDA

# + id="RZeZ8Jt7Pogj"
titanic = pd.read_csv('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv')

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="ewSqxMsNPzPi" outputId="28a16177-d568-4c73-ebc5-f176796a0222"
titanic.head()

# + id="VN5xQf4zP49-"
# colum renaming and dropna
titanic.rename(columns={ col: col.lower() for col in titanic.columns}, inplace = True )
titanic.dropna(inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="wJc7Z2aNRLk3" outputId="ce1b34f2-66ed-43cb-f87a-93ed993a6e89"
titanic.dtypes

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="ZNfhn7LgRThi" outputId="52713caa-9557-4d44-d501-728f03b0c3fc"
#masks for sex columns
titanic.sex.mask(titanic.sex == 'female', '1', inplace=True)
titanic.sex.mask(titanic.sex == 'male', '0', inplace=True)
titanic.head()

# + id="mVBsXUbjRkXH"
#independent and dependent variables 
X = titanic[['pclass', 'sex', 'age', 'sibsp', 'parch', 'fare']]
y = titanic[['survived']]

# + [markdown] id="wDkM-DFyRrXL"
# #Data split

# + colab={"base_uri": "https://localhost:8080/"} id="4N9Z8PpERu5D" outputId="ae538605-dd5d-4629-9c0a-8eef5207fe29"
X_train, X_test, y_train, y_test = skm.train_test_split(X, y, random_state=111, test_size=0.3)
print('training data shape: ', X_train.shape, y_train.shape)
print('testing data shape', X_test.shape, y_test.shape)

# + [markdown] id="wfW4EZ9zVVR1"
# #Training

# + id="EJg5CbueVXIn"
from sklearn.ensemble import RandomForestClassifier

# + id="pHuc4pOMVd4e"
# oob_score indica si usar muestras sout-of-bag para estimar score de generalización
rf = RandomForestClassifier(n_jobs=-1, oob_score=True)
#n_estimators -> How many trees will generate the forest

grid = {'n_estimators': [600,800,1000], 'criterion': ['gini', 'entropy'], 'min_samples_leaf':[5,7,9,11]}

#Althougth RandomForest does not need cv, GreadSearchCV object needs 3 k-folds at least.
gs = skm.GridSearchCV(rf, param_grid=grid, scoring='precision', cv=3, n_jobs=-1, return_train_score=True)

# + colab={"base_uri": "https://localhost:8080/"} id="4Q8zDspGYg9_" outputId="7b08f61f-b6f0-4cd6-b19d-3ae7a7a0bd9c"
#Training 24 models
gs.fit(X_train, y_train.values.ravel())

# + id="7miLRVjHYm93"
best_model = gs.best_estimator_

# + colab={"base_uri": "https://localhost:8080/"} id="hiB1i6EjZ2aU" outputId="b04b6540-72cd-47ca-c271-293f15fbc3b3"
type(best_model)

# + colab={"base_uri": "https://localhost:8080/"} id="CpS1OFOAaSpD" outputId="2a0c13fb-c853-41bb-a6c9-07f06e6c73e6"
best_model.get_params()

# + colab={"base_uri": "https://localhost:8080/"} id="ez-XQB-tajLk" outputId="4dbed861-7b3b-488a-e069-d7fcebd3c171"
best_model.oob_score_
# Out of bag score associated with accuracy

# + colab={"base_uri": "https://localhost:8080/"} id="RTNoVL76apth" outputId="47c07964-ae71-4cd8-ae2a-5aea118dd963"
gs.best_score_

# + [markdown] id="efNnBhPDbz4v"
# **Feature importance**

# + colab={"base_uri": "https://localhost:8080/"} id="rH8fBUtCb2zk" outputId="afe44ff5-95d1-4e24-cda9-09cb1f539481"
best_model.feature_importances_

# + colab={"base_uri": "https://localhost:8080/"} id="gROMB-B6b8l_" outputId="57001008-7a2b-4444-a711-6636804d2064"
X.columns.values

# + [markdown] id="0WGsgHmScJob"
# **Predictions**

# + colab={"base_uri": "https://localhost:8080/"} id="Hvlbq58EcMIv" outputId="09daa846-59b0-4fc1-e7d1-eb850e92d8de"
predicted_labels = best_model.predict(X_test)
predicted_labels[:10]

# + colab={"base_uri": "https://localhost:8080/"} id="W6cyzciJdQLT" outputId="6eac180b-eb17-49e7-bed2-712acee2fb16"
predicted_scores = best_model.predict_proba(X_test)
predicted_scores[:10,]

# + [markdown] id="8PpfVo4-dc2M"
# **Metrics**

# + colab={"base_uri": "https://localhost:8080/"} id="6T98YGf9dfsl" outputId="a5ec348e-8cd8-402e-c60a-95d05cd7ef54"
#Accuracy
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predicted_labels)
accuracy

# + id="aDAfDaCrd4u-"
#ROC y AUC
from sklearn.metrics import roc_auc_score, roc_curve

# + colab={"base_uri": "https://localhost:8080/", "height": 279} id="Y5DiwM99eFOa" outputId="daedd51e-82d9-4ec2-bc05-e56d468c8bde"
fpr, tpr, threshold = roc_curve(y_test, predicted_scores[:,1], pos_label=True)

plt.clf()
plt.plot([0,1], [0,1], 'k--', c='red')
plt.plot(fpr,tpr)
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="8ZzUak4TfCAF" outputId="4e3185d6-78f2-4c91-9ec7-ebb8b1bcf2ea"
#confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, predicted_labels)

# + colab={"base_uri": "https://localhost:8080/"} id="XW3U6m6GfQST" outputId="824adcf1-a62a-423a-a47f-b7a7908fbfe2"
#precision, recall and f1score
from sklearn.metrics import recall_score, precision_score, f1_score

print('precision', precision_score(y_test, predicted_labels))
print('recall', recall_score(y_test, predicted_labels) )
print('f1_score', f1_score(y_test, predicted_labels) )




StarcoderdataJupyterScriptsDedupFiltered