>>> signal = np.array([1,2,3,4,3,2])
>>> np.fft.fft(signal)
array([15.+0.j, -4.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, -4.+0.j])
>>> np.fft.hfft(signal[:4])
array([15., -4., 0., -1., 0., -4.])
>>> np.fft.hfft(signal,6)
array([15., -4., 0., -1., 0., -4.])
>>> freq_spectrum = np.fft.hfft(signal)
>>> freq_spectrum
array([[ 1., 1.],
[ 2., -2.]])
>>> a = np.asarray(a)
>>> a
array([1, 2])
>>> a = [1,2]
>>> type(a)
<class 'list'>
>>> a = np.array([1,2],dtype=np.float32)
>>> np.asarray(a,dtype=np.float32) is a
True
>>> np.asarray(a,dtype=np.float64) is a
False
>>> issubclass(np.recarray,np.ndarray)
True
>>> np.asanyarray(range(5))
array([0, 1, 2, 3, 4])
>>> x = np.arange(6).reshape(2,3)
>>> np.ascontiguousarray(x,dtype=np.float32)
array([[0., 1., 2.],
[3., 4., 5.]], dtype=float32)
>>> b = np.arange(6).reshape(2,3)
>>> b
array([[0, 1, 2],
[3, 4, 5]])
>>> x.flags['C_CONTIGUOUS']
True
#注意:此函数返回一个至少具有一维(1-d)的数组,因此它将不保留0-d数组。
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> y = x
>>> x[0] = 10
>>> x
array([[10, 10, 10],
[ 3, 4, 5]])
>>> y
array([[10, 10, 10],
[ 3, 4, 5]])
>>> z = np.copy(x)
>>> x[0] = 5
>>> x
array([[5, 5, 5],
[3, 4, 5]])
>>> y
array([[5, 5, 5],
[3, 4, 5]])
>>> z
array([[10, 10, 10],
[ 3, 4, 5]])
>>> s = b'hello world'
>>> np.frombuffer(s,dtype='S1',count=5,offset=6)
array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
>>> np.frombuffer(b'\x01\x02',dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.frombuffer(b'\x01\x02\x03\x04\x05',dtype=np.uint8,count=3)
array([1, 2, 3], dtype=uint8)
将原始数据保存到磁盘
>>> dt = np.dtype([('time',[('min',np.int64),('sec',np.int64)]),('temp',float)])>>> x = np.zeros((1,),dtype=dt)
>>> x['time']['min'] = 10; x['temp'] = 98.25
>>> x
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])
>>> import tempfile
>>> fname = tempfile.mkstemp()[1]
>>> x.tofile(fname)
>>> np.save(fname,x)
>>> np.load(fname+'.npy')
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])
function :callable
该函数使用N个参数调用,其中N是的秩 shape。
每个参数代表沿特定轴变化的数组坐标。
例如,如果shape 为(2, 2),
则参数将为(2, 2)array([[0, 0], [1, 1]])
和 array([[0, 1], [0, 1]])
shape :(N,)个整数元组
输出数组的形状,它也确定传递给function的坐标数组的形状。
dtype:bool,可选
传递给function的坐标数组的数据类型。默认情况下dtype为float。
>>> np.fromfunction(lambda i,j: i == j,(3,3),dtype=int)
array([[ True, False, False],
[False, True, False],
[False, False, True]])
>>> np.fromfunction(lambda i,j: i+j,(3,3),dtype=int)
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
>>> np.fromfunction(lambda i,j: i+j,(4,4),dtype=int)
array([[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
iterable :iterable object
提供数组数据的可迭代对象。
dtype :data-type
返回数组的数据类型。
count:int, 可选
从iterable中读取的项目数。默认值为-1,表示读取所有数据
>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable,float)
array([ 0., 1., 4., 9., 16.])
string :str
包含数据的字符串。
dtype :data-type, 可选
数组的数据类型;默认值:浮动。对于二进制输入数据,
数据必须完全采用这种格式。支持大多数内置数字类型,
并且可能支持扩展名类型。
1.18.0版中的新功能:复杂dtypes。
count :int, 可选
dtype从数据中读取此数量的元素。如果为负(默认值),
则将根据数据的长度确定计数。
sep :str, 可选
在数据中分隔数字的字符串;
元素之间的额外空格也会被忽略。
自1.14版本以来已弃用:传递sep= ''(默认值)是已弃用的,
因为它将触发此函数的已弃用二进制模式。
这种模式将字符串解释为二进制字节,
而不是使用十进制数字的ASCII文本,
这种操作更好地拼写为frombuffer(string、dtype、count)。
如果string包含unicode文本,
fromstring的二进制模式将首先使用utf-8 (python 3)
或默认编码(python 2)将其编码为字节,
这两种编码都不会产生正常的结果
>>> np.fromstring('1 2',dtype=int,sep=' ')
array([1, 2])
>>> np.fromstring('1,2',dtype=int,sep=',')
array([1, 2])
fname:file, str,或 pathlib.Path
要读取的文件,文件名或生成器。
如果文件扩展名是.gz或.bz2,
则首先将文件解压缩。 请注意,
生成器应返回字节字符串。
dtype:data-type, 可选
结果数组的数据类型; 默认值:float。
如果这是结构化数据类型,
则结果数组将为一维,并且每一行将被解释为数组的元素。
在这种情况下,使用的列数必须与数据类型中的字段数匹配。
comments:str 或 str的sequence, 可选
用于指示注释开始的字符或字符列表。 无表示无评论。
为了向后兼容,字节字符串将被解码为“latin1”。
默认值为“#”。
delimiter:str, 可选
用于分隔值的字符串。 为了向后兼容,
字节字符串将被解码为“latin1”。 默认为空格。
converters:dict, 可选
将列号映射到函数的字典,
该函数会将列字符串解析为所需的值。 例如,
如果第0列是日期字符串:
converters = {0:datestr2num}。
转换器也可以用于提供缺失数据的默认值
(但另请参见 genfromtxt):
converters = {3:lambda s:float(s.strip()或0)}。
默认值:None。
skiprows:int, 可选
跳过第一行,包括注释; 默认值:0
usecols:int 或 sequence, 可选
要读取的列,第一列为0。 例如,usecols =(1,4,5)
将提取第二,第五和第六列。 默认值为“None”,
将导致读取所有列。在版本1.11.0中进行了更改:
当必须读取单个列时,可以使用整数而不是元组。
例如,usecols = 3读取第四列的方式与
usecols =(3,)相同。
unpack:bool, 可选
如果为True,则对返回的数组进行转置,
以便可以使用x,y,z = loadtxt(...)解压缩参数。
与结构化数据类型一起使用时,将为每个字段返回数组。
默认值为False。
ndmin:int, 可选
返回的数组将至少具有ndmin维。 否则,将挤压一维轴。
合法值:0(默认),1或2。
1.6.0版中的新功能。
encoding:str,可选
用于解码输入文件的编码。 不适用于输入流。
特殊值 ‘bytes’可实现向后兼容的解决方法,
可确保您尽可能接收字节数组作为结果,
并将“latin1”编码的字符串传递给转换器。
覆盖此值以接收unicode数组,
并将字符串作为输入传递给转换器。
如果设置为None,则使用系统默认值。
默认值为 ‘bytes’。
1.14.0版中的新功能。
max_rows:int, 可选
在行数之后读取max_rows内容行。 默认为读取所有行。
注意: 文本里面要用空格隔开,而不是逗号隔开
>>> import os
>>> np.loadtxt(os.path.abspath(".")+"/materials/data.txt")
array([1., 2., 3., 4., 5., 6., 7.])
>>> from io import StringIO #StringIO 具有一个像file对象的行为,但是只能读取一边
>>> c = StringIO("0 1\n 2 3")
>>> np.loadtxt(c) #loadtxt函数可以处理字符串路径也可以处理file对象
array([[0., 1.],
[2., 3.]])
>>> s = StringIO('10.01 21.23- \n 19.22 53.2\n17.57- 63.93')>>> def conv(fld):... return -float(fld[:-1] if fld.endswith(b'-') else fld)
...
>>> np.loadtxt(s,converters={0: conv,1: conv})
array([[-10.01, -21.23],
[-19.22, -53.2 ],
[-17.57, -63.93]])
>>> np.loadtxt(s,converters={0: lambda fld:float(fld[:-1] if fld.endswith(b'-') else fld),1: conv})
array([[ 19.22, -53.2 ],
[ 17.57, -63.93]])
arrayList :list 或 tuple
类数组对象的列表(例如,列表,元组和ndarray)。
dtype :data-type, 可选
所有数组的有效dtype
shape :int 或 ints的tuple, 可选
所得数组的Shape。 如果未提供,则从arrayList [0]推断。
formats, names, titles, aligned, byteorder:
如果dtype为None,
则将这些参数传递给numpy.format_parser以构造dtype。
有关详细文档,请参见该功能。
>>> x1 = np.array([1,2,3,4])
>>> x2 = np.array(['a','dd','xyz','12'])
>>> x3 = np.array([1.1,2,3,4])
>>> r = np.core.records.fromarrays([x1,x2,x3],names="a,b,c")
>>> r[1]
(2, 'dd', 2.)
>>> x1[1] = 34
>>> r.a
array([1, 2, 3, 4])
>>>
>>> x1 = np.array([1,2,3,4])
>>> x2 = np.array(['a','dd','xyz','12'])
>>> x3 = np.array([1.1,2,3,4])
>>> r = np.core.records.fromarrays([x1,x2,x3],dtype=np.dtype([('a',np.int32),('b','S3'),('c',np.float32)]))
>>> r
rec.array([(1, b'a', 1.1), (2, b'dd', 2. ), (3, b'xyz', 3. ),
(4, b'12', 4. )],
dtype=[('a', '<i4'), ('b', 'S3'), ('c', '<f4')])
recList :sequence
同一字段中的数据可能是异构的-它们将被提升为最高数据类型。
dtype :data-type, 可选
所有数组的有效dtype
shape :int 或 int的tuple, 可选
formats, names, titles, aligned, byteorder:
如果dtype为None,
则将这些参数传递给numpy.format_parser以构造dtype。
有关详细文档,请参见该功能。
如果format和dtype均为None,
则将自动检测格式。
使用元组的list而不是列表的list可以加快处理速度。
>>> r = np.core.records.fromrecords([(456,'dbe',1.2),(1,'de',1.3)],names='col1,col2,col3')
>>> r[0]
(456, 'dbe', 1.2)
>>> r.col1
array([456, 1])
>>> r.col2
array(['dbe', 'de'], dtype='<U3')
>>> import pickle
>>> pickle.loads(pickle.dumps(r))
rec.array([(456, 'dbe', 1.2), ( 1, 'de', 1.3)],
dtype=[('col1', '<i8'), ('col2', '<U3'), ('col3', '<f8')])
>>>
datastring :bytes-like
二进制数据缓冲区
dtype :data-type, 可选
shape :int 或int的tuple, 可选
每个数组的形状。
offset :int, 可选
在缓冲区中开始读取的位置。
formats, names, titles, aligned, byteorder:
如果dtype为None,
则将这些参数传递给numpy.format_parser以构造dtype。
>>> a = b'\x01\x02\x03abc'
>>> np.core.records.fromstring(a,dtype='u1,u1,u1,S3')
rec.array([(1, 2, 3, b'abc')],
dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])
fd:str 或 file type
如果file是字符串或类似路径的对象,
则将打开该文件,否则假定它是文件对象。
文件对象必须支持随机访问
(即,它必须具有tell 和 seek方法)。
dtype :data-type, 可选
所有数组的有效dtype
shape :int 或 int的tuple,可选
offset :int, 可选
在文件中开始读取的位置。
formats, names, titles, aligned, byteorder:
如果dtype为None,
则将这些参数传递给numpy.format_parser以构造dtype。
>>> from tempfile import TemporaryFile
>>> a = np.empty(10,dtype='f8,i4,a5')
>>> a[5] = (0.5,10,'abcde')
>>> fd = TemporaryFile()
>>> a = a.newbyteorder('<')
>>> a.tofile(fd)
>>> _ = fd.seek(0)
>>> r = np.core.records.fromfile(fd,formats='f8,i4,a5',shape=10,byteorder='<')
>>> r[5]
(0.5, 10, b'abcde')
>>> r.shape
(10,)
start :number, 可选
stop :number
间隔结束。 该间隔不包括该值,除非在某些情况下,
step不是整数,并且浮点舍入会影响out的长度。
step :number, 可选
值之间的间距。 对于任何输出out,
这是两个相邻值out[i+1] - out[i]之间的距离。
默认步长为1。如果将step指定为位置参数,
则还必须指定start。
dtype :dtype
输出数组的类型。 如果未给出dtype,
则从其他输入参数推断数据类型。
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([0., 1., 2.])
>>> np.arange(3,7,2)
array([3, 5])
>>> a = np.array([(1.0,2),(3.0,4)],dtype='f4,i4').view(np.recarray)
>>> np.asarray(a) is a
False
>>> np.asanyarray(a) is a
True
#浅拷贝
>>> a = np.array([1,'m',[2,3,4]],dtype=object)
>>> b = np.copy(a)
>>> b[2][0] = 10
>>> b
array([1, 'm', list([10, 3, 4])], dtype=object)
>>> a
array([1, 'm', list([10, 3, 4])], dtype=object)
#深拷贝
>>> a = np.array([1,'m',[2,3,4]],dtype=object)
>>> c = copy.deepcopy(a)
>>> c[2][0] = 10
>>> c
array([1, 'm', list([10, 3, 4])], dtype=object)
>>> a
array([1, 'm', list([2, 3, 4])], dtype=object)
>>> g = lambda x:x+1
>>> g(1)
2
>>> g(2)
3
>>> def g(x):
... return x+1
...
>>> g(1)
2
>>> g(2)
3
>>> def is_ood(n):
... return n%2 == 1
...
>>> newlist = filter(is_ood,[1,2,3,4,5,6,7,8,9,10])
>>> newlist
<filter object at 0x7fb774ef1820>
>>> newlist = list(newlist)
>>> newlist
[1, 3, 5, 7, 9]
>>> newlist = filter(lambda x:x%2==1,[1,2,3,4,5,6,7,8,9,10])
>>> newlist = list(newlist)
>>> newlist
[1, 3, 5, 7, 9]
>>> newone = map(lambda x:x**2,[1,2,3,4,5])
>>> newone =list(newone)
>>> newone
[1, 4, 9, 16, 25]
>>> newone = map(lambda x,y:x*y,[1,2,3,4,5],[5,4,3,2,1])
>>> newone = list(newone)
>>> newone
[5, 8, 9, 8, 5]
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> newlist = [x for x in a ]
>>> newlist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> newlist = [x for x in a if x%2 != 0]
>>> newlist
[1, 3, 5, 7, 9]
top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径,文件夹名字, 文件名】。
topdown --可选,
为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。
如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。
onerror -- 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。
followlinks -- 设置为 true,则通过软链接访问目录。
>>> for root,dirs,files in os.walk(os.getcwd()):
... print('----------')
... print(root)
... print(dirs)
... print(files)
... print()
...
----------
/home/user/PythonFires/学习文献/Numpy
['materials', 'images']
['Numpy1.md', 'Numpy2.md', 'math_every.md', 'math.md']
----------
/home/user/PythonFires/学习文献/Numpy/materials
[]
['data.txt']