numpy的插值来增加数组大小

numpy的插值来增加数组大小

本文介绍了numpy的插值来增加数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我的previous问题相关的,但是这一次我正在寻找一种方法做增加了二维数组大小,而不是一个矢量。

的想法是,我有坐标夫妇(X; Y)我要平滑与所需数量(X线; Y)

对于一个矢量解决方案,我用@AGML用户的答案有很好的效果。

 从scipy.interpolate进口UnivariateSpline高清enlargeVector(矢量,大小):
    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)
    返回SPL(new_indices)


解决方案

您可以使用函数 map_coordinates scipy.ndimage.interpolation 模块。

 导入numpy的是NP
从scipy.ndimage.interpolation进口map_coordinatesA = np.random.random((10,10))
new_dims = []
对于original_length,new_length拉链(A.shape,(100,100)):
    new_dims.append(np.linspace(0,original_length-1,new_length))COORDS = np.meshgrid(* new_dims,索引='IJ')
B = map_coordinates(A,COORDS)

this question is related with my previous question How to use numpy interpolation to increase a vector size, but this time I'm looking for a method to do increase the 2D array size and not a vector.

The idea is that I have couples of coordinates (x;y) and I want to smooth the line with a desired number of (x;y) pairs

for a Vector solution I use the answer of @AGML user with very good results

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)
解决方案

You can use the function map_coordinates from the scipy.ndimage.interpolation module.

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)

这篇关于numpy的插值来增加数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:06