NumPy 的使用
安装方法使用方法dtype选项其他创建数组的方式基本运算矩阵运算函数使用索引,切片,迭代条件和布尔数组数组的形状操作数组操作其他的一般概念
NumPy is a basic package for scientific computing with Python and especially for data analysis. In fact, this library is the basis of a large amount of mathematical and scientific Python packages, with its powerful support,we can do a lot of things
1. 安装方法
sudo apt-get install python-numpy
pip
install numpy
conda
install numpy
2. 使用方法
>>> import numpy
as np
创建最简单的一维数组
>>> a
= np
.array
([1,2,3,4])
>>> a
.size
4
>>> a
.itemsize
4
>>> a
.ndim
1
>>> a
.shape
(4,)
>>> a
.dtype
dtype
('int32')
当然也可以创建多维数组
>>> b
= np
.array
([[1,3.4,2],[2,4,5]])
>>> b
.size
6
>>> b
.itemsize
8
>>> b
.ndim
2
>>> b
.shape
(2, 3)
>>> b
.dtype
dtype
('float64')
3. dtype 选项
Data TypeDescription
bool_boolean (true or false) stored as a byteint_Default integer type (same as C long; normally either int64 or int32)intcidentical to C int (normally int32 or int64)intpinteger used for indexing (same as C size_t; normally either int32 or int64)int8byte (–128 to 127)int16integer (–32768 to 32767)int64integer (–9223372036854775808 to 9223372036854775807)uint8unsigned integer (0 to 255) uint16 unsigned integer (0 to 65535)uint32unsigned integer (0 to 4294967295)uint64unsigned integer (0 to 18446744073709551615)float_Shorthand for float64float16half precision float: sign bit, 5-bit exponent, 10-bit mantissafloat32Single precision float: sign bit, 8-bit exponent, 23-bit mantissafloat64Double precision float: sign bit, 11-bit exponent, 52-bit mantissacomplex_Shorthand for complex128complex64Complex number, represented by two 32-bit floats (real and imaginary components)complex128Complex number, represented by two 64-bit floats (real and imaginary components)
>>> f
= np
.array
([1,2,3],dtype
=complex)
>>> f
array
([1.+0.j, 2.+0.j, 3.+0.j])
4. 其他创建数组的方式
创建元素全为0的指定大小数组
>>> np
.zeros
((3,3))
array
([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
创建元素全为1的指定大小数组
>>> np
.ones
((3,3))
array
([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
创建一个顺序数组
>>> np
.arange
(0,10)
array
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np
.arange
(0,10,3)
array
([0, 3, 6, 9])
>>> np
.linspace
(0,10,5)
array
([ 0. , 2.5, 5. , 7.5, 10. ])
创建随机数组
>>> np
.random
.random
(4)
array
([0.96215695, 0.64629081, 0.86759819, 0.90073058])
>>> np
.random
.random
((3,3))
array
([[0.27513149, 0.11492121, 0.3915962 ],
[0.08766173, 0.46365986, 0.0249593 ],
[0.42738941, 0.00530375, 0.48070045]])
reshape函数变化数组形状,但元素数目不变
>>> a
= np
.arange
(12).reshape
((3,4))
>>> a
array
([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
5. 基本运算
>>> a
array
([0, 1, 2, 3])
>>> a
+ 4
array
([4, 5, 6, 7])
>>> a
* 2
array
([0, 2, 4, 6])
>>> b
= np
.arange
(4,8)
>>> b
array
([4, 5, 6, 7])
>>> a
+ b
array
([ 4, 6, 8, 10])
>>> a
- b
array
([-4, -4, -4, -4])
>>> a
* b
array
([ 0, 5, 12, 21])
>>> a
* np
.sin
(b
)
array
([-0. , -0.95892427, -0.558831 , 1.9709598 ]) >>> a
* np
.sqrt
(b
)
array
([0. , 2.23606798, 4.89897949, 7.93725393])
6. 矩阵运算
矩阵乘法
>>> A
= np
.array
([[1,2,3], [4, 5, 6], [7,8,9]])
>>> A
array
([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> B
array
([[2, 3, 4],
[5, 6, 7],
[8, 9, 0]])
>>> A
.dot
(B
)
array
([[ 36, 42, 18],
[ 81, 96, 51],
[126, 150, 84]])
>>> np
.dot
(A
, B
)
array
([[ 36, 42, 18],
[ 81, 96, 51],
[126, 150, 84]])
7. 函数使用
一般函数
array
([1, 2, 3, 4])
>>> np
.sqrt
(a
)
array
([1. , 1.41421356, 1.73205081, 2. ])
>>> np
.sin
(a
)
array
([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
>>> np
.log
(a
)
array
([0. , 0.69314718, 1.09861229, 1.38629436])
>>> np
.cos
(a
)
array
([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])
>>> np
.tan
(a
)
array
([ 1.55740772, -2.18503986, -0.14254654, 1.15782128])
>>> np
.tanh
(a
)
array
([0.76159416, 0.96402758, 0.99505475, 0.9993293 ])
>>> np
.sinh
(a
)
array
([ 1.17520119, 3.62686041, 10.01787493, 27.2899172 ])
聚合函数
>>> a
= np
.array
([3.3, 4.5,1.2,5.7,0.3])
>>> a
.sum()
15.0
>>> a
.min()
0.3
>>> a
.max()
5.7
>>> a
.mean
()
3.0
>>> a
.std
()
2.0079840636817816
8. 索引,切片,迭代
索引
>>> a
= np
.arange
(10,16)
>>> a
array
([10, 11, 12, 13, 14, 15])
>>> a
[4]
14
>>> a
[-1]
15
>>> a
[-6]
10
>>> a
[[1,3,4]]
array
([11, 13, 14])
>>> A
= np
.arange
(10,19).reshape
((3,3))
>>> A
array
([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> A
[1,2]
15
切片
>>> a
= np
.arange
(10,16)
>>> a
array
([10, 11, 12, 13, 14, 15])
>>> a
[1:5]
array
([11, 12, 13, 14])
>>> a
[1:5:2]
array
([11, 13])
>>> a
[::2]
array
([10, 12, 14])
>>> a
[:5:]
array
([10, 11, 12, 13, 14])
>>> A
= np
.arange
(10,19).reshape
((3,3))
>>> A
array
([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> A
[0,:]
array
([10, 11, 12])
>>> A
[:,0]
array
([10, 13, 16])
>>> A
[0:2,0:2]
array
([[10, 11],
[13, 14]])
>>> A
[[0,2],0:2]
array
([[10, 11],
[16, 17]])
迭代
>>> for i
in a
:
... print(i
)
...
10
11
12
13
14
15
>>> for row
in A
:
... print(row
)
...
[10 11 12]
[13 14 15]
[16 17 18]
>>> for item
in A
.flat
:
... print(item
)
...
10
11
12
13
14
15
16
17
18
聚合函数
>>> A
= np
.arange
(10,19).reshape
((3,3))
>>> A
array
([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> np
.apply_along_axis
(np
.mean
,axis
=0,arr
=A
)
array
([13., 14., 15.])
>>> np
.apply_along_axis
(np
.mean
,axis
=1,arr
=A
)
array
([11., 14., 17.])
>>> def foo(x
):
... return x
/2
...
>>> np
.apply_along_axis
(foo
, axis
=1,arr
=A
)
array
([[5. , 5.5, 6. ],
[6.5, 7. , 7.5],
[8. , 8.5, 9. ]])
9. 条件和布尔数组
>>> A
= np
.random
.random
((4,4))
>>> A array
([[0.77114544, 0.08274154, 0.3380893 , 0.09124811],
[0.38458493, 0.04101999, 0.5752505 , 0.36101901],
[0.281558 , 0.17994346, 0.5637831 , 0.82207882],
[0.87628886, 0.84768998, 0.9459325 , 0.93927827]])
>>> A
< 0.5
[ True, True, False, True],
[ True, True, False, False],
[False, False, False, False]])
>>> A
[A
< 0.5]
0.36101901, 0.281558 , 0.17994346])
10. 数组的形状操作
>>> a
= np
.random
.random
(12)
>>> a
array
([0.79770216, 0.56088829, 0.82827561, 0.77196219, 0.98281623,
0.69956778, 0.11579138, 0.22625572, 0.13765588, 0.13327294,
0.5892318 , 0.55351276])
>>> A
= a
.reshape
(3,4)
>>> A
array
([[0.79770216, 0.56088829, 0.82827561, 0.77196219],
[0.98281623, 0.69956778, 0.11579138, 0.22625572],
[0.13765588, 0.13327294, 0.5892318 , 0.55351276]])
>>> a
.shape
= (3,4)
>>> a
array
([[0.79770216, 0.56088829, 0.82827561, 0.77196219],
[0.98281623, 0.69956778, 0.11579138, 0.22625572],
[0.13765588, 0.13327294, 0.5892318 , 0.55351276]])
>>> a
= a
.ravel
()
>>> a
array
([0.79770216, 0.56088829, 0.82827561, 0.77196219, 0.98281623,
0.69956778, 0.11579138, 0.22625572, 0.13765588, 0.13327294,
0.5892318 , 0.55351276])
>>> A
.transpose
()
array
([[0.79770216, 0.98281623, 0.13765588],
[0.56088829, 0.69956778, 0.13327294],
[0.82827561, 0.11579138, 0.5892318 ],
[0.77196219, 0.22625572, 0.55351276]])
11. 数组操作
数组的合并
>>> A
= np
.ones
((3,3))
>>> B
= np
.zeros
((3,3))
>>> np
.vstack
((A
,B
))
array
([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
>>> np
.hstack
((A
,B
))
array
([[1., 1., 1., 0., 0., 0.],
[1., 1., 1., 0., 0., 0.],
[1., 1., 1., 0., 0., 0.]])
>>> a
= np
.array
([0,1,2])
>>> b
= np
.array
([3,4,5,])
>>> c
= np
.array
([6,7,8])
>>> np
.column_stack
((a
,b
,c
))
array
([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
>>> np
.row_stack
((a
,b
,c
))
array
([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
数组的切分
>>> A
= np
.arange
(16).reshape
(4,4)
>>> A
array
([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> [B
,C
] = np
.hsplit
(A
, 2)
>>> B
array
([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]])
>>> C
array
([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])
>>> [B
,C
] = np
.vsplit
(A
, 2)
>>> B
array
([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> C
array
([[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> [A1
,A2
,A3
] = np
.split
(A
,[1,3],axis
=1)
>>> A1
array
([[ 0],
[ 4],
[ 8],
[12]])
>>> A2
array
([[ 1, 2],
[ 5, 6],
[ 9, 10],
[13, 14]])
>>> A3
array
([[ 3],
[ 7],
[11],
[15]])
12. 其他的一般概念
对象的浅拷贝和深拷贝是两个不同的概念:浅拷贝之后的对象执行任何操作,原对象都不会受到影响,但是深拷贝的对象会随之改变
>>> a
= np
.array
([1, 2, 3, 4])
>>> b
= a
>>> b
array
([1, 2, 3, 4])
>>> a
[2] = 0
>>> b
array
([1, 2, 0, 4])
>>> c
= a
[0:2]
>>> c
array
([1, 2])
>>> a
[0] = 0
>>> c
array
([0, 2])
>>> a
= np
.array
([1, 2, 3, 4])
>>> c
= a
.copy
()
>>> c
array
([1, 2, 3, 4])
>>> a
[0] = 0
>>> c
array
([1, 2, 3, 4])
numpy中数组的向量化,当numpy中直接执行连个数组相乘的时候,并不会获得理想的结果,而只是对应位置的相乘,for example:
>>> A
array
([[0.67874916, 0.09553741, 0.39305942],
[0.02969179, 0.97060658, 0.93600762],
[0.77268671, 0.2525612 , 0.03904034]])
>>> A
= np
.arange
(6).reshape
(2,3)
>>> A
array
([[0., 1., 2.],
[3., 4., 5.]])
>>> B
= np
.ones
((2,3))
>>> A
* B
array
([[0., 1., 2.],
[3., 4., 5.]])
广播
当两个及两个以上的数组满足一定的条件之后,即使两个数组的形状不同可以进行运算,称为广播运算 条件:他们所有的维度都可以兼容,每一个维数都必须相同,或者有一个必须为1
>>> A
= np
.arange
(16).reshape
(4, 4)
>>> b
= np
.arange
(4)
>>> A
array
([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> b
array
([0, 1, 2, 3])
>>> np
.row_stack
((b
,b
,b
,b
))
array
([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
结构体数组, 数组中的元素可以是不同的,数据类型可以表示为:
类型表示
bytesb1inti1, i2, i4, i8unsigned intsu1, u2, u4, u8floatsf2, f4, f8complexc8, c16fixed length stringsa<n>
>>> structured
= np
.array
([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3, 2-2j), (3, 'Third', 0.8, 1+3j)],dtype
=('i2, a6, f4, c8'))
>>> structured
array
([(1, b
'First', 0.5, 1.+2.j), (2, b
'Second', 1.3, 2.-2.j),
(3, b
'Third', 0.8, 1.+3.j)],
dtype
=[('f0', '<i2'), ('f1', 'S6'), ('f2', '<f4'), ('f3', '<c8')])
>>> structured
= np
.array
([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3, 2-2j), (3, 'Third', 0.8, 1+3j)],dtype
=('int16, a6, float32, complex64'))
>>> structured
array
([(1, b
'First', 0.5, 1.+2.j), (2, b
'Second', 1.3, 2.-2.j),
(3, b
'Third', 0.8, 1.+3.j)],
dtype
=[('f0', '<i2'), ('f1', 'S6'), ('f2', '<f4'), ('f3', '<c8')])
>>> structured
[1]
(2, b
'Second', 1.3, 2.-2.j)
>>> structured
['f1']
array
([b
'First', b
'Second', b
'Third'], dtype
='|S6')
Numpy 的主要操作就介绍到这里了,当然这只是沧海一粟,想要获得更多的资料,可以参考官方网站