【问题标题】:How do you slice a meshgrid array of points in numpy so that the order is maintained?如何在 numpy 中切片网格点数组以保持顺序?
【发布时间】:2020-01-05 14:29:19
【问题描述】:

我在 numpy 中有一个网格点,我正在尝试对其进行插值。生成它们的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import Rbf

x = np.linspace(-1, 1, 100)[:, None]  # inject trailing singleton for broadcasting
a, b = np.mgrid[0:1:0.1, 0:1:0.1]

#create dose distribution matrix 
D = a.ravel() * x**2 + b.ravel() * x + 1

#perform SVD Decomposition  
U, S, V = np.linalg.svd(D)


fig = plt.figure()
ax = plt.axes(projection="3d")

c0 = V[0]
#ax.scatter(a, b, c0)

#delete some points for testing interpolation
a_trng = a.ravel()
a_trng = np.append(a_trng[0:50], a_trng[70:100])
b_trng = b.ravel()
b_trng = np.append(b_trng[0:50], b_trng[70:100])

a_test = a_trng[50:70]
b_test = b_trng[50:70]


c0_trng = np.append(c0[0:50], c0[70:100])
c0_test = c0[50:70]

rbfi = Rbf(a_trng, b_trng, c0_trng)
ci = rbfi(a, b)
ax.scatter(a_trng, b_trng, c0_trng)
ax.scatter(a_test, b_test, c0_test)

我正在尝试删除一些点,test 点为插值创建 trng 点。然而,```test`` 点被移动了。如何对网格网格进行切片以免发生这种情况?

【问题讨论】:

    标签: python arrays numpy multidimensional-array


    【解决方案1】:

    我认为你应该使用:

    a_test = a.ravel()[50:70]
    b_test = b.ravel()[50:70]
    

    【讨论】:

    • 是的,谢谢,这行得通。但是,为什么它应该与我的版本不同?
    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    相关资源
    最近更新 更多