from minisom import MiniSom from sklearn.datasets import load_iris # load the iris dataset iris_data = load_iris().data # initialize the SOM som = MiniSom(5, 5, 4, sigma=0.3, learning_rate=0.5) # train the SOM som.train_random(iris_data, 100) # get the cluster assignments for each data point cluster_labels = som.labels_map(iris_data) # visualize the clusters from matplotlib import pyplot as plt plt.imshow(som.distance_map()) plt.colorbar() plt.show()
from minisom import MiniSom from sklearn.datasets import load_breast_cancer # load the breast cancer dataset cancer_data = load_breast_cancer().data # initialize the SOM som = MiniSom(10, 10, 30, sigma=0.3, learning_rate=0.5) # train the SOM som.train_random(cancer_data, 100) # project the data onto the SOM grid mapped_data = som.win_map(cancer_data) # visualize the data from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for i, data in enumerate(mapped_data.values()): xs, ys, zs = zip(*data) ax.scatter(xs, ys, zs, label='cluster %d' % i) ax.legend() plt.show()MiniSom is a package library for Python.