【问题标题】:while training model with AgglomerativeClustering algorithm getting memory error使用 AgglomerativeClustering 算法训练模型时出现内存错误
【发布时间】:2020-06-16 17:18:28
【问题描述】:

我正在训练一种用于无监督学习的模型。数据集有 1,40,000 行和 6 列。文件大小为 10,637 KB 的 csv 类型。

import numpy as np
import pandas as pd
import seaborn as sns
%matplotlib qt
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans 
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import AgglomerativeClustering

以上导入的库。

Rev =  pd.read_csv(r"Updated_Rev.csv")
labelEncoder = LabelEncoder()
labelEncoder.fit(Rev["Technology"])
Rev["Technology"] = labelEncoder.transform(Rev["Technology"])

一列是字符串,因此对其进行了编码但未用于培训,将来可能需要。

train = Rev.iloc[:,:4]
clustering = AgglomerativeClustering()
clustering.fit(train)

这是训练文件,因此训练需要所有行并从中选择 4 列。 这样做时出现此错误

MemoryError: Unable to allocate 67.9 GiB for an array with shape (9117833280,) and data type float64

注意事项

  1. 我没有系统管理员权限。
  2. 是windows操作系统,使用Anaconda的基础环境。
  3. 仅为特定用户安装 Anaconda。
MemoryError                               Traceback (most recent call last)
<ipython-input-16-0f4f354e9aaf> in <module>
      1 train = Rev.iloc[:,:4]
----> 2 clustering.fit(train)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\cluster\_agglomerative.py in fit(self, X, y)
    857                                          n_clusters=n_clusters,
    858                                          return_distance=return_distance,
--> 859                                          **kwargs)
    860         (self.children_,
    861          self.n_connected_components_,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\joblib\memory.py in __call__(self, *args, **kwargs)
    353 
    354     def __call__(self, *args, **kwargs):
--> 355         return self.func(*args, **kwargs)
    356 
    357     def call_and_shelve(self, *args, **kwargs):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\cluster\_agglomerative.py in ward_tree(X, connectivity, n_clusters, return_distance)
    232                           stacklevel=2)
    233         X = np.require(X, requirements="W")
--> 234         out = hierarchy.ward(X)
    235         children_ = out[:, :2].astype(np.intp)
    236 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\cluster\hierarchy.py in ward(y)
    828 
    829     """
--> 830     return linkage(y, method='ward', metric='euclidean')
    831 
    832 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\cluster\hierarchy.py in linkage(y, method, metric, optimal_ordering)
   1054                          'matrix looks suspiciously like an uncondensed '
   1055                          'distance matrix')
-> 1056         y = distance.pdist(y, metric)
   1057     else:
   1058         raise ValueError("`y` must be 1 or 2 dimensional.")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\spatial\distance.py in pdist(X, metric, *args, **kwargs)
   2002     out = kwargs.pop("out", None)
   2003     if out is None:
-> 2004         dm = np.empty((m * (m - 1)) // 2, dtype=np.double)
   2005     else:
   2006         if out.shape != (m * (m - 1) // 2,):

MemoryError: Unable to allocate 67.9 GiB for an array with shape (9117833280,) and data type float64

【问题讨论】:

  • 您可能无法使用庞大的数据集,请参阅此了解更多详细信息Link
  • 感谢 manojk。 如何使用 10k 记录训练模型并保存模型 ->重复直到记录结束。我一直在寻找这种方法。
  • 即使我不确定,谷歌在集群中进行一些批量培训或在 datascience.stackexchange.com 上发布一个新问题
  • 批量训练不会改变任何准确性。它的工作原理与正常的 k 均值算法相同。训练整个数据集没有任何问题
  • 那看看datascience.stackexchange.com上是否已经有帖子,如果没有则创建,这个站点(SO)更多的是从编程角度来看,数据科学站点肯定会有解决方案。跨度>

标签: python python-3.x numpy scikit-learn hierarchical-clustering


【解决方案1】:

聚合聚类不支持大数据

根据定义,该算法需要 O(n²) 内存和 O(n³) 运行时间。

对数据进行采样或使用不同的算法

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

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2019-09-26
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多