numpy 插值以增加数组大小
numpy interpolation to increase array size
这个问题与我之前的问题 有关,但这次我正在寻找一种方法来增加二维数组的大小,而不是向量。
我的想法是我有几个坐标 (x;y)
并且我想用所需数量的 (x;y)
对
平滑线
对于 Vector 解决方案,我使用了@AGML 用户的答案,结果非常好
from scipy.interpolate import UnivariateSpline
def enlargeVector(vector, size):
old_indices = np.arange(0,len(a))
new_length = 11
new_indices = np.linspace(0,len(a)-1,new_length)
spl = UnivariateSpline(old_indices,a,k=3,s=0)
return spl(new_indices)
您可以使用 scipy.ndimage.interpolation
模块中的函数 map_coordinates
。
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
A = np.random.random((10,10))
new_dims = []
for original_length, new_length in zip(A.shape, (100,100)):
new_dims.append(np.linspace(0, original_length-1, new_length))
coords = np.meshgrid(*new_dims, indexing='ij')
B = map_coordinates(A, coords)
这个问题与我之前的问题
我的想法是我有几个坐标 (x;y)
并且我想用所需数量的 (x;y)
对
对于 Vector 解决方案,我使用了@AGML 用户的答案,结果非常好
from scipy.interpolate import UnivariateSpline
def enlargeVector(vector, size):
old_indices = np.arange(0,len(a))
new_length = 11
new_indices = np.linspace(0,len(a)-1,new_length)
spl = UnivariateSpline(old_indices,a,k=3,s=0)
return spl(new_indices)
您可以使用 scipy.ndimage.interpolation
模块中的函数 map_coordinates
。
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
A = np.random.random((10,10))
new_dims = []
for original_length, new_length in zip(A.shape, (100,100)):
new_dims.append(np.linspace(0, original_length-1, new_length))
coords = np.meshgrid(*new_dims, indexing='ij')
B = map_coordinates(A, coords)