gpt4 book ai didi

python - csv转二进制文件异常减小文件大小

转载 作者:太空狗 更新时间:2023-10-30 00:01:13 24 4
gpt4 key购买 nike

我有 csv 大小为 5.2GB 的数据集(取自 here )。它有大约 700 万行 dimension = 29。这些值的类型为 float64。我想将此数据集转换为二进制文件。为此,我执行以下简单几行:

import numpy as np
import pandas as pd

df = pd.read_csv('data.csv', sep=',')
np.asarray(df.values).tofile('data_binary.dat')

数据的快照如下所示:

0.000000000000000000e+00,9.439358860254287720e-02,1.275558676570653915e-02,9.119330644607543945e-01,-9.083136916160583496e-02,-2.335745543241500854e-01,-1.054220795631408691e+00,-9.759366512298583984e-01,-1.067278265953063965e+00,-6.138502955436706543e-01,7.542607188224792480e-01,-9.256605505943298340e-01,-5.289512276649475098e-01,1.235263347625732422e+00,8.606486320495605469e-01,-2.320102453231811523e-01,-4.043335020542144775e-01,-1.559396624565124512e+00,-8.154401183128356934e-01,-1.376865267753601074e+00,6.759096682071685791e-02,1.372575879096984863e+00,-5.736824870109558105e-01,-1.368692040443420410e+00,-4.793794453144073486e-01,1.529256343841552734e+00,-5.757816433906555176e-01,-1.290232419967651367e+00,4.999999694824218750e+02
1.000000000000000000e+00,3.272003531455993652e-01,-2.395536154508590698e-01,-1.592038273811340332e+00,-2.324983835220336914e+00,-5.070934891700744629e-01,1.574625492095947266e+00,-1.050106048583984375e+00,9.686639308929443359e-01,1.312386870384216309e+00,7.542607188224792480e-01,-9.113077521324157715e-01,-1.718587398529052734e+00,3.751282095909118652e-01,8.606486320495605469e-01,-3.711451292037963867e-01,-5.625200271606445312e-01,-2.721544206142425537e-01,-8.154401183128356934e-01,-3.339428007602691650e-01,1.058411240577697754e+00,4.364815354347229004e-01,-5.736824870109558105e-01,-2.172690257430076599e-02,-5.791836977005004883e-01,-3.260441124439239502e-01,-2.024624943733215332e-01,-4.585579931735992432e-01,7.500000000000000000e+02

新的二进制文件 data_binary.dat 缩减为 1.5GB。这是一个巨大的减少,这让我怀疑我将 csv 转换为二进制格式的方式是否出了问题。这种减少是预期的吗?至少这么多?

最佳答案

好的,所以我去下载了数据样本。每行是这样的:

0.000000000000000000e+00,9.439358860254287720e-02,1.275558676570653915e-02 ...

每个数字总共似乎有 25 个字符,如果包括逗号,实际上是 26 个左右。所以每个字符一个字节,大约 25 个字节。使用 64 位 float 的二进制表示将需要 ... 64 位,即每个数字 8 个字节。所以你应该期望二进制文件的大小小于 1/3,所以这似乎是正确的:

5.2/3 = 1.73...

更好的估计是每个数字大约 26 个字符(包括逗号和换行符),因此:

In [2]: (8/26)*5.2
Out[2]: 1.6

似乎是合法的。

关于python - csv转二进制文件异常减小文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50309668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com