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