gpt4 book ai didi

python - scikitlearn - HashingVectorizer 之后 MiniBatchKMeans 聚类过程中出现内存错误

转载 作者:行者123 更新时间:2023-12-01 09:05:04 29 4
gpt4 key购买 nike

我的目标是从数百万行的数据集中执行文本聚类,其中每一行都是一串单词,与正确的单词不对应文档,而是“关键字”列表。这个想法是,每一行代表一个 Twitter 用户,其关键字列表取自他/她的推文,以下是行的示例:

"remove United States District Attorney Carmen Ortiz office overreach case Aaron Swartz"

这是我的代码:

from __future__ import print_function
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.cluster import MiniBatchKMeans
from time import time
import csv

# LOAD CSV
print("Loading Dataset from a CSV...")
csvinputfile = '...'
t = time()
dataset = open(csvinputfile, 'r')
print("done in %0.3fs" % (time() - t))
print("")

# TERM OCCURRENCES
print("Calculating Term Occurrences...")
t = time()
vectorizer = HashingVectorizer(n_features=300000, stop_words=None, alternate_sign=False, norm='l2', binary=False)
x = vectorizer.fit_transform(dataset)
print("done in %0.3fs" % (time() - t))
print("")

# CLUSTERING
print("MiniBatchKMeans Clustering...")
t = time()
km = MiniBatchKMeans(n_clusters=10000, init='k-means++', n_init=1, init_size=10000, batch_size=10000, verbose=False)
clusters = km.fit(x)
print("done in %0.3fs" % (time() - t))
print("")

我的问题是,当进行集群时,我遇到内存错误:

MiniBatchKMeans Clustering...
Traceback (most recent call last):
File ".../cluster-users.py", line 32, in <module> clusters = km.fit(x)
File ".../python2.7/site-packages/sklearn/cluster/k_means_.py", line 1418, in fit init_size=init_size)
File ".../python2.7/site-packages/sklearn/cluster/k_means_.py", line 684, in _init_centroids x_squared_norms=x_squared_norms)
File ".../python2.7/site-packages/sklearn/cluster/k_means_.py", line 79, in _k_init centers = np.empty((n_clusters, n_features), dtype=X.dtype)
MemoryError
[Finished in 22.923s]

我对pythonscikitlearn很陌生,所以我不太明白发生了什么,但我认为这是因为,因为我正在处理一个大数据集,聚类阶段正在尝试将 n_samples 和 n_features 的巨大矩阵加载到内存中。

该错误的一部分,我不明白,因为我认为 MiniBatchKMeansHashingVectorizer 可以帮助解决内存限制,我也不太明白知道要使用的最佳参数(我按照 KMeans 和 MiniBatchKMeans 的 scikitlearn 教程作为聚类文本的基础,您可以在此处找到它 http://scikit-learn.org/stable/auto_examples/text/document_clustering.html#sphx-glr-auto-examples-text-document-clustering-py )。

要记住的事情:

  • 无法使用任何机器学习NLPMapReduce之类的技术
  • 集群需要以某种方式代表具有相似兴趣的用户,因此使用相似的关键字

所以我的问题是:如何修复内存错误?如果有人对如何正确设置集群有一些提示,或者如果我的方法是错误的,那也很好。

最佳答案

包含这样的文本的行,“删除美国地方检察官 Carmen Ortiz 办公室越权案件 Aaron Swartz”确实是

要修复内存错误,请确保满足以下几点;

  • 一行中的所有关键字都相关吗?如果没有,请尝试通过删除停用词、标点符号等来减少它们。

  • 专注于从文本中聚合相关关键字。您可以创建此类关键字的列表。

  • 在 python 中查找regex 库。它可以帮助您进行数据清理。

  • 要确定最佳参数,请查找诸如簇内平方和平均轮廓间隙统计之类的术语。

聚类并不是某种会产生结果的黑暗魔法。如果你输入垃圾,就会产生垃圾。

附注请不要为同一问题创建新问题。已经有另一个similar question that you've asked recently 。除非这两个问题根本不同,否则创建一个新问题,否则在您的帖子中明确说明,这个问题与上一个问题有何不同。

关于python - scikitlearn - HashingVectorizer 之后 MiniBatchKMeans 聚类过程中出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129773/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com