python - from CSV to ndarray, and rpy2, -
i can make numpy ndarrays 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, have no idea how take r data frame usable rpy2,
p = roptim(theta,robjects.r['ols'],method="bfgs",hessian=true ,y= robjects.floatvector(y),x = base.matrix(x)) valueerror: nothing can done type <class 'numpy.core.records.recarray'> @ moment. p = roptim(theta,robjects.r['ols'],method="bfgs",hessian=true ,y= robjects.floatvector(y),x = base.matrix(array(x))) valueerror: nothing can done type <type 'numpy.ndarray'> @ moment.
i'm not 100% sure understand issue, couple things:
1) if it's ok, can read csv r directly, is:
robjects.r('name <- read.csv(filename.csv)')
after can refer resulting data frame in later functions.
or 2) can convert numpy array data frame - need import package 'rpy2.robjects.numpy2ri'
then 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 wanted. there may more direct way - frustrated going between 2 data types , more use option 1) if possible.
would either of these methods need be?
Comments
Post a Comment