Last active
July 19, 2021 22:50
-
-
Save Aphellirus/62027c4f4e8caf8e9eca2b8086826e41 to your computer and use it in GitHub Desktop.
Self Organizing Maps to detect frauds in credit card applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Self Organizing Maps to detect frauds in credit card applications | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
dataset = pd.read_csv('Credit_Card_Applications.csv') | |
X = dataset.iloc[:, :-1].values | |
y = dataset.iloc[:,-1].values | |
from sklearn.preprocessing import MinMaxScaler | |
sc = MinMaxScaler(feature_range=(0,1)) | |
X = sc.fit_transform(X) | |
from minisom import MiniSom | |
som = MiniSom(x=10, y=10, input_len=15, sigma=1.0, learning_rate=0.5) | |
som.random_weights_init(X) | |
som.train_random(data=X, num_iteration=100) | |
from pylab import bone, pcolor, colorbar, plot, show | |
bone() | |
pcolor(som.distance_map().T) | |
colorbar() | |
markers = ['o', 's'] | |
colors = ['r', 'g'] | |
for i,x in enumerate(X): | |
w = som.winner(x) | |
plot(w[0] + 0.5, | |
w[1] + 0.5, | |
markers[y[i]], | |
markeredgecolor = colors[y[i]], | |
markerfacecolor = 'None', | |
markersize = 10, | |
markeredgewidth = 2) | |
show() | |
mapping = som.win_map(X) | |
frauds = np.concatenate( (mapping[(8,1)], mapping[(9,2)]), axis = 0 ) | |
frauds = sc.inverse_transform(frauds) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment