gpt4 book ai didi

python - 对于几乎相同的数组,插值给出不同的结果

转载 作者:行者123 更新时间:2023-12-01 05:01:15 31 4
gpt4 key购买 nike

我正在尝试插入两个二维数组。

原始数组在下图中以蓝色显示。显示的第二个红色数组与蓝色数组相同,但向上移动并稍微向右移动,并且从右上角删除了一些点。

enter image description here

我使用相同的过程(参见问题结尾)在现有的点之间插入额外的点,我得到了这个结果:

enter image description here

第二个数组(红色)现在已插值,其下部显然是错误的。

为什么会发生这种情况以及如何才能使两种插值都正确?

MWE:

import matplotlib.pyplot as plt

def interp(x, y):
'''
Returns interpolated x, y arrays.
'''
N = 1000
t = np.linspace(0, len(x), N)
xi = np.interp(t, np.arange(len(x)), x)
yi = np.interp(t, np.arange(len(x)), y)
intp = np.asarray([xi, yi])

return intp

# Data
a = [[1.6050000000000004, 1.491999999999999, 1.4189999999999987, 1.3709999999999987, 1.3330000000000002, 1.299999999999999, 1.2650000000000006, 1.229000000000001, 1.186, 1.1319999999999997, 0.9959999999999996, 0.8200000000000003, 0.6580000000000004, 0.30200000000000005, 0.26400000000000023, 0.19399999999999995, 0.125, 0.06899999999999995, 0.03100000000000014, 0.006000000000000005, -0.0020000000000000018, -0.04499999999999993, -0.07400000000000007, -0.08300000000000007, -0.10300000000000004, -0.10799999999999998, -0.121, -0.122, -0.132, -0.13699999999999996, -0.139, -0.14300000000000002, -0.1449999999999999, -0.14700000000000002, -0.14700000000000002, -0.14700000000000002, -0.1439999999999999, -0.14100000000000001, -0.1379999999999999, -0.1379999999999999, -0.137, -0.13599999999999968, -0.13500000000000023, -0.13600000000000012, -0.13999999999999968, -0.14700000000000024, -0.1529999999999998, -0.1589999999999998, -0.1589999999999998, -0.15399999999999991, -0.1469999999999998, -0.14000000000000012, -0.133, -0.125, -0.11800000000000033, -0.1080000000000001, -0.09799999999999986, -0.0950000000000002, -0.08599999999999985, -0.07500000000000018, -0.06099999999999994, -0.040999999999999925, -0.007000000000000117, 0.019000000000000128, 0.05600000000000005, 0.10899999999999999, 0.18599999999999994, 0.28200000000000003, 0.3939999999999997, 0.54, 0.7159999999999997, 0.8460000000000001, 0.9720000000000002, 1.023, 1.101, 1.1420000000000001, 1.178, 1.2109999999999999, 1.243, 1.276, 1.306, 1.3379999999999999, 1.3649999999999998, 1.3309999999999997, 1.297, 1.265, 1.232, 1.1989999999999998, 1.1589999999999998, 1.123, 1.0060000000000002, 0.8420000000000001, 0.7129999999999996, 0.5959999999999996, 0.5219999999999998, 0.45500000000000007, 0.39600000000000035, 0.32099999999999973, 0.2520000000000002, 0.19700000000000006, 0.14700000000000024, 0.10000000000000009, 0.06300000000000017, 0.10400000000000009, 0.15700000000000003, 0.2200000000000002, 0.28600000000000003, 0.3620000000000001, 0.4299999999999997, 0.4979999999999998, 0.5699999999999998, 0.6589999999999998, 0.754, 0.8720000000000003, 1.028, 1.105, 1.1969999999999998, 1.2349999999999999, 1.2679999999999998, 1.3079999999999998, 1.335, 1.3689999999999998, 1.4049999999999998, 1.44, 1.475, 1.5100000000000002, 1.541, 1.573, 1.601, 1.6269999999999998, 1.6509999999999998, 1.6730000000000005, 1.694, 1.6750000000000003, 1.6910000000000003, 1.7069999999999999, 1.706],
[12.488, 11.541, 10.944, 10.473, 10.078, 9.647, 9.269, 8.87, 8.439, 8.026, 7.101, 6.145, 5.359, 3.366, 3.077, 2.737, 2.438, 2.197, 2.008, 1.856, 1.807, 1.438, 1.068, 0.92, 0.525, 0.407, 0.053, 0.032, -0.237, -0.448, -0.493, -0.715, -0.92, -1.015, -1.123, -1.31, -1.493, -1.672, -1.846, -1.862, -1.931, -2.007, -2.049, -2.07, -2.063, -2.018, -1.985, -1.963, -1.955, -1.94, -2.007, -2.163, -2.296, -2.425, -2.55, -2.675, -2.802, -2.844, -2.931, -3.02, -3.113, -3.205, -3.286, -3.319, -3.334, -3.331, -3.308, -3.263, -3.191, -3.087, -2.945, -2.79, -2.563, -2.496, -2.576, -2.684, -2.795, -2.905, -3.007, -3.113, -3.218, -3.316, -3.421, -3.328, -3.23, -3.125, -3.024, -2.928, -2.856, -2.858, -3.119, -3.33, -3.457, -3.546, -3.597, -3.641, -3.68, -3.727, -3.761, -3.787, -3.805, -3.819, -3.837, -3.911, -3.942, -3.946, -3.932, -3.908, -3.88, -3.852, -3.815, -3.767, -3.708, -3.615, -3.442, -3.308, -3.19, -3.23, -3.312, -3.416, -3.477, -3.59, -3.69, -3.793, -3.903, -3.995, -4.071, -4.152, -4.213, -4.268, -4.35, -4.4, -4.452, -4.424, -4.469, -4.494, -4.492]]
b = [[2.0069999999999997, 2.006, 1.9910000000000003, 1.994, 1.9750000000000003, 1.9730000000000005, 1.9509999999999998, 1.9269999999999998, 1.901, 1.873, 1.841, 1.8100000000000003, 0.5200000000000002, 0.457, 0.5860000000000001, 0.4040000000000001, 0.6620000000000001, 1.7750000000000001, 0.7299999999999998, 0.7979999999999998, 0.36300000000000016, 0.4000000000000001, 0.8699999999999999, 0.44700000000000023, 1.74, 0.49700000000000005, 0.9589999999999999, 0.5520000000000003, 0.6209999999999998, 1.054, 1.7049999999999998, 0.6960000000000004, 0.7550000000000001, 1.1720000000000004, 0.8219999999999998, 1.6689999999999998, 0.8959999999999997, 1.635, 1.0129999999999997, 1.328, 1.6649999999999998, 1.6079999999999999, 0.35600000000000004, 0.409, 1.1420000000000001, 1.6309999999999998, 0.3190000000000001, 1.638, 1.5679999999999998, 0.48599999999999993, 1.405, 0.29299999999999987, 0.5820000000000001, 1.597, 1.535, 1.606, 0.25900000000000006, 0.6939999999999997, 1.4969999999999999, 1.565, 1.3060000000000003, 0.23900000000000005, 1.576, 0.8400000000000001, 1.532, 0.2249999999999998, 1.5430000000000001, 1.0159999999999998, 0.21400000000000013, 1.4989999999999999, 1.511, 1.423, 1.4589999999999999, 0.2049999999999998, 0.20200000000000012, 1.478, 1.1460000000000001, 1.4420000000000002, 0.1919999999999999, 1.401, 1.2720000000000002, 0.18199999999999966, 1.323, 0.175, 0.16699999999999998, 0.15999999999999986, 0.16399999999999987, 0.1600000000000003, 0.16499999999999976, 0.15299999999999975, 0.1640000000000003, 0.1530000000000002, 0.14700000000000019, 0.14100000000000018, 0.14100000000000018, 0.14600000000000007, 0.16299999999999998, 0.1620000000000001, 0.1620000000000001, 0.15899999999999997, 0.15600000000000008, 0.15299999999999997, 0.15299999999999997, 0.15299999999999997, 0.15500000000000008, 0.15699999999999997, 0.16099999999999998, 0.16300000000000003, 0.16799999999999998, 0.178, 0.179, 0.192, 0.19699999999999995, 0.21699999999999992, 0.22599999999999992, 0.25500000000000006, 0.298, 0.306, 0.3310000000000001, 0.36899999999999994, 0.425, 0.49399999999999994, 0.5640000000000003, 0.6020000000000001, 0.9580000000000004, 1.1200000000000003, 1.2959999999999996, 1.4319999999999997],
[8.836, 8.838000000000001, 8.861, 8.878, 8.906, 8.93, 8.98, 9.062000000000001, 9.117, 9.178, 9.259, 9.335, 9.384, 9.388, 9.398, 9.419, 9.422, 9.427, 9.45, 9.478, 9.493, 9.511, 9.515, 9.525, 9.536999999999999, 9.543, 9.563, 9.568999999999999, 9.603, 9.622, 9.64, 9.65, 9.689, 9.715, 9.733, 9.74, 9.784, 9.853, 9.873000000000001, 9.888, 9.909, 9.914, 9.996, 9.999, 10.0, 10.002, 10.011, 10.014, 10.018, 10.022, 10.022, 10.044, 10.067, 10.1, 10.1, 10.112, 10.125, 10.139, 10.14, 10.205, 10.211, 10.217, 10.217, 10.243, 10.306000000000001, 10.31, 10.323, 10.385, 10.399000000000001, 10.402000000000001, 10.425, 10.472, 10.474, 10.486, 10.528, 10.535, 10.54, 10.646, 10.655000000000001, 10.754, 10.767, 10.780000000000001, 10.834, 10.905000000000001, 11.034, 11.167, 11.26, 11.267, 11.281, 11.312000000000001, 11.323, 11.323, 11.345, 11.367, 11.375, 11.39, 11.399000000000001, 11.468, 11.484, 11.658, 11.837, 12.02, 12.207, 12.315, 12.41, 12.615, 12.837, 12.882, 13.093, 13.362, 13.383000000000001, 13.737, 13.855, 14.25, 14.398, 14.768, 15.137, 15.186, 15.338000000000001, 15.527000000000001, 15.768, 16.067, 16.407, 16.696, 18.689, 19.475, 20.431, 21.356]]

f, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_title('Not interpolated')
ax1.scatter(a[0], a[1], s=30, c='blue', lw=0.1)
ax1.scatter(b[0], b[1], s=30, c='red', lw=0.1)

# Get interpolated arrays.
a_i = interp(*a)
b_i = interp(*b)

ax2.set_title('Interpolated')
ax2.scatter(a_i[0], a_i[1], s=30, c='blue', lw=0.1)
ax2.scatter(b_i[0], b_i[1], s=30, c='red', lw=0.1)

plt.show()

最佳答案

代码中的问题不是插值函数,而是数据本身。 b 中的点未按“正确”顺序排列,即,如果您通过变量 t 参数化曲线,则 b 中的点不按 t 递增的顺序。

如果将 b 视为由某个向量翻译的 a 并删除一些元素,您可以看到这一点:

print np.array(a).shape, np.array(b).shape
b = np.array(a) + np.array([[2, 3]]).T
b = b[:, [x for x in range(len(b[0])) if x < 100 or (x > 100 and x % 2)]]
print np.array(a).shape, np.array(b).shape

关于python - 对于几乎相同的数组,插值给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26131383/

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