Python SciPy UnivariateSpline与R smooth.spline [英] Python SciPy UnivariateSpline vs R smooth.spline

查看:193
本文介绍了Python SciPy UnivariateSpline与R smooth.spline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将用R编写的脚本移植到Python.在R中,我使用smooth.spline,而在Python中,我使用SciPy UnivariateSpline.它们不会产生相同的结果(即使它们都是基于三次样条方法).有没有一种方法或UnivariateSpline的替代方法,可以使Python样条返回与R相同的样条?

I am porting a script written in R over to Python. In R I am using smooth.spline and in Python I am using SciPy UnivariateSpline. They don't produce the same results (even though they are both based on a cubic spline method). Is there a way, or an alternative to UnivariateSpline, to make the Python spline return the same spline as R?

我是数学家.我了解花键的一般概念.但不是在Python或R中实现它们的详细信息.

I'm a mathematician. I understand the general idea of splines. But not the fine details of their implementation in Python or R.

这是R中的代码,然后是Python.两者的输入数据相同.

Here is the code in R and then Python. The input data is the same for both.

以下是输入数据:

x =  0.0,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1.0
y =   -1,    1,    1,   -1,    1,    0,   .5,   .5,   .4,   .5,   -1

这是R代码

x = seq(0,1, by = .1); 
y = c(-1,1,1, -1,1,0, .5,.5,.4,  .5, -1);
spline_xy = smooth.spline(x,y)
predict(spline_xy,x)

输出:

$x
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

$y
 [1]  0.120614583  0.170800975  0.210954680  0.238032338  0.253672155
 [6]  0.253684815  0.236432643  0.200264536  0.145403302  0.074993797
[11] -0.004853825

这是Python代码

import numpy as np
from scipy.interpolate import UnivariateSpline
x = np.linspace(0, 1, num = 11, endpoint=True)    
y = np.array([-1,1,1, -1,1,0, .5,.5,.4,  .5, -1]) 
spline_xy = UnivariateSpline(x,y)
print('x =', x)
print('ysplined =',spline_xy(x))

输出:

x = [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]

ysplined = 
[-0.26433566 -0.02587413  0.18857809 0.36585082  0.49277389  
  0.55617716 0.54289044  0.43974359  0.23356643 -0.08881119 
 -0.54055944]

我希望R $ y和ysplined在Python中的输出相同.但事实并非如此.

I hoped the outputs, in R $y and in Python ysplined would be identical. But they aren't.

任何帮助,例如如何设置参数或进行解释,将不胜感激!预先谢谢你.

Any help, for example how to set the parameters, or explanations would be appreciated! Thank you in advance.

推荐答案

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

You can use R functions in Python with rpy2:

import numpy as np
import rpy2.robjects as robjects
x = np.linspace(0, 1, num = 11, endpoint=True)    
y = np.array([-1,1,1, -1,1,0, .5,.5,.4,  .5, -1])

r_x = robjects.FloatVector(x)
r_y = robjects.FloatVector(y)
r_smooth_spline = robjects.r['smooth.spline'] #extract R function
spline_xy = r_smooth_spline(x=r_x, y=r_y)
print('x =', x)
print('ysplined =',np.array(robjects.r['predict'](spline_xy,robjects.FloatVector(x)).rx2('y')))

输出:

x = [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
ysplined = [ 0.12061458  0.17080098  0.21095468  0.23803234  0.25367215  0.25368481
0.23643264  0.20026454  0.1454033   0.0749938  -0.00485382]

完全如您所愿.

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

If you want to directly set lambda: spline_xy = 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.

这篇关于Python SciPy UnivariateSpline与R smooth.spline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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