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

# + [markdown] id="mXU4vYj7eIE2" colab_type="text"
#
#
# **Credit card Fraud versus a set of Models**

# + [markdown] id="ii325n0OeIE3" colab_type="text"
# _This notebook contains an example of running several models against a credit card fraud dataset pulled from Kaggle._
#

# + [markdown] id="ogFOxktmeIE4" colab_type="text"
# # Setup

# + [markdown] id="Cal-fSpOeIE4" colab_type="text"
# First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures. We also check that Python 3.5 or later is installed (although Python 2.x may work, it is deprecated so we strongly recommend you use Python 3 instead), as well as Scikit-Learn ≥0.20.

# + id="wQPExwX7eIE5" colab_type="code" colab={}
# Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)

# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
np.random.seed(42)

# To plot pretty figures
# %matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

import warnings
warnings.filterwarnings(action="ignore", message="^internal gelsd")

# + [markdown] id="QsLxUJZBrt8_" colab_type="text"
# # Load Data

# + [markdown] id="QVpFYPD2X10O" colab_type="text"
# I loaded the card fraud data from an S3 bucket on AWS for you.

# + id="gFWAsLuQsR3N" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="4638d866-34fa-4df2-e4b0-6b3a776614fd"
import pandas as pd
cfraud=pd.read_csv("https://s3.amazonaws.com/www.ruxton.ai/creditcardfraud.zip")
cfraud.head()

# + [markdown] colab_type="text" id="_Ju81LsLhMG8"
# # Some Minimal EDA

# + id="T95OhBmPhJnn" colab_type="code" colab={}


# + id="6He7X8dBZQU2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 624} outputId="e1466eac-0604-4705-850c-fc28eb7808eb"
from string import ascii_letters

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="white")

# Generate a large random dataset
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                 columns=list(ascii_letters[26:]))

# Compute the correlation matrix
corr = cfraud.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})

# + id="sz1DWkSIWti0" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6c73429b-1424-44a1-f2b9-59e20146b368"
use=list(cfraud.columns.values[[1,2,3,4,5,6,7,9,10,11,12,14,16,17,18,19,28]])  # use all the predictor data for example
print(use)
 

# + [markdown] id="Dursgx1LXZ0W" colab_type="text"
# EDA: Before fitting models, you should do EDA. Do you want to add any features as combos of others?

# + [markdown] id="W1gSBePLXZP5" colab_type="text"
# Transform data here

# + [markdown] id="tb5pxZxsWoqk" colab_type="text"
# That looks awful. Let's try and dientify predictors that are intrinsic to banks balance sheet.

# + [markdown] id="13UBYWXPXBHR" colab_type="text"
# That looks better. Now try some other methods like random forest SVM, xgboost, decisio trees. Try tuning them. Which do you choose?

# + id="Gi_Cs3ihhYbF" colab_type="code" outputId="6c511665-308a-4a93-84aa-60b73a30f502" colab={"base_uri": "https://localhost:8080/", "height": 1000}
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from xgboost import XGBClassifier
from sklearn import metrics

h = .02  # step size in the mesh

names = [ "Linear SVM", "Logistic",
         "Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
         "Naive Bayes", "QDA","XGBoost"]

classifiers = [
    SVC(kernel="linear", C=0.025),
    LogisticRegression(),
    DecisionTreeClassifier(max_depth=5),
    RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
    MLPClassifier(alpha=1, max_iter=1000),
    AdaBoostClassifier(),
    GaussianNB(),
    QuadraticDiscriminantAnalysis(),
    XGBClassifier()]

X, y = make_classification(n_features=5, n_redundant=0, n_informative=2,
                           random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)

i=1.  # figure counter

# preprocess dataset, split into training and test part
X, y = cfraud[use],cfraud["Class"]
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = \
    train_test_split(X, y, test_size=.3, random_state=42)



# iterate over classifiers
for name, clf in zip(names, classifiers):
    figure = plt.figure(num=i,figsize=(108, 6))
    ax = plt.subplot(1, len(classifiers) + 1, i)
    clf.fit(X_train, y_train)

    fpr, tpr, _ = metrics.roc_curve(y_test, clf.predict(X_test))
    roc_auc = metrics.auc(fpr, tpr)
   # Plot of a ROC curve for a specific class
   # plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
            lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC for '+ name )
    plt.legend(loc="lower right")
    plt.show()

StarcoderdataJupyterScriptsDedupFiltered