# ---
# 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/ViFLara/Statistics-and-Machine-Learning/blob/master/random_forest.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="pP-1cIjfaPO3" colab={"base_uri": "https://localhost:8080/", "height": 379} outputId="f4f92220-9577-450c-925a-7cfd2d4220a3"
import pandas as pd

data = pd.read_csv("drive/MyDrive/creditcard.csv")
data.head(10) # amount -> transaction value

# + colab={"base_uri": "https://localhost:8080/"} id="lv6iAiw5r5-8" outputId="100f8936-4e8c-4799-f0d8-dfe998aea9b1"
print(data.isna().sum())

# + colab={"base_uri": "https://localhost:8080/"} id="0uNKu9VDutUE" outputId="fe233b10-9222-4db1-8ce2-03f2857d727d"
n_transactions = data['Class'].count()
n_frauds = data['Class'].sum() # sum of all rows
n_no_frauds = n_transactions - n_frauds
frauds_percentage = n_frauds / n_transactions
no_frauds_percentage = n_no_frauds / n_transactions
print("Number of transactions: ", n_transactions)
print("Number of frauds: ", n_frauds, "%.2f" %(frauds_percentage * 100))
print("Number of transactions without frouds: ", n_no_frauds, "%.2f" %(no_frauds_percentage * 100))

# + id="fe1ttmivuteI"
from sklearn.model_selection import StratifiedShuffleSplit

def run_validator(x, y):
  
  validator = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=0)
  for train_id, test_id in validator.split(x, y) :
      x_train, x_test = x[train_id], x[test_id]
      y_train, y_test = y[train_id], y[test_id]
  return x_train, x_test, y_train, y_test


# + colab={"base_uri": "https://localhost:8080/"} id="YffyvETYJTSX" outputId="173013ac-df9f-425c-e737-1173b2d0872c"
# %%time
from sklearn import tree

def run_classifier(classifier, x_train, x_test, y_train):
  
  tree = classifier.fit(x_train, y_train)
  y_pred = tree.predict(x_test) # predict whether transactions are fraud or not
  return y_pred


# + id="5m7LlLq3QQ1F"
import matplotlib.pyplot as plt

def save_tree(classifier, name):
  
  plt.figure(figsize=(200,100))
  tree.plot_tree(classifier, filled=True, fontsize=14)
  plt.savefig(name)
  plt.close()


# + id="FyIXR2-6Y2Jk"
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

def validate_tree(y_test, y_pred):

  print(accuracy_score(y_test, y_pred))
  print(confusion_matrix(y_test, y_pred))
  print(precision_score(y_test, y_pred)) # fraud transactions
  print(recall_score(y_test, y_pred)) # number of fraud hits / number of transactions classified as frauds


# + id="SUsqth9rF8xH"
# validator execution
x = data.drop('Class', axis=1).values
y = data['Class'].values

x_train, x_test, y_train, y_test = run_validator(x, y)

# + id="Oy4_GhvdSKrd"
# classifier execution of decision_tree_classifier
decision_tree_classifier = tree.DecisionTreeClassifier()
y_predict_decision_tree = run_classifier(decision_tree_classifier, x_train, x_test, y_train)

# + id="BSrOnxa8S9fj"
# creation of the decision tree figure
save_tree(decision_tree_classifier, 'tree_decision1.png')

# + colab={"base_uri": "https://localhost:8080/"} id="KvURjOM3acoc" outputId="48eaa448-237f-431e-a6a5-f20f9864ed0d"
# decision tree validation
validate_tree(y_test, y_predict_decision_tree)

# + colab={"base_uri": "https://localhost:8080/"} id="0HNnFUVg1EUx" outputId="ab4ec168-f354-4295-e7da-cfda7a5bb7e6"
print(decision_tree_classifier)
print(decision_tree_classifier.get_depth())

# + id="ShnMsNed1vol"
# classifier execution of decision_tree_classifier
decision_tree_classifier = tree.DecisionTreeClassifier(max_depth=10, random_state=0)
y_predict_decision_tree = run_classifier(decision_tree_classifier, x_train, x_test, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="s1QUFwk32Ihu" outputId="f617c090-7346-40be-929b-f941c7bb893b"
# decision tree validation
validate_tree(y_test, y_predict_decision_tree)

# + id="ZT2cisBC2hTl"
# classifier execution of decision_tree_classifier
decision_tree_classifier = tree.DecisionTreeClassifier(max_depth=10, random_state=0, min_samples_leaf=10)
y_predict_decision_tree = run_classifier(decision_tree_classifier, x_train, x_test, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="GjG--mD02vhG" outputId="5bdfdef9-2a78-4a91-b399-e12277fc0f7f"
# decision tree validation
validate_tree(y_test, y_predict_decision_tree)

# + colab={"base_uri": "https://localhost:8080/"} id="FcJsUqV63qcb" outputId="4f19421e-83b2-4f7b-a0a7-6b1b49db0b2c"
# %%time
# classifier execution of decision_tree_classifier
decision_tree_classifier = tree.DecisionTreeClassifier(max_depth=5, random_state=0)
y_predict_decision_tree = run_classifier(decision_tree_classifier, x_train, x_test, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="sw5QgWjT31mj" outputId="a28d721f-6aee-418b-aeab-48144e9e775e"
# decision tree validation
validate_tree(y_test, y_predict_decision_tree)

# + colab={"base_uri": "https://localhost:8080/"} id="_y8nQQg0h0YU" outputId="d6aa297a-fd1e-4a46-c3ff-21b34107c11d"
# %%time
from sklearn.ensemble import RandomForestClassifier

random_forest_classifier = RandomForestClassifier(n_estimators=50, random_state=0, max_depth=10) # n_estimators -> number of trees
y_predict_random_forest = run_classifier(random_forest_classifier, x_train, x_test, y_train)

# + id="bKC6SKkHoGkE"
save_tree(random_forest_classifier.estimators_[0], "random_forest1")
save_tree(random_forest_classifier.estimators_[1], "random_forest2")

# + colab={"base_uri": "https://localhost:8080/"} id="DcFFIfLSrsBS" outputId="7eb98bb1-6a64-42bf-e0f0-ed9099c26dc3"
validate_tree(y_test, y_predict_random_forest)

# + colab={"base_uri": "https://localhost:8080/"} id="BWgXR1Za0OhB" outputId="4f217249-8e67-46f1-a61f-58e54ccd0832"
# %%time
from sklearn.ensemble import AdaBoostClassifier
adaboost_classifier = AdaBoostClassifier(random_state=0)
y_predict_adaboost = run_classifier(adaboost_classifier, x_train, x_test, y_train)

# + id="VPfun-lL15zc"
save_tree(adaboost_classifier.estimators_[0], "adaboost1")
save_tree(adaboost_classifier.estimators_[1], "adaboost2")

# + colab={"base_uri": "https://localhost:8080/"} id="TEupWpjb2d_9" outputId="7bb2a228-cd6c-4bbb-b1f6-e8e8f1848abc"
validate_tree(y_test, y_predict_adaboost)

# + colab={"base_uri": "https://localhost:8080/"} id="DK-LgUNU340G" outputId="4d0b52d5-73b7-460a-dd4e-9883ec347c63"
# %%time
from sklearn.ensemble import AdaBoostClassifier
adaboost_classifier = AdaBoostClassifier(random_state=0, n_estimators=100)
y_predict_adaboost = run_classifier(adaboost_classifier, x_train, x_test, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="phBkk-oB4RuW" outputId="d50f3045-3f6d-4193-d1ad-0648ebfb59d6"
validate_tree(y_test, y_predict_adaboost)

# + colab={"base_uri": "https://localhost:8080/"} id="5xQ-D34y5Kx_" outputId="2e95dee0-ad78-4611-fbaa-01adc23f1bac"
# %%time
from sklearn.ensemble import AdaBoostClassifier
adaboost_classifier = AdaBoostClassifier(random_state=0, n_estimators=200)
y_predict_adaboost = run_classifier(adaboost_classifier, x_train, x_test, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="Asy8HCcD5K6_" outputId="f9fcdb0f-0b71-4616-debe-db71be25ae02"
validate_tree(y_test, y_predict_adaboost)

StarcoderdataJupyterScriptsDedupFiltered