<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive/')
cd drive/'My Drive'/'Colab Notebooks'/dataset/
!pip install pycaret
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import itertools
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
from sklearn.metrics import cohen_kappa_score
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from xgboost import XGBClassifier
from sklearn.ensemble import VotingClassifier
# -*- coding: utf-8 -*-
"""
Created on Mon Feb  4 13:25:04 2019

@author: paras
"""
dataset = pd.read_csv('creditcard.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 30].values
from pycaret.classification import *
clf1 = setup(data = dataset, target = 'Class')
compare_models()
plt.hist(y)
dataset.head()
def plotCorrelationMatrix(df, graphWidth):
    filename = 'People Analytics'
    df = df[[col for col in df if df[col].nunique() > 1]] # keep columns where there are more than 1 unique values
    # print(df.columns)
    corr = df.corr()
    # print(corr)
    plt.figure(num=None, figsize=(graphWidth, graphWidth), dpi=80, facecolor='w', edgecolor='k')
    corrMat = plt.matshow(corr, fignum = 1)
    plt.xticks(range(len(corr.columns)), corr.columns, rotation=90)
    plt.yticks(range(len(corr.columns)), corr.columns)
    plt.gca().xaxis.tick_bottom()
    plt.colorbar(corrMat)
    plt.title(f'Correlation Matrix for {filename}', fontsize=15)
    plt.show()

plotCorrelationMatrix(dataset, 15)
def get_folds(X,y, k=5):
    return list(StratifiedKFold(n_splits=k, shuffle=True, random_state=1).split(X, y))
folds = get_folds(X, y)
fold = folds[0]
X_train, X_test, y_train, y_test = X[fold[0]], X[fold[1]], y[fold[0]], y[fold[1]]
plt.hist(y_train)
from imblearn.over_sampling import SMOTE
smote = SMOTE('minority')
X_train_sampled, y_train_sampled = smote.fit_sample(X_train, y_train)
y_train
plt.hist(y_train)
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)
pd.Series(pca.explained_variance_ratio_)
X_train_pca = pd.DataFrame(X_train_pca)
X_test_pca = pd.DataFrame(X_test_pca)
plt.figure(figsize=(15,10))
plt.scatter(X_train_pca.iloc[np.where(y_train == 1)][0], X_train_pca.iloc[np.where(y_train == 1)][1], c = 'r', marker='o')
plt.scatter(X_train_pca.iloc[np.where(y_train == 0)][0], X_train_pca.iloc[np.where(y_train == 0)][1], c = 'b', marker='.')
plt.show()
from sklearn.utils import class_weight
weights = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)
nlabels = len(np.unique(y_train))
weights_dict = { i: weights[i] for i in range(nlabels)}
clf1 = LogisticRegression(C=0.1, class_weight=weights_dict, random_state=0)
clf2 = RandomForestClassifier(500, class_weight=weights_dict ,random_state=0)
clf3 = XGBClassifier(n_estimators=250, n_jobs=-1)
clf4 = GaussianNB()
clf5 = SVC(C=0.1, kernel='rbf',random_state=0, probability=True, class_weight=weights_dict)
clf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('xgb', clf3), ('gnb', clf4), ('svc', clf5)], voting='soft')

clf.fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)
print (confusion_matrix(y_test, y_pred))

target_names = [0,1]
print(classification_report(y_test, y_pred, target_names))

cohen_kappa_score(y_test, y_pred)







# #separating positive examples
# positive_x = []
# positive_y = []
# negative_x = []
# negative_y = []
# for i in range(len(y)):
#     if y[i] == 1 :
#         positive_x.append(X[i])
#         positive_y.append(y[i])
#     else :
#         negative_x.append(X[i])
#         negative_y.append(y[i])

# #splitting dataset
# from sklearn.model_selection import train_test_split
# X_train, X_test, y_train, y_test = train_test_split(
#         negative_x, negative_y, test_size=0.2, random_state=42)

# #Adding the positive examples to test set
# X_test.extend(positive_x)
# y_test.extend(positive_y)

# #Feature Scaling
# from sklearn.preprocessing import StandardScaler
# sc_X = StandardScaler()
# X_train = sc_X.fit_transform(X_train)
# X_test = sc_X.transform(X_test)

# #Calculating mean for each feature
# Mean_X_train = []
# for i in range(X_train.shape[1]):
#   Mean_X_train.append(X_train[:, i].mean()) 

# #Calculating standard deviation for each feature
# std_X_train = []
# for j in range(X_train.shape[1]):
#   std_X_train.append(np.std(X_train[:, j], axis=0)) 
  
# #Calculating the probability of each example in test set
# import math
# y_pred = []
# def norm(x, mean, std):
#     variance = float(std)**2
#     denom = (2*math.pi*variance)**.5
#     numer = math.exp(-(float(x)-float(mean))**2/(2*variance))
#     return numer/denom
  
  
# for i in range(len(X_test)):
#      p = 1
#      for j in range(len(Mean_X_train)):
#          p *= norm(X_test[i, j], Mean_X_train[j], std_X_train[j])
#      y_pred.append(p)

# print ('Max probability', max(y_pred))
# print ('Min probability', min(y_pred))
# from sklearn.metrics import precision_score, recall_score, f1_score
# from sklearn.metrics import confusion_matrix
# y_out = []
# for i in range(len(y_pred)):
#     if y_pred[i] < 9.914460408105776e-28 :
#         y_out.append(1) 
#     else:
#         y_out.append(0)
        
# #checking efficiency with confusion matrix
# print (confusion_matrix(y_test, y_out))
# print ('Precision Score = ', precision_score(y_test, y_out))
# print ('Recall Score = ', recall_score(y_test, y_out))
# print ('F1 Score = ', f1_score(y_test, y_out))
# y_out = []
# for i in range(len(y_pred)):
#     if y_pred[i] < 9.914460408105776e-21:
#         y_out.append(1) 
#     else:
#         y_out.append(0)
        
# #checking efficiency with confusion matrix
# from sklearn.metrics import confusion_matrix
# confusion_matrix(y_test, y_out)
# #Calculating the probability of each exaple in training set
# import scipy.stats
# y_train_pred = []
# for i in range(len(X_train)):
#     p = 1
#     for j in range(len(Mean_X_train)):
#         p *= scipy.stats.norm(Mean_X_train[j], std_X_train[j]).pdf(X_train[i, j])
#     y_train_pred.append(p)
# import statsmodels.formula.api as sm
# X = np.append(arr = np.ones((len(X), 1)).astype(int), values = X, axis =1)
# X_opt = X[:, :]
# regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
# regressor_OLS.summary()
# # Visualising the Training set results
# plt.scatter(X_train[:, 0], y_train, marker= 'x',color = 'red')
# plt.scatter(X_train[:, 0], y_train_pred, marker= '.', color = 'blue')
# plt.show()

# # Visualising the Test set results
# plt.scatter(X_test[:, 0], y_test, marker= 'x', color = 'red')
# plt.scatter(X_train[:, 0], y_train_pred, marker= '.', color = 'blue')
# plt.show()
# # Visualising the Training set results
# plt.scatter(X_train[:, 1], y_train, color = 'red')
# plt.plot(X_train[:, 1], y_train_pred, color = 'blue')
# # plt.title('Salary vs Experience (Training set)')
# # plt.xlabel('Years of Experience')
# # plt.ylabel('Salary')
# plt.show()

# # Visualising the Test set results
# plt.scatter(X_test[:, 1], y_test, marker= 'x', color = 'red')
# plt.plot(X_train[:, 1], y_train_pred, color = 'blue')
# # plt.title('Salary vs Experience (Test set)')
# # plt.xlabel('Years of Experience')
# # plt.ylabel('Salary')
# plt.show()
# # Visualising the Training set results
# plt.scatter(X_train[:, 2], y_train, marker= 'x',color = 'red')
# plt.scatter(X_train[:, 2], y_train_pred, marker= '.', color = 'blue')
# plt.show()

# # Visualising the Test set results
# plt.scatter(X_test[:, 2], y_test, marker= 'x', color = 'red')
# plt.scatter(X_train[:, 2], y_train_pred, marker= '.', color = 'blue')
# plt.show()
# #Feature Scaling
# from sklearn.preprocessing import StandardScaler
# sc_X = StandardScaler()
# X_train = sc_X.fit_transform(X_train)
# X_test = sc_X.transform(X_test)
# sc_y = StandardScaler()
# y_train = sc_y.fit_transform(y_train)
# Mean_X_train = []
# for i in range(len(X_train)):
#   Mean_X_train.append(X_train[i].mean()) 
# std_X_train = []
# for j in range(len(X_train)):
#   std_X_train.append(np.std(X_train[j], axis=0)) 
# import scipy.stats
# prob = []
# for k in range(len(Mean_X_train)):
  
#   l = scipy.stats.norm(Mean_X_train[k], std_X_train[k]).pdf(k)
#   prob.append(l)
# MAX_X = []
# MIN_X = []
# for i in range(len(X_train)):
#   MAX_X.append(max(X_train[i]))
#   MIN_X.append(min(X_train[i])) 
# X_grid = np.arange(MIN_X, MAX_X, 0.01)
# len(MAX_X)
# len(y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15599680474723268, 'fraction_numerical': 0.01700330936893758, 'mean_word_length': 5.006854009595614, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '458453', 'license': 'MIT', 'n_tokens_mistral': 3476, 'n_tokens_neox': 3377, 'n_words': 707, 'path': 'Anomaly_Detection_Pycaret.ipynb', 'repo_name': 'parasgulati8/Fraud-Detection', 'size': '8763'}