- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是从数百万行的数据集中执行文本聚类,其中每一行都是一串单词,与正确的单词不对应文档,而是“关键字”列表。这个想法是,每一行代表一个 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]
我对python和scikitlearn很陌生,所以我不太明白发生了什么,但我认为这是因为,因为我正在处理一个大数据集,聚类阶段正在尝试将 n_samples 和 n_features 的巨大矩阵加载到内存中。
该错误的一部分,我不明白,因为我认为 MiniBatchKMeans 和 HashingVectorizer 可以帮助解决内存限制,我也不太明白知道要使用的最佳参数(我按照 KMeans 和 MiniBatchKMeans 的 scikitlearn 教程作为聚类文本的基础,您可以在此处找到它 http://scikit-learn.org/stable/auto_examples/text/document_clustering.html#sphx-glr-auto-examples-text-document-clustering-py )。
要记住的事情:
所以我的问题是:如何修复内存错误?如果有人对如何正确设置集群有一些提示,或者如果我的方法是错误的,那也很好。
最佳答案
包含这样的文本的行,“删除美国地方检察官 Carmen Ortiz 办公室越权案件 Aaron Swartz”确实是脏
。
要修复内存错误,请确保满足以下几点;
一行中的所有关键字都相关吗?如果没有,请尝试通过删除停用词、标点符号等来减少它们。
专注于从文本中聚合相关关键字。您可以创建此类关键字的列表。
在 python 中查找regex
库。它可以帮助您进行数据清理。
要确定最佳参数,请查找诸如簇内平方和
、平均轮廓
或间隙统计
之类的术语。
聚类并不是某种会产生结果的黑暗魔法
。如果你输入垃圾,就会产生垃圾。
附注请不要为同一问题创建新问题。已经有另一个similar question that you've asked recently 。除非这两个问题根本不同,否则创建一个新问题,否则在您的帖子中明确说明,这个问题与上一个问题有何不同。
关于python - scikitlearn - HashingVectorizer 之后 MiniBatchKMeans 聚类过程中出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129773/
我对 sklearn.cluster.MiniBatchKMeans 感兴趣作为使用庞大数据集的一种方式。无论如何,我对 MiniBatchKMeans.partial_fit() 之间的区别有点困惑
我正在使用 scikitlearn 中的 MiniBatchKMeans() 函数。出色地,在其文档中有: batch_size : int, optional, default: 100 Size
我正在尝试使用 Sklearn 的 Minibatch K-Means 对图像 block 进行聚类,以重现 this paper 的结果。以下是有关我的数据集的一些信息: 400,000 行 108
vectors = model.syn0 n_clusters_kmeans = 20 # more for visualization 100 better for clustering min_k
我的目标是从数百万行的数据集中执行文本聚类,其中每一行都是一串单词,与正确的单词不对应文档,而是“关键字”列表。这个想法是,每一行代表一个 Twitter 用户,其关键字列表取自他/她的推文,以下是行
我正在尝试根据使用 sklearn.cluster.MiniBatchKMeans 的轮廓分数找到正确的簇数 k。 from sklearn.cluster import MiniBatchKMean
我正在浏览 Clustering 上的 scikit-learn 用户指南| .他们有一个比较 K-Means and MiniBatchKMeans 的例子. 我对示例中的以下代码有点困惑: # W
我是一名优秀的程序员,十分优秀!