簡體   English   中英

處理海量數據時出現內存錯誤

[英]Memory error when dealing with huge data

我想用matplotlib繪制直方圖。 但是,由於我發送到hist()函數的巨大數據(包含大約100,000個數字的列表),繪制兩個數字時會出現錯誤。 但它只是在繪制兩個圖中的任何一個時順利進行。 誰能幫我解決這個問題? 提前致謝。

以下是顯示錯誤的簡化代碼:

f_120 = plt.figure(1)
plt.hist(taccept_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'b', label = 'accepted answer')
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()

plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 2 hrs)')
plt.ylim(0,1)
plt.xlim(0,120)
f_120.show()
f_120.savefig('7month_0_120.png', format = 'png' )
plt.close()

f_2640 = plt.figure(2)
plt.hist(taccept_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'b', label = 'accepted answer')
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '4 hours')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 day')
plt.legend(loc= 4)

plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 48)')
plt.ylim(0,1)
plt.xlim(0,2640)
f_2640.show()
f_2640.savefig('7month_0_2640.png', format = 'png' )

以下是錯誤的詳細信息:

plt.hist(tfirst_list,bins = 6000000,normed = True,histt​​ype =“step”,cumulative = True,color ='g',label ='first answer')

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ pyplot.py”,第2160行,在hist ret = ax.hist(x,bins,range,normed,weights,cumulative,bottom,histt​​ype,align ,方向,rwidth,log,顏色,標簽,** kwargs)

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ axes.py”,第7775行,在hist closed = False,edgecolor = c,fill = False))

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ axes.py”,第6384行,填寫為self中的poly._get_patches_for_fill(* args,** kwargs):

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ axes.py”,第317行,在_grab_next_args中為seg in self._plot_args(其余,kwargs):

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ axes.py”,第304行,在_plot_args中seg = func(x [:,j%ncx],y [:,j%ncy],kw ,kwargs)

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ matplotlib \\ axes.py”,第263行,在_makefill(x [:,np.newaxis],y [:,np.newaxis]))中,

文件“C:\\ software \\ Python26 \\ lib \\ site-packages \\ numpy \\ core \\ shape_base.py”,第270行,在hstack中返回_nx.concatenate(map(atleast_1d,tup),1)

的MemoryError

正如其他人所指出的那樣,600萬個箱子聽起來並不是很有用。 但是簡單的事情是重用同一張圖:由於唯一改變的圖元素是直方圖以外的其他內容,因此請嘗試以下操作:

vline1 = plt.axvline(...)
vline2 = plt.axvline(...)
lgd = legend()

在savefig之后,不要關閉該圖並繪制新的直方圖,而要重新使用它,更改需要更改的內容:

# change vline1 and vline2 positions and labels
vline1.set_data([240,240],[0,1])
vline1.set_label('new label')
vline2.set_data(...)
vline2.set_label(...)
# remove old legend, replace with new
lgd.remove()
lgd = plt.legend(loc=4)
plt.xlabel('new xlabel')
# etc

最后再使用新文件名調用savefig。

你繪制600萬個箱子,然后放大(大概)一小部分。 每個數字有兩行,即1200萬個數據點,當您嘗試在下一個數字中繪制另外1200萬個數據時,matplotlib崩潰並不令我感到驚訝。 我非常懷疑您是否真的需要600萬個垃圾箱,所以讓我們嘗試將直方圖減小到更易於管理的大小!

假設您的數據跨越了您希望查看的44或48小時。 然后有600萬個箱子,這意味着你的數據分辨率為30毫秒。 考慮到你顯示的分鍾分辨率,這似乎是不合理的。 另外,您的分辨率為秒,因此600萬個bin意味着您的數據跨度為70天,但您只看其中的兩個。

假設您對兩天的數據感興趣,其分辨率為秒或分鍾。

將bin作為多個bin指定時,還可以指定一系列值。 因此,對於您的第一張圖,您可以說

plt.hist(taccept_list, bins=range(120), normed = True, histtype ="step", cumulative = True, color = 'b', label = 'accepted answer')
plt.hist(tfirst_list, bins=range(120), normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')

前120分鍾以分鍾為單位的分辨率。 直方圖將忽略任何高於120的值,這很好,因為無論如何您都不會在圖中顯示它。

在幾秒鍾內解決的替代方案可能是:

numpy.linspace(0,120,7200)

現在,直方圖中的點數更加合理,並且可能與您正在查看/顯示的數據更加一致。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM