MemoryError:无法分配617.GiB表示形状为(82754714206)、数据类型为float64的数组.在W



我在Jupyter笔记本中尝试了以下聚合集群。我的数据集的形状是(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我在这里找到了-如何提高Jupyter笔记本的内存限制?

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

AggregationClustering的内存消耗为O(n²(,这意味着它与数据大小相比呈指数级增长。使用single链接,从O(n³(到O(n²(的计算速度可以更快,但不幸的是,这不适用于内存[1]。单个聚类也具有"0"的下侧;富人越富";集群往往只有几个大的集群,而其他集群则接近于零大小的集群[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

最新更新