<jupyter_start><jupyter_text>importing related package<jupyter_code># standard library
import os 
import pathlib    # object oriented filesystems paths which instantiates a concrete path for the platform the code is running on.
import os.path    # common pathname manipulations
import fileinput  # Iterate over lines from multiple input streams 
import stat       # Interpreting stat() results
import filecmp    # file and directory comparisions
import tempfile   # generate tempory file and directories
import glob       #  Unix style pathname pattern expansion
import itertools

# third parties library 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns # provide high level interface for drawing attractive and informative statistical graphics


from sklearn.preprocessing import StandardScaler
import platform, psutil
print('System         :', platform.system())
print('Python Version :', platform.python_version())
print('Version tuple  :', platform.python_version_tuple())
print('Compiler       :', platform.python_compiler())
print('RAM            :', str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB")
print('Build          :', platform.python_build())<jupyter_output>System         : Windows
Python Version : 3.7.3
Version tuple  : ('3', '7', '3')
Compiler       : MSC v.1915 64 bit (AMD64)
RAM            : 8 GB
Build          : ('default', 'Apr 24 2019 15:29:51')<jupyter_text>Importing dataset<jupyter_code>data = pd.read_csv("./Dataset/creditcard.csv")
data.head(5)

print(data.shape)<jupyter_output>(284807, 31)<jupyter_text>To know whether any cell value is empty or not<jupyter_code>#data.empty 
data.isnull().values.any()
classes_count = pd.value_counts(data['Class'],sort = True).sort_index()
classes_count.plot(kind='bar', color = "Gray")
plt.title('Fraud Class Histogram', fontweight='bold', fontsize = '15', color = "gray")
plt.xlabel('Class', fontweight='bold', fontsize = '15', color = "gray")
plt.ylabel('Frequency', fontweight='bold', fontsize = '15', color = "gray")
plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()
data.info(memory_usage='deep')
data.describe()<jupyter_output><empty_output><jupyter_text>Classify Fradulent and Normal Transactions<jupyter_code>fraud = data[data['Class'] == 1]
normal = data[data['Class'] == 0]
print('Fradulent transations     :', fraud.shape)
print('Normal(Valid) transaction :',normal.shape)
data.Class.value_counts()
print(normal.Amount.describe())<jupyter_output>count    284315.000000
mean         88.291022
std         250.105092
min           0.000000
25%           5.650000
50%          22.000000
75%          77.050000
max       25691.160000
Name: Amount, dtype: float64<jupyter_text>Graph to show relation between normal trasactions with respect to amount<jupyter_code>fig, axes1  = plt.subplots(sharex = True, figsize=(8,8))

bins = 50

axes1.hist(normal.Amount, bins = bins, color = 'Grey' )
axes1.set_title('Normal')

plt.xlabel('Amount', fontweight='bold', fontsize = '15', color = "gray")
plt.ylabel('Number of transactions', fontweight='bold', fontsize = '15', color = "gray")
plt.xlim(0,20000)
plt.yscale('log')
plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()
print(fraud.Amount.describe())<jupyter_output>count     492.000000
mean      122.211321
std       256.683288
min         0.000000
25%         1.000000
50%         9.250000
75%       105.890000
max      2125.870000
Name: Amount, dtype: float64<jupyter_text>Graph to show relation between fraud trasactions with respect to amount<jupyter_code>fig, axes1  = plt.subplots(sharex = True, figsize=(10,10) )

bins = 50

axes1.hist(fraud.Amount, bins = bins, color = 'Grey' )
axes1.set_title('Fraud', fontweight='bold', fontsize = '15', color = "gray")

plt.xlabel('Amount', fontweight='bold', fontsize = '15', color = "gray")
plt.ylabel('Number of transactions', fontweight='bold', fontsize = '15', color = "gray")
plt.xlim(0,20000)
plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()

normal.Time.describe()<jupyter_output><empty_output><jupyter_text>Graph of Normal transaction between Amount with respect to TIme<jupyter_code>fig, axestime1  = plt.subplots(sharex = True, figsize=(10,10) )

axestime1.scatter(normal.Time, normal.Amount, color = 'Grey' )
axestime1.set_title('Normal', fontweight='bold', fontsize = '15', color = "gray" )

plt.xlabel('Time', fontweight='bold', fontsize = '15', color = "gray")
plt.ylabel('Amount', fontweight='bold', fontsize = '15', color = "gray")

plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()
fraud.Time.describe()<jupyter_output><empty_output><jupyter_text>Graph of fraud transactions between Amount with respect to Time<jupyter_code>fig, axestime2  = plt.subplots(sharex = True, figsize=(10,10) )

axestime2.scatter(fraud.Time, fraud.Amount, color = 'Grey' )
axestime2.set_title('Fraud', fontweight='bold', fontsize = '15', color = "gray" )

plt.xlabel('Time', fontweight='bold', fontsize = '15', color = "gray")
plt.ylabel('Amount', fontweight='bold', fontsize = '15', color = "gray")

plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()<jupyter_output><empty_output><jupyter_text>Different feature classification based on class<jupyter_code>from scipy.stats import norm
col_features = data.iloc[:,1:29].columns
# col_features
bins = 50 

plt.figure( figsize = (12, 28 * 4))
gs = gridspec.GridSpec(28,1)

for count, elem in enumerate(data[col_features]):
    ax = plt.subplot(gs[count])
    sns.distplot(data[elem][data.Class == 1], bins = bins, fit=norm )
    sns.distplot(data[elem][data.Class == 0], bins = bins, fit=norm)
    ax.set_xlabel('')
    ax.set_title('histogram of feature: ' + str(elem), fontsize = '15', color = "gray" )
    plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()<jupyter_output><empty_output><jupyter_text>Data processing    - Normalizing column amount<jupyter_code>scaler = StandardScaler() 

data['normalized_amount'] = scaler.fit_transform(data['Amount'].values.reshape(-1,1))
data['normalized_time'] = scaler.fit_transform(data['Time'].values.reshape(-1,1))

data.normalized_amount.describe()
data.normalized_time.describe()<jupyter_output><empty_output><jupyter_text>Droping column amount and time<jupyter_code>data = data.drop(['Time','Amount'],axis =1)
data.head()<jupyter_output><empty_output><jupyter_text>separating features and labels<jupyter_code>xData = data.iloc[:,data.columns != 'Class'] # feature
xData.head(5)
print(xData.shape)
print(xData.columns)
yData = data.iloc[:,data.columns == 'Class']
yData.head(5)
print(yData.shape)
print(yData.columns)<jupyter_output>(284807, 1)
Index(['Class'], dtype='object')<jupyter_text>oversampling<jupyter_code>from imblearn.over_sampling import SMOTE

sm = SMOTE(random_state= 42)
x_Sampled,y_Sampled = sm.fit_sample(xData,yData.values.ravel())

Source_data_no_fraud_count = len(data[data.Class==0])
Source_data_fraud_count = len(data[data.Class==1])
print('Percentage of fraud counts in original dataset:{}%'.format((Source_data_fraud_count*100)/(Source_data_no_fraud_count+Source_data_fraud_count)))

Sampled_data_no_fraud_count = len(y_Sampled[y_Sampled==0])
Sampled_data_fraud_count = len(y_Sampled[y_Sampled==1])
print('Percentage of fraud counts in the new data:{}%'.format((Sampled_data_fraud_count*100)/(Sampled_data_no_fraud_count+Sampled_data_fraud_count)))<jupyter_output>Using TensorFlow backend.<jupyter_text>Splitting data into train and test set<jupyter_code>from sklearn.model_selection import train_test_split

x_Train , x_Test, y_Train, y_Test = train_test_split (xData,yData,test_size=0.2, random_state = 40)

print("Number of train dataset      : ", len(x_Train))
print("Number of test dataset       : ", len(x_Test))
print("Total number of transactions : ", len(x_Train) + len(x_Test))

#undersampled dataset

x_Train_Sampled, x_Test_Sampled, y_Train_Sampled, y_Test_Sampled = train_test_split (x_Sampled, y_Sampled, test_size = 0.2, random_state = 40)
print("\n")
print("Number of train dataset      : ", len(x_Train_Sampled))
print("Number of test dataset       : ", len(x_Test_Sampled))
print("Total number of transactions : ", len(x_Train_Sampled) + len(x_Test_Sampled))


x_train_sampled_data = pd.DataFrame(x_Train_Sampled)
y_train_sampled_data = pd.DataFrame(y_Train_Sampled)
x_test_sampled_data  = pd.DataFrame(x_Test_Sampled)
y_test_sampled_data  = pd.DataFrame(y_Test_Sampled)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report 
kf = KFold(n_splits=5, shuffle=False).split(range(25))

# print the contents of each training and testing set
print('{} {:^61} {}'.format('Iteration', 'Training set observations', 'Testing set observations'))
for iteration, data in enumerate(kf, start=1):
    print('{:^9} {} {:^25}'.format(iteration, data[0], str(data[1])))


from sklearn.model_selection import cross_val_score
lr = LogisticRegression(solver = 'lbfgs')
lr.fit(x_train_sampled_data, y_train_sampled_data)
yPred=lr.predict(x_test_sampled_data)
scores = cross_val_score(lr, y_test_sampled_data, yPred, cv=10, scoring='accuracy')
print(scores)
print(scores.mean())
y_predicted =  np.array(lr.predict(x_test_sampled_data))
y_right = np.array(y_test_sampled_data)

cnf = confusion_matrix(y_right, y_predicted )
np.set_printoptions(precision=2)
cnf
from sklearn.metrics import accuracy_score
print ('Accuracy Score :',accuracy_score(y_right, y_predicted)) 
from sklearn.metrics import classification_report
print(classification_report(y_right, y_predicted))
from sklearn.metrics import precision_recall_curve

precision , recall , thresolds = precision_recall_curve(y_right, y_predicted)
precision    
recall
thresolds
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Greys):
   
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=0)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        
    else:
        1
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()
class_names = ['Fraud','Normal']
plt.figure()
plot_confusion_matrix(cnf,classes=class_names,title='Confusion matrix')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13821731593832678, 'fraction_numerical': 0.03412097436365295, 'mean_word_length': 5.564071856287425, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 52, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '205268', 'license': 'MIT', 'n_tokens_mistral': 4087, 'n_tokens_neox': 3742, 'n_words': 774, 'path': 'Exploring-the-analysis-of-data.ipynb', 'repo_name': 'ghimiresunil/Credit-Card-Fraud-Detection', 'size': '10961'}