【问题标题】:MemoryError: Unable to allocate 617. GiB for an array with shape (82754714206,) and data type float64 On Windows and using PythonMemoryError: Unable to allocate 617. GiB for an array with shape (82754714206,) and data type float64 On Windows and using Python
【发布时间】:2021-08-18 02:02:03
【问题描述】:

我在 Jupyter notebook 中尝试了以下凝聚聚类。 我的数据集的形状是(406829, 8)

我尝试了以下代码:

import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import os
from sklearn.preprocessing import StandardScaler, LabelEncoder
import scipy.cluster.hierarchy as shc
from sklearn.cluster import AgglomerativeClustering


# Apply the agglomerative clustering with ward linkage
aggloclust = AgglomerativeClustering(affinity='euclidean',linkage='ward', memory=None, n_clusters=5).fit(data)
print(aggloclust)

# Agglomerative clustering labels
labels = aggloclust.labels_

# Show the clusters on the graph
plt.scatter(x[:,0], x[:,1], c=labels)
plt.show()

然后我遇到了一个错误 - MemoryError: Unable to allocate 617. GiB for an array with shape (82754714206,) and data type float64

我正在使用 16GB RAM 的 Windows 机器。 Python 版本 - 3.8.5 谁能告诉我如何解决这个问题。

我试图用谷歌搜索这个错误并得到了解决方案 - 创建 jupyter 配置文件 然后更新该文件中的 max_buffer_size 我在这里找到了 - How to increase Jupyter notebook Memory limit?

我尝试了上面链接中提供的解决方案,但没有奏效。 请帮帮我。

【问题讨论】:

  • 如果您只有 16 GB 的物理 RAM,您无法通过增加配置文件中的数字来神奇地获得 617 GB 的内存。
  • 82754714206这个数字是从哪里来的?

标签: python python-3.x machine-learning artificial-intelligence


【解决方案1】:

AgglomerativeClustering 的内存消耗为 O(n²),这意味着它与数据大小相比呈指数增长。使用single 链接,计算可以更快地从 O(n³) 到 O(n²),但不幸的是这不适用于内存 [1]。单一聚类也有“富者越富”行为的缺点,即集群往往只有几个大的集群,而其他集群的大小接近于零 [2]。所以,至少在 scipy 或 scikit 里面的选项对微调是不好的。

另一种选择是在拟合模型时输入更少的数据(= 进行训练)。为此,您可以将方法用于数据框(假设 data 对象是数据框):

data.sample(frac = 0.5) 

这会以指数方式缩小内存使用量。一开始不要使用大量数据。来自[3]:

我对 0.02% 的数据运行了算法,得到了结果,但是当我需要标记所有记录时出现了问题。

来源

[1]http://wwwens.aero.jussieu.fr/lefrere/master/SPE/docs-python/scipy-doc/generated/scipy.cluster.hierarchy.linkage.html

[2]https://scikit-learn.org/stable/modules/clustering.html

[3]https://datascience.stackexchange.com/questions/47889/how-to-run-agglomerativeclustering-on-a-big-data-in-python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2022-12-27
    • 2020-05-13
    • 2023-01-09
    相关资源
    最近更新 更多