HDF存储:表格.选择还有拉姆乌萨格

2024-09-29 23:25:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从大约1GB的HDFStore表中随机选择行。当我请求大约50个随机行时,内存使用量会爆炸式增长。在

{1>使用熊猫。在

在第一种情况下,RAM的使用符合chunk的大小

with pd.get_store("train.h5",'r') as train:
for chunk in train.select('train',chunksize=50):
    pass

在第二种情况下,似乎整个表都被加载到RAM中

^{pr2}$

在最后一个例子中,RAM的使用符合chunk大小

r=random.choice(400000,size=30,replace=False)    
train.select('train',pd.Term("index",r))

我很不解,为什么从30行随机移动到40行会导致RAM使用量急剧增加。在

注意:创建表时,已使用以下代码索引index=range(nrows(table)):

def txtfile2hdfstore(infile, storefile, table_name, sep="\t", header=0, chunksize=50000 ):
    max_len, dtypes0 = txtfile2dtypes(infile, sep, header, chunksize)

    with pd.get_store( storefile,'w') as store:
        for i, chunk in enumerate(pd.read_table(infile,header=header,sep=sep,chunksize=chunksize, dtype=dict(dtypes0))):
            chunk.index= range( chunksize*(i), chunksize*(i+1))[:chunk.shape[0]]
            store.append(table_name,chunk, min_itemsize={'values':max_len})

谢谢你的洞察力

编辑回答Zelazny7

这是我以前写的文件火车.csv训练h5。我是用Zelazny7的代码元素从How to trouble-shoot HDFStore Exception: cannot find the correct atom type编写的

import pandas as pd
import numpy as np
from sklearn.feature_extraction import DictVectorizer


def object_max_len(x):
    if x.dtype != 'object':
        return
    else:
        return len(max(x.fillna(''), key=lambda x: len(str(x))))

def txtfile2dtypes(infile, sep="\t", header=0, chunksize=50000 ):
    max_len = pd.read_table(infile,header=header, sep=sep,nrows=5).apply( object_max_len).max()
    dtypes0 = pd.read_table(infile,header=header, sep=sep,nrows=5).dtypes

    for chunk in pd.read_table(infile,header=header, sep=sep, chunksize=chunksize):
        max_len = max((pd.DataFrame(chunk.apply( object_max_len)).max(),max_len))
        for i,k in enumerate(zip( dtypes0[:], chunk.dtypes)):
            if (k[0] != k[1]) and (k[1] == 'object'):
                dtypes0[i] = k[1]
    #as of pandas-0.11 nan requires a float64 dtype
    dtypes0.values[dtypes0 == np.int64] = np.dtype('float64')
    return max_len, dtypes0


def txtfile2hdfstore(infile, storefile, table_name, sep="\t", header=0, chunksize=50000 ):
    max_len, dtypes0 = txtfile2dtypes(infile, sep, header, chunksize)

    with pd.get_store( storefile,'w') as store:
        for i, chunk in enumerate(pd.read_table(infile,header=header,sep=sep,chunksize=chunksize, dtype=dict(dtypes0))):
            chunk.index= range( chunksize*(i), chunksize*(i+1))[:chunk.shape[0]]
            store.append(table_name,chunk, min_itemsize={'values':max_len})

应用于

txtfile2hdfstore('Train.csv','train.h5','train',sep=',')

Tags: storeinforlenastabletrainmax
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:42

这是一个已知问题,请参阅此处的参考文献:https://github.com/pydata/pandas/pull/2755

实际上,查询被转换成一个numexpr表达式进行计算。有一个问题 我不能将很多or条件传递给numexpr(它依赖于 生成的表达式)。在

所以我只限制传递给numexpr的表达式。如果它超过一定数量的or条件,那么查询将作为过滤器而不是内核内选择来完成。基本上,这意味着表被读取,然后重新编制索引。在

这是我的配件清单:https://github.com/pydata/pandas/issues/2391(17)。在

作为一种解决方法,只需将查询拆分为多个查询并合并结果。应该快得多,并且使用恒定的内存

相关问题 更多 >

    热门问题