【问题标题】:scipy interpolation is not smoothing my datascipy 插值没有平滑我的数据
【发布时间】:2016-03-02 13:34:07
【问题描述】:

我正在尝试在 scipy 中使用插值。这是我的代码:

from Constants import LOWER_LAT, LOWER_LONG, UPPER_LAT, UPPER_LONG, GRID_RESOLUTION

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
from cmath import sin
from scipy.signal.windows import cosine
from scipy import interpolate
from scipy.interpolate import RectBivariateSpline
import numpy
from numpy import meshgrid

#===============================================================
y_range = GRID_RESOLUTION
delta = (UPPER_LAT - LOWER_LAT)/float(GRID_RESOLUTION)
x_range = int((UPPER_LONG - LOWER_LONG)/delta) + 1
x = numpy.linspace(0,x_range-1,x_range)
y = numpy.linspace(0,y_range-1,y_range)
X,Y = meshgrid(x,y)
Z = numpy.zeros((y.size, x.size))
base_val = 0

# fill values for Z
with open('map.txt','rb') as fp:
    for line in fp:
        parts = line[:-1].split("\t")
        tup = parts[0]
        tup = tup[:-1]
        tup = tup[1:]
        yx = tup.strip().replace(" ","").split(",")
        y_val = int(yx[0])
        x_val = int(yx[1])
        h_val = int(parts[-1])

        for i in range(y_range):
            tx = X[i];
            ty = Y[i];
            tz = Z[i];
            for j in range(x_range):
                if (int(tx[j])==x_val) and (int(ty[j])==y_val):
                    tz[j] = h_val + base_val
Z = numpy.array(Z)

# spline = RectBivariateSpline(y, x, Z)
# Z2 = spline(y, x)
f = interpolate.interp2d(x, y, Z,'cubic')
Z2 = f(x,y)


# Plot here
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, alpha=0.3, cmap='Accent')
ax.set_xlabel('X')
ax.set_xlim(0, 50)
ax.set_ylabel('Y')
ax.set_ylim(0, 50)
ax.set_zlabel('Z')
# ax.set_zlim(0, 1000)
plt.show()

以下是上述代码顶部的一些常量:

LOWER_LAT = 32.5098
LOWER_LONG = -84.7485
UPPER_LAT = 47.5617
UPPER_LONG = -69.1699
GRID_RESOLUTION = 50

我的代码创建一维数组xy,然后使用函数meshgrid 创建网格。 Z 中的值是从您可以找到 here 的文本文件中填充的。文本文件中每一行的格式为(y_value,x_value) z_value。 创建网格并插入函数后,我绘制它。但是,我得到的数字与我没有插值得到的数字相同。具体来说,这两行产生了相同的图形:

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.3, cmap='Accent')
ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, alpha=0.3, cmap='Accent')

在上面的行中,Z2 的值来自插值函数,Z 的值是原始值。 我怎样才能使插值工作? 这是图。

【问题讨论】:

    标签: python numpy scipy interpolation smoothing


    【解决方案1】:

    我认为您将smoothinginterpolation 混淆了。

    在这两种情况下,您都在拟合产生输入数据的连续近似值的函数。但是,在插值的情况下,插值被限制为精确地通过您的输入点,而在平滑时,这个约束被放松了。

    在上面的示例中,您执行的是 插值 而不是平滑。由于您在与原始数据完全相同的输入点网格上评估插值,因此保证Z2Z 几乎完全相同。进行插值的目的是让您可以在给定一组 不同 x 值和 y 值(例如,间距更细的网格)的情况下评估近似 z 值。

    如果您想执行 平滑 而不是插值,您可以尝试将非零值作为 s= 参数传递给 RectBivariateSpline,例如:

    spline = RectBivariateSpline(y, x, Z, s=5E7)
    Z2 = spline(y, x)
    
    fig, ax = plt.subplots(1, 2, sharex=True, sharey=True,
                           subplot_kw={'projection':'3d'})
    
    ax[0].plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.3, cmap='Accent')
    ax[1].plot_surface(X, Y, Z2, rstride=1, cstride=1, alpha=0.3, cmap='Accent')
    ax[0].set_title('Original')
    ax[1].set_title('Smoothed')
    
    fig.tight_layout()
    plt.show()
    

    【讨论】:

    • 谢谢你,正是我想要的,事实上我混淆了这些概念。你能解释一下'5E7'的含义或给我一个参考吗?
    • 这只是 50000000 的科学记数法,即 5 x 10^7
    • 我看到你用链接做了什么
    猜你喜欢
    • 2023-04-07
    • 2022-01-13
    • 2016-06-18
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多