码迷,mamicode.com
首页 > 其他好文 > 详细

numpy的一维线性插值函数

时间:2018-06-05 21:19:22      阅读:924      评论:0      收藏:0      [点我收藏+]

标签:lines   图像   代码   pre   连接   port   没有   官方   生成   

前言:
? ? ?在用生成对抗网络生成二维数据点的时候遇到代码里的一个问题,就是numpy中的一维线性插值函数interp到底是怎么用的,在这个上面费了点功夫,因此现将其用法给出。
? ? ?在生成对抗网络的二维样本生成的例子中,涉及了一维线性插值,代码里使用的是:

numpy.interp(x, xp, fp, left=None, right=None, period=None)

上网查了百度和谷歌发现都没有具体的中文的解释,只有官方的英文解释:

\(One-dimensional\) \(linear\) \(interpolation.\) \(Returns\) \(the\) \(one-dimensional\) \(piecewise\) \(linear\) \(interpolant\) \(to\) \(a\) \(function\) \(with\) \(given\) \(values\) \(at\) \(discrete\) \(data-points.\)
官方给出的例子如下:

 >>> xp = [1, 2, 3]
>>> fp = [3, 2, 0]
>>> np.interp(2.5, xp, fp)
1.0
>>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
array([ 3. ,  3. ,  2.5 ,  0.56,  0. ])
>>> UNDEF = -99.0
>>> np.interp(3.14, xp, fp, right=UNDEF)
-99.0

Plot an interpolant to the sine function:

>>> x = np.linspace(0, 2*np.pi, 10)
>>> y = np.sin(x)
>>> xvals = np.linspace(0, 2*np.pi, 50)
>>> yinterp = np.interp(xvals, x, y)
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(xvals, yinterp, '-x')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.show()      

其中对于第一例子,只要画出图像就很好理解了:
技术分享图片
也就是说只要参数中的\(2.5\)是我们要插入的值,我们要做的是连接\((2,2)\)\((3,0)\)这两个点,然后在\(x=2.5\)这里做垂线,那么相交的那个点(也就是\((2.5,1.0)\)这个点)就是我们要插入的点了。

numpy的一维线性插值函数

标签:lines   图像   代码   pre   连接   port   没有   官方   生成   

原文地址:https://www.cnblogs.com/Stoner/p/9141464.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!