Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 磁盘上的稀疏数据帧比密集版本大_Python_Pandas_Sparse Matrix_Sparse Array - Fatal编程技术网

Python 磁盘上的稀疏数据帧比密集版本大

Python 磁盘上的稀疏数据帧比密集版本大,python,pandas,sparse-matrix,sparse-array,Python,Pandas,Sparse Matrix,Sparse Array,我发现数据帧的稀疏版本在保存到磁盘时实际上比密集版本大得多。我做错了什么 test = pd.DataFrame(ones((4,4000))) test.ix[:,:] = nan test.ix[0,0] = 47 test.to_hdf('test3', 'df') test.to_sparse(fill_value=nan).to_hdf('test4', 'df') test.to_pickle('test5') test.to_sparse(fill_value=nan).to_

我发现数据帧的稀疏版本在保存到磁盘时实际上比密集版本大得多。我做错了什么

test = pd.DataFrame(ones((4,4000)))
test.ix[:,:] = nan
test.ix[0,0] = 47

test.to_hdf('test3', 'df')
test.to_sparse(fill_value=nan).to_hdf('test4', 'df')

test.to_pickle('test5')
test.to_sparse(fill_value=nan).to_pickle('test6')

....
ls -sh test*
200K test3   16M test4  164K test5  516K test6
使用版本0.12.0

我最终希望以大约10%的密度高效地存储10^7×60个阵列,然后将它们拉入熊猫数据帧并使用它们


编辑:谢谢杰夫回答了原来的问题。后续问题:这似乎只为酸洗节省了成本,而在使用其他格式(如HDF5)时则没有。酸洗是我最好的路线吗

print shape(array_activity) #This is just 0s and 1s
(1020000, 60)

test = pd.DataFrame(array_activity)
test_sparse = test.to_sparse()
print test_sparse.density
0.0832333496732

test.to_hdf('1', 'df')
test_sparse.to_hdf('2', 'df')
test.to_pickle('3')
test_sparse.to_pickle('4')
!ls -sh 1 2 3 4
477M 1  544M 2  477M 3   83M 4

这些数据作为Matlab.mat文件中的索引列表,小于12M。我很想把它转换成HDF5/Pytables格式,这样我就可以只获取特定的索引(其他文件要大得多,加载到内存中的时间要长得多),然后随时对它们进行Pandasy操作。也许我的方法不对?

您正在创建一个包含4000列、只有4行的框架;稀疏是按行处理的,因此将维度反转

In [2]: from numpy import *

In [3]: test = pd.DataFrame(ones((4000,4)))

In [4]: test.ix[:,:] = nan

In [5]: test.ix[0,0] = 47

In [6]: test.to_hdf('test3', 'df')

In [7]: test.to_sparse(fill_value=nan).to_hdf('test4', 'df')

In [8]: test.to_pickle('test5')

In [9]: test.to_sparse(fill_value=nan).to_pickle('test6')

In [11]: !ls -sh test3 test4 test5 test6
164K test3  148K test4  160K test5   36K test6
后续行动。您提供的存储是以
table
格式编写的,因此保存了密集版本(对于非常灵活且可查询的表格式,不支持稀疏,请参阅)

此外,您可能希望尝试使用稀疏格式的两种不同表示形式保存文件

下面是一个示例会话:

df = 
In [1]: df = pd.read_hdf('store_compressed.h5','test')

In [2]: type(df)
Out[2]: pandas.core.frame.DataFrame

In [3]: df.to_sparse(kind='block').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [4]: df.to_sparse(kind='integer').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [5]: df.to_sparse(kind='block').to_hdf('test_block.h5','test',mode='w',complib='blosc',complevel=9)

In [6]: df.to_sparse(kind='integer').to_hdf('test_integer.h5','test',mode='w',complib='blosc',complevel=9)

In [7]: df.to_hdf('test_dense_fixed.h5','test',mode='w',complib='blosc',complevel=9)

In [8]: df.to_hdf('test_dense_table.h5','test',mode='w',format='table',complib='blosc',complevel=9)

In [9]: !ls -ltr *.h5
-rwxrwxr-x 1 jreback users 57015522 Feb  6 18:19 store_compressed.h5
-rw-rw-r-- 1 jreback users 30335044 Feb  6 19:01 test_block.h5
-rw-rw-r-- 1 jreback users 28547220 Feb  6 19:02 test_integer.h5
-rw-rw-r-- 1 jreback users 44540381 Feb  6 19:02 test_dense_fixed.h5
-rw-rw-r-- 1 jreback users 57744418 Feb  6 19:03 test_dense_table.h5
IIRC是0.12中的一个bug,因为
to_hdf
无法传递所有参数,因此您可能希望使用:

with get_store('test.h5',mode='w',complib='blosc',complevel=9) as store:
    store.put('test',df)
它们基本上存储为
SparseSeries
的集合,因此如果密度低且不连续,那么它就不会像大小一样小。Pandas sparse suite可以更好地处理数量较少的连续块,尽管YMMV.scipy也提供了一些稀疏处理工具

尽管在IMHO中,这些对于HDF5文件来说都是非常小的大小,但无论如何,您可以处理大量的行;并且可以轻松处理大小为10和100 GB的文件(尽管推荐)


此外,如果你可以查询的话,你可以考虑使用表格式。< /P>添加压缩过滤器,请参阅这里:用一个稠密的数据框和FraceFr==9和AuthBuil=’BLoSC’,将我们从544米降低到26m。更好的是,但仍然不能跟上12m。尝试用稀疏数据框进行压缩会引发一个Type错误。r:

TypeError:无法为以下内容正确创建存储程序:[[u TABLE\u MAP][group->/test\u sparse(group)”,value->,TABLE->True,append->True,kwargs->{'encoding':None}]
hmm…。格式不正确;它应该使用TABLE=False保存;但这也是默认值。让我看看。你能发布保存的帧吗(以密集格式是可以的),请压缩!比如说一个dropbox链接?给你: