memory error by using rbf with scipy

萝らか妹 提交于 2019-12-06 08:42:09

It appears to me Rbf interpolation is quite computationally intensive and leads to O(N^2) operations (correct me if I am wrong). So in order to avoid memory error you can do one of the following, instead of

zz = rbf(xx, yy)

1. Iterate with nditer

Slow, but works for small arrays:

for iz, ix, iy in np.nditer(
    [zz, xx, yy],
    flags=['external_loop', 'buffered'], 
    op_flags=['readwrite']
):
    iz[...] = rbf(ix, iy)

2. Use dask.array

Faster option and uses threads

import dask.array as da

n1 = xx.shape[1]
ix = da.from_array(xx, chunks=(1, n1))
iy = da.from_array(yy, chunks=(1, n1))
iz = da.map_blocks(rbf, ix, iy)
zz = iz.compute()

Hope this works for you.

P.S: As suggested on another question and scipy docs on Rbf, Rbf also allows you to replace the norm calculation function with a callable (see LowLevelCallable and the Pythran blog). It is a bit involved, but it might also solve your problem, while increasing the performance as well.

P.P.S: I managed to speedup by a factor of 50 by making this modification:

import numpy as np

def euclidean_norm_numpy(x1, x2):
    return np.linalg.norm(x1 - x2, axis=0)

rbf = Rbf(x, y, d, function='gaussian', norm=euclidean_norm_numpy)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!