Numpy科學計算IO讀寫小技巧npy二進位制檔案
阿新 • • 發佈:2021-01-15
技術標籤:演算法
在科學計算中,我們常常需要把numpy陣列得到結果儲存到磁碟上,然後在用畫圖軟體畫圖。因此檔案的IO是必不可少的。
這裡一個技巧是利用numpy自帶的save和load方法,就可以把numpy陣列寫入到二進位制檔案中,之後直接呼叫load進行處理,不用自己寫迴圈輸出成txt在處理。
# 計算chi0 time_start=time.time() print("Start Calculate chi0") chi0 = calculate_chi0(omegas,Gk_up_r,Gk_up_a,Gk_down_r,Gk_down_a,T,mu,N) time_end = time.time() print("Calculate chi0 cost :{:.2f}s".format(time_end-time_start)) np.save('chi0.npy',chi0) np.save('chi0_00static.npy',chi0[0,:,0,0]) np.save('chi0_00.npy',chi0[:,:,0,0]) # RPA time_start=time.time() print("Start Calculate chi_RPA") chi_RPA = chi_RPA(chi0,U) time_end = time.time() print("Calculate chi_RPA cost :{:.2f}s".format(time_end-time_start)) np.save('chi_RPA.npy',chi_RPA) np.save('chi_RPA_static.npy',chi_RPA[0,:,0,0]) np.save('chi_RPA.npy',chi_RPA[:,:,0,0])