聚类功能
在这个例子中,我们将看到如何使用 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();
