是否有一个Python等效于R中的smooth.spline函数 [英] Is there a Python equivalent to the smooth.spline function in R

查看:715
本文介绍了是否有一个Python等效于R中的smooth.spline函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中的smooth.spline函数允许在粗糙度(由二阶导数的积分平方定义)和拟合点(由残差平方求和定义)之间进行权衡.这种权衡是通过spar或df参数完成的.在一个极端处,您得到的线最少,而在另一端处,您得到的曲线很摆动,与所有数据点相交(或者如果x值重复且y值不同,则平均值).

The smooth.spline function in R allows a tradeoff between roughness (as defined by the integrated square of the second derivative) and fitting the points (as defined by summing the squares of the residuals). This tradeoff is accomplished by the spar or df parameter. At one extreme you get the least squares line, and the other you get a very wiggly curve which intersects all of the data points (or the mean if you have duplicated x values with different y values)

我已经看过python中的scipy.interpolate.UnivariateSpline和其他样条线变体,但是,它们似乎只能通过增加结数并为允许的SS残差设置阈值(称为s)来权衡.相比之下,R中的smooth.spline允许在所有x值处都具有结,而不必具有摆动到所有点的弯曲曲线-惩罚来自二阶导数.

I have looked at scipy.interpolate.UnivariateSpline and other spline variants in Python, however, they seem to only tradeoff by increasing the number of knots, and setting a threshold (called s) for the allowed SS residuals. By contrast, the smooth.spline in R allows having knots at all the x values, without necessarily having a wiggly curve that hits all the points -- the penalty comes from the second derivative.

Python是否具有以这种方式运行的样条拟合机制?允许所有的麻烦,但要惩罚二阶导数吗?

Does Python have a spline fitting mechanism that behaves in this way? Allowing all knots but penalizing the second derivative?

推荐答案

您可以在rpy2的Python中使用R函数:

You can use R functions in Python with rpy2:

import rpy2.robjects as robjects
r_y = robjects.FloatVector(y_train)
r_x = robjects.FloatVector(x_train)

r_smooth_spline = robjects.r['smooth.spline'] #extract R function# run smoothing function
spline1 = r_smooth_spline(x=r_x, y=r_y, spar=0.7)
ySpline=np.array(robjects.r['predict'](spline1,robjects.FloatVector(x_smooth)).rx2('y'))
plt.plot(x_smooth,ySpline)

如果要直接设置lambda:spline1 = r_smooth_spline(x=r_x, y=r_y, lambda=42)不起作用,因为lambda在Python中已经具有另一种含义,但是有一个解决方案:

If you want to directly set lambda: spline1 = r_smooth_spline(x=r_x, y=r_y, lambda=42) doesn't work, because lambda has already another meaning in Python, but there is a solution: How to use the lambda argument of smooth.spline in RPy WITHOUT Python interprating it as lambda.

要运行代码,首先需要定义数据x_trainy_train,如果要在全高清分辨率下将其绘制在-3和5之间,则可以定义x_smooth=np.array(np.linspace(-3,5,1920)). .

To get the code running you first need to define the data x_train and y_train and you can define x_smooth=np.array(np.linspace(-3,5,1920)). if you want to plot it between -3 and 5 in Full-HD-resolution.

这篇关于是否有一个Python等效于R中的smooth.spline函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆