python rpy2时间序列_使用rpy2将R ListVector转换为Python列表

本文介绍如何使用rpy2库在Python中将R的ListVector转换为Python对象。通过示例展示了在时间序列分析中,如何处理R的ListVector,并提取所需数据。
摘要由CSDN通过智能技术生成

I have a function that takes the items in a Python list, puts them through a function in R, and outputs them as a R ListVector. The problem is that I can't find in the documentation how to convert from a ListVector into a regular Python object. Here's my code:

from rpy2.robjects.packages import importr

from rpy2.robjects import r

forecast = importr("forecast")

parallel = importr("multicore")

data = [[1, 2, 3], [4, 5, 6,], [7, 8, 9]]

tuples = tuple(tuple(x) for x in data)

data_list = []

for i in range(0, len(data)):

result1 = "k = as.numeric((list%r))" % (tuples[i],)

data_list.append(result1)

def forecaster(item):

rcode = item

r(rcode)

rcode1 = 'j

r(rcode1)

rcode2 = 'p

r(rcode2)

rcode3 = 'collect(list(p))'

return r(rcode3)

z = [forecaster(x) for x in data_list]

Running z gives me output like this:

[

[ListVector]

[ListVector],

...And so on. Could someone please help me figure out how to convert these ListVectors into something I can actually use in Python? Thanks.

解决方案

I have done a similar example using rpy2 as follow:

x = [1,2,3,4,1,2,3,4,1,2]

v = robjects.FloatVector(x)

t = robjects.r['ts'](v)

fit = robjects.r['auto.arima'](t)

next = robjects.r['forecast'](fit,h=1)

It is clear to know that I was doing a simple example of using arima to analysis time seires.When I got the next, I found that it was a ListVector.Then I used codes as follow to get the value I wanted.

count = len(next) - 2

#ListVector->FloatVector->Float

print next.rx('mean')[0][0]

Who knows whether this method is effective to you or not, just try it

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值