- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个只有二进制(1 或 0)值的 51K X 8.5K 数据框。
我写了下面的代码:
outfile=open("df_preference.p", "wb")
pickle.dump(df_preference,outfile)
outfile.close()
它抛出如下内存错误:
MemoryError Traceback (most recent call last)
<ipython-input-48-de66e880aacb> in <module>()
2
3 outfile=open("df_preference.p", "wb")
----> 4 pickle.dump(df_preference,outfile)
5 outfile.close()
我假设这意味着这个数据很大而且不能 pickle?但它只有二进制值。
在此之前,我从另一个具有正常计数和大量零的数据框中创建了这个数据集。使用了以下代码:
df_preference=df_recommender.applymap(lambda x: np.where(x >0, 1, 0))
这本身就花了一些时间来创建 df_preference。矩阵大小相同。
我担心的是,如果使用 applymap 创建数据框需要时间,并且 ii) 由于内存错误甚至没有 pickle 数据框,那么继续我需要使用 SVD 和交替最小值对这个 df_prefence 进行矩阵分解广场。那它会更慢吗?如何解决这个缓慢的运行并解决内存错误?
谢谢
最佳答案
更新:
对于 1
和 0
值,您可以使用 int8
(1 字节)dtype,这将减少至少 4 的内存使用次。
(df_recommender > 0).astype(np.int8).to_pickle('/path/to/file.pickle')
这是一个 51K x 9K 数据帧的例子:
In [1]: df = pd.DataFrame(np.random.randint(0, 10, size=(51000, 9000)))
In [2]: df.shape
Out[2]: (51000, 9000)
In [3]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 51000 entries, 0 to 50999
Columns: 9000 entries, 0 to 8999
dtypes: int32(9000)
memory usage: 1.7 GB
源DF需要1.7GB内存
In [6]: df_preference = (df>0).astype(int)
In [7]: df_preference.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 51000 entries, 0 to 50999
Columns: 9000 entries, 0 to 8999
dtypes: int32(9000)
memory usage: 1.7 GB
生成的 DF 再次需要 1.7 GB 的内存
In [4]: df_preference = (df>0).astype(np.int8)
In [5]: df_preference.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 51000 entries, 0 to 50999
Columns: 9000 entries, 0 to 8999
dtypes: int8(9000)
memory usage: 437.7 MB
使用 int8
dtype 只需要 438 MB
现在让我们将它保存为 Pickle 文件:
In [10]: df_preference.to_pickle('d:/temp/df_pref.pickle')
文件大小:
{ temp } » ls -lh df_pref.pickle
-rw-r--r-- 1 Max None 438M May 28 09:20 df_pref.pickle
旧答案:
试试这个:
(df_recommender > 0).astype(int).to_pickle('/path/to/file.pickle')
解释:
In [200]: df
Out[200]:
a b c
0 4 3 3
1 1 2 1
2 2 1 0
3 2 0 1
4 2 0 4
In [201]: (df>0).astype(int)
Out[201]:
a b c
0 1 1 1
1 1 1 1
2 1 1 0
3 1 0 1
4 1 0 1
PS 您可能还想将 DF 保存为 HDF5 文件而不是 Pickle - 参见 this comparison详情
关于python - 将数据帧酸洗到磁盘时出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493165/
我在服务器启动时创建一个缓存(服务器启动每次都需要10分钟)。目前我正在使用内存缓存(Ehcache)。现在我想建立一个机制,以便一旦数据被缓存我应该能够在几秒钟内启动服务器。比如将缓存的持久副本写入
我编写 json 结构的方式使得文件(在进行了一个月的测量后)存储在磁盘上时仍然只有 100 MB 左右。但是现在文件大约是 20mb,但我看到我的脚本需要的内存大约是 200/300 mb。显然,脚
Solaris9 x86下如何挂载和永久挂载windows fat32分区 临时挂载Shell 命令; mout –F pcfs /dev/dsk/c1d0p0:c /mnt/c mount
磁盘ID中的资源组名称大小写不敏感。重现此问题的步骤 - 在 Azure 中创建独立磁盘,检查 ID。对于例如 -“/subscriptions/subscriptionID/resourceGrou
我已将附加数据磁盘的备份还原到新虚拟机。当我发出命令 sudo blkid 时,我发现它与附加到原始虚拟机的数据磁盘具有相同的 UUID,因此我无需更改 fstab 即可在启动时挂载它。然而,它似乎是
在用户态中,执行磁盘 IO 就像链接 C 库一样简单,或者,如果您喜欢冒险,可以直接执行系统调用。我想知道内核本身是如何执行 IO 的。 换句话说,假设我在裸机上以特权模式运行应用程序。我将如何访问通
我已将附加数据磁盘的备份还原到新虚拟机。当我发出命令 sudo blkid 时,我发现它与附加到原始虚拟机的数据磁盘具有相同的 UUID,因此我无需更改 fstab 即可在启动时挂载它。然而,它似乎是
我正在尝试使用 laravel 和 ffmpeg 创建缩略图。但是我收到了这个错误。 磁盘 [视频] 没有配置驱动程序。 我的代码 public function index() { FFMp
我的目标是读/写 usb。 首先必须打开并读取 usb 低级别,如“程序” 我使用 visual c++ 和 winAPI 下面是我的测试代码 char path[64]; sprintf(path,
内核缓冲区缓存何时为空?这似乎不是 LINE Buffering。如果我写 () 一个没有换行符的字符串,它会立即输出到文件。 另外,socket文件的输入输出缓冲区是否也像Disk I/O一样使用内
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有一个大型调用中心,有 250 个并发调用。队列日志的队列应用程序平面文件。该系统使用 Asterisk 和 Queuemetrics。两个服务都在同一台服务器上运行。规范为 16 核和 64 GB
我在使用安装了 Centos7 的 VMWare VM 时遇到问题。 lsblk 命令给出如下内容 df -h 给出这个 我正在尝试将 root lvm 扩展到分区,但无论我如何尝试都无法做到这一点。
在基于内存的计算模型中,通过考虑数据结构,可以抽象地完成唯一需要进行的运行时计算。 但是,关于高性能磁盘 I/O 算法的文档并不多。因此,我提出了以下一组问题: 1) 我们如何估计磁盘 I/O 操作的
我不是在寻找调用命令行实用程序的代码,它可以解决问题。我实际上很想知道用于创建 RAM 磁盘的 API。 编辑 动机:我有一个第三方库,它需要一个目录名,以便以某种方式处理该目录中的文件。我将这些文件
MySQL 数据库显示磁盘 I/O 利用率持续保持在 100% 左右。数据库服务器有 24 GB 内存。 我们尝试优化查询,但效果不佳。 请检查如下所示的当前配置参数: 参数 当前值 key_buff
这是交易。我们本可以采用完全静态 html 的方式来解决性能问题,但由于该站点将是部分动态的,因此这对我们来说行不通。我们想到的是使用 memcache + eAccelerator 来加速 PHP
对于游戏 Minecraft,运行服务器应用程序时的一般方法是在 RAMDisk 中运行它,因为它使用数百个小文件来生成世界,I/O 速度是主要瓶颈。 在最近的尝试中,我尝试使用 Dokan/ImDi
当我查找文件中的某个位置并写入少量数据(20 字节)时,幕后发生了什么? 我的理解 据我所知,可以从磁盘写入或读取的最小数据单位是一个扇区(传统上是 512 字节,但该标准现在正在改变)。这意味着要写
如何使用golang获取xen服务器的内存、磁盘、网络和cpu信息? 是否有任何可用的软件包? 最佳答案 与其他服务器有什么不同?如果没有 - 有一堆 Go 包可以做到这一点,我正在使用这个 - ht
我是一名优秀的程序员,十分优秀!