Home >Backend Development >Python Tutorial >Summary of simple usage of python numpy

Summary of simple usage of python numpy

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-08-21 17:59:302771browse

Summary of simple usage of python numpy

Simple usage of Numpy

import numpy as np

1. Create ndarray object

Convert the list to ndarray:

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

Get a random floating point number

>>> np.random.rand(3, 4)
array([[ 0.16215336,  0.49847764,  0.36217369,  0.6678112 ],
     [ 0.66729648,  0.86538771,  0.32621889,  0.07709784],
     [ 0.05460976,  0.3446629 ,  0.35589223,  0.3716221 ]])

Get a random integer

>>> np.random.randint(1, 5, size=(3,4))
array([[2, 3, 1, 2],
     [3, 4, 4, 4],
     [4, 4, 4, 3]])

Get zero

>>> np.zeros((3,4))
array([[ 0.,  0.,  0.,  0.],
     [ 0.,  0.,  0.,  0.],
     [ 0.,  0.,  0.,  0.]])

Get one

>>> np.ones((3,4))
array([[ 1.,  1.,  1.,  1.],
     [ 1.,  1.,  1.,  1.],
     [ 1.,  1.,  1.,  1.]])

Get empty (it’s best not to use it, find out more about it, the versions are different return The values ​​are different)

>>> np.empty((3,4))
array([[ 1.,  1.,  1.,  1.],
     [ 1.,  1.,  1.,  1.],
     [ 1.,  1.,  1.,  1.]])

Take the integer zero or one

>>> np.ones((3,4),int)
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
>>> np.zeros((3,4),int)
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

Imitate the range command to create ndarray:

>>> np.arange(2,10,2) # 开始,结束,步长
array([2, 4, 6, 8])

Related recommendations: "Python Video Tutorial"

2. Viewing and operating ndarray attributes:

Look at ndarray attributes:

>>> a = [[1,2,3,4,5],[6,7,8,9,0]]
>>> b = np.array(a)
>>> b.ndim  #维度个数(看几维)
2
>>> b.shape  #维度大小(看具体长宽)
(5,2)
>>>b.dtype
dtype('int32')

Specify attributes when creating ndarray:

>>> np.array([1,2,3,4,5],dtype=np.float64)
array([ 1.,  2.,  3.,  4.,  5.])
>>> np.zeros((2,5),dtype=np.int32)
array([[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0]])

Attribute transfer:

>>> a = np.array([1,2,3,4,5],dtype=np.float64)
>>> a
array([ 1.,  2.,  3.,  4.,  5.])
>>> a.astype(np.int32)
array([1, 2, 3, 4, 5])

三, Simple operation:

Batch operation:

>>> a = np.array([1,2,3,4,5],dtype=np.int32)
>>> a
array([1, 2, 3, 4, 5])
>>> a + a
array([ 2,  4,  6,  8, 10])
>>> a * a
array([ 1,  4,  9, 16, 25])
>>> a - 2
array([-1,  0,  1,  2,  3])
>>> a / 2
array([ 0.5,  1. ,  1.5,  2. ,  2.5])
#等等

Change dimension:

>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0]],dtype=np.int32)
>>> a
array([[1, 2, 3, 4, 5],
       [6, 7, 8, 9, 0]])
>>> a.reshape((5,2))
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8],
       [9, 0]])

Matrix conversion (It is essentially different from changing dimensions, please be careful):

>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0]],dtype=np.int32)
>>> a
array([[1, 2, 3, 4, 5],
       [6, 7, 8, 9, 0]])
>>> a.transpose()
array([[1, 6],
       [2, 7],
       [3, 8],
       [4, 9],
       [5, 0]])

Disrupt (can only disrupt one dimension):

>>> a = np.array([[1,2],[3,4],[5,6],[7,8],[9,0]],dtype=np.int32)
>>> a
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8],
       [9, 0]])
       
>>> np.random.shuffle(a)
>>> a
array([[9, 0],
       [1, 2],
       [7, 8],
       [5, 6],
       [3, 4]])

4. Slicing and indexing:

One-dimensional array:

>>> a = np.array(range(10))
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[3]
3
>>> a[2:9:2]
array([2, 4, 6, 8])

Multi-dimensional array:

>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0],[11,12,13,14,15]],dtype=np.int32)
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9,  0],
       [11, 12, 13, 14, 15]])
       
>>> a[:, 1:4]
array([[ 2,  3,  4],
       [ 7,  8,  9],
       [12, 13, 14]])

Conditions Index:

>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0],[11,12,13,14,15]],dtype=np.int32)
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9,  0],
       [11, 12, 13, 14, 15]])
       
>>> a > 5
array([[False, False, False, False, False],
       [ True,  True,  True,  True, False],
       [ True,  True,  True,  True,  True]], dtype=bool)
>>> a[a>5]
array([ 6,  7,  8,  9, 11, 12, 13, 14, 15])
>>> a%3 == 0
Out[128]: 
array([[False, False,  True, False, False],
       [ True, False, False,  True,  True],
       [False,  True, False, False,  True]], dtype=bool)
>>> a[a%3 == 0]
array([ 3,  6,  9,  0, 12, 15])

5. Function (numpy core knowledge points)

Calculation function:

np.ceil(): 向上最接近的整数,参数是 number 或 array
np.floor(): 向下最接近的整数,参数是 number 或 array
np.rint(): 四舍五入,参数是 number 或 array
np.isnan(): 判断元素是否为 NaN(Not a Number),参数是 number 或 array
np.multiply(): 元素相乘,参数是 number 或 array
np.divide(): 元素相除,参数是 number 或 array
np.abs():元素的绝对值,参数是 number 或 array
np.where(condition, x, y): 三元运算符,x if condition else y
>>> a = np.random.randn(3,4)
>>> a
array([[ 0.37091654,  0.53809133, -0.99434523, -1.21496837],
       [ 0.00701986,  1.65776152,  0.41319601,  0.41356973],
       [-0.32922342,  1.07773886, -0.27273258,  0.29474435]])
>>> np.ceil(a)      
array([[ 1.,  1., -0., -1.],
       [ 1.,  2.,  1.,  1.],
       [-0.,  2., -0.,  1.]])
>>> np.where(a>0, 10, 0)
array([[10, 10,  0,  0],
       [10, 10, 10, 10],
       [ 0, 10,  0, 10]])

Statistical function

np.mean():所有元素的平均值
np.sum():所有元素的和,参数是 number 或 array
np.max():所有元素的最大值
np.min():所有元素的最小值,参数是 number 或 array
np.std():所有元素的标准差
np.var():所有元素的方差,参数是 number 或 array
np.argmax():最大值的下标索引值,
np.argmin():最小值的下标索引值,参数是 number 或 array
np.cumsum():返回一个一维数组,每个元素都是之前所有元素的累加和
np.cumprod():返回一个一维数组,每个元素都是之前所有元素的累乘积,参数是 number 或 array
>>> a = np.arange(12).reshape(3,4).transpose()
>>> a
array([[ 0,  4,  8],
       [ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11]])
>>> np.mean(a)
5.5
>>> np.sum(a)
66
>>> np.argmax(a)
11
>>> np.std(a)
3.4520525295346629
>>> np.cumsum(a)
array([ 0,  4, 12, 13, 18, 27, 29, 35, 45, 48, 55, 66], dtype=int32)

Judgment function:

np.any(): 至少有一个元素满足指定条件,返回True
np.all(): 所有的元素满足指定条件,返回True
>>> a = np.random.randn(2,3)
>>> a
array([[-0.65750548,  2.24801371, -0.26593284],
       [ 0.31447911, -1.0215645 , -0.4984958 ]])
>>> np.any(a>0)
True
>>> np.all(a>0)
False

Remove duplicates:

np.unique(): 去重
>>> a = np.array([[1,2,3],[2,3,4]])
>>> a
array([[1, 2, 3],
       [2, 3, 4]])
>>> np.unique(a)
array([1, 2, 3, 4])

The above is the detailed content of Summary of simple usage of python numpy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete