【问题标题】:Efficient usage of memory with large data size高效利用大数据量的内存
【发布时间】:2018-07-09 21:39:15
【问题描述】:

我正在执行如下的情节:

for i in range(len(classederror)):
    plt.scatter(xlag, classederror[i, :])
plt.show()

变量的大小为:

xlag = np.array(2, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250)

xlag.size = (11,)
  • classederror = 176501 行 x 11 列

但是,我遇到了内存问题,这是由于classederror 的大小太大。

有没有一种pythonic/更有效的方法来做到这一点而不会出现内存问题?

我想做什么

如下图所示,x 轴为xlag,y 轴为classederror

我想在classederror 中为一系列 x 轴值绘制每一行并研究数据的分布,最后我应该获得类似于下图的东西。

【问题讨论】:

  • classederror 很小。假设是 64 位数字类型,例如np.float64,这大概是 (176501* 11 * 8 * 1e-6) = 15.53 兆字节......现在,我对matplotlib 的内部了解不多,但即使它在下面创建副本引擎盖,我不认为这会导致内存问题。但可能会有更熟悉的人发表评论。
  • 所以在我的计算机上使用(10000, 11) 形状的数组进行粗略测试,我消耗了大约 300 兆字节(整个过程)。假设此内存使用量呈线性增长,那么这将解释您的问题。尝试绘制 176501 行可能会导致此错误也就不足为奇了……您到底想做什么?
  • @juanpa.arrivillaga 目前的代码产生 176501 行;一个Line2D 对象肯定会花费超过 8 个字节。
  • 代码中没有点,只有行。因此,您显示的代码无论如何都不会产生显示的输出。这就是为什么我要求您提供minimal reproducible example 以及对xlag 中数据的解释。
  • 你愿意无视我的cmets吗?在这种情况下,答案很简单:如果目标是看到 11 条线,那么绘制 170000 个点是没有意义的。

标签: python numpy matplotlib memory optimization


【解决方案1】:

绘制单个散点图当然比绘制 176501 个散点图更有效。

import numpy as np
import matplotlib.pyplot as plt

xlag = np.array([2, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250])
classederror = (np.random.randn(176501, 11)*25)*(0.2+np.sort(np.random.rand(11)))

plt.scatter(np.tile(xlag,len(classederror)), classederror.flatten())

plt.show()

鉴于可以从这样的图中获得的信息有限,直接绘制 11 条线可能是有意义的。

import numpy as np
import matplotlib.pyplot as plt

xlag = np.array([2, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250])
classederror = (np.random.randn(176501, 11)*25)*(0.2+np.sort(np.random.rand(11)))

vals = np.c_[classederror.min(axis=0),classederror.max(axis=0)].T
x= np.c_[xlag,xlag].T
plt.plot(x,vals, color="C0", lw=2)

plt.show()

要获得有关点密度的信息,可以使用其他方法,例如小提琴情节。

plt.violinplot(classederror, xlag, points=50, widths=20,
                  showmeans=True, showextrema=True, showmedians=True)

【讨论】:

  • 你画的插图真漂亮 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 2023-03-16
相关资源
最近更新 更多