处理内存错误(Python sklearn 集群)
Dealing with Memory Error (Python sklearn clustering)
我有一个数据集,每个数据都有稀疏标签。
所以,下面是数据的样子。
[["Snow","Winter","Freezing","Fun","Beanie","Footwear","Headgear","Fur","Playing in the snow","Photography"],["Tree","Sky","Daytime","Urban area","Branch","Metropolitan area","Winter","Town","City","Street light"],...]
标签总数在50个左右,数据个数200K。我想对这些数据进行聚类,但我在处理它时遇到了麻烦。
我想使用四种聚类算法(AgglomerativeClustering、SpectralClustering、MiniBatchKMeans、KMeans)对该数据进行聚类,但由于内存问题,其中 none 有效。
下面是我的代码。
from scipy.sparse import csr_matrix
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.cluster import SpectralClustering
import json
NUM_OF_CLUSTERS = 10
with open('./data/sample.json') as json_file:
json_data = json.load(json_file)
indptr = [0]
indices = []
data = []
vocabulary = {}
for d in json_data:
for term in d:
index = vocabulary.setdefault(term, len(vocabulary))
indices.append(index)
data.append(1)
indptr.append(len(indices))
X = csr_matrix((data, indices, indptr), dtype=int).toarray()
# None of these algorithms work properly. I think it's because of memory issues.
# miniBatchKMeans = MiniBatchKMeans(n_clusters=NUM_OF_CLUSTERS, n_init=5, random_state=0).fit(X)
# agglomerative = AgglomerativeClustering(n_clusters=NUM_OF_CLUSTERS).fit(X)
# spectral = SpectralClustering(n_clusters=NUM_OF_CLUSTERS, assign_labels="discretize", random_state=0).fit(X)
#
# print(miniBatchKMeans.labels_)
# print(agglomerative.labels_)
# print(spectral.labels_)
with open('data.json', 'w') as outfile:
json.dump(miniBatchKMeans.labels_.tolist(), outfile)
是否有针对我的问题的解决方案或其他建议?
X
的大小是多少?
使用 toarray()
,您正在将数据转换为有意义的格式。这显着增加了内存需求。
对于 200k 个实例,您不能使用谱聚类而不是仿射传播,因为它们需要 O(n²) 内存。因此,您要么选择其他算法,要么对数据进行二次抽样。显然,同时执行 kmeans 和 minibatch kmeans(这是 kmeans 的近似值)也没有用。只用一个。
要有效地处理稀疏数据,您可能需要自己实现算法。 Kmeans 是为密集数据设计的,因此默认情况下为密集数据计时实施是有意义的。事实上,在稀疏数据上使用 mean 是相当值得怀疑的。因此,我也不希望使用 kmeans 对您的数据产生很好的结果。
我有一个数据集,每个数据都有稀疏标签。 所以,下面是数据的样子。
[["Snow","Winter","Freezing","Fun","Beanie","Footwear","Headgear","Fur","Playing in the snow","Photography"],["Tree","Sky","Daytime","Urban area","Branch","Metropolitan area","Winter","Town","City","Street light"],...]
标签总数在50个左右,数据个数200K。我想对这些数据进行聚类,但我在处理它时遇到了麻烦。
我想使用四种聚类算法(AgglomerativeClustering、SpectralClustering、MiniBatchKMeans、KMeans)对该数据进行聚类,但由于内存问题,其中 none 有效。
下面是我的代码。
from scipy.sparse import csr_matrix
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.cluster import SpectralClustering
import json
NUM_OF_CLUSTERS = 10
with open('./data/sample.json') as json_file:
json_data = json.load(json_file)
indptr = [0]
indices = []
data = []
vocabulary = {}
for d in json_data:
for term in d:
index = vocabulary.setdefault(term, len(vocabulary))
indices.append(index)
data.append(1)
indptr.append(len(indices))
X = csr_matrix((data, indices, indptr), dtype=int).toarray()
# None of these algorithms work properly. I think it's because of memory issues.
# miniBatchKMeans = MiniBatchKMeans(n_clusters=NUM_OF_CLUSTERS, n_init=5, random_state=0).fit(X)
# agglomerative = AgglomerativeClustering(n_clusters=NUM_OF_CLUSTERS).fit(X)
# spectral = SpectralClustering(n_clusters=NUM_OF_CLUSTERS, assign_labels="discretize", random_state=0).fit(X)
#
# print(miniBatchKMeans.labels_)
# print(agglomerative.labels_)
# print(spectral.labels_)
with open('data.json', 'w') as outfile:
json.dump(miniBatchKMeans.labels_.tolist(), outfile)
是否有针对我的问题的解决方案或其他建议?
X
的大小是多少?
使用 toarray()
,您正在将数据转换为有意义的格式。这显着增加了内存需求。
对于 200k 个实例,您不能使用谱聚类而不是仿射传播,因为它们需要 O(n²) 内存。因此,您要么选择其他算法,要么对数据进行二次抽样。显然,同时执行 kmeans 和 minibatch kmeans(这是 kmeans 的近似值)也没有用。只用一个。
要有效地处理稀疏数据,您可能需要自己实现算法。 Kmeans 是为密集数据设计的,因此默认情况下为密集数据计时实施是有意义的。事实上,在稀疏数据上使用 mean 是相当值得怀疑的。因此,我也不希望使用 kmeans 对您的数据产生很好的结果。