自组织神经网络(SOM)的Python第三方库minisom聚类功能实现

举报
府学路18号车神 发表于 2021/12/24 23:52:30 2021/12/24
3.8k+ 0 0
【摘要】 聚类功能 在这个例子中,我们将看到如何使用 MiniSom 对 iris 数据集进行聚类。 首先,让我们加载数据并训练我们的 SOM: from minisom import MiniSom imp...

聚类功能

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

  
 

在这里插入图片描述

原文链接:https://github.com/JustGlowing/minisom/blob/master/examples/Clustering.ipynb

文章来源: blog.csdn.net,作者:府学路18号车神,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_44333889/article/details/118215584

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。