从 CSV 到 ndarray 和 rpy2,
我可以用rec2csv制作numpy ndarrays,
data = recfromcsv(dataset1, names=True)
xvars = ['exp','exp_sqr','wks','occ','ind','south','smsa','ms','union','ed','fem','blk']
y = data['lwage']
X = data[xvars]
c = ones_like(data['lwage'])
X = add_field(X, 'constant', c)
但是,我不知道如何将其放入Rpy2可用的R数据框中,
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(X))
ValueError: Nothing can be done for the type <class 'numpy.core.records.recarray'> at the moment.
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(array(X)))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.
I can make numpy ndarrays with rec2csv,
data = recfromcsv(dataset1, names=True)
xvars = ['exp','exp_sqr','wks','occ','ind','south','smsa','ms','union','ed','fem','blk']
y = data['lwage']
X = data[xvars]
c = ones_like(data['lwage'])
X = add_field(X, 'constant', c)
But, I have no idea how to take this into an R data frame usable by Rpy2,
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(X))
ValueError: Nothing can be done for the type <class 'numpy.core.records.recarray'> at the moment.
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(array(X)))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了从 csv 文件获取 RPY2 DataFrame,在 RPY2.3 中,您可以执行以下操作:
Documentation 此处。
Just to get an RPY2 DataFrame from a csv file, in RPY2.3, you can just do:
Documentation here.
我不是100%确定我理解你的问题,但有几点:
1)如果可以,你可以直接将csv读入R,也就是说:
之后你可以在后面的函数中引用生成的数据帧。
或者 2)您可以将 numpy 数组转换为数据框 - 为此,您需要导入包“rpy2.robjects.numpy2ri”
然后您可以执行以下操作:
或者您想要的任何其他函数。
可能有一种更直接的方法 - 我在两种数据类型之间切换时感到沮丧,因此如果可能的话,我更有可能使用选项 1)。
这些方法中的任何一种都能让您到达您需要的地方吗?
I'm not 100% sure I understand your issue, but a couple things:
1) if it's ok, you can read a csv into R directly, that is:
After which you can refer to the resulting data frame in later functions.
Or 2) you can convert a numpy array into a data frame - to do this you need to import the package 'rpy2.robjects.numpy2ri'
Then you could do something like:
Or whatever other functions you wanted.
There may be a more direct way - I get frustrated going between the two data types and so I am much more likely to use option 1) if possible.
Would either of these methods get you to where you need to be?