开发者

convert numpy array to Matrix rpy2, Kmeans

I have a numpy 2D array self.sub and i want to use it in rpy2 kmeans. k = r开发者_如何学运维objects.r.kmeans(self.sub,2,20) i always get the following error: valueError: nothing can be done for the type at the moment! what can i do?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜