Numpy基础知识

2019-10-23  本文已影响0人  jxc1

数据类型

1.数据类型之间的转换

>>> import numpy as np
>>> a=np.arange(12,dtype=float)
>>> a
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])
>>> a=np.arange(12,dtype='i')
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)
>>> a.dtype
dtype('int32')
>>> a.astype('float')
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])

2.数组标量

3.溢出错误

#100的8次方超出32位存储空间
>>> np.power(100, 8, dtype=np.int64)
10000000000000000
>>> np.power(100, 8, dtype=np.int32)
1874919424
>>> np.finfo(float)
finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
>>> np.iinfo(int)
iinfo(min=-2147483648, max=2147483647, dtype=int32)
>>> np.iinfo(np.int64)
iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

如果64位整数仍然太小,则结果可能会转换为浮点数。浮点数提供了更大但不精确的可能值范围。
np.power(100,100,dtype=np.float64)

扩展精度

创建数组

1. 从其他Python结构(例如,列表,元组)转换

>>> a=np.array((1,2,3,4))
>>> a
array([1, 2, 3, 4])
>>> b=np.array([5,6,2])
>>> b
array([5, 6, 2])
>>> c = np.array([[1,2],(3,4)])
>>> c
array([[1, 2],
       [3, 4]])

2. numpy原生数组的创建(例如,arange、ones、zeros等)

np.zeros((n,m)) 填充0
np.ones((n,m))  填充1
#n,m代表数组的形状
np.linspace(n,m,p)
n为开始,p为结束,m为个数

3. 从磁盘读取数组,无论是标准格式还是自定义格式

4. 通过使用字符串或缓冲区从原始字节创建数组

5. 使用特殊库函数(例如,random)

Numpy输入与输出

使用genfromtxt导入数据

定义输入

将行拆分为列

delimiter为None的时候,按制表符分割

>>> from io import BytesIO
>>> data = b"1,2,3\n4,5,6"
>>> np.genfromtxt(BytesIO(data),delimiter=",")
array([[1., 2., 3.],
       [4., 5., 6.]])
>>> data=b"     1       2       2"
>>> np.genfromtxt(BytesIO(data),delimiter=None)
array([1., 2., 2.])
>>> data=b"12345\n  3 4\n56  7"
>>> np.genfromtxt(BytesIO(data),delimiter=(3,2))
array([[123.,  45.],
       [  3.,   4.],
       [ 56.,   7.]])
#数组第一列宽为3,第二列宽为2
>>> data=b"""#
... #comment
... #comment
... 1.2
... 3.4
... 5.6
... #commeng
... """
>>> np.genfromtxt(BytesIO(data),delimiter='.',comments="#")
array([[1., 2.],
       [3., 4.],
       [5., 6.]])
>>> data = "\n".join(str(i) for i in range(10))
>>> np.genfromtxt(BytesIO(data),)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> np.genfromtxt(BytesIO(data),
...               skip_header=3, skip_footer=5)
array([ 3.,  4.])
>>> data = "1 2 3\n4 5 6"
>>> np.genfromtxt(BytesIO(data), usecols=(0, -1))
array([[ 1.,  3.],
       [ 4.,  6.]])
#获取第一列和最后一列

设置名称

>>> data=b"1 2 3\n4 5 6"
>>> np.genfromtxt(BytesIO(data),names="a,b,c",usecols=("a","c"))
array([(1., 3.), (4., 6.)], dtype=[('a', '<f8'), ('c', '<f8')])
>>> np.genfromtxt(BytesIO(data),usecols=(0,-1))
array([[1., 3.],
       [4., 6.]])

选择数据的类型

接受一个dtype参数设置数据类型

>>> np.genfromtxt(BytesIO(data),usecols=(0,-1),dtype=int)
array([[1, 3],
       [4, 6]])
>>> np.genfromtxt(BytesIO(data),usecols=(0,-1),dtype='i4')
array([[1, 3],
       [4, 6]])
>>> np.genfromtxt(BytesIO(data),names="a,b,c",dtype=[('a',int),('b',float)])
array([(1, 2.), (4, 5.)], dtype=[('a', '<i4'), ('b', '<f8')])
>>> np.genfromtxt(BytesIO(data),names="a,b,c",dtype=(int,float,float))
array([(1, 2., 3.), (4, 5., 6.)],
      dtype=[('a', '<i4'), ('b', '<f8'), ('c', '<f8')])

除直接的dtype=int之外,其他的返回的都是一个带有结构化dtype的一维数组。

调整转换

>>> data=b"1,2,3\n4,5,6\n"
>>> names="a,b,c"
>>> np.genfromtxt(BytesIO(data))
array([nan, nan])
>>> np.genfromtxt(BytesIO(data),delimiter=',')
array([[1., 2., 3.],
       [4., 5., 6.]])
>>> np.genfromtxt(BytesIO(data),delimiter=',',names=names)
array([(1., 2., 3.), (4., 5., 6.)],
      dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])
>>> def func(x):
...     return '0'
...
>>> 
np.genfromtxt(BytesIO(data),delimiter=',',dtype=int,names=names,converters={'b':func})
array([(1, 0, 3), (4, 0, 6)],
      dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<i4')])

索引

切片索引

>>> y = np.arange(35).reshape(5,7)
>>> y
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])
>>> y[1:5:2,::3]
array([[ 7, 10, 13],
       [21, 24, 27]])

索引数组

>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])

索引多维数组

>>> y[np.array([0,2,4]), np.array([0,1,2])]
array([ 0, 15, 30])

在这种情况下,如果索引数组具有匹配的形状,并且索引数组的每个维度都有一个索引数组,则结果数组具有与索引数组相同的形状

>>> y[np.array([0,2,4]), np.array([0,1])]
<type 'exceptions.ValueError'>: shape mismatch: objects cannot be
broadcast to a single shape

如果索引数组的形状不同,则尝试将它们广播为相同的形状。如果它们无法广播到相同的形状,则会引发异常

>>> y[np.array([0,2,4]), 1]
array([ 1, 15, 29])

广播机制允许索引数组与其他索引的标量组合

>>> y[np.array([0,2,4])]
array([[ 0,  1,  2,  3,  4,  5,  6],
       [14, 15, 16, 17, 18, 19, 20],
       [28, 29, 30, 31, 32, 33, 34]])

布尔或掩码索引数组

>>> b = y>20
>>> y[b]
array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34])
>>> b[:,5]
array([False, False, False,  True,  True])
>>> y[b[:,5]]
array([[21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])
#此时的b[:,5]相当于【3,4】
>>> x
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14]],

       [[15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]]])
>>> a=np.array([True,True,False,True,False])
>>> a
array([ True,  True, False,  True, False])
>>> b=np.array([[a,a,a],[a,a,a]])
>>> b
array([[[ True,  True, False,  True, False],
        [ True,  True, False,  True, False],
        [ True,  True, False,  True, False]],

       [[ True,  True, False,  True, False],
        [ True,  True, False,  True, False],
        [ True,  True, False,  True, False]]])
>>> x[b]
array([ 0,  1,  3,  5,  6,  8, 10, 11, 13, 15, 16, 18, 20, 21, 23, 25, 26,
       28])

在程序中使用索引

>>> indices = (1,1,1,1)
>>> z[indices]
40
>>> indices = (1,1,1,slice(0,2)) # same as [1,1,1,0:2]
>>> z[indices]
array([39, 40])
>>> indices = (1, Ellipsis, 1) # same as [1,...,1]
>>> z[indices]
array([[28, 31, 34],
       [37, 40, 43],
       [46, 49, 52]])

广播

术语广播(Broadcasting)描述了 numpy 如何在算术运算期间处理具有不同形状的数组

>>> a = np.array([1.0, 2.0, 3.0])
>>> b = 2.0
>>> a * b
array([ 2.,  4.,  6.])

一般广播规则

在两个数组上运行时,NumPy会逐元素地比较它们的形状。它从尾随尺寸开始,并向前发展。两个尺寸兼容时
1.他们是平等的,或者
2.其中一个是1

>>> import numpy as np
>>> a=np.array([[1,2,3],[3,4,5],[5,6,7]])
>>> b=np.array([1,2,3]),
>>> a*b
array([[ 1,  4,  9],
       [ 3,  8, 15],
       [ 5, 12, 21]])
A      (2d array):  5 x 4
B      (1d array):      1
Result (2d array):  5 x 4

A      (2d array):  5 x 4
B      (1d array):      4
Result (2d array):  5 x 4

A      (3d array):  15 x 3 x 5
B      (3d array):  15 x 1 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 1
Result (3d array):  15 x 3 x 5
>>> x = np.arange(4)
>>> xx = x.reshape(4,1)
>>> y = np.ones(5)
>>> z = np.ones((3,4))
>>> x.shape
(4,)
>>> y.shape
(5,)
>>> x + y
ValueError: operands could not be broadcast together with shapes (4,) (5,)
>>> xx.shape
(4, 1)
>>> y.shape
(5,)
>>> (xx + y).shape
(4, 5)
>>> xx + y
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.,  4.]])
>>> x.shape
(4,)
>>> z.shape
(3, 4)
>>> (x + z).shape
(3, 4)
>>> x + z
array([[ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.]])
>>> a = np.array([0.0, 10.0, 20.0, 30.0])
>>> b = np.array([1.0, 2.0, 3.0])
>>> a[:, np.newaxis] + b
array([[  1.,   2.,   3.],
       [ 11.,  12.,  13.],
       [ 21.,  22.,  23.],
       [ 31.,  32.,  33.]])

字节交换

字节排序和ndarrays

ndarray是一个为内存中的数据提供python数组接口的对象

结构化数组

>>> import numpy as np
>>> x = np.array([('李栋良',22,84),('Daenerys',22,55)],
...              dtype=[('name','U10'),('age','i4'),('weight','f4')]
...
...
... )
>>> x
array([('李栋良', 22, 84.), ('Daenerys', 22, 55.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
>>> x[name]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> x['name']
array(['李栋良', 'Daenerys'], dtype='<U10')
>>> x['age']=23
>>> x
array([('李栋良', 23, 84.), ('Daenerys', 23, 55.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
>>> x['weight'][1]=54
>>> x
array([('李栋良', 23, 84.), ('Daenerys', 23, 54.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])

结构化数据类型创建

>>> np.dtype({'names':['A','B'],'formats':['i4','f4']})
dtype([('A', '<i4'), ('B', '<f4')])

可以通过names使用相同长度的字符串序列分配属性来修改字段名称。

dtype对象还具有类似字典的属性,fields其键是字段名称, 其值是包含每个字段的dtype和字节偏移量的元组。

>>> d.fields
mappingproxy({'x': (dtype('int64'), 0), 'y': (dtype('float32'), 8)})
>>> d=np.dtype('u1,i8,f4,u2,u1,i4')
>>> d.fields
mappingproxy({'f0': (dtype('uint8'), 0), 'f1': (dtype('int64'), 1), 'f2': (dtype('float32'), 9), 'f3': (dtype('uint16'), 13), 'f4': (dtype('uint8'), 15), 'f5': (dtype('int32'), 16)})
>>> [d.fields[name][1] for name in d.names]
[0, 1, 9, 13, 15, 16]
>>> d=np.dtype('u1,i8,f4,u2,u1,i4',align=True)
>>> [d.fields[name][1] for name in d.names]
[0, 8, 16, 20, 22, 24]

索引和分配给结构化数组

>>> x=np.array(2,dtype='i4,u1')
>>> x
array((2, 2), dtype=[('f0', '<i4'), ('f1', 'u1')])

索引

>>> x = np.zeros((2, 2), dtype=[('a', np.int32), ('b', np.float64, (3, 3))])
>>> x
array([[(0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]),
        (0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])],
       [(0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]),
        (0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])]],
      dtype=[('a', '<i4'), ('b', '<f8', (3, 3))])
>>> x['a'].shape
(2, 2)
>>> x['b'].shape
(2, 2, 3, 3)
>>> a=np.zeros(3)
>>> a
array([0., 0., 0.])
>>> a=np.zeros(3, dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'f4')])
>>> a
array([(0, 0, 0.), (0, 0, 0.), (0, 0, 0.)],
      dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<f4')])
>>> a[['a','c']]
array([(0, 0.), (0, 0.), (0, 0.)],
      dtype={'names':['a','c'], 'formats':['<i4','<f4'], 'offsets':[0,8], 'itemsize':12})

结构比较

>>> a=np.ones(2,dtype='i4,u1')
>>> a
array([(1, 1), (1, 1)], dtype=[('f0', '<i4'), ('f1', 'u1')])
>>> b=np.ones(2,dtype='i4,u1')
>>> a==b
array([ True,  True])

记录数组

上一篇 下一篇

猜你喜欢

热点阅读