How I can reduce False Negatives in my neural network?

Viewed 118

I'm working with this dataset: https://drive.google.com/file/d/1j7Gdeq5IiZlmRB7aMrJheLu1cFt9G5d7/view?usp=sharing

I'm trying to train a neural network that predicts fraud, based in a variable called 'fraud' (a dummy variable, 1 for fraud, 0 for not fraud).

What happens is that my model predicts properly not fraud (8429), but it can't predict the frauds (68), and when I print my confusion matrix, the 68 frauds are considered as false negatives. Here's my code

import tensorflow as tf
from tensorflow import keras
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from random import randint

y=dataset.pop('fraud') # creating a 'y' variable, equal to the dummy variable
dataset = dataset.drop(['cust_id','activated_date','last_payment_date','Unnamed: 0'], axis=1) # droping the not usefull variables for my model
x=dataset.values # a new 'x' variable with the remaining values of dataset 
y=np.array(y) # converting 'y' values to numpy array
counter=(np.count_nonzero(y)) # counter it's a variable that uses numpy.nonzero to count all zero values (to count not fraudulent users)
print("Data from not fraudulent users: " + str(len(y)-counter))
print("Data from fraudulent users: " + str(counter))
print("percetnage of not fraudulent: " +str(100*(1-(counter/len(y)))))
print("percetnage of fraudulent: "+str(100*((counter/len(y)))))

over sampling for training data set:

from imblearn.over_sampling import RandomOverSampler

oversample = RandomOverSampler(sampling_strategy='minority')
x_over, y_over = oversample.fit_resample(x, y)
counter=(np.count_nonzero(y_over))
print("Amount of data that is not fraudulent " + str(len(y_over)-counter))
print("Amount that indeed is fraudulent " + str(counter))
print("Percentage of non fraud " +str(100*(1-(counter/len(y_over)))))
print("Percentage of fraud "+str(100*((counter/len(y_over)))))

train my neural network:

epochs=90
model = keras.Sequential([
    keras.layers.Dense(32,input_shape=(17,),activation='relu'), # first layer with 17 variables for input, and 32 nodes
    keras.layers.Dense(64, activation='relu'), #2nd layer with 64 nodes and lineal activation method
    keras.layers.Dense(32,activation='relu'), # 3rd layer with 32 nodes and linear activation
    keras.layers.Dense(2, activation='sigmoid') # 4th layer with only two nodes and sigmoid classification (logistic) for 1 or 0 prediciton
])
print(model.summary())

Compile my neural network:

# First, I need to compile the model with Adam optimizer
model.compile(optimizer='SGD',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_over, y_over, epochs=epochs,validation_split=0.25,verbose=1) # fit training data set on our neural network

And finally, my confusion matrix:

comp=model.predict(x)
comp=np.array([np.argmax(u) for u in comp])
cm = confusion_matrix(y_true=y, y_pred=comp)
print(cm)
[[8429    0]
 [  68    0]]

What can I do to in order to enhance my model and make that 68 FN to be correctly classificated?

0 Answers
Related