Python Numpy函数和方法完全总结

官网在这.

https://numpy.org/doc/1.17/user/quickstart.html

1 基础

1.1 函数(functions),方法(method)或者属性(attribute)

NumPy’s array class is called ndarray. It is also known by the alias array. Note that numpy.array is not the same as the Standard Python Library class array.array, which only handles one-dimensional arrays and offers less functionality. The more important attributes of an ndarray object are:

  • ndarray.ndim属性

array的维度数.

  • ndarray.shape方法

array的维度大小.类似于R中的dim()函数

  • ndarray.size方法

array的cell(数据点)数目.

  • ndarray.dtype方法

array中元素的数据类型.

  • ndarray.itemsize方法

The size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.

1.2 例子

import numpy as np
a = np.arange(15).reshape(3, 5)
a
## array([[ 0,  1,  2,  3,  4],
##        [ 5,  6,  7,  8,  9],
##        [10, 11, 12, 13, 14]])
a.ndim
## 2
a.shape
## (3, 5)
a.size
## 15
a.dtype
## dtype('int32')
a.itemsize
## 4

2 创建array

  • array()函数

创建array.可以将列表,元组等转变为array对象.

import numpy as np
np.array((1,2,3,4))
## array([1, 2, 3, 4])
np.array([1,2,3,4,5])
## array([1, 2, 3, 4, 5])
np.array([(1,2,3), (4,5,6)])
## array([[1, 2, 3],
##        [4, 5, 6]])

有时候我们需要提前创建一个array,然后再往里面填充内容.比如在一个循环中,每一个循环生成一些数,然后再填充到array中.

  • np.zeros(),np.ones()np.empty()函数

生成一个array,然后其中用0/1或者随机的非常小的数填充.参数shape用来设定array的维度.

##zero
np.zeros(shape = 2)
## array([0., 0.])
np.zeros(shape = (3,4))
## array([[0., 0., 0., 0.],
##        [0., 0., 0., 0.],
##        [0., 0., 0., 0.]])
np.zeros(shape = (3,4,5))
##one
## 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.]],
## 
##        [[0., 0., 0., 0., 0.],
##         [0., 0., 0., 0., 0.],
##         [0., 0., 0., 0., 0.],
##         [0., 0., 0., 0., 0.]]])
np.ones(shape = 2)
## array([1., 1.])
np.ones(shape = (3,4))
##empty
## array([[1., 1., 1., 1.],
##        [1., 1., 1., 1.],
##        [1., 1., 1., 1.]])
np.empty(shape = 2)
## array([1., 1.])
np.empty(shape = (3,4))
## array([[1., 1., 1., 1.],
##        [1., 1., 1., 1.],
##        [1., 1., 1., 1.]])
  • np.arange()函数

跟基础函数arange类似,可以生成连续的array.所以同样三个参数,start,stopstep.

np.arange(1, 10, 2)
## array([1, 3, 5, 7, 9])

这种办法,end参数不一定包含在最后的sequence中.为了让最后的end一定包含再sequence中,然后中间的参数,是使用线性回归得到的,可以使用另外一个参数.

  • np.linspace()函数

三个参数,start,stopnum.

np.linspace(start=0, stop=10, num=8)
## array([ 0.        ,  1.42857143,  2.85714286,  4.28571429,  5.71428571,
##         7.14285714,  8.57142857, 10.        ])
  • reshape()方法和reshape()函数

用来将array重新进行shape.

arr = np.arange(1,10,1)
arr
## array([1, 2, 3, 4, 5, 6, 7, 8, 9])
arr.reshape(3,3)
## array([[1, 2, 3],
##        [4, 5, 6],
##        [7, 8, 9]])
arr.reshape(3,3,order="C")
## array([[1, 2, 3],
##        [4, 5, 6],
##        [7, 8, 9]])
arr.reshape(3,3,order="F")
## array([[1, 4, 7],
##        [2, 5, 8],
##        [3, 6, 9]])
arr.reshape(3,3,order="A")
## array([[1, 2, 3],
##        [4, 5, 6],
##        [7, 8, 9]])
np.reshape(arr, newshape=(3,3))
## array([[1, 2, 3],
##        [4, 5, 6],
##        [7, 8, 9]])

3 基础运算符

array是支持向量化运算的,所以可以直接对array进行常见的数学运算.注意,如果两个array长度不一致,会报错.

a = np.array( [20,30,40,50] )
b = np.arange(4)
a
## array([20, 30, 40, 50])
b
## array([0, 1, 2, 3])
a + b
## array([20, 31, 42, 53])
a - b
## array([20, 29, 38, 47])
a * b
## array([  0,  30,  80, 150])
a * 2
## array([ 40,  60,  80, 100])
a / 2
## array([10., 15., 20., 25.])
a < 2
## array([False, False, False, False])
Avatar
Xiaotao Shen
Postdoctoral Research Fellow

Metabolomics, Multi-omics, Bioinformatics, Systems Biology.

Related

Next
Previous
comments powered by Disqus