Self Organizing Maps – simplest way – with source code – 2024

In today’s blog, we will see how we can implement self organizing maps in Python. This is going to be a very short blog. So without any further due, Let’s do it…

Table of Contents

Code for Self Organizing Maps…

# importing libararies
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#importing data
dataset = pd.read_csv('Credit_Card_Applications.csv')
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values

# scaling/normalizing the data
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler()
X = sc.fit_transform(X)

#initializing self organizing map
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)

#visualizing the som
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]],
         markersize=10,
         markeredgewidth=2,
         markerfacecolor='None'
         )
show()

#finding the frauds
mappings = som.win_map(X)
frauds = np.concatenate((mappings[(1,1)],mappings[(1,2)]),axis=0)
frauds = sc.inverse_transform(frauds)

Final Results…

Self Organizing maps

Download the Source code…

Do let me know if there’s any query regarding the project by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: FACE RECOGNITION BASED ATTENDANCE SYSTEM

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Reply

Your email address will not be published. Required fields are marked *