<jupyter_start><jupyter_text>Self Organizing Map<jupyter_code># Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset =  pd.read_csv(r"D:\freelance posters and projects\Udemy deep learning course\Self_Organizing_Maps\Credit_Card_Applications.csv")
dataset.head()<jupyter_output><empty_output><jupyter_text>Here the columns of importance are:* The customerID (for verifying the fraudulent customers in the ending)* Columns A1 to A14 (for feeding the algorithm some data to find a pattern)* Column Class which mentions whether the application was approved or not__[More can be found out here](http://archive.ics.uci.edu/ml/datasets/Statlog+(Australian+Credit+Approval))__<jupyter_code>X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values<jupyter_output><empty_output><jupyter_text>Since the variables are all in different ranges, it makes more sense to normalize all these variables to a value between 0 & 1 which can be done using the MinMaxScaler feature of the scikit learn library<jupyter_code># Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
X = sc.fit_transform(X)<jupyter_output><empty_output><jupyter_text>To understand how to run the MiniSOM library, go through the doumentation [here](https://github.com/JustGlowing/minisom)<jupyter_code># Training the SOM
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)<jupyter_output><empty_output><jupyter_text>*** **The trained SOM can now be visualised to pinpoint the predicted frauds*** The circles mean that those applications were not approved ( _class value was 0_ )* The squares mean that those applications were approved ( _class value was 1_ )* The color of the background box represents the potential outliers i.e the values that the algorithm detected to be frauds. Basically we consider only the extremely white values which correspond to a value of 1<jupyter_code># Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone() #gives you a white map
pcolor(som.distance_map().T) #assigning colors to mean interneuron distances
colorbar()
markers = ['o', 's'] #circle and square
colors = ['r', 'g']
for i, x in enumerate(X):
    w = som.winner(x)
    plot(w[0] + 0.5, # because we want to keep the marker at center of the box
         w[1] + 0.5,
         markers[y[i]], # to correspond squares with approved and circle with not approved
         markeredgecolor = colors[y[i]],
         markerfacecolor = 'None',
         markersize = 10,
         markeredgewidth = 2)
show()<jupyter_output><empty_output><jupyter_text>Now that it is visible which are the outliers in the SOM, we gather the customerID of these outliers by inversing the results. _Note_ : Each time we run the SOM, there will be different values pointed by the map. So when running this step, kindly hardcode the coordinates of the outliers<jupyter_code># Finding the frauds
frauds=[]
mappings = som.win_map(X) # gets the mapping of all blocks on the SOM in the form of block coordinates and the people associated with it
frauds = np.concatenate((mappings[(7,6)], mappings[(1,2)]), axis = 0) # this is hardcoding, and will differ in each case to get the number of potential frauds 
frauds_final = sc.inverse_transform(frauds)<jupyter_output><empty_output><jupyter_text>*** Given below is the list of potential fraudulent customers as predicted by the algorithm. Since this is a type of unsupervised learning algorithm, there is no way to verify the results except experiencing a fraud.<jupyter_code>pd.DataFrame(data=np.int_(frauds_final), columns=['CustomerID', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9',
       'A10', 'A11', 'A12', 'A13', 'A14'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09251213902376693, 'fraction_numerical': 0.014822386915410171, 'mean_word_length': 5.323101777059774, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '480352', 'license': 'CC0-1.0', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1173, 'n_words': 461, 'path': 'Self_Organizing_Map.ipynb', 'repo_name': 'IrishMehta/Fraud-Detection-using-SOM', 'size': '3913'}