根据一个数组值进行插值 [英] interpolation based on one array values

查看:282
本文介绍了根据一个数组值进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有值的数组:

I have two arrays with values:

x = np.array([100, 123, 123, 118, 123])
y = np.array([12, 1, 14, 13])

我想评估例如函数:

def func(a, b):
    return a*0.8 * (b/2)

所以,我想填写y缺失值.

So, I want to fill the y missing values.

我正在使用:

import numpy as np
from scipy import interpolate

def func(a, b):
    return a*0.8 * (b/2)


x = np.array([100, 123, 123, 118, 123])
y = np.array([12, 1, 14, 13])

X, Y = np.meshgrid(x, y)

Z = func(X, Y)

f = interpolate.interp2d(x, y, Z, kind='cubic')

现在,我不确定如何从这里继续.如果尝试:

Now, I am not sure how to continue from here.If I try:

xnew = np.linspace(0,150,10)
ynew = np.linspace(0,150,10)

Znew = f(xnew, ynew)

Znew填充有nan值.

Znew is filled with nan values.

我也想相反.

如果x小于y,并且我想始终基于x值进行插值.

If x is smaller than y and I want to interpolate always based on x values.

例如,

x = np.array([1,3,4]) y = np.array([1,2,3,4,5,6,7])

我现在想从y中删除值.

I want to remove values from y now.

我该如何进行呢?

推荐答案

要从一维数组进行插值,可以使用np.interp,如下所示:

To interpolate from a 1d array you can use np.interp as follow :

np.interp(np.linspace(0,1,len(x)), np.linspace(0,1,len(y)),y)

您可以查看文档以获得全部详细信息,但总而言之:

you can have a look at the documentation for full details but in short :

  • 考虑您的数组y具有引用从0到1的值(示例[5,2,6,3,9]将具有索引[0,0.25,0.5,0.75,1])
  • 该函数的第二个和第三个参数是索引和向量y
  • 第一个参数是y
  • 的内插值的索引
  • consider that your array y have value with references from 0 to 1 (example [5,2,6,3,9] will have indexes [0,0.25,0.5,0.75,1])
  • The second and the third argument of the function are the indexes and the vector y
  • The first argument is the indexes of the interpolated value of y

例如:

>>> y = [0,5]
>>> indexes = [0,1]
>>> new_indexes = [0,0.5,1]
>>> np.interp(new_indexes, indexes, y)
[0,2.5,5]

这篇关于根据一个数组值进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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