Created
April 17, 2010 04:56
-
-
Save hamilton/369290 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
import som | |
from grid import hexagonal_grid # comes with som repository | |
from distance import euclidean # also comes with the som repository | |
##### Create a funky, curved data set in 3 dimensions. ##### | |
data = np.transpose(np.matrix([ | |
np.random.normal(0,1,20000), | |
np.random.normal(0,1,20000), | |
np.random.normal(0,1,20000) | |
])) | |
indices = [] | |
n, p = data.shape | |
for i in range(n): | |
if (euclidean(np.array(data[i,:])[0], np.array([0,0,0])) > .5) and \ | |
(data[i,0] > 0 and data[i,1] > 1): | |
indices.append(i) | |
new_data = np.zeros([len(indices), 3]) | |
for i, j in enumerate(indices): | |
new_data[i,:] = data[j,:] | |
data = np.matrix(new_data) | |
######################## Let's make a SOM! ##################### | |
# First, create a SOM with 3 dimensions, and width and height 20 cells | |
# (that's 400 cells / prototypes / models). | |
hex_som = som.SelfOrganizingMap(3, 20, 20, | |
grid_type = hexagonal_grid) | |
# if you've got a numpy matrix, training is as simple as this. | |
hex_som.train(data) | |
# this special function plots 3 dimensional data, so you can see what your | |
# map looks like hanged on the original dataset. | |
hex_som.preview_3d_data_and_map(data) | |
# get the prototype vectors, in the original R^n space: | |
pvs = hex_som.prototype_vector | |
# get the grid coordinates, in the new R^2 space: | |
som_coords = hex_som.som | |
# the indices of the two are identical. pv[i,:] corresponds to som_coords[i,:]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment