text_html_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import confusion_matrix

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('../input/creditcard.csv')

data['normalized_amt'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
data = data.drop(['Amount'], axis=1)

data = data.drop(['Time'], axis=1)

x = data.iloc[:, data.columns != 'Class']
y = data.iloc[:, data.columns == 'Class']

y_train.values

y_train.values.ravel()

random_forest = RandomForestClassifier(n_estimators=100)
random_forest.fit(x_train, y_train.values.ravel())

y_pred = random_forest.predict(x_test)
random_forest.score(x_test, y_test)

def plot_confusion_matrix(cm, classes, normalize=False, title='None', cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.0
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            plt.text(j, i, format(cm[i, j], fmt), ha='center', va='center', color='white' if cm[i, j] > thresh else 'black')
    plt.tight_layout()

cnf_matrix = confusion_matrix(y_test, y_pred)

y_pred1 = random_forest.predict(x)
y_pred1

y_pred1.round()

cnf_matrix = confusion_matrix(y, y_pred1)
plot_confusion_matrix(cnf_matrix, classes=[0, 1])