在 Python 中插入 3D 数组
Interpolate a 3D array in Python
我有一个 3D NumPy 数组,如下所示:
arr = np.empty((4,4,5))
arr[:] = np.nan
arr[0] = 1
arr[3] = 4
arr
>>> [[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]]
[[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]]
[[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]]]
我想沿 axis=0
进行插值,以便得到以下内容:
>>> [[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]]
[[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]]
[[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]]]
我一直在查看 SciPy 模块,似乎有方法可以在 1D 和 2D 阵列上执行此操作,但不是我需要的 3D - 尽管我可能错过了一些东西。
使用apply_along_axis
的解决方案:
import numpy as np
def pad(data):
good = np.isfinite(data)
interpolated = np.interp(np.arange(data.shape[0]),
np.flatnonzero(good),
data[good])
return interpolated
arr = np.arange(6, dtype=float).reshape((3,2))
arr[1, 1] = np.nan
print(arr)
new = np.apply_along_axis(pad, 0, arr)
print(arr)
print(new)
输出:
[[ 0. 1.]
[ 2. nan]
[ 4. 5.]]
[[ 0. 1.]
[ 2. nan]
[ 4. 5.]]
[[0. 1.]
[2. 3.]
[4. 5.]]
[edit] 第一个提出的解决方案:
对 this answer 中的代码进行了一些修改:
import numpy as np
from scipy import interpolate
A = np.empty((4,4,5))
A[:] = np.nan
A[0] = 1
A[3] = 4
indexes = np.arange(A.shape[0])
good = np.isfinite(A).all(axis=(1, 2))
f = interpolate.interp1d(indexes[good], A[good],
bounds_error=False,
axis=0)
B = f(indexes)
print(B)
给出:
[[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]]
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
[[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]]]
只有当 NaN 都在同一个切片上时它才能正常工作。包含孤立 NaN 的切片将被忽略。
根据 xdze2 和之前 answer here 提供的评论,我想出了这个:
import numpy as np
def pad(data):
bad_indexes = np.isnan(data)
good_indexes = np.logical_not(bad_indexes)
good_data = data[good_indexes]
interpolated = np.interp(bad_indexes.nonzero()[0], good_indexes.nonzero()[0],
good_data)
data[bad_indexes] = interpolated
return data
arr = np.empty((4,4,5))
arr[:] = np.nan
arr[0] = 25
arr[3] = 32.5
# Apply the pad method to each 0 axis
new = np.apply_along_axis(pad, 0, arr)
'pad' 方法实质上应用插值,np.apply_along_axis
方法确保将其应用于 3D 阵列。
我有一个 3D NumPy 数组,如下所示:
arr = np.empty((4,4,5))
arr[:] = np.nan
arr[0] = 1
arr[3] = 4
arr
>>> [[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]]
[[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]
[ nan nan nan nan nan]]
[[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]]]
我想沿 axis=0
进行插值,以便得到以下内容:
>>> [[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2.]]
[[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]
[ 3. 3. 3. 3. 3.]]
[[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]
[ 4. 4. 4. 4. 4.]]]
我一直在查看 SciPy 模块,似乎有方法可以在 1D 和 2D 阵列上执行此操作,但不是我需要的 3D - 尽管我可能错过了一些东西。
使用apply_along_axis
的解决方案:
import numpy as np
def pad(data):
good = np.isfinite(data)
interpolated = np.interp(np.arange(data.shape[0]),
np.flatnonzero(good),
data[good])
return interpolated
arr = np.arange(6, dtype=float).reshape((3,2))
arr[1, 1] = np.nan
print(arr)
new = np.apply_along_axis(pad, 0, arr)
print(arr)
print(new)
输出:
[[ 0. 1.]
[ 2. nan]
[ 4. 5.]]
[[ 0. 1.]
[ 2. nan]
[ 4. 5.]]
[[0. 1.]
[2. 3.]
[4. 5.]]
[edit] 第一个提出的解决方案:
对 this answer 中的代码进行了一些修改:
import numpy as np
from scipy import interpolate
A = np.empty((4,4,5))
A[:] = np.nan
A[0] = 1
A[3] = 4
indexes = np.arange(A.shape[0])
good = np.isfinite(A).all(axis=(1, 2))
f = interpolate.interp1d(indexes[good], A[good],
bounds_error=False,
axis=0)
B = f(indexes)
print(B)
给出:
[[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]]
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
[[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]]]
只有当 NaN 都在同一个切片上时它才能正常工作。包含孤立 NaN 的切片将被忽略。
根据 xdze2 和之前 answer here 提供的评论,我想出了这个:
import numpy as np
def pad(data):
bad_indexes = np.isnan(data)
good_indexes = np.logical_not(bad_indexes)
good_data = data[good_indexes]
interpolated = np.interp(bad_indexes.nonzero()[0], good_indexes.nonzero()[0],
good_data)
data[bad_indexes] = interpolated
return data
arr = np.empty((4,4,5))
arr[:] = np.nan
arr[0] = 25
arr[3] = 32.5
# Apply the pad method to each 0 axis
new = np.apply_along_axis(pad, 0, arr)
'pad' 方法实质上应用插值,np.apply_along_axis
方法确保将其应用于 3D 阵列。