从 CSV 到 ndarray 和 rpy2,

发布于 2024-10-02 17:31:26 字数 769 浏览 2 评论 0原文

我可以用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-10-09 17:31:26

只是为了从 csv 文件获取 RPY2 DataFrame,在 RPY2.3 中,您可以执行以下操作:

df = robjects.DataFrame.from_csvfile('filename.csv')

Documentation 此处

Just to get an RPY2 DataFrame from a csv file, in RPY2.3, you can just do:

df = robjects.DataFrame.from_csvfile('filename.csv')

Documentation here.

北渚 2024-10-09 17:31:26

我不是100%确定我理解你的问题,但有几点:

1)如果可以,你可以直接将csv读入R,也就是说:

robjects.r('name <- read.csv(filename.csv)')

之后你可以在后面的函数中引用生成的数据帧。

或者 2)您可以将 numpy 数组转换为数据框 - 为此,您需要导入包“rpy2.robjects.numpy2ri”

然后您可以执行以下操作:

array_ex = np.array([[4,3],[3,2], [1,5]])
rmatrix = robjects.r('matrix')
rdf = robjects.r('data.frame')
rlm = robjects.r('lm')

mat_ex = rmatrix(array_ex, ncol = 2)
df_ex = rdf(mat_ex) 
fit_ex = rlm('X1 ~ X2', data = df_ex)

或者您想要的任何其他函数。
可能有一种更直接的方法 - 我在两种数据类型之间切换时感到沮丧,因此如果可能的话,我更有可能使用选项 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:

robjects.r('name <- read.csv(filename.csv)')

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:

array_ex = np.array([[4,3],[3,2], [1,5]])
rmatrix = robjects.r('matrix')
rdf = robjects.r('data.frame')
rlm = robjects.r('lm')

mat_ex = rmatrix(array_ex, ncol = 2)
df_ex = rdf(mat_ex) 
fit_ex = rlm('X1 ~ X2', data = df_ex)

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文