numpy学习(用于数组的文件输入输出)

NumPy能够读写磁盘上的文本数据或二进制数据。

将数组以二进制格式保存到磁盘

np.save和np.load是读写磁盘数组数据的两个主要函数。默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为.npy的文件中的。

如果文件路径末尾没有扩展名.npy,则该扩展名会被自动加上。然后就可以通过np.load读取磁盘上的数组:

通过np.savez可以将多个数组保存到一个压缩文件中,将数组以关键字参数的形式传入即可:

加载.npz文件时,你会得到一个类似字典的可迭代对象,可以通过对象加下标名的方式输出具体的值。

存取文本文件

将数组保存为txt本通过np.savetxt:

加载txt文本通过loadtxt函数,官方文档如下:

loadtxt(fname,dtype=float,comments='#',delimiter=None,
converters=None,skiprows=0,usecols=None,unpack=False,
ndmin=0):
Loaddatafromatextfile.

Eachrowinthetextfilemusthavethesamenumberofvalues.

Parameters
----------
fname:file,str,orpathlib.Path
File,filename,orgeneratortoread.Ifthefilenameextensionis
``.gz``or``.bz2``,thefileisfirstdecompressed.Notethat
generatorsshouldreturnbytestringsforPython3k.
dtype:data-type,optional
Data-typeoftheresultingarray;default:float.Ifthisisa
structureddata-type,theresultingarraywillbe1-dimensional,and
eachrowwillbeinterpretedasanelementofthearray.Inthis
case,thenumberofcolumnsusedmustmatchthenumberoffieldsin
thedata-type.
comments:strorsequence,optional
Thecharactersorlistofcharactersusedtoindicatethestartofa
comment;
default:'#'.
delimiter:str,optional
Thestringusedtoseparatevalues.Bydefault,thisisany
whitespace.
converters:dict,optional
Adictionarymappingcolumnnumbertoafunctionthatwillconvert
thatcolumntoafloat.E.g.,ifcolumn0isadatestring:
``converters={0:datestr2num}``.Converterscanalsobeusedto
provideadefaultvalueformissingdata(butseealso`genfromtxt`):
``converters={3:lambdas:float(s.strip()or0)}``.Default:None.
skiprows:int,optional
Skipthefirst`skiprows`lines;default:0.

usecols:intorsequence,optional
Whichcolumnstoread,with0beingthefirst.Forexample,
usecols=(1,4,5)willextractthe2nd,5thand6thcolumns.
Thedefault,None,resultsinallcolumnsbeingread.

..versionadded::1.11.0

Alsowhenasinglecolumnhastobereaditispossibletouse
anintegerinsteadofatuple.E.g``usecols=3``readsthe
fourthcolumnthesamewayas`usecols=(3,)``would.

unpack:bool,optional
IfTrue,thereturnedarrayistransposed,sothatargumentsmaybe
unpackedusing``x,y,z=loadtxt(...)``.Whenusedwithastructured
data-type,arraysarereturnedforeachfield.DefaultisFalse.
ndmin:int,optional
Thereturnedarraywillhaveatleast`ndmin`dimensions.
Otherwisemono-dimensionalaxeswillbesqueezed.
Legalvalues:0(default),1or2.

..versionadded::1.6.0

Returns
-------
out:ndarray
Datareadfromthetextfile.

SeeAlso
--------
load,fromstring,fromregex
genfromtxt:Loaddatawithmissingvalueshandledasspecified.
scipy.io.loadmat:readsMATLABdatafiles

Notes
-----
Thisfunctionaimstobeafastreaderforsimplyformattedfiles.The
`genfromtxt`functionprovidesmoresophisticatedhandlingof,e.g.,
lineswithmissingvalues.

..versionadded::1.10.0

ThestringsproducedbythePythonfloat.hexmethodcanbeusedas
inputforfloats.

Examples
--------
>>>fromioimportStringIO#StringIObehaveslikeafileobject
>>>c=StringIO("01\n23")
>>>np.loadtxt(c)
array([[0.,1.],
[2.,3.]])

>>>d=StringIO("M2172\nF3558")
>>>np.loadtxt(d,dtype={'names':('gender','age','weight'),
...'formats':('S1','i4','f4')})
array([('M',21,72.0),('F',35,58.0)],
dtype=[('gender','|S1'),('age','<i4'),('weight','<f4')])

>>>c=StringIO("1,0,2\n3,0,4")
>>>x,y=np.loadtxt(c,delimiter=',',usecols=(0,2),unpack=True)
>>>x
array([1.,3.])
>>>y
array([2.,4.])

对于CSV文件,实际上是一个逗号分隔符文件,也可能过np.loadtxt函数进行加载,不过需要指定分隔符;

原文地址:https://www.cnblogs.com/anner-nie/p/8570012.html