如何解决MemoryError:无法为形状和数据类型为 float64 的数组分配 GiB - 在稀疏矩阵上
我正在处理文本数据并有一个文档项矩阵,用 head
稀疏矩阵表示(为了提高内存效率)。
我构建了一个类,在其中训练主题模型(主题模型的结果是矩阵 scipy
。
目前,我正在对不同的模型进行一些后期分析,代码如下:
prob_word_given_topic
然而,在colnames = ['Model','Coherence','SVD_values','Min_c0','Max_c0','Min_c1','Max_c1','Min_sv0','Max_sv0','Min_sv1','Max_sv1','PWGT']
analysis_two_factors = pd.DataFrame(columns=colnames)
directory = 'C:~/Images/'
#Experiment with: singular values,number of topics,weighting methods
for i,top in enumerate(range(3,28,2)):
for weighting_method in [2,3,4,5,1]:
print(type(top))
one_round=[]
model = FLSA(input_file = data_list,num_topics = top,num_words = 20,word_weighting =weighting_method,svd_factors=2,cluster_method='fcm')
model.plot_svd_graph_2D(directory)
model.plot_cluster_datapoints_graph(directory)
one_round.append(model.setting)
one_round.append(model.calc_coherence_value)
one_round.append(model.s)
one_round.append(min(model.cluster_centers[:,0]))
one_round.append(max(model.cluster_centers[:,0]))
one_round.append(min(model.cluster_centers[:,1]))
one_round.append(min(model.cluster_centers[:,1]))
one_round.append(min(model.svd_data[:,0]))
one_round.append(max(model.svd_data[:,0]))
one_round.append(min(model.svd_data[:,1]))
one_round.append(model.prob_word_given_topic)
analysis_two_factors.loc[i] = one_round
print('Finished iteration',str(i))
时,我突然收到以下错误:
top = 19
这让我感到惊讶,因为它确实在循环中执行了所有先前的迭代,而且 Traceback (most recent call last):
File "<ipython-input-687-fe7cf1e4ea7a>",line 15,in <module>
cluster_method='fcm')
File "<ipython-input-672-e9c098fb0e45>",line 92,in __init__
prob_word_given_doc = np.asarray(self.sparse_weighted_matrix / self.sparse_weighted_matrix.sum(1))
File "c:~\continuum\anaconda3\lib\site-packages\scipy\sparse\base.py",line 620,in __truediv__
return self._divide(other,true_divide=True)
File "c:~\continuum\anaconda3\lib\site-packages\scipy\sparse\base.py",line 599,in _divide
return np.true_divide(self.todense(),other)
MemoryError: Unable to allocate 2.87 GiB for an array with shape (4280,90140) and data type float64
是一个稀疏矩阵 (self.sparse_weighted_matrix
),所以我不希望这里有如此高的内存要求。有人可以解释为什么我收到这个错误吗?我能做些什么来克服这个问题?
dok_matrix
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。