Chapter 2, detailed explanation of common functions of python-numpy

Note: The size in the meaning of the following function refers to the value of the attribute shape of the array

Introduction to Numpy

NumPy is a Python package. It stands for "Numeric Python". It is a library consisting of multidimensional array objects and a collection of routines for manipulating arrays.

Numeric, the predecessor of NumPy, was developed by Jim Hugunin. Another package, Numarray, has also been developed which has some additional functionality. In 2005, Travis Oliphant created the NumPy package by integrating the functionality of Numarray into the Numeric package. This open source project has many contributors.

Students who learn Python should know the Numpy library, and also know that this library is an indispensable library in the field of machine learning. Here I have compiled some commonly used functions of the Numpy library for you, so that you can find it when you use it.

Numpy library function explanation

Numpy data types

type meaning
np.int8 and np.uint8 integer (-128 to 127) and unsigned integer (0 to 255)
np.int16 and np.uint16 Integer (-32768 to 32767) and unsigned integer (0 to 65535)
np.int32 and np.uint32 integers (-2147483648 to 2147483647) and unsigned integers (0 to 4294967295)
np.int64 and np.uint64 integers (-9223372036854775808 to 9223372036854775807) and unsigned integers (0 to 18446744073709551615)
np.float16 Half-precision floating-point number (accurate to the last four digits after the decimal point)
np.float32 Single-precision floating-point number (accurate to the last 8 digits after the decimal point)
np.float64 double precision floating point
np.complex64 Complex numbers, represented by two 32-bit floating-point numbers (consisting of real and imaginary numbers)
np.complex128 Complex numbers, represented by two 64-bit floating-point numbers (consisting of real and imaginary numbers)
np.bool_ Boolean value consisting of True and False

Creation of Numpy

function meaning
np.array(object, dtype=None,copy=True) odject = [] or (), create a one-dimensional group. object = [[],[],…] or ((),()…), create a two-dimensional array. dtype can choose the data type, if not written, the system will automatically judge and fill in the data type. copy defaults to True
np.asarray(object,dtype=None) When np.array(copy=False) two functions same

Explain the usage of the parameter copy in np.array, the text description is very troublesome, let's demonstrate the code

import numpy as np
a = [-1,2,2]
a = np.array(a)
c = np.array(a,copy = False)
a[0] = 100
print('当copy=False')
print('c数组')
print(c)
print('a数组')
print(a)
#代码运行结果:
当copy=False
c数组
[100   2   2]
a数组
[100   2   2]
当copy=True
c数组
[-1  2  2]
a数组
[100   2   2]

Through the code results, we can see the difference between copy is True and False, and also understand the difference between np.array and np.asarray

function meaning
Create an arithmetic sequence np.arange([start,]stop, [step],[dtype]) Create an array from start to stop-1 with a step size of step, where start defaults to 0, step defaults to 1, and stop is a parameter that must be filled in
Create an arithmetic sequence np.linspace(start, stop, [num], [endpoint], [dtype]) Create an arithmetic series from start to stop, num is the number of arithmetic series, the default is 50, endpoint defaults to True, indicating that the last item of the series contains stop, otherwise it does not
array of special values ​​np.zeros(shape, [dtype],) shape is a value, create a one-dimensional array with a value of 0, and shape is a tuple or list, create an array with a value of 0 equal to the size of shape.
array of special values ​​np.ones(shape,[dtype]) shape is a value, create a one-dimensional array with a value of 1, and shape is a tuple or list, create an array with a value of 1 with the size of shape
special value array np.eye(n,[m],[k],[dtype]) n is the number of rows in the array. m is the number of output columns, the default is n. The default k is 0, which means that the main diagonal is 1, and the rest are 0. The smaller the negative number, the diagonal of 1 goes below the main diagonal, and the larger the positive number, the diagonal of 1 goes to the main diagonal. Go above the diagonal.

numpy create random array

function meaning
np.random.seed(k) If the same value of k is used, the random numbers generated each time are the same. If this value is not set, the system will choose this value according to the time. At this time, the random numbers generated each time are different due to time differences.
np.random.random([size]) Randomly generate an array in the interval [0,1), szie only generates an array of data when it is not filled in, size can receive a list or a tuple, and randomly generates an array of the same size as size
np.random.rand(x,x1,x2…) Randomly generate an array conforming to the size and shape of (x, x1, x2...) in the interval [0,1)
np.random.randn(x,x1,x2…) Generate an array of (x,x1,x2...) size and shape conforming to the standard normal distribution
np.random.randint(low, [high], [size], [dtype]) Generate an array with the same size in the interval [low, high), and generate an array with the interval [0, low) when the parameter high is not filled. When the shape is not filled in, only an array of data is generated
np.random.choice(a,[size],[replace=True],[p]) If a is a number, a number is randomly drawn from [0, a), and a is a number randomly drawn from a list, tuple, and array (must be one-dimensional). When size is not filled in, only one piece of data will be randomly generated, size is a number, and size pieces of data are randomly generated to form a one-dimensional array. If size is a list or tuple, an array of the same size as size will be generated. The default value of replace is True, which means that the same number can be taken, and False means that the same number cannot be taken. p corresponds to a, which means the probability of selecting each element in the array a, and the default is that the probability of selecting each element is the same.
numpy.random.normal(loc=0.0, scale=1.0, size=None) loc: floating-point data, the mean (center) of the distribution. scale: floating-point data, the standard deviation (width) of the distribution. size: an integer or a tuple of integers. If size is given, an array of the same size as size is generated. If size is None (the default), then a value is returned.

Property functions of Numpy arrays

function meaning
it's me return int. Indicates the dimensions of the array
shape return tuple. Represents the size of the array. For a matrix with n rows and m columns, the shape is (n,m)
size return int. Represents the total number of elements in the array, which is equal to the product of the shape of the array
dtype return type. Describes the type of elements in the array
itemsize return int. Represents the size in bytes of each element of the array
nbytes return int. Indicates the space occupied by the array

Numpy change array shape

function meaning
np.reshape(a, newshape,) 或a.reshape(shape) The two functions have the same effect. The parameter newshape of np.reshape can only receive lists and tuples, but a.reshape can not only receive lists and tuples, but each element of the parameter is passed in as a separate parameter. The number of elements in the transformed array The number is the same as the original number of elements, otherwise an error will be reported
np.resize(a,new_shape)或a.reszie() new_shape can only receive lists and tuples, and a.resize can receive single or list and tuples. The number of elements of the transformed array can be different from that of the original array, np.shape can be filled repeatedly, and a.resize can be filled with 0.
np.ravel(a) or a.ravel() Convert the array a into a one-dimensional array
a.flatten() Convert the array a into a one-dimensional array
np.squeeze(a) Remove dimensions with one element
np.transpose(a,axes)或a.transpose() a 为输入的数组。axes为元组 或 列表, 如果指定,它必须是包含[0,1,…,N-1]的排列的元组或列表,其中N是a的轴数,返回数组的第i个轴将与输入的编号为axes [i]的轴相对应。 axes默认为range(a.ndim)[::-1],这将反转轴的顺序。a.transpose功能与np.transpose一样,a.transpose可以接收多个数,元组或列表(这个难以理解,建议使用a.shape来查看使用该函数前后的变化)
np.swapaxes(a, axis1, axis2)或a.swapaxes() 交换axis1和axis2的所代表的两个轴,axis1和axis2都为整数。a.swapaxes也只能接收两个整数。切记这两个函数不能输入两个整数的列表或元组

Numpy统计函数

函数 含义
np.sum(a, axis=None)或a.sum(axis = None) 根据给定轴axis计算数组a相关元素之和,axis整数或元组
np.mean(a, axis=None)或a.mean(axis = None) 根据给定轴axis计算数组a相关元素的期望,axis整数或元组
np.average(a,axis=None,weights=None) 根据给定轴axis计算数组a相关元素的加权平均值
np.std(a, axis=None)或a.std(axis = None) 根据给定轴axis计算数组a相关元素的标准差
np.var(a, axis=None)或a.var(axis = None) 根据给定轴axis计算数组a相关元素的方差
np.min(a,axis=None)或a.min(axis=None) 计算数组a中元素的最大值
np.max(a,axis=None)或a.max(axis=None) 计算数组a中元素的最大值
np.argmin(a,axis=None)或 a.argmin(axis = None) 计算数组a中元素最小值降为一维后的下标
np.argmax(a,axis=None)或 a.argmax(axis = None) 计算数组a中元素最大值降为一维后的下标
np.ptp(a,axis=None)或a.ptp(axis = None) 计算数组a中元素最大值与最小值的差
np.median(a,axis=None)或 a.median(axis = None) 计算数组a中元素的中位数(中值)

Numpy算数操作

函数 含义
x1+x2或np.add(x1,x2) x1与x2相加,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同
x1-x2或np.subtract(x1,x2) x1减x2,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同
-x或np.negative(x) 取x的负数,x可以为数值也可以为数组
x1*x2或 np.multiply(x1,x2) x1x与2相乘,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同
x1/x2或np.divide(x1,x2) x1除以x2,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同,x2为零的时候,值为inf(无限大)
x1//x2或 np.floor_divide(x1,x2) x1整除x2,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同
x1**x2或 np.power(x1,x2) x1的x2次方,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同
x1%x1或 np.mod(x1,x2) x1除以x2的余数,x1与x2可以为数值也可以都为数组,数组的时候其shape的尺寸大小要相同

Numpy比较操作

函数 含义
x1==x2或np.equal(x1,x2) 返回布尔数组
x1!=x2或no.not_equal(x1,x2) 返回布尔数组
x1<x2或np.less(x1,x2) 返回布尔数组
x1<=x2或np.less_equal(x1,x2) 返回布尔数组
x1>x2或np.greater(x1,x2) 返回布尔数组
x1>=x2或 np.greater_equal(x1,x2) 返回布尔数组

Numpy的指数,对数,三角函数

三角函数输入和输出的是弧度制

函数 含义
np.deg2rad 将角度制化为弧度制
np.rad2deg 将弧度制化为角度制

在使用三角函数的时候我们可以先将角度制化为弧度制,再带入三角函数。

函数 含义
np.exp 计算指数(e^x)
np.log,np.log10,np.log2,np.log1p 求以e为底,以10为低,以2为低,以(1+x)为底的对数
np.sign 将数组中的值标签化,大于0的变成1,等于0的变成0,小于0的变成-1
np.cos,np.sin,np.tan 三角函数
np.arccos,np.arcsin,np.arctan 反三角函数

Numpy取整,四舍五入

函数 含义
np.ceil 朝着无穷大的方向取整,比如5.1会变成6,-6.3会变成-6
np.floor 朝着负无穷大方向取证,比如5.1会变成5,-6.3会变成-7
np.rint(a) 四舍五入取整
np.round(a,decimals=0) 返回四舍五入后的值,decimals为小数点后的位数
np.modf 将整数和小数分隔开来形成两个数组
array.astype(‘数据类型’) 转换数组array的数据类型

Numpy矩阵运算

函数 含义
np.dot(a,b) a与b进行数组的行列式相乘
np.linalg.inv(a) 求矩阵a的逆
np.linalg.det(a) 求a的行列式
np.linalg.eig(a) 求a的特征值和特征向量
np.linalg.sval(a) 求a的奇异值分解
np.linalg.norm(a, ord=2, axis=None,) ord=1:求一范数,ord=2:求二范数,ord=np.inf:无穷范数,

Numpy数组合并

函数 含义
np.append(arr, values, axis=None) arr:需要被合并values的数组。values:合并到数组arr中的数组。axis:可选参数,如果axis没有给出,合并后返回值为一维数组。axis被填写,按照axis指定的维度合并(axis填写时arr和values需有相同的shape的尺寸大小)
np.concatenate(arrays, axis=None) array多个数组组成的列表或元组。axis填写后将按照axis指定的维度合并(array中的数组可以是不同的shape的尺寸大小)
np.stack(arrays, axis=0) array多个数组组成的列表或元组。axis填写后将按照axis指定的维度合并(array中的数组需为相同的shape的尺寸大小)
np.hstack(tup) 横向合并,tup多个数组组成的列表或元组。建议使用二维数组,多维数组当shape的尺寸大小不同时有时可以合并,有时不可以。
np.vstack(tup) 竖向合并,tup多个数组组成的列表或元组。

Numpy数组分割

函数 含义
np.split(ary, indices_or_sections, axis=0) ary:要切分的数组。indices_or_sections:如果是一个整数,表示将arry切割为改整数份,如果是一个列表或元组(第一个值不要为0),列表值进行切分。axis:沿着哪个维度进行切向,默认为0,横向切分,1表示竖向切分。
np.hsplit(ary, indices_or_sections) 竖向切分。ary:要切分的数组。indices_or_sections:如果是一个整数,表示将arry切割为改整数份,如果是一个列表或元组(第一个值不要为0),列表值进行切分。
np.vsplit(ary, indices_or_sections) 横向切分。ary:要切分的数组。indices_or_sections:如果是一个整数,表示将arry切割为改整数份,如果是一个列表或元组(第一个值不要为0),列表值进行切分。

Numpy其他函数

函数 含义
np.take(a,indices,axis = None)/a.take() a:要提取的数组。indices;要提取的值的索引的列表。axis=0:抽取相应索引行的数据。axis=1:抽取相应索引列的数据。axis=None:将数组转化为一维数据再进行索引相对应的数据
np.argmax(array, axis=None) /array.argmax(axis=None) array:代表输入数组;axis=0:对array取每一列的最大值的索引,axis=1:对array取每一行的最大值的索引。axis=None:j将array转为一维数据再进行取最大值的索引(axis为1和0所对应的列和行没有写错,它是和别人不同)

np.tensordot讲解

np.tensordot函数是对于高维矩阵进行的内积运算,这个内积的过程不好理解,建议从维度上进行理解就好了

np.tensordot(a,b,axes)

a,b是要进行内积运算的矩阵
axes是矩阵要运算的维度

1.当axes = n时,n为整数时,代表的是a的后n个维度与b的前n个维度进行内积后,a去掉后n个维度,b去掉前n个维度两者再结和的维度,看例子:

首先定义两个矩阵

>>> a = np.random.rand(4,5,3,2)
>>> b = np.random.rand(2,3,5,4)

这里设定axes = 0时代表没有维度去掉,故新矩阵维度为(4, 5, 3, 2, 2, 3, 5, 4)
axes = 1的时候运算后的矩阵的大小为去掉了a的后一个维度与b 的前一个维度故新矩阵维度为(4, 5, 3, 3, 5, 4)

>>> print(np.tensordot(a,b,axes = 0).shape)
(4, 5, 3, 2, 2, 3, 5, 4)
>>> print(np.tensordot(a,b,axes = 1).shape)
(4, 5, 3, 3, 5, 4)

这里是axes = 2的时候,但是a的后2个维度与b的前2各维度不相同,就会报错

>>> print(np.tensordot(a,b,axes = 2).shape)
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    print(np.tensordot(a,b,axes = 2).shape)
  File "<__array_function__ internals>", line 200, in tensordot
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\core\numeric.py", line 1117, in tensordot
    raise ValueError("shape-mismatch for sum")
ValueError: shape-mismatch for sum

修改b的维度使得b的前2个维度与a的后2个维度相同,新矩阵的大小为(4, 5, 5, 4)

>>> b = np.random.rand(3,2,5,4)
>>> print(np.tensordot(a,b,axes = 2).shape)
(4, 5, 5, 4)

2.当axes = [m,n],代表a的m维度与b的n维度进行的内积,新的矩阵大小为去掉a的m维b的n维然后再组合
首先我们看一下a,b的各维度的大小

>>> print(a.shape,b.shape)
(4, 5, 3, 2) (3, 2, 5, 4)

接着尝试内积a的2维b的0维,去掉后可以看见新矩阵的大小为(4, 5, 2, 2, 5, 4)

>>> print(np.tensordot(a,b,axes = [2,0]).shape)
(4, 5, 2, 2, 5, 4)

但是如果想要内积的维度大小不同,会报错

>>> print(np.tensordot(a,b,axes = [1,1]).shape)
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    print(np.tensordot(a,b,axes = [1,1]).shape)
  File "<__array_function__ internals>", line 200, in tensordot
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\core\numeric.py", line 1117, in tensordot
    raise ValueError("shape-mismatch for sum")
ValueError: shape-mismatch for sum

3.如果axes接收的是一个嵌套列表的列表:[[m], [n]],等于说可以选多个维度内积 (注意这里的两个列表相对应的维度的大小要相同),然后去掉a,b相应的维度,再组合
首先查看a,b的各维度大小,接着我们这里想要在大小为5和2的维度上内积,这里一定要注意两个列表中维度大小要相同

>>> print(a.shape,b.shape)
(4, 5, 3, 2) (3, 2, 5, 4)
>>> print(np.tensordot(a,b,axes = [[1,3],[2,1]]).shape)
(4, 3, 3, 4)

结尾

Numpy函数太多了,这里就写了一些比较常用的函数,总结这些函数真是太费时间了,函数的用法我是自己上机实验过,按照自己的理解整理的,内容可能不全,讲解的不够详细。如果你有什么疑惑或不懂的地方,建议亲自实践。以后遇见了更多的函数我会继续补充的。希望大家多多支持。

Guess you like

Origin blog.csdn.net/m0_59151709/article/details/129355556