使用 rpy2 创建临时数据帧:内存问题 [英] Create temporary dataframe with rpy2: memory issue

查看:47
本文介绍了使用 rpy2 创建临时数据帧:内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我的上一个类似但更简单.这是我使用 rpy2 从 python 创建 R 数据帧的代码:

This question is similar to but simpler than my previous one. Here is the code that I use to create R dataframes from python using rpy2:

import numpy as np
from rpy2 import robjects

Z = np.zeros((10000, 500))
df = robjects.r["data.frame"]([robjects.FloatVector(column) for column in Z.T])

我的问题是重复使用它会导致巨大的内存消耗.我试图从这里中调整这个想法,但没有成功.如何在不逐渐使用我所有内存的情况下将许多 numpy 数组转换为数据帧以供 R 方法处理?

My problem is that using it repetitively results in huge memory consumption. I tried to adapt the idea from here but without success. How can I convert many numpy arrays to dataframe for treatment by R methods without gradually using all my memory?

推荐答案

您应该确保您使用的是最新版本的 rpy2.使用 rpy2 2.4.2 版,以下效果很好:

You should make sure that you're using the latest version of rpy2. With rpy2 version 2.4.2, the following works nicely:

import gc

import numpy as np
from rpy2 import robjects
from rpy2.robjects.numpy2ri import numpy2ri


for i in range(100):
    print i
    Z = np.random.random(size=(10000, 500))
    matrix = numpy2ri(Z)
    df = robjects.r("data.frame")(matrix)

    gc.collect()

我的计算机上的内存使用量从未超过 600 MB.

Memory usage never exceeds 600 MB on my computer.

这篇关于使用 rpy2 创建临时数据帧:内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆