Python 如何重塑numpy阵列以与scipy插值一起使用?
我有3个numpy阵列,具有long、lat和rain值:Python 如何重塑numpy阵列以与scipy插值一起使用?,python,arrays,numpy,scipy,Python,Arrays,Numpy,Scipy,我有3个numpy阵列,具有long、lat和rain值: [-7.4989786799999996, -7.5215012000000003, -7.4764561599999997, -7.4989786799999996, -7.5215012000000003, -7.4764561599999997] [41.90415308, 41.90415308, 41.90415308, 41.881630559999998, 41.881630559999998, 41.881630559
[-7.4989786799999996, -7.5215012000000003, -7.4764561599999997, -7.4989786799999996, -7.5215012000000003, -7.4764561599999997]
[41.90415308, 41.90415308, 41.90415308, 41.881630559999998, 41.881630559999998, 41.881630559999998]
[0.020928397800000002, 0.0299166963, 0.0171956848, 0.0340920761, 0.0429551788, 0.0301877651]
Scipy插值需要1个具有3个不同LON的数组,另一个具有2个不同LON的数组和一个3x2 rain值数组。我如何重塑它们?听起来你想要一个二维插值 例如,让我们使用一些与您类似的随机数据并绘制它:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1) # Make example exactly reproducable
num_points = 20
lon = 0.1 * np.random.random(num_points) - 7.5
lat = 0.1 * np.random.random(num_points) + 41.8
z = 0.05 * np.random.random(num_points)
fig, ax = plt.subplots()
artist = ax.scatter(lon, lat, c=z, s=200, cmap='gist_earth')
fig.colorbar(artist)
ax.axis('tight')
plt.show()
在这种情况下,插值算法的最佳选择是某种径向基函数(例如样条曲线)。在scipy中,这是scipy.interpolate.Rbf
:
import scipy.interpolate
interp = scipy.interpolate.Rbf(lon, lat, z, function='linear')
# 20x20 grid of points to interpolate on
yy, xx = np.mgrid[lat.min():lat.max():20j, lon.min():lon.max():20j]
zi = interp(xx, yy)
# Plot the results
fig, ax = plt.subplots()
artist = ax.scatter(lon, lat, c=z, s=100, cmap='gist_earth',
vmin=zi.min(), vmax=zi.max())
ax.pcolormesh(xx, yy, zi, cmap='gist_earth')
ax.axis('tight')
plt.show()
听起来你想要一个2D插值 例如,让我们使用一些与您类似的随机数据并绘制它:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1) # Make example exactly reproducable
num_points = 20
lon = 0.1 * np.random.random(num_points) - 7.5
lat = 0.1 * np.random.random(num_points) + 41.8
z = 0.05 * np.random.random(num_points)
fig, ax = plt.subplots()
artist = ax.scatter(lon, lat, c=z, s=200, cmap='gist_earth')
fig.colorbar(artist)
ax.axis('tight')
plt.show()
在这种情况下,插值算法的最佳选择是某种径向基函数(例如样条曲线)。在scipy中,这是scipy.interpolate.Rbf
:
import scipy.interpolate
interp = scipy.interpolate.Rbf(lon, lat, z, function='linear')
# 20x20 grid of points to interpolate on
yy, xx = np.mgrid[lat.min():lat.max():20j, lon.min():lon.max():20j]
zi = interp(xx, yy)
# Plot the results
fig, ax = plt.subplots()
artist = ax.scatter(lon, lat, c=z, s=100, cmap='gist_earth',
vmin=zi.min(), vmax=zi.max())
ax.pcolormesh(xx, yy, zi, cmap='gist_earth')
ax.axis('tight')
plt.show()
我的问题是,我必须考虑3个不同的连接数组。尝试重塑我的问题是,我必须考虑3个不同的连接数组。非常清楚(和仔细地解释)。然而,我的问题是关于阵列重塑。我也做了一些努力。@Hugo-你用的是哪种插值函数?对于大多数阵列,您不需要重新调整阵列的形状。我正在使用RectBivariateSpline@Hugo-矩形双变量样条线不适用于你的情况。它用于插值已经在常规网格上的数据。你大概在处理“分散”的输入数据。不。我处理的是“矩形”网格,而不是“四边形”网格。纬度和经度之间的距离是不同的。非常清楚(谨慎)的解释。然而,我的问题是关于阵列重塑。我也做了一些努力。@Hugo-你用的是哪种插值函数?对于大多数阵列,您不需要重新调整阵列的形状。我正在使用RectBivariateSpline@Hugo-矩形双变量样条线不适用于你的情况。它用于插值已经在常规网格上的数据。你大概在处理“分散”的输入数据。不。我处理的是“矩形”网格,而不是“四边形”网格。经纬度之间的距离是不同的。