Home > Article > Backend Development > Detailed introduction to python's numpy module
This article brings you relevant knowledge about python, which mainly introduces related issues about the numpy module. Numpy is the abbreviation of Numerical Python extensions, which literally means Python numerical calculation extension. Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended learning: python video tutorial
## The abbreviation of Numerical Python extensions, literally means Python numerical calculation extension. Numpy is a dependency of many machine learning libraries in python, which implement basic matrix calculations through Numpy. Numpy
supports high-order, large-scale calculations of matrix and vector calculations, and also provides a relatively rich set of functions. In addition, Numpy is based on a more modern programming language - python. Python is popular in the technology circle with its open source, free, flexible, easy to learn, and good engineering features. It has become mainstream programming in the fields of machine learning, data analysis, etc. language. 1. Array type
1.1Basic use of array type
import numpy as np # 通过np.array()方法创建一个名为array的array类型,参数是一个list array = np.array([1, 2, 3, 4]) print(array) # 结果为:[1 2 3 4] # 获取array中元素的最大值 print(array.max()) # 结果为:4 # 获取array中元素的最小值 print(array.min()) # 结果为:1 # 获取array中元素的平均值 print(array.mean()) # 结果为:2.5 # 直接将array乘以2,python将每个元素都乘以2 print(array*2) # 结果为:[2 4 6 8] print(array+1) # 结果为:[2 3 4 5] print(array/2) # 结果为:[0.5 1. 1.5 2. ] # 将每一个元素都除以2,得到浮点数表示的结果 print(array % 2) # 结果为:[1 0 1 0] array_1 = np.array([1, 0, 2, 0]) # 获取该组数据中元素值最大的那个数据的首个索引,下标从0开始 print(array_1.argmax()) # 结果为:2Through the above code, we can understand the basic use of array type in Numpy method. We can see that array is actually a class that is instantiated into an object by passing in a list parameter, thereby encapsulating the data.
1.2 Processing of higher dimensional data
import numpy as np # 创建一个二维数组,用以表示一个3行2列的矩阵 array = np.array([[1, 2], [3, 4], [5, 6]]) print(array) # 查看数据的维度属性,下面输出结果(3,2)表示3行2列 print(array.shape) # 结果为:(3, 2) # 查看元素个数 print(array.size) # 结果为:6 # 查看元素最大值的索引 print(array.argmax()) # 结果为:5 # 将shape为(3,2)的array转换为一行表示 print(array.flatten()) # 结果为:[1 2 3 4 5 6] # 我们可以看到,flatten()方法是将多维数据“压平”为一维数组的过程 #将array数据从shape为(3,2)的形式转为(2,3)的形式 print(array.reshape(2, 3)) '''结果为: [[1 2 3] [4 5 6]]''' #将array数据从shape为(3,2)的形式转为(1,6)的形式 print(array.reshape(1, 6)) # 结果为:[[1 2 3 4 5 6]]The more advanced ones are flatten() and reshape() function, please note that the result returned by reshape() is array type
1.3Numpy creates a special type of array type
import numpy as np # 生成所有元素为 array_zeros = np.zeros((2, 3, 3)) print(array_zeros) '''结果为: [[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]] ''' array_ones = np.ones((2, 3, 3)) print(array_ones) '''结果为: [[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]] ''' print(array_ones.shape) # 结果为:(2, 3, 3)
array_zeros = np.zeros((3, 3)) print(array_zeros) '''结果为: [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] '''
it generates an array with 3 rows and 3 columns
##1.3.2np .arrange() and np.linspace()
Returns uniformly distributed values within a given interval.
Values are generated within the half-open interval ``[start, stop)`` (in other words, the interval including `start` but excluding `stop`). For integer arguments, this function is equivalent to Python's built-in `range` function, but returns an ndarray instead of a list. When using non-integer step sizes (e.g. 0.1), the results are often inconsistent. For these cases, it's better to use `numpy.linspace`. linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Returns evenly distributed numbers within a specified time interval.
Return "num" uniformly distributed samples, calculated on the interval [`start`, `stop`]. start: The starting value of the sequence.
stop:The ending value of the sequence, unless `endpoint` is set to False. In this case, the sequence consists of all but the last "num 1" uniformly distributed samples, thus excluding "stop". Note that when `endpoint` is False, the step size changes.
num=50:The number of samples to be generated. The default value is 50. Must be non-negative.
endpoint=True:If true, `stop` is the last sample. Otherwise, not included. Defaults to true.
retstep=False:如果为 True,则返回 (`samples`, `step`),其中 `step` 是样本之间的间距。
dtype=None:输出数组的类型。如果 `dtype` 没有给出,数据类型是从 `start` 和 `stop` 推断出来的。推断的 dtype 永远不会是整数;即使参数会产生一个整数数组,也会选择`float`。
因此以下代码就很容易理解了
# 生成一个array,从0递增到10,步长为1 array_arange = np.arange(10) print(array_arange) # 结果为:[0 1 2 3 4 5 6 7 8 9] # 生成一个array,从0递增到10,步长为2 array_arange_1 = np.arange(0, 10, 2) print(array_arange_1) # 结果为:[0 2 4 6 8] # 生成一个array,将0-10等分为5部分 array_linspace = np.linspace(0, 10, 5) print(array_linspace) # 结果为:[ 0. 2.5 5. 7.5 10. ]
import numpy as np # 取绝对值 print(np.abs([1, -2, 3, -4])) # [1 2 3 4] # 求正弦值 print(np.sin(np.pi/2)) # 1.0 # 求反正切值 print(np.arctan(1)) # 0.7853981633974483 # 求e的2次方 print(np.exp(2)) # 7.38905609893065 # 求2的三次方 print(np.power(2, 3)) # 8 # 求向量[1,2]与[3,4]的点积 print(np.dot([1, 2], [3, 4])) # 11 # 求开方 print(np.sqrt(4)) # 2.0 # 求和 print(np.sum([1, 2, 3, 4])) # 10 # 求平均值 print(np.mean([1, 2, 3, 4])) #2.5 # 求标准差 print(np.std([1, 2, 3, 4])) # 1.118033988749895
前面我们已经了解到array类型及其基本操作方法,了解array类型可以表示向量、矩阵和多维张量。
线性代数计算在科学计算领域中非常重要,因此接下来了解以下Numpy提供的线性代数操作
import numpy as np vector_a = np.array([1, 2, 3]) vector_b = np.array([2, 3, 4]) # 定义两入向量vector_a与vector_b m = np.dot(vector_a, vector_b) # 将两个向量相乘,在这里也就是点乘,结果为20 print(m) n = vector_a.dot(vector_b) print(n) # 将vector_a与vector_b相乘,结果为20 o = np.dot(vector_a, vector_b.T) print(o) ''' 将一个行向量与一个列向量叉乘的结果相当于将两个行向量求点积,这里测试了dot()方法。其中array类型的T()方法表示转置。 测试结果表明: dot()方法默认对两个向量求点积。对于符合叉乘格式的矩阵,自动进行又乘。''' # 我们看一下下面这个例子: matrix_a = np.array([[1, 2], [3, 4]]) # 定义一个2行2列的方阵 matrix_b = np.dot (matrix_a, matrix_a.T) # 这里将该方阵与其转置叉乘,将结果赋予matrix_b变量 print(matrix_b) '''结果为: array([[5,11], [11,25]])''' p = np.linalg.norm([1, 2]) print(p) # 求一个向量的范数的值,结果为2.2360679774997898 # 如果norm()方法没有指定第2个参数,则默认为求2范数 np.linalg.norm([1, -2], 1) # 指定第2个参数值为1,即求1范数。我们在前面介绍过,1范数的结果为向量中各元素绝对值之和,结果为3.0 q = np.linalg.norm([1, 2, 3, 4], np. inf) print(q) # 求向量的无穷范数,其中np.inf表示正无穷,也就是向量中元素值最大的那个,其结果为4.0 r = np.linalg .norm([1, 2, 3, 4], -np.inf) print(r) # 同理,求负无穷范数的结果为1, 也就是向量中元素的最小值 # 求行列式 s = np.linalg.det(matrix_a) print(s) # -2.0000000000000004 t = np.trace(matrix_a) print(t) # 求矩阵matrix_a的迹,结果为5 u = np.linalg.matrix_rank(matrix_a) # 求矩阵的秩,结果为2 print(u) v = vector_a * vector_b # 使用*符号将两个向量相乘,是将两个向量中的元素分别相乘,也就是我们所讲到的哈达马乘积 print(v) # [ 2 6 12] w = vector_a ** vector_b print(w) # 使用二元运算符**对两个向量进行操作,结果为array([1, 8, 81],dtype = int32) # 表示将向量vector. a中元素对应vector. b中的元素值求幂运算。例如最终结果[1,8,81]可以表示为[1*1,2*2*2,3*3*3*3] # 求逆矩阵 z = np.linalg.inv(matrix_a) print(z) ''' [[-2. 1. ] [ 1.5 -0.5]]'''
Numpy除了为我们提供常规的数学计算函数和矩阵相关操作之外,还提供很多功能丰富的模块,随机数模块就是其中一部分。
利用随机数模块可以生成随机数矩阵,比python自带的随机数模块功能还要强大。
import numpy as np # 设置随机数种子 np.random.seed() # 从[1,3)中生成一个整型的随机数,连续生成10个 a = np.random.randint(1, 3, 10) print(a) # [1 1 1 2 1 1 1 1 2 2] # 若要连续产生[1,3}之间的浮点数,可以使用以下方法: # ① b = 2*np.random.random(10)+1 print(b) ''' [2.88458839 2.07004167 2.80814156 1.83247535 2.33649809 2.62763357 2.0549351 2.33464915 1.70562208 2.66257726]''' # ② c = np.random.uniform(1, 3, 10) print(c) ''' [1.76967412 1.37703868 2.48838004 1.45986254 2.04487418 2.51107658 1.25673115 1.31416097 2.56218317 2.90575438]''' # 生成一个满足正态分布(高斯分布)的矩阵,其维度是4*4 d = np. random.normal(size=(4, 4)) print(d) ''' [[ 0.76164366 0.11588368 0.49221559 -0.28222691] [ 0.47638143 -0.21197541 -1.0776362 0.49241666] [ 0.26038756 -0.20406522 1.11210954 -1.191425 ] [ 0.58255677 1.84047863 -0.21366512 -0.85425828]]''' # 随机产生10个n=5、p=0.5的二项分布数据: e = np.random.binomial(n=5, p=0.5, size=10) print(e) # [1 1 5 2 1 2 1 2 1 2] # 产生一个0到9的序列 data = np.arange(10) print(data) # [0 1 2 3 4 5 6 7 8 9] # 从data数据中随机采集5个样本,采集过程是有放回的 f = np.random.choice(data, 5) print(f) # [1 7 3 3 4] # 从data数据中随机采集5个样本,采集过程是没有放回的 g = np.random.choice(data, 5, replace=False) print(g) # [8 9 1 5 0] # 对data进行乱序 h = np.random.permutation(data) print(h) # [8 5 3 9 2 0 4 6 1 7] # 对data进行乱序,并替换为新的data np.random.shuffle(data) print(data) # [9 7 0 3 8 5 2 1 4 6]
推荐学习:python视频教程
The above is the detailed content of Detailed introduction to python's numpy module. For more information, please follow other related articles on the PHP Chinese website!