本文所包括的numpy库中的函数主要应用于深度学习领域所涉及到的,会持续更新.python版本3.7。
np.xxx函数总结
- 1.np.meshgrid( )
- 2.np.nansum() || np.nanmean()
- 3.np.arange(x,y,h)
- 4.np.c_() || np.r_()
- 5.np.dot()
- 6.np.multiply()
- 7.np.linalg.norm()
- 8.np.power()
- 9.np.linspace()
- 10.np.hstack() || np.vstack()
- 11.np.linalg.inv()
1.np.meshgrid( )
X, Y = np.meshgrid(x, y) 代表的是将x中每一个数据和y中每一个数据组合生成很多点,然后将这些点的x坐标放入到X中,y坐标放入Y中,并且相应位置是对应的
代码:
import numpy
import numpy as np
x = [1,2,3,4,5,6]
y = [7,8,9]
X,Y = np.meshgrid(x,y)
print(X)
print("------")
print(Y)
输出:
[[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]]
------
[[7 7 7 7 7 7]
[8 8 8 8 8 8]
[9 9 9 9 9 9]]
2.np.nansum() || np.nanmean()
nan代表一个“不是数字”的值,如果有这种值就会报错,用nansum会在求和,取均值的时候忽略nan(取均值:mean)
w = [1,2,4,5,np.nan]
W = np.sum(w)
print(W)
nan
Process finished with exit code 0
w = [1,2,4,5,np.nan]
W = np.nansum(w)
print(W)
12.0
Process finished with exit code 0
np.nanmean() 同理,mean是取均值的函数
3.np.arange(x,y,h)
函数返回一个有终点和起点的固定步长的排列。
(1)只有一个参数时候:参数为终点,起点默认为0,步长默认为1;
x = np.arange(5)
print(x)
[0 1 2 3 4]
Process finished with exit code 0
(2)有两个参数的时候,参数为起点和终点,步长默认为1
x = np.arange(1,5)
print(x)
[1 2 3 4]
Process finished with exit code 0
如果起点值大于终点,返回空数组
(3)三个参数为起点终点步长按顺序排列
x = np.arange(1,10,2)
print(x)
[1 3 5 7 9]
Process finished with exit code 0
4.np.c_() || np.r_()
c_ : 中的c是column(列)的缩写,就是按列叠加两个矩阵,把两个矩阵左右组合,要求行数相等,
同理,np.r_是row(列)按行连接两个矩阵,就是把两矩阵上下相加,要求列数相等
x = np.array([1,2,3,4,5,6]).reshape(3,2)
y = np.array([7,8,9,4,5,6,1,2,3]).reshape(3,3)
t = np.c_[x,y]
print(x)
print(y)
print(t)
[[1 2]
[3 4]
[5 6]]
[[7 8 9]
[4 5 6]
[1 2 3]]
[[1 2 7 8 9]
[3 4 4 5 6]
[5 6 1 2 3]]
Process finished with exit code 0
5.np.dot()
函数主要有两个功能,向量点积和矩阵乘法
(1)向量和向量之间是点积
(2)矩阵和向量之间,向量会自动转化为矩阵进行矩阵乘法
(3)矩阵和矩阵之间是矩阵乘法计算
x = np.array([1,2,3,4,5,6])
y = np.array([6,7,8,9,10,1])
z = x.reshape(3,2)
m = np.array([1,2])#z,m 的列数要确保相同
n = y.reshape(2,3)
a = np.dot(x,y)
b = np.dot(z,m)
c = np.dot(z,n)
print(x)
print(y)
print("-----------")
print(z)
print("-----------")
print(a)
print("-----------")
print(b)
print("-----------")
print(c)
[1 2 3 4 5 6]
[ 6 7 8 9 10 1]
-----------
[[1 2]
[3 4]
[5 6]]
-----------
136
-----------
[ 5 11 17]
-----------
[[24 27 10]
[54 61 28]
[84 95 46]]
Process finished with exit code 0
6.np.multiply()
由于multiply是ufunc函数,ufunc函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同),相同则是计算内积。如果shape不同的话,会将小规格的矩阵延展成与另一矩阵一样大小,再求两者内积。
x = np.array([1,2,3,4,5,6]).reshape(3,2)
y = np.array([6,7,8,9,10,1]).reshape(3,2)
a = np.multiply(x,y)
print(x)
print("-----------")
print(y)
print("-----------")
print(a)
[[1 2]
[3 4]
[5 6]]
-----------
[[ 6 7]
[ 8 9]
[10 1]]
-----------
[[ 6 14]
[24 36]
[50 6]]
Process finished with exit code 0
当矩阵维度不一致的时候:
x = np.array([1,2,3,4,5,6]).reshape(3,2)
z = np.array([1,2,3]).reshape(3,1)
b = np.multiply(x,z)
print(b)
[[1 2]
[3 4]
[5 6]]
-----------
[[1]
[2]
[3]]
-----------
[[ 1 2]
[ 6 8]
[15 18]]
7.np.linalg.norm()
np.linalg.norm()用于求范数,linalg本意为linear(线性) + algebra(代数),norm则表示范数。
用法:
np.linalg.norm(x, ord=None, axis=None, keepdims=False)
1.x: 表示矩阵(一维数据也是可以的~)
2.ord: 表示范数类型
ord=1:表示求列和的最大值
ord=2:|λE-ATA|=0,求特征值,然后求最大特征值得算术平方根
ord=∞:表示求行和的最大值
ord=None:表示求整体的矩阵元素平方和,再开根号
3.axis:
axis = 1:按行向量进行处理
axis = 0:按列向量进行处理
axis = None:表示整个矩阵的范数
4.keepdims:表示是否保持矩阵的二位特性,True表示保持,False表示不保持,默认为False
8.np.power()
该函数是用于数组函数求n次方,
a = np.power(2,3)
print(a)
8
Process finished with exit code 0
b = np.random.randn(3,3)
a = np.power(b,2)
print(b)
print("-------")
print(a)
[[-0.48641285 -0.28778445 0.54128437]
[-0.38554216 1.8861078 1.03012565]
[ 0.13224982 0.62738545 0.85826017]]
-------
[[0.23659746 0.08281989 0.29298877]
[0.14864276 3.55740264 1.06115885]
[0.01749002 0.39361251 0.73661052]]
Process finished with exit code 0
9.np.linspace()
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
该函数主要是用来创建等差数列的:
Return evenly spaced numbers over a specified interval.
(在start和stop之间返回均匀间隔的数据)
Returns num evenly spaced samples, calculated over the interval [start, stop].
(返回的是 [start, stop]之间的均匀分布)
The endpoint of the interval can optionally be excluded.
Changed in version 1.16.0: Non-scalar start and stop are now supported.
(可以选择是否排除间隔的终点)
每个参数的含义:
start:返回样本数据开始点
stop:返回样本数据结束点
num:生成的样本数据量,默认为50
endpoint:True则包含stop;False则不包含stop
retstep:If True, return (samples, step), where step is the spacing between samples.(即如果为True则结果会给出数据间隔)
dtype:输出数组类型
axis:0(默认)或-1
import numpy as np
x = np.linspace(-3*np.pi,3*np.pi,100)
print(x)
[-9.42477796 -9.23437841 -9.04397885 -8.8535793 -8.66317974 -8.47278019
-8.28238063 -8.09198108 -7.90158152 -7.71118197 -7.52078241 -7.33038286
-7.1399833 -6.94958375 -6.75918419 -6.56878464 -6.37838508 -6.18798553
-5.99758598 -5.80718642 -5.61678687 -5.42638731 -5.23598776 -5.0455882
-4.85518865 -4.66478909 -4.47438954 -4.28398998 -4.09359043 -3.90319087
-3.71279132 -3.52239176 -3.33199221 -3.14159265 -2.9511931 -2.76079354
-2.57039399 -2.37999443 -2.18959488 -1.99919533 -1.80879577 -1.61839622
-1.42799666 -1.23759711 -1.04719755 -0.856798 -0.66639844 -0.47599889
-0.28559933 -0.09519978 0.09519978 0.28559933 0.47599889 0.66639844
0.856798 1.04719755 1.23759711 1.42799666 1.61839622 1.80879577
1.99919533 2.18959488 2.37999443 2.57039399 2.76079354 2.9511931
3.14159265 3.33199221 3.52239176 3.71279132 3.90319087 4.09359043
4.28398998 4.47438954 4.66478909 4.85518865 5.0455882 5.23598776
5.42638731 5.61678687 5.80718642 5.99758598 6.18798553 6.37838508
6.56878464 6.75918419 6.94958375 7.1399833 7.33038286 7.52078241
7.71118197 7.90158152 8.09198108 8.28238063 8.47278019 8.66317974
8.8535793 9.04397885 9.23437841 9.42477796]
10.np.hstack() || np.vstack()
hstack()函数是用于两数组的水平方向合并,合并后行数不变;而vstack()函数,是用于两数组的垂直方向的合并,合并后列数不变,即horizontal和vertical.
a = np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)
b = np.ones([3,1])
print(a,'\n',b)
c = np.hstack((a,b))
print(c)
C:\Users\admin\anaconda3\envs\torch11\python.exe D:/Code/d2l-zh-pytorch/test.py
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1.]
[1.]
[1.]]
[[1. 2. 3. 1.]
[4. 5. 6. 1.]
[7. 8. 9. 1.]]
Process finished with exit code 0
11.np.linalg.inv()
这是一个求矩阵的逆矩阵的函数,要求矩阵本身是可逆的!
e = np.array([2,3,1,0,1,3,1,2,5]).reshape(3,3)
f = np.linalg.inv(e)
print(f)
[[-0.16666667 -2.16666667 1.33333333]
[ 0.5 1.5 -1. ]
[-0.16666667 -0.16666667 0.33333333]]
a = np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)
b = np.ones([3,1])
print(a,'\n',b)
c = np.hstack((a,b))
print(c)
e = np.array([2,3,1,0,1,3,1,2,5]).reshape(3,3)
f = np.linalg.inv(e)
print(f)
d = np.linalg.inv(a)
C:\Users\admin\anaconda3\envs\torch11\python.exe D:/Code/d2l-zh-pytorch/test.py
Traceback (most recent call last):
File "D:/Code/d2l-zh-pytorch/test.py", line 52, in <module>
d = np.linalg.inv(a)
File "<__array_function__ internals>", line 5, in inv
File "C:\Users\admin\anaconda3\envs\torch11\lib\site-packages\numpy\linalg\linalg.py", line 545, in inv
ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
File "C:\Users\admin\anaconda3\envs\torch11\lib\site-packages\numpy\linalg\linalg.py", line 88, in _raise_linalgerror_singular
raise LinAlgError("Singular matrix")
numpy.linalg.LinAlgError: Singular matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1.]
[1.]
[1.]]
[[1. 2. 3. 1.]
[4. 5. 6. 1.]
[7. 8. 9. 1.]]
[[-0.16666667 -2.16666667 1.33333333]
[ 0.5 1.5 -1. ]
[-0.16666667 -0.16666667 0.33333333]]
Process finished with exit code 1
Traceback 报错就是a矩阵不可逆