gpt4 book ai didi

python - 在 scipy 中插值 np.nan 值

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:41 24 4
gpt4 key购买 nike

我想通过插入粗体显示的元素来填充 np.nan 数据值。它们是np.nan在其他维度上相同位置对应的元素。

import numpy as np
from scipy.interpolate import interp1d
data = np.array([[[3, 2, 1, 3, 2],
[**np.nan**, 1, 1, 4, 4],
[4, 2, 3, 3, 4],
[1, 1, 4, 1, 5],
[2, 4, 5, 2, 1]],

[[6, 7, 10, 6, 6],
[**5**, 9, 8, 6, 9],
[6, 10, 9, 8, 10],
[6, 8, 7, 10, 8],
[10, 9, 9, 10, 8]],

[[12, 14, 12, 15, 15],
[**21**, 11, 14, 14, 11],
[13, 13, 16, 15, 11],
[14, 15, 14, 16, 14],
[13, 15, 11, 11, 14]]])

result = interp1d(data, kind='cubic')
print result

结果

TypeError: __init__() takes at least 3 arguments (3 given)

最好的方法是什么?由于我必须处理非常大的数组,因此我正在寻找有效的方法。谢谢。

最佳答案

你的问题太笼统了,插值不需要来自5*5矩阵的信息,只需要其他维度相同单元格中的值?如果是这样的话,那么,它仍然太板了,因为有太多的插值工具来满足不同的需求。我想说,也许最近邻方法应该给你一个好的开始,尽管 scipy.interpolate 的文档对某些人来说有点薄弱:

In [1]:

data = np.array([[[3, 2, 1, 3, 2],
[np.nan, 1, 1, 4, 4],
[4, 2, 3, 3, 4],
[1, 1, 4, 1, 5],
[2, 4, 5, 2, 1]],

[[6, 7, 10, 6, 6],
[5, 9, 8, 6, 9],
[6, 10, 9, 8, 10],
[6, 8, 7, 10, 8],
[10, 9, 9, 10, 8]],

[[12, 14, 12, 15, 15],
[21, 11, 14, 14, 11],
[13, 13, 16, 15, 11],
[14, 15, 14, 16, 14],
[13, 15, 11, 11, 14]]])
In [2]:

data1=data.reshape((3,-1))
In [3]:
#the one you want to interpolate
data1[:,(np.isnan(data.reshape((3,-1))).any(0))]
Out[3]:
array([[ nan],
[ 5.],
[ 21.]])
In [4]:
#the other 'good' data points
data1[:,~(np.isnan(data.reshape((3,-1))).any(0))]
Out[4]:
array([[ 3., 2., 1., 3., 2., 1., 1., 4., 4., 4., 2.,
3., 3., 4., 1., 1., 4., 1., 5., 2., 4., 5.,
2., 1.],
[ 6., 7., 10., 6., 6., 9., 8., 6., 9., 6., 10.,
9., 8., 10., 6., 8., 7., 10., 8., 10., 9., 9.,
10., 8.],
[ 12., 14., 12., 15., 15., 11., 14., 14., 11., 13., 13.,
16., 15., 11., 14., 15., 14., 16., 14., 13., 15., 11.,
11., 14.]])
In [5]:

import scipy.interpolate as si
In [6]:

Q=si.NearestNDInterpolator(data1[:,~(np.isnan(data.reshape((3,-1))).any(0))][[1,2]].T,
data1[:,~(np.isnan(data.reshape((3,-1))).any(0))][0])
In [8]:
#the first value is the answer, the 2nd is the index of the nearest neighbor.
Q.tree.query([5,21])
Out[8]:
(6.082762530298219, 3)

关于python - 在 scipy 中插值 np.nan 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com