09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

We will also need to normalize the input data, so we create a function to do so:

Chapter 10

def normalize(df):

result = df.copy()

for feature_name in df.columns:

max_value = df[feature_name].max()

min_value = df[feature_name].min()

result[feature_name] = (df[feature_name] - min_value) / (max_

value - min_value)

return result.astype(np.float32)

Let us read the data. The data contains Red, Green, and Blue channel values. Let us

normalize them:

## Reading input data from file

import pandas as pd

df = pd.read_csv('colors.csv') # The last column of data file is a

label

data = normalize(df[['R', 'G', 'B']]).values

name = df['Color-Name'].values

n_dim = len(df.columns) - 1

# Data for Training

colors = data

color_names = name

Let us create our SOM and fit it:

som = WTU(30, 30, n_dim, 400, sigma=10.0)

som.fit(colors)

Now, let's look at the result of the trained model. In the following code, you can see

the color map in the 2D neuron lattice:

# Get output grid

image_grid = som.get_centroids()

# Map colours to their closest neurons

mapped = som.map_vects(colors)

# Plot

plt.imshow(image_grid)

plt.title('Color Grid SOM')

for i, m in enumerate(mapped):

[ 391 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!