首页 > 解决方案 > 在 Python 中创建没有 for 循环的内核矩阵

问题描述

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

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是 shape 数组(2,)。我需要对其进行vertorize以提高性能。我看了看np.fromfunction,,np.outer但它们似乎不是我要找的……

先感谢您。抱歉,如果某处已经有答案!

标签: pythonnumpyvectorization

解决方案


假设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.  ]]

推荐阅读