Python 中的六边形自组织映射

发布于 2024-08-23 07:17:46 字数 480 浏览 10 评论 0原文

我正在Python上寻找六角形 自组织映射 .

六角形平铺

  1. 准备好模块。如果存在的话。
  2. 绘制六角形单元
  3. 算法的方法,以将六角形单元用作数组或其他

关于: 自组织映射 (SOM) 或自组织特征映射 (SOFM) 是一种人工神经网络,使用无监督学习进行训练以产生低维(通常是二维)

I am looking for hexagonal self-organizing map on Python.

hexagonal tiling

  1. ready module. If one exists.
  2. way to plot hexagonal cell
  3. algorithms to work with hexagonal cells as array or smth else

About:
A self-organizing map (SOM) or self-organizing feature map (SOFM) is a type of artificial neural network that is trained using unsupervised learning to produce a low-dimensional (typically two-dimensional)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-08-30 07:17:46

我知道这个讨论已经有 4 年了,但是我还没有在网上找到令人满意的答案。

如果您有一个将输入映射到神经元的数组和一个与每个神经元的位置相关的二维数组。

例如,考虑这样的事情:

hits = array([1, 24, 14, 16,  6, 11,  8, 23, 15, 16, 15,  9, 20,  1,  3, 29,  4,
              32, 22,  7, 26, 26, 35, 23,  7,  6, 11,  9, 18, 17, 22, 19, 34,  1,
              36,  3, 31, 10, 22, 11, 21, 18, 29,  3,  6, 32, 15, 30, 27],
             dtype=int32)
centers = array([[ 1.5       ,  0.8660254 ],
                 [ 2.5       ,  0.8660254 ],
                 [ 3.5       ,  0.8660254 ],
                 [ 4.5       ,  0.8660254 ],
                 [ 5.5       ,  0.8660254 ],
                 [ 6.5       ,  0.8660254 ],
                 [ 1.        ,  1.73205081],
                 [ 2.        ,  1.73205081],
                 [ 3.        ,  1.73205081],
                 [ 4.        ,  1.73205081],
                 [ 5.        ,  1.73205081],
                 [ 6.        ,  1.73205081],
                 [ 1.5       ,  2.59807621],
                 [ 2.5       ,  2.59807621],
                 [ 3.5       ,  2.59807621],
                 [ 4.5       ,  2.59807621],
                 [ 5.5       ,  2.59807621],
                 [ 6.5       ,  2.59807621],
                 [ 1.        ,  3.46410162],
                 [ 2.        ,  3.46410162],
                 [ 3.        ,  3.46410162],
                 [ 4.        ,  3.46410162],
                 [ 5.        ,  3.46410162],
                 [ 6.        ,  3.46410162],
                 [ 1.5       ,  4.33012702],
                 [ 2.5       ,  4.33012702],
                 [ 3.5       ,  4.33012702],
                 [ 4.5       ,  4.33012702],
                 [ 5.5       ,  4.33012702],
                 [ 6.5       ,  4.33012702],
                 [ 1.        ,  5.19615242],
                 [ 2.        ,  5.19615242],
                 [ 3.        ,  5.19615242],
                 [ 4.        ,  5.19615242],
                 [ 5.        ,  5.19615242],
                 [ 6.        ,  5.19615242]])

所以我使用以下方法执行此操作:

from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

def plot_map(hits, n_centers, w=10):
    """
    Plot Map
    """

    fig = plt.figure(figsize=(w, .7 * w))
    ax = fig.add_subplot(111)
    hits_count = np.histogram(hits, bins=n_centers.shape[0])[0]
    # Discover difference between centers
    collection = RegularPolyCollection(
        numsides=6, # a hexagon 
        rotation=0, sizes=( (6.6*w)**2 ,),
        edgecolors = (0, 0, 0, 1),
        array= hits_count,
        cmap = cm.winter,
        offsets = n_centers,
        transOffset = ax.transData,
    )
    ax.axis('off')
    ax.add_collection(collection, autolim=True)
    ax.autoscale_view()
    fig.colorbar(collection)
    return ax

_ = plot_map(som_classif, matrix)

最后我得到了这个输出:

在此处输入图像描述

编辑

此代码的更新版本位于 https://stackoverflow.com/a/23811383/575734

I know this discussion is 4 years old, however I haven't find a satisfactory answer over the web.

If you have something as a array mapping the input to the neuron and a 2-d array related to the location for each neuron.

For example consider something like this:

hits = array([1, 24, 14, 16,  6, 11,  8, 23, 15, 16, 15,  9, 20,  1,  3, 29,  4,
              32, 22,  7, 26, 26, 35, 23,  7,  6, 11,  9, 18, 17, 22, 19, 34,  1,
              36,  3, 31, 10, 22, 11, 21, 18, 29,  3,  6, 32, 15, 30, 27],
             dtype=int32)
centers = array([[ 1.5       ,  0.8660254 ],
                 [ 2.5       ,  0.8660254 ],
                 [ 3.5       ,  0.8660254 ],
                 [ 4.5       ,  0.8660254 ],
                 [ 5.5       ,  0.8660254 ],
                 [ 6.5       ,  0.8660254 ],
                 [ 1.        ,  1.73205081],
                 [ 2.        ,  1.73205081],
                 [ 3.        ,  1.73205081],
                 [ 4.        ,  1.73205081],
                 [ 5.        ,  1.73205081],
                 [ 6.        ,  1.73205081],
                 [ 1.5       ,  2.59807621],
                 [ 2.5       ,  2.59807621],
                 [ 3.5       ,  2.59807621],
                 [ 4.5       ,  2.59807621],
                 [ 5.5       ,  2.59807621],
                 [ 6.5       ,  2.59807621],
                 [ 1.        ,  3.46410162],
                 [ 2.        ,  3.46410162],
                 [ 3.        ,  3.46410162],
                 [ 4.        ,  3.46410162],
                 [ 5.        ,  3.46410162],
                 [ 6.        ,  3.46410162],
                 [ 1.5       ,  4.33012702],
                 [ 2.5       ,  4.33012702],
                 [ 3.5       ,  4.33012702],
                 [ 4.5       ,  4.33012702],
                 [ 5.5       ,  4.33012702],
                 [ 6.5       ,  4.33012702],
                 [ 1.        ,  5.19615242],
                 [ 2.        ,  5.19615242],
                 [ 3.        ,  5.19615242],
                 [ 4.        ,  5.19615242],
                 [ 5.        ,  5.19615242],
                 [ 6.        ,  5.19615242]])

So I'do this using a the following method:

from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

def plot_map(hits, n_centers, w=10):
    """
    Plot Map
    """

    fig = plt.figure(figsize=(w, .7 * w))
    ax = fig.add_subplot(111)
    hits_count = np.histogram(hits, bins=n_centers.shape[0])[0]
    # Discover difference between centers
    collection = RegularPolyCollection(
        numsides=6, # a hexagon 
        rotation=0, sizes=( (6.6*w)**2 ,),
        edgecolors = (0, 0, 0, 1),
        array= hits_count,
        cmap = cm.winter,
        offsets = n_centers,
        transOffset = ax.transData,
    )
    ax.axis('off')
    ax.add_collection(collection, autolim=True)
    ax.autoscale_view()
    fig.colorbar(collection)
    return ax

_ = plot_map(som_classif, matrix)

Finally I got this output:

enter image description here

EDIT

An updated version of this code on https://stackoverflow.com/a/23811383/575734

夏有森光若流苏 2024-08-30 07:17:46

我没有第 1 点的答案,但有第 2 点和第 3 点的一些提示。在您的上下文中,您不是在建模物理 2D 空间,而是在建模具有 6 个邻居的图块的概念空间。这可以用按列排列的方形图块进行建模,其中奇数列垂直移动正方形大小的一半。我将尝试一个 ASCII 图:

 ___     ___     ___     
|   |___|   |___|   |___
|___|   |___|   |___|   |
|   |___|   |___|   |___|
|___|   |___|   |___|   |
|   |___|   |___|   |___|
|___|   |___|   |___|   |
    |___|   |___|   |___|

您可以轻松地看到每个方块有 6 个邻居(当然除了边缘的方块)。这可以很容易地建模为一个 2D 正方形数组,并且计算位置 (i, j) 处的正方形坐标的规则非常简单,i 是行,j 是列:

如果 j 是偶数:

(i+1, j), (i-1, j), (i, j-1), (i, j+1), (i-1, j-1), (i+1, j-1)

如果 j 是奇数:(

(i+1, j), (i-1, j), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j+1)

前 4 个项相同)

I don't have an answer for point 1, but some hints for point 2 and 3. In your context, you're not modelling a physical 2D space but a conceptual space with tiles that have 6 neighbors. This can be modelled with square tiles arranged in columns with the odd colums shifted vertically by half the size of a square. I'll try an ASCII diagram:

 ___     ___     ___     
|   |___|   |___|   |___
|___|   |___|   |___|   |
|   |___|   |___|   |___|
|___|   |___|   |___|   |
|   |___|   |___|   |___|
|___|   |___|   |___|   |
    |___|   |___|   |___|

You can see easily that each square has 6 neighbors (except the ones on the edges of course). This gets easily modeled as a 2D array of squares, and the rules to compute the coordinates of the square at at position (i, j), i being the row and j the column are quite simple:

if j is even:

(i+1, j), (i-1, j), (i, j-1), (i, j+1), (i-1, j-1), (i+1, j-1)

if j is odd:

(i+1, j), (i-1, j), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j+1)

(the 4 first terms are identical)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文