Python中高效的逐元素函数计算 [英] Efficient element-wise function computation in Python

查看:133
本文介绍了Python中高效的逐元素函数计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下优化问题.给定两个np.arrays XY和一个函数K,我想尽可能快地计算矩阵发生率gram_matrix,其中(i,j)-th元素被计算为K(X[i],Y[j]).

I have the following optimization problem. Given two np.arrays X,Y and a function K I would like to compute as fast as possible the matrix incidence gram_matrix where the (i,j)-th element is computed as K(X[i],Y[j]).

这里有一个使用嵌套的for循环的实现,这被认为是解决这类问题的最慢的方法.

Here there an implementation using nested for-loops, which are acknowledged to be the slowest to solve these kind of problems.

def proxy_kernel(X,Y,K):
    gram_matrix = np.zeros((X.shape[0], Y.shape[0]))
    for i, x in enumerate(X):
        for j, y in enumerate(Y):
            gram_matrix[i, j] = K(x, y)
    return gram_matrix

我们非常感谢您的帮助.

Any help is truly appreciated.

推荐答案

np.vectorize确实提高了速度-大约提高了2倍(这里我将math.atan2用作带有2个标量参数的黑盒函数).

np.vectorize does make some improvement in speed - about 2x (here I'm using math.atan2 as an black box function that takes 2 scalar arguments).

In [486]: X=np.linspace(0,1,100)
In [487]: K_vect=np.vectorize(math.atan2)

In [488]: timeit proxy_kernel(X,X,math.atan2)
100 loops, best of 3: 7.84 ms per loop

In [489]: timeit K_vect(X[:,None],X)
100 loops, best of 3: 3.49 ms per loop

In [502]: timeit np.arctan2(X[:,None],X)  # numpy compiled loop
1000 loops, best of 3: 871 µs per loop

其中

def proxy_kernel(X,Y,K):
    gram_matrix = np.zeros((X.shape[0], Y.shape[0]))
    for i, x in enumerate(X):
            for j, y in enumerate(Y):
                    gram_matrix[i, j] = K(x, y)
    return gram_matrix

只要K是黑匣子,您就会受到调用KX.shape[0]*Y.shape[0]所需时间的限制.您可以尝试减少迭代时间,但是仍然受到所有这些函数调用的限制.

As long as K is a black box, you are limited by the time it takes to invoke K the X.shape[0]*Y.shape[0] times. You can try to minimize the iteration time, but you are still limited by all those function calls.

https://stackoverflow.com/a/29733040/901925 通过高斯内核加快了计算速度,利用np.linalg.norm函数的axis参数.

https://stackoverflow.com/a/29733040/901925 speeds up the calculation with a Gausian kernel, by taking advantage of the axis parameter of the np.linalg.norm function.

这篇关于Python中高效的逐元素函数计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆