from tkinter import messagebox
from tkinter import *
from tkinter import simpledialog
import tkinter
from tkinter import filedialog
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
from tkinter.filedialog import askopenfilename
from numpy.core.arrayprint import repr_format
import pandas as pd
from sklearn.model_selection import train_test_split 
from sklearn.metrics import accuracy_score
# from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
from sklearn.preprocessing import normalize
from sklearn.metrics import confusion_matrix

main = tkinter.Tk()
main.title("CREDIT CARD FRAUD DETECTION USING RANDOM FOREST ALOGORITM")
main.geometry("1300x1200")

global filename
global accuracy
global X, Y
global X_train, X_test, y_train, y_test
global rf

def upload():
    global filename
    filename = filedialog.askopenfilename(initialdir="dataset")
    pathlabel.config(text=filename)
    text.delete('1.0', END)
    text.insert(END,filename+" loaded\n");

def processDataset():
    global X, Y
    global X_train, X_test, y_train, y_test
    text.delete('1.0', END)
    dataset = pd.read_csv(filename)
    dataset = dataset.sample(frac=1)#randomize the whole dataset
    X = dataset.drop(["Time","Class"],axis=1)
    Y = pd.DataFrame(dataset[["Class"]])
    X = X.values
    Y = Y.values
    X_train, X_test, y_train, y_test = train_test_split(X,Y,train_size=0.80)
    X_train = normalize(X_train)
    X_test = normalize(X_test)
    text.insert(END,'Dataset contains total records : '+str(len(X))+"\n")
    text.insert(END,"Application using 80% dataset records to train RF Algorithm : "+str(len(X_train))+"\n")
    text.insert(END,"Application using 20% dataset records to test RF Algorithm  : "+str(len(X_test))+"\n")  

def prediction(X_test, cls):  #prediction done here
    y_pred = cls.predict(X_test) 
    for i in range(50):
      print("X=%s, Predicted=%s" % (X_test[i], y_pred[i]))
    return y_pred 
	
# Function to calculate accuracy 
def cal_accuracy(y_test, y_pred, details): 
    accuracy = accuracy_score(y_test,y_pred)*100
    text.insert(END,details+"\n\n")
    text.insert(END,"Accuracy : "+str(accuracy)+"\n\n")
    return accuracy 
     
def runRF():
    global random_acc
    global rf
    global cls
    global train, test, X_train, X_test, y_train, y_test
    #Importing Decision Tree classifier
    rf=RandomForestClassifier(n_estimators=50,max_depth=2,random_state=0,class_weight='balanced')

    #Fitting the classifier into training set
    rf.fit(X_train,y_train)
    text.insert(END,"Prediction Results\n\n")
    prediction_data = prediction(X_test, rf)
    cm = confusion_matrix(y_test, prediction_data)
    random_acc = cal_accuracy(y_test, prediction_data,'Random Forest  Accuracy')
    plt.figure(figsize=(10,7))
    sn.heatmap(cm, annot=True)
    plt.xlabel('Predicted')
    plt.ylabel('Truth')
    plt.show()



# def runKMEANS():
#     global accuracy
#     global kmeans
#     kmeans = KMeans(n_clusters=2,random_state=0,algorithm="elkan",max_iter=10000,n_jobs=-1)
#     kmeans.fit(X_train)
#     predict = kmeans.predict(X_test)
#     accuracy = accuracy_score(y_test,predict)
#     text.insert(END,'\nkMEANS Prediction Accuracy : '+str(accuracy)+"\n")

#     plt.scatter(X_test[:, 0], X_test[:, 1], c=predict, s=50, cmap='viridis')
#     centers = kmeans.cluster_centers_
#     plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5)
#     plt.show()

def evaluateTransaction():
    text.delete('1.0', END)
    test = filedialog.askopenfilename(initialdir="dataset")
    dataset = pd.read_csv(test)
    dataset = dataset.drop(["Time"],axis=1)
    dataset = dataset.values
    dataset = normalize(dataset)
    predict = rf.predict(dataset)                                 
    for i in range(len(predict)):
        if predict[i] == 0:
            text.insert(END,"X=%s, Predicted = %s" % (dataset[i], 'Transaction Contains Cleaned Signatures')+"\n\n")
        if predict[i] == 1:
            text.insert(END,"X=%s, Predicted = %s" % (dataset[i], 'Transaction Contains Fraud Transaction Signatures')+"\n\n")
     
def graph():
    global random_acc
    accuracy = random_acc * 100
    error = 100 - (accuracy)
    height = [accuracy,error]
    bars = ('RandomForest Correct Prediction Accuracy','Incorrect Prediction Error Rate')
    y_pos = np.arange(len(bars))
    plt.bar(y_pos, height)
    plt.xticks(y_pos, bars)
    plt.show()

def close():
    main.destroy()
    
font = ('times', 14, 'bold')
title = Label(main, text='CREDIT CARD FRAUD DETECTION USING RANDOM FOREST ALGORITHM')
title.config(bg='#6dd66d', fg='black')  
title.config(font=font)           
title.config(height=3, width=120)       
title.place(x=0,y=5)

font1 = ('times', 13, 'bold')
uploadButton = Button(main, text="Upload Credit Card Dataset", command=upload)
uploadButton.place(x=50,y=100)
uploadButton.config(font=font1)  

pathlabel = Label(main)
pathlabel.config(bg='brown', fg='white')  
pathlabel.config(font=font1)           
pathlabel.place(x=310,y=100)

processButton = Button(main, text="Generate Train and Test Model", command=processDataset)
processButton.place(x=50,y=150)
processButton.config(font=font1) 

kmeansButton = Button(main, text="Run Random Forest Algorithm", command=runRF)
kmeansButton.place(x=310,y=150)
kmeansButton.config(font=font1) 

evaluate = Button(main, text="Detect Fraud From Test Data", command=evaluateTransaction)
evaluate.place(x=580,y=150)
evaluate.config(font=font1) 

graphbutton = Button(main, text="Display Graph", command=graph)
graphbutton.place(x=50,y=200)
graphbutton.config(font=font1) 

# exitb = Button(main, text="Exit", command=close)
# exitb.place(x=310,y=200)
# exitb.config(font=font1) 


font1 = ('times', 12, 'bold')
text=Text(main,height=20,width=150)
scroll=Scrollbar(text)
text.configure(yscrollcommand=scroll.set)
text.place(x=10,y=250)
text.config(font=font1)


main.config(bg='#6d70d6')
main.mainloop() 

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling