python - 在 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)
其中x1
和x2
是 shape 数组(2,)
。我需要对其进行vertorize以提高性能。我看了看np.fromfunction
,,np.outer
但它们似乎不是我要找的……
先感谢您。抱歉,如果某处已经有答案!
解决方案
假设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. ]]
推荐阅读
- jquery - 如何在 Laravel 中存储动态输入
- java - Java readUTF() 在服务器中挂起(套接字连接)
- kubernetes - 作业未按计划运行 - Kubernetes CronJob
- javascript - 从外部文件 React Typescript 定义 useSelector 类型
- javascript - JS中的时间函数
- python-3.x - 我怎样才能删除一个
Django QuerySet 中的对象 - reactjs - 将 BaseWeb 的 Select TriangleDownIcon 更改为另一个 BaseWeb 图标
- c# - 算术运算导致 int 溢出
- python - python pool 只显示一个 PID > 这是并行处理的吗?
- javascript - Javascript函数返回“无法读取未定义的属性长度”