Python Numpy數組的讀入、存儲操作


從文件中加載ndarray數組

  • 從文本文件中加載ndarray數組 np.loadtxt
>>> np.loadtxt(textfile) # textfile是文本文件
  • .npy或者.npz文件中加載ndarray數組np.load
    如果是.npy結尾的文件,則返回單個ndarray數組
    如果是.npz結尾的文件,則返回一個字典類型對象,{filename: array}
>>> np.load(textfile) # textfile是.npz或.npy結尾的二進制文件

將ndarray數組存入文件

  • 將單個ndarray數組存入一個二進制文件中np.save
>>> x = np.arange(10) 
>>> np.save(outfile, x) # outfile必須以.npy結尾
  • 將多個ndarray數組存入一個二進制文件中 np.savez
>>> x = np.arange(10) 
>>> y = np.sin(x)
>>> np.savez(outfile, x,y) # outfile必須以.npz結尾
  • 將ndarray數組存入文本文件中 np.savetxt
>>> x = np.arange(10) 
>>> y = np.sin(x)
>>> np.savetext(outfile, x, delimiter=',') # outfile為文本文件
>>> np.savetext(outfile, (x,y))
>>> np.savetext(outfile, x, fmt='%1.4e')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM