题
我可以用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.
解决方案
我不是100%确定我理解您的问题,但是有几件事:
1)如果可以的话,您可以直接读取CSV,也就是说:
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)。
这些方法中的任何一个都可以带您到需要的地方吗?
其他提示
不隶属于 StackOverflow