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 (将#修改为@)