<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
import pandas as pd


drive.mount('/content/gdrive')

!unzip -q gdrive/'My Drive'/data/creditcardfraud.zip
cf_data = pd.read_csv('creditcard.csv')




import matplotlib.pyplot as plt

print(cf_data)

index = pd.Index(cf_data.Class)
index.value_counts()

positive_idx = []
negative_idx = []
for t in range(index.size):
  if index.array[t] == 0:
    positive_idx.append(t)
  else:
    negative_idx.append(t)


print("negative cases", negative_idx)




import seaborn as sb
import numpy as np


pearsoncorr = cf_data.corr(method='pearson')
#print(pearsoncorr(V1))

X = np.array([pearsoncorr.V1, pearsoncorr.V2, pearsoncorr.V3,pearsoncorr.V4,pearsoncorr.V5])
X = X.T


sb.heatmap(X, 
            xticklabels=pearsoncorr.columns,
            yticklabels=pearsoncorr.columns,
            cmap='RdBu_r',
            annot=True,
            linewidth=2.0)
data_ = cf_data.iloc[:,1:29]
data = pearsoncorr = data_.corr(method='pearson')
print("Pearson-Correlation ")

#data = pearsoncorr.iloc[2:,2:]
print(data.iloc[:, 1])

print("---------------------------------")

max_idx = np.argpartition(data.iloc[:, 1], -4)[-4:]



#print(data.iloc[:, 1])


#print("---------------------------------")
def largest_indices(ary, n):
    """Returns the n largest indices from a numpy array."""
    flat = ary #.flatten()
    indices = np.argpartition(flat, -n)[-n:]
    indices = indices[np.argsort(-flat[indices])]
    return  np.unravel_index(indices, ary.shape)


def find_maxmin_corr(datap, idx):

    #val = largest_indices(datap.iloc[:, idx],4)
    #print(val)
    #print(val, datap.iloc[idx,val.flatten()])

    max_idx = np.argpartition(abs(datap.iloc[:, idx]), -4)[-4:]
    min_idx = np.argpartition(abs(datap.iloc[:, idx]), 4)[:4]
    #print("Max - Idx  ------::: ----MIN IDX ")

    #print( datap.iloc[idx,max_idx[0:4]], datap.iloc[idx,min_idx[0:5]] )
    return [max_idx, min_idx], [datap.iloc[idx,max_idx[0:4]], datap.iloc[idx,min_idx[0:5]]]
    #print(datap.iloc[:, 1])

rmax_history = []
rmin_history = []
#[max_index, min_index], [max_val, min_val] = find_maxmin_corr(data, 1)
#rmax_history.append(max_val)
#rmin_history.append(min_val)

for idx in range(28):
  [max_index, min_index], [max_val, min_val] = find_maxmin_corr(data, idx)
  rmax_history.append(max_val)
  rmin_history.append(min_val)

print("history max:", rmax_history)
print("history min:", rmin_history)

plt.plot(list(rmax_history))
plt.show()
#print(data.iloc[1,max_idx[1,:]])
# get the tick label font size
import seaborn as sns

fontsize_pt = plt.rcParams['ytick.labelsize']
dpi = 72.27

data = pearsoncorr
# comput the matrix height in points and inches
matrix_height_pt = float(30.0) * data.shape[0]
matrix_height_in = float(matrix_height_pt) / dpi

# compute the required figure height 
top_margin = 0.04  # in percentage of the figure height
bottom_margin = 0.04 # in percentage of the figure height
figure_height = matrix_height_in / (1 - top_margin - bottom_margin)


# build the figure instance with the desired height
fig, ax = plt.subplots(
        figsize=(20,figure_height), 
        gridspec_kw=dict(top=1-top_margin, bottom=bottom_margin))

# let seaborn do it's thing
ax = sns.heatmap(data, ax=ax)



X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5])
X = X.T

Y = cf_data.Class

print(X.shape)

import sklearn
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

Xtrain, Xtest, ytrain, ytest = train_test_split(X, Y,
                                                random_state=0)
model = RandomForestClassifier(n_estimators=100)
model.fit(Xtrain, ytrain)
#ypred = model.predict(Xtest)




from sklearn.metrics import confusion_matrix
import seaborn as sns; sns.set()
from sklearn import metrics

ypred = model.predict(Xtest)

print(metrics.classification_report(ypred, ytest))
mat = confusion_matrix(ytest, ypred)
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False)
plt.xlabel('true label')
plt.ylabel('predicted label');



print(X.shape)

import sklearn
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
import seaborn as sns; sns.set()
from sklearn import metrics

def random_forest_test(X,Y, estimators):
  Xtrain, Xtest, ytrain, ytest = train_test_split(X, Y,
                                                  random_state=0)
  model = RandomForestClassifier(n_estimators=estimators)
  model.fit(Xtrain, ytrain)

  ypred = model.predict(Xtest)

  report = print(metrics.classification_report(ypred, ytest))
  mat = confusion_matrix(ytest, ypred)
  sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False)
  plt.xlabel('true label')
  plt.ylabel('predicted label');
  return report, mat


X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5])
X = X.T

Y = cf_data.Class
#random_forest_test(X,Y, 100)



X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7])
X = X.T
random_forest_test(X,Y, 100)


X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7])
X = X.T
random_forest_test(X,Y, 300)


X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7])
X = X.T
random_forest_test(X,Y, 50)


X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7])
X = X.T
random_forest_test(X,Y, 30)



X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7, cf_data.V8, cf_data.V9, cf_data.V10])
X = X.T
random_forest_test(X,Y, 30)



X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7, cf_data.V8, cf_data.V9, cf_data.V10, cf_data.V11, cf_data.V12, cf_data.V13,cf_data.V14,cf_data.V15, cf_data.V16, cf_data.V17, cf_data.V18, cf_data.V19, cf_data.V20])


X = X.T
print(X.shape)
random_forest_test(X,Y, 30)


history_mat = []
history_report = []

X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7, cf_data.V8, cf_data.V9, cf_data.V10, cf_data.V11, cf_data.V12, cf_data.V13,cf_data.V14,cf_data.V15, cf_data.V16, cf_data.V17, cf_data.V18, cf_data.V19, cf_data.V20])


X = X.T
print(X.shape)
report, mat = random_forest_test(X,Y, 30)
history_mat.append(mat)
history_report.append(report)


history_mat = []
history_report = []

X = np.array([cf_data.V1, cf_data.V2, cf_data.V3,cf_data.V4,cf_data.V5, cf_data.V6, cf_data.V7, cf_data.V8, cf_data.V9, cf_data.V10,
              cf_data.V11, cf_data.V12, cf_data.V13,cf_data.V14,cf_data.V15, cf_data.V16, cf_data.V17, cf_data.V18, cf_data.V19, cf_data.V20, 
             cf_data.V21, cf_data.V22, cf_data.V23,cf_data.V24,cf_data.V25, cf_data.V26, cf_data.V27, cf_data.V28])


X = X.T
print(X.shape)
report, mat = random_forest_test(X,Y, 30)
history_mat.append(mat)
history_report.append(report)

print(report)
import numpy as np
 
def plot_var(V, V_legend):

  x_1 = V.array[positive_idx]
  y_1 = V.array[negative_idx]
  maxval = max(V)
  fig = plt.figure()

  ax = fig.add_subplot(111)

  ax.set(title="Credit Card Fraud Data - "+V_legend, label="Positive")

  xaxis = np.linspace(0,maxval, max(x_1.shape))
  ax.scatter(list(xaxis) , list(x_1),label="Positive", c='b', marker='.')

  yaxis = np.linspace(0,maxval, max(y_1.shape))
  ax.scatter(yaxis, list(y_1),label="Negative", c='r', marker='x')
  plt.legend()
  plt.show()

plot_var(cf_data.V1, "V1")
plot_var(cf_data.V2, "V2")
plot_var(cf_data.V3, "V3")
plot_var(cf_data.V4, "V4")
plot_var(cf_data.V5, "V5")
plot_var(cf_data.V6, "V6")
plot_var(cf_data.V7, "V7")
plot_var(cf_data.V8, "V8")
plot_var(cf_data.V9, "V9")
plot_var(cf_data.V10, "V10")

import numpy as np
 
 
def plot_var2(V, V_legend):

  x_1 = V.array[positive_idx]
  y_1 = V.array[negative_idx]

  fig,(ax1,ax2,ax3) = plt.subplots(1,3,  figsize=(20, 5))

  ax1.set(title="Credit Card Fraud Data - "+V_legend)

  maxval = max(V)
  xaxis = np.linspace(0,maxval, max(x_1.shape))
  ax1.scatter(xaxis , list(x_1),label="Positive", c='b', marker='.')

  yaxis = np.linspace(0,maxval, max(y_1.shape))
  ax1.scatter(yaxis, list(y_1), label="Negative", c='r', marker='x')
  ax1.legend()
  plt.style.use('ggplot')
  ax2.hist(list(x_1), bins=20, color = "skyblue", label='Positive', alpha=0.5)
  ax2.legend()
  ax3.hist(list(y_1), bins=20, label='Negative', alpha=0.5)
  ax3.legend()

  plt.legend()
  plt.show()

plot_var2(cf_data.V11, "V11")
plot_var2(cf_data.V12, "V12")
plot_var2(cf_data.V13, "V13")
plot_var2(cf_data.V14, "V14")
plot_var2(cf_data.V15, "V15")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1632324162541729, 'fraction_numerical': 0.03833314147576839, 'mean_word_length': 4.409713574097136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '151450', 'license': 'MIT', 'n_tokens_mistral': 3923, 'n_tokens_neox': 3692, 'n_words': 631, 'path': 'RandomForest_CrediCardFraud_detection.ipynb', 'repo_name': 'anandkc812/data-analytics', 'size': '8687'}