# Self Organizing Maps – simplest way – with source code – 2023 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…

## Code for Self Organizing Maps…

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

#importing data
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.5,
w+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)```
{"ft_lang_label":"__label__en","ft_lang_prob":0.58192396,"math_prob":0.9883271,"size":1735,"snap":"2023-14-2023-23","text_gpt3_token_len":447,"char_repetition_ratio":0.11034084,"word_repetition_ratio":0.0,"special_character_ratio":0.25648415,"punctuation_ratio":0.18475074,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99772155,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-04-01T08:33:08Z\",\"WARC-Record-ID\":\"<urn:uuid:eda4dabf-f7e2-4541-9296-b0ebdb5d887e>\",\"Content-Length\":\"103866\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d545b14-c04a-4c1c-846f-f773dc7f5375>\",\"WARC-Concurrent-To\":\"<urn:uuid:11c28587-ceb1-474f-ad0e-982a3bf0bec9>\",\"WARC-IP-Address\":\"89.117.27.68\",\"WARC-Target-URI\":\"https://machinelearningprojects.net/self-organizing-maps/?noamp=mobile\",\"WARC-Payload-Digest\":\"sha1:FYHPD55K5IKO4TWI6V7RRZ2XRYPUAPDS\",\"WARC-Block-Digest\":\"sha1:4KI6F7WN2KTKGUFJEI325LVYU5LOZUKC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949701.56_warc_CC-MAIN-20230401063607-20230401093607-00320.warc.gz\"}"}