Amazing technological breakthrough possible @S-Logix

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • +91- 81240 01111

Social List

How to 0?


To implement Deep Neural Networks model for detect fraud transaction using keras in python.


Credit card transaction data set.


Confusion matrix
Classification report
Accuracy score
Precision and recall



   Load the data set.

   Define the independent and dependent variable.

  Build the deep neural networks.

   Initiate activation and optimizer functions according to the problem.

  Split the data into train and testing set.

   Fit the training set into the model.

  Predict the test results using DNN.

   Calculate the accuracy, precision and recall.

Sample Code

#import necessary libraries
import time
import random
import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
from sklearn.metrics import classification_report, confusion_matrix

#load the sample data from sample.xlsx file
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/creditcard.csv’)

#Make it as a data frame
df = pd.DataFrame(data)

#Checking missing values
print(“Checking missing values\n”)

#Count plot for target variable
plt.rcParams[“figure.figsize”] = [15,11]
ax = sns.countplot(x=”Class”,data=df)
plt.title(“Count plot for categorical variable”)

#correlation plotting
plt.rcParams[“figure.figsize”] = [19,11]
corr = df.corr(method=’pearson’)
ax = sns.heatmap(corr,annot=True,cmap=’BuGn’)

#feature selection
X = df.iloc[:,0:29]
y = df.iloc[:,30]
#Split the data into train and testing
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.3, random_state=0)

#Print training data
print(“Training data\n”,X_train,”\n”,Y_train)

#Print testing data
print(“Testing data\n”,X_test)

#make dependent variable categorical
Y_train = np_utils.to_categorical(Y_train,num_classes=2)
Y_test = np_utils.to_categorical(Y_test,num_classes=2)

batch_size = 100

#Build Deep neural networks
n_cols_2 = X_train.shape[1]

#create model
model = Sequential()

#add layers to model
model.add(Dense(250, activation=’relu’, input_shape=(n_cols_2,)))
model.add(Dense(250, activation=’relu’))
model.add(Dense(250, activation=’relu’))
model.add(Dense(2, activation=’softmax’))

#Compile the model
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’, metrics=[‘accuracy’])

#Here we train the Network., Y_train, batch_size = batch_size, epochs = 3, verbose = 5)

#Here we evaluate the model
score,acc = model.evaluate(X_test,Y_test,verbose = 2,batch_size = batch_size)
#Predict the test results
prediction = model.predict(X_test)
length = len(prediction)
y_label = np.argmax(Y_test,axis=1)
predict_label = np.argmax(prediction,axis=1)

#classification report
print(“Confusion Matrix\n”,confusion_matrix(Y_test.argmax(axis=1),prediction.argmax(axis=1)))
print(“Classification Report\n”,classification_report(Y_test.argmax(axis=1),prediction.argmax(axis=1)))
accuracy = np.sum(y_label==predict_label)/length * 100
print(“Accuracy : “,accuracy)

To implement Deep Neural Networks model for detect fraud transaction using keras in python
Load the data set
Define the independent and dependent variable
Classification report
Accuracy score
Build the deep neural networks
Initiate activation and optimizer functions according to the problem
Split the data into train and testing set
import classification_report, confusion_matrix
Checking missing values
Make it as a data frame
Predicting the Test set results
import pandas as pd
import numpy as np