convert numpy array to Matrix rpy2, Kmeans

拥有回忆 提交于 2019-12-03 20:49:12

From the rpy2 docs, R matrices are just vectors with their dim attribute set. So for a numpy two-dimensional array x

import rpy2.robjects as robj

nr, nc = x.shape
xvec = robj.FloatVector(x.transpose().reshape((x.size))
xr = robj.r.matrix(xvec, nrow=nr, ncol=nc)

You have to transpose the numpy array because R fills matrices by columns.

Edit: Actually, you could just set byrow=True in the R matrix function, and then you wouldn't need to transpose.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!