聚類功能
在這個例子中,我們将看到如何使用 MiniSom 對 iris 資料集進行聚類。
首先,讓我們加載資料并訓練我們的 SOM:
from minisom import MiniSom
import numpy as np
import pandas as pd
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/00236/seeds_dataset.txt',
names=['area', 'perimeter', 'compactness', 'length_kernel', 'width_kernel',
'asymmetry_coefficient', 'length_kernel_groove', 'target'], usecols=[0, 5],
sep='\t+', engine='python')
# data normalization
data = (data - np.mean(data, axis=0)) / np.std(data, axis=0)
data = data.values
# Initialization and training
som_shape = (1, 3)
som = MiniSom(som_shape[0], som_shape[1], data.shape[1], sigma=.5, learning_rate=.5,
neighborhood_function='gaussian', random_seed=10)
som.train_batch(data, 500, verbose=True)
[ 500 / 500 ] 100% - 0:00:00 左
量化誤差:0.864828807271489
現在我們将映射到特定神經元的所有樣本視為一個簇。為了更容易地識别每個簇,我們将 SOM 上神經元的二維索引轉換為單維索引:
# each neuron represents a cluster
winner_coordinates = np.array([som.winner(x) for x in data]).T
# with np.ravel_multi_index we convert the bidimensional
# coordinates to a monodimensional index
cluster_index = np.ravel_multi_index(winner_coordinates, som_shape)
我們可以用不同的顔色繪制每個叢集:
import matplotlib.pyplot as plt
%matplotlib inline
# plotting the clusters using the first 2 dimentions of the data
for c in np.unique(cluster_index):
plt.scatter(data[cluster_index == c, 0],
data[cluster_index == c, 1], label='cluster='+str(c), alpha=.7)
# plotting centroids
for centroid in som.get_weights():
plt.scatter(centroid[:, 0], centroid[:, 1], marker='x',
s=80, linewidths=35, color='k', label='centroid')
plt.legend();
