栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用scipy.interpole.interpn公司插值N维数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用scipy.interpole.interpn公司插值N维数组

我会试着用2D向你解释这件事,这样你就能更好地了解事情的来龙去脉
正在发生。首先,让我们创建一个线性阵列来进行测试。

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cm# Set up grid and array of valuesx1 = np.arange(10)x2 = np.arange(10)arr = x1 + x2[:, np.newaxis]# Set up grid for plottingX, Y = np.meshgrid(x1, x2)# Plot the values as a surface plot to depictfig = plt.figure()ax = fig.gca(projection='3d')surf = ax.plot_surface(X, Y, arr, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, alpha=0.8)fig.colorbar(surf, shrink=0.5, aspect=5)

然后,假设你想沿着一条线插值,也就是说,沿着一个点第一维度,但所有点都沿第二维度。这些要点
显然不在原始数组

(x1,x2)
中。假设我们想插值到“x1=3.5”点,该点位于x1轴。

from scipy.interpolate import interpninterp_x = 3.5# only one value on the x1-axisinterp_y = np.arange(10) # A range of values on the x2-axis# Note the following two lines that are used to set up the# interpolation points as a 10x2 array!interp_mesh = np.array(np.meshgrid(interp_x, interp_y))interp_points = np.rollaxis(interp_mesh, 0, 3).reshape((10, 2))# Perform the interpolationinterp_arr = interpn((x1, x2), arr, interp_points)# Plot the resultax.scatter(interp_x * np.ones(interp_y.shape), interp_y, interp_arr, s=20,c='k', depthshade=False)plt.xlabel('x1')plt.ylabel('x2')plt.show()

这会得到所需的结果:请注意,黑点的位置正确
在平面上,x1值为3.5。!【插值曲面图】
[分数](https://i.stack.imgur.com/AKDzK.png)
请注意,大多数的“魔力”,以及你的问题的答案,在于这些
两条线:

interp_mesh = np.array(np.meshgrid(interp_x, interp_y))interp_points = np.rollaxis(interp_mesh, 0, 3).reshape((10, 2))

我已经解释了它的工作原理
其他地方. 总之,这是什么
做的是创建一个大小为10x2的数组,包含10的坐标
要在“arr”处插入的点。(唯一的区别是
我为这篇文章写了一个解释

np.mgrid公司
,其中
是写作的捷径
np.网格
为了一群阿兰奇)
对于您的4x4案例,您可能需要以下内容:

interp_mesh = np.meshgrid([0.1], [9], np.linspace(0, 30, 3),    np.linspace(0, 0.3, 4))interp_points = np.rollaxis(interp_mesh, 0, 5)interp_points = interp_points.reshape((interp_mesh.size // 4, 4))result = interpn(points, arr, interp_points)

Hope that helps!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号