MemoryError: Unable to allocate 617. GiB for an array with shape (82754714206,) and data type float64 On Windows and using Python

MemoryError: Unable to allocate 617. GiB for an array with shape (82754714206,) and data type float64 On Windows and using Python

我在 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

我正在 windows 16GB 内存的机器上工作。 Python 版本 - 3.8.5 谁能告诉我如何解决这个问题。

我尝试 google 这个错误并得到了解决方案 - 创建 jupyter 配置文件 然后更新该文件中的 max_buffer_size 我在这里找到它 -

我尝试了上面link中提供的解决方案,但没有用。 请帮助我。

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

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

data.sample(frac = 0.5) 

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

I ran the algorithm on 0.02% of my data and I got the result but the problem raised when I need to label all records.

来源:

[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