【问题标题】:Creating a Kernel matrix without for-loops in Python在 Python 中创建没有 for 循环的内核矩阵
【发布时间】:2021-10-09 15:09:20
【问题描述】:

我知道还有其他帖子提出了类似的问题,但没有找到可以回答我具体问题的内容。 我有下面的代码:

def kernel_function(self, x1, x2):
    h = 0.5
    return np.exp(-(np.linalg.norm(x2 - x1)/h)**2)

for i, x1 in enumerate(train_x):
    for j, x2 in enumerate(train_x):
        K[i,j] = self.kernel_function(x1, x2)

其中x1x2 是形状为(2,) 的数组。我需要对其进行vertorize以提高性能。我看了np.fromfunctionnp.outer,但它们似乎不是我要找的……

提前谢谢你。抱歉,如果某处已经有答案!

【问题讨论】:

    标签: python numpy vectorization


    【解决方案1】:

    假设train_x具有以下格式:

    >>> train_x = np.array(((-.2, -.1), (0, .1), (.2, 0), (.1, -.1)))
    

    执行你得到的代码:

    >>> np.set_printoptions(precision=2)
    >>> K
    [[1.   0.73 0.51 0.7 ]
     [0.73 1.   0.82 0.82]
     [0.51 0.82 1.   0.92]
     [0.7  0.82 0.92 1.  ]]
    

    你可以重塑train_x

    >>> train_x_cols = train_x.T.reshape(2, -1, 1)
    >>> train_x_rows = train_x.T.reshape(2, 1, -1)
    

    所以,多亏了广播,当你减去它们时,你会得到所有的组合:

    >>> train_x_rows - train_x_cols
    [[[ 0.   0.2  0.4  0.3]
      [-0.2  0.   0.2  0.1]
      [-0.4 -0.2  0.  -0.1]
      [-0.3 -0.1  0.1  0. ]]
    
     [[ 0.   0.2  0.1  0. ]
      [-0.2  0.  -0.1 -0.2]
      [-0.1  0.1  0.  -0.1]
      [ 0.   0.2  0.1  0. ]]]
    

    您可以重写kernel_function() 以仅计算第一个轴上的范数:

    def kernel_function(x1, x2):
        h = 0.5
        return np.exp(-(np.linalg.norm(x2 - x1, axis=0) / h) ** 2)
    

    然后你得到:

    >>> kernel_function(train_x_cols, train_x_rows)
    [[1.   0.73 0.51 0.7 ]
     [0.73 1.   0.82 0.82]
     [0.51 0.82 1.   0.92]
     [0.7  0.82 0.92 1.  ]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      相关资源
      最近更新 更多