命令行记录-numpy基本用法

参考:https://blog.csdn.net/duanlianvip/article/details/94393763

从该博客可以获取关于numpy矩阵运算的基本认识。首先是记录从这篇博客里了解的内容。

 1 import numpy as np
 2  
 3 # 一维数组
 4 a = np.array([1, 2, 3, 4, 5, 6, 7, 8])  
 5 print(a.shape)  
 6 # 输出:(8,),最外层矩阵有8个元素
 7 print(a.shape[0])  
 8 # 输出:8,因为有8个数据
 9 # print(a.shape[1])  
10 # 输出:IndexError: tuple index out of range
11  
12 # 二维数组
13 a = np.array([[1, 2, 3, 4],[5, 6, 7, 8]])  
14 print(a.shape)  
15 # 输出:(2, 4)
16 print(a.shape[0])  
17 # 输出:2,最外层矩阵有2个元素,2个元素还是矩阵。
18 print(a.shape[1])  
19 # 输出:4,内层矩阵有4个元素。
20 # print(a.shape[2])  
21 # 输出:IndexError: tuple index out of range
22  
23 y = np.zeros((2, 3, 4, 5))
24 print(y.shape)  
25 # 输出:(2, 3, 4, 5)
26 print(y)

y=np.zeros((2,3,4,5))运行结果:

1、reshape()方法

(1)用于改变数组的形状(数组中的值不会改变)

 1 a=np.array((2,4))
 2 print(a)
 3 #输出:[1 2 3 4 5 6 7 8]
 4 d=a.reshape((2,4))
 5 print(d)
 6 #输出:
 7 [[1 2 3 4]
 8   [5 6 7 8]]
 9 d=a.reshape((2,2,2))
10 print(d)
11 #输出:
12 [[[1 2]
13   [3 4]]
14 
15 [[5 6]
16   [7 8]]]

(2)改变形状后所包含的元素个数与原来元素个数相同,否则会报错

1 >>> d=a.shape((2,2))
2 Traceback (most recent call last):
3   File "<stdin>", line 1, in <module>
4 TypeError: 'tuple' object is not callable

(3)reshape()函数生成的新数组合原始数组共用一个内存

 1 >>> a=np.array([1,2,3,4,5,6,7,8])
 2 >>> d=a.reshape((2,4))
 3 >>> print(d)
 4 [[1 2 3 4]
 5  [5 6 7 8]]
 6 >>> a[0]=111
 7 >>> print(a)
 8 [111   2   3   4   5   6   7   8]
 9 >>> print(d)
10 [[111   2   3   4]
11  [  5   6   7   8]]

(4)当不知道某一个维度值为多大时,可以使用-1,-1只能用一次,否则会报错(有多种输出可能)

 1 >>> d=np.array([1,2,3,4,5,6,7,8])
 2 >>> print(d)
 3 [1 2 3 4 5 6 7 8]
 4 >>> print(d.reshape(-1,8))
 5 [[1 2 3 4 5 6 7 8]] 8 >>> print(d.reshape([8,-1]))
 9 [[1]
10  [2]
11  [3]
12  [4]
13  [5]
14  [6]
15  [7]
16  [8]]
17 >>> print(d.reshape(-1))
18 [1 2 3 4 5 6 7 8]
27 >>> print(d.reshape(-1,2,1))
28 [[[1]
29   [2]]
30 
31  [[3]
32   [4]]
33 
34  [[5]
35   [6]]
36 
37  [[7]
38   [8]]]
39 >>> print(d.reshape(2,-1,2))
40 [[[1 2]
41   [3 4]]
42 
43  [[5 6]
44   [7 8]]]

 1.1NumPy基本用法

import numpy as np

#构建多维数组

 1 >>> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
 2 ...         17, 18, 19, 20, 21, 22, 23]
 3 >>> a = np.array(data) #一维数组,24 个数
 4 >>> a = np.linspace(0,24, 24) #一维数组,将 0-24 等分为 24 个数字(含 0 与 24),均为小数
 5 >>> b=np.linspace(1,24,24)#输出为1-24,均为整数
 6 >>> a = np.arange(24) #简洁方式,输出为0-23,均为整数
 7 >>> print(a)
 8 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
 9 >>> b = a.reshape(3,8) #将一维数组 a 转换为维数(3,8)的数组
10 >>> print(b)
11 [[ 0  1  2  3  4  5  6  7]
12  [ 8  9 10 11 12 13 14 15]
13  [16 17 18 19 20 21 22 23]]
14 >>> c = a.reshape(2,3,4)#将一维数组 a 转换为维数(2,3,4)的数组
15 >>> print(c)
16 [[[ 0  1  2  3]
17   [ 4  5  6  7]
18   [ 8  9 10 11]]
19 
20  [[12 13 14 15]
21   [16 17 18 19]
22   [20 21 22 23]]]
23 >>> print(a.shape)#显示数组 a 维数
24 (24,)
25 >>> print(b.shape)#显示数组 b 维数
26 (3, 8)
27 >>> print(c.shape)#显示数组 c 维数
28 (2, 3, 4)

 #构建数值全0或为1的数组或用随机数初始化数组,行3列8

 1 >>> c=np.zeros((3,8),dtype=np.int8)
 2 >>> print(c)
 3 [[0 0 0 0 0 0 0 0]
 4  [0 0 0 0 0 0 0 0]
 5  [0 0 0 0 0 0 0 0]]
 6 >>> d=np.ones((3,8))
 7 >>> print(d)
 8 [[1. 1. 1. 1. 1. 1. 1. 1.]
 9  [1. 1. 1. 1. 1. 1. 1. 1.]
10  [1. 1. 1. 1. 1. 1. 1. 1.]]
11 >>> e=np.random.randn(3,8)#按照标准正态分布,随机生成3x8的数值
12 >>> print(e)
13 [[ 0.17334448  0.32090053 -0.29102203  0.5890599  -0.75401811  0.57766044
14   -3.04179385 -0.71977731]
15  [ 1.61396639  0.04356218 -1.49661407 -1.45487388 -0.17099261  0.085367
16    0.49247614  1.21652539]
17  [ 0.97344041  1.59037408 -0.41302225  1.28036219 -1.31535703  0.50801735
18    1.75759039  0.40855037]]
19 >>> f=np.random.rand(3,8)
20 >>> print(f)
21 [[0.08401005 0.39389431 0.73887778 0.33075659 0.71730399 0.60109836
22   0.39391439 0.25942722]
23  [0.84138857 0.85799698 0.74984625 0.84414232 0.5342516  0.22544323
24   0.1128967  0.81637407]
25  [0.8509715  0.6880721  0.63241358 0.63177309 0.01612053 0.48118934
26   0.62607982 0.73950726]]

#代数计算(加减乘除等),注意:参与计算的数组维数相同

 1 >>> a=np.sin(b)+d#数学计算
 2 >>> print(a)
 3 [[1.00000000e+00 1.84147098e+00 1.90929743e+00 1.14112001e+00
 4   2.43197505e-01 4.10757253e-02 7.20584502e-01 1.65698660e+00]
 5  [1.98935825e+00 1.41211849e+00 4.55978889e-01 9.79344930e-06
 6   4.63427082e-01 1.42016704e+00 1.99060736e+00 1.65028784e+00]
 7  [7.12096683e-01 3.86025081e-02 2.49012753e-01 1.14987721e+00
 8   1.91294525e+00 1.83665564e+00 9.91148691e-01 1.53779596e-01]]
 9 >>> a=b>5#逻辑运算
10 >>> print(a)
11 [[False False False False False False  True  True]
12  [ True  True  True  True  True  True  True  True]
13  [ True  True  True  True  True  True  True  True]]
14 >>> a = (b>5)*(b+1) + (b<=5)*(b-1) #混合计算,如: b 中,大于 5 值加 1,小于等于 5 值减 1
15 >>> print(a)
16 [[-1  0  1  2  3  4  7  8]
17  [ 9 10 11 12 13 14 15 16]
18  [17 18 19 20 21 22 23 24]]

#另外还有np.exp,np.sqrt,np.floor,np.sin等函数

#数组拷贝,数据类型转换

1 >>> a = np.arange(24) #简洁方式,输出为0-23,均为整数
2 >>> b = a.reshape(3,8) #将一维数组 a 转换为维数(3,8)的数组
3 >>> a=np.copy(b)#拷贝
4 >>> b=a.astype(np.float)#整型数组a转换为浮点型数组b
5 >>> print(b)
6 [[ 0.  1.  2.  3.  4.  5.  6.  7.]
7  [ 8.  9. 10. 11. 12. 13. 14. 15.]
8  [16. 17. 18. 19. 20. 21. 22. 23.]]

#数值统计

1、首先要说明的是轴的概念

axis的重点在于方向,而不是行和列。具体到各种用法而言也是如此。当axis=1时,如果是求平均,那么是从左到右横向求平均;如果是拼接,那么也是左右横向拼接;如果是drop,那么也是横向发生变化,体现为列的减少。

当考虑了方向,即axis=1为横向操作,axis=0为纵向操作,而不是行和列,那么所有的例子就都统一了。
————————————————
引自:https://blog.csdn.net/qq_41149269/article/details/81384523

 1 >>> print(b)
 2 [[ 0.  1.  2.  3.  4.  5.  6.  7.]
 3  [ 8.  9. 10. 11. 12. 13. 14. 15.]
 4  [16. 17. 18. 19. 20. 21. 22. 23.]]
 5 >>> np.sum(b,axis=0)
 6 array([24., 27., 30., 33., 36., 39., 42., 45.])
 7 >>> np.sum(b,axis=1)
 8 array([ 28.,  92., 156.])
 9 >>> np.sum(b)
10 276.0
1 >>> np.max(b,axis=0)#列求最大值
2 array([16., 17., 18., 19., 20., 21., 22., 23.])
3 >>> np.max(b,axis=1)#行求最大值
4 array([ 7., 15., 23.])
5 >>> np.max(b)
6 23.0

#其它还有np.mean,std,min,max,median,sum,cumsum等,沿某个轴向

#数组访问,数组切片或分片访问

 1 >>> print(a)
 2 [[ 0  1  2  3  4  5  6  7]
 3  [ 8  9 10 11 12 13 14 15]
 4  [16 17 18 19 20 21 22 23]]
 5 >>> print(a[2,3])#提取数组a中第二行第3列的数值
 6 19
 7 >>> a[2,3]+=1
 8 >>> print(a[1:3,3:7])#提取数组 a 中 1~2 行,3~6 列的子数组,类似滤波窗口
 9 
1011 [[11 12 13 14]
12  [20 20 21 22]]
13 >>> a[1:3,3:7]=np.zeros((2,4))#将数组a中1~2行,3~6列的值置换为0
14 >>> print(a)
15 [[ 0  1  2  3  4  5  6  7]
16  [ 8  9 10  0  0  0  0 15]
17  [16 17 18  0  0  0  0 23]]
18 >>> a[[0,1],[0,2]]#将数组a中(0,0),(1,2)两个位置的数值置换为-1
19 array([ 0, 10])
20 >>> a[[0,1],[0,2]]=-1#将数组a中(0,0),(1,2)两个位置的数值置换为-1
21 >>> print(a)
22 [[-1  1  2  3  4  5  6  7]
23  [ 8  9 -1  0  0  0  0 15]
24  [16 17 18  0  0  0  0 23]]

#数组拼接与拆分

 1 >>> print(a)
 2 [[-1  1  2  3  4  5  6  7]
 3  [ 8  9 -1  0  0  0  0 15]
 4  [16 17 18  0  0  0  0 23]]
 5 >>> print(b)
 6 [[ 0.  1.  2.  3.  4.  5.  6.  7.]
 7  [ 8.  9. 10. 11. 12. 13. 14. 15.]
 8  [16. 17. 18. 19. 20. 21. 22. 23.]]
 9 >>> c=np.hstack((a,b))#沿1维,将a,b拼接成一个新数组
10 >>> print(c)
11 [[-1.  1.  2.  3.  4.  5.  6.  7.  0.  1.  2.  3.  4.  5.  6.  7.]
12  [ 8.  9. -1.  0.  0.  0.  0. 15.  8.  9. 10. 11. 12. 13. 14. 15.]
13  [16. 17. 18.  0.  0.  0.  0. 23. 16. 17. 18. 19. 20. 21. 22. 23.]]
14 >>> c=np.concatenate((a,b),axis=1)#沿1维,将a,b拼接成一个新数组
15 >>> c=np.vstack((a,b))#沿0维,将a,b拼接成一个新数组
16 >>> c=np.concatenate((a,b),axis=0)#沿0维,将a,b拼接成一个新数组
17 >>> print(c)
18 [[-1.  1.  2.  3.  4.  5.  6.  7.]
19  [ 8.  9. -1.  0.  0.  0.  0. 15.]
20  [16. 17. 18.  0.  0.  0.  0. 23.]
21  [ 0.  1.  2.  3.  4.  5.  6.  7.]
22  [ 8.  9. 10. 11. 12. 13. 14. 15.]
23  [16. 17. 18. 19. 20. 21. 22. 23.]]
24 >>> a[np.newaxis,:]#将数组a增加了1维,2维变3维
25 array([[[-1,  1,  2,  3,  4,  5,  6,  7],
26         [ 8,  9, -1,  0,  0,  0,  0, 15],
27         [16, 17, 18,  0,  0,  0,  0, 23]]])
28 >>> d=np.concatenate([a[np.newaxis,:],b[np.newaxis,:],axis=0])#合并数组a,b,生成3维数组c
29   File "<stdin>", line 1
30     d=np.concatenate([a[np.newaxis,:],b[np.newaxis,:],axis=0])#合并数组a,b,生成3维数组c
31                                                           ^
32 SyntaxError: invalid syntax
33 >>> d=np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis=0])#合并数组a,b,生成3维数组c
34   File "<stdin>", line 1
35     d=np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis=0])#合并数组a,b,生成3维数组c
36                                                              ^
37 SyntaxError: invalid syntax
38 >>> d=np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis=0)#合并数组a,b,生成3维数组c
39 >>> d=np.stack((a,b))
40 >>> d.shape
41 (2, 3, 8)
42 >>> print(c)
43 [[-1.  1.  2.  3.  4.  5.  6.  7.]
44  [ 8.  9. -1.  0.  0.  0.  0. 15.]
45  [16. 17. 18.  0.  0.  0.  0. 23.]
46  [ 0.  1.  2.  3.  4.  5.  6.  7.]
47  [ 8.  9. 10. 11. 12. 13. 14. 15.]
48  [16. 17. 18. 19. 20. 21. 22. 23.]]
49 >>> np.array_split(c,2,axis=0)#将数组c拆分为2个行数为3的数组
50 [array([[-1.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
51        [ 8.,  9., -1.,  0.,  0.,  0.,  0., 15.],
52        [16., 17., 18.,  0.,  0.,  0.,  0., 23.]]), array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
53        [ 8.,  9., 10., 11., 12., 13., 14., 15.],
54        [16., 17., 18., 19., 20., 21., 22., 23.]])]
55 >>> np.vsplit(c,2)
56 [array([[-1.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
57        [ 8.,  9., -1.,  0.,  0.,  0.,  0., 15.],
58        [16., 17., 18.,  0.,  0.,  0.,  0., 23.]]), array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
59        [ 8.,  9., 10., 11., 12., 13., 14., 15.],
60        [16., 17., 18., 19., 20., 21., 22., 23.]])]

#这里补充一点点关于np.newaxis的内容,np.newaxis的作用是增加维度

 1 >>> print(a)
 2 [[ 3  2  0]
 3  [ 1 -1  0]
 4  [ 0  5  1]]
 5 >>> ee=a[:,:,np.newaxis]
 6 >>> print(ee)
 7 [[[ 3]
 8   [ 2]
 9   [ 0]]
10 
11  [[ 1]
12   [-1]
13   [ 0]]
14 
15  [[ 0]
16   [ 5]
17   [ 1]]]
18 >>> ee=a[:,np.newaxis]
19 >>> print(ee)
20 [[[ 3  2  0]]
21 
22  [[ 1 -1  0]]
23 
24  [[ 0  5  1]]]
25 >>> ee=a[:,np.newaxis,:]
26 >>> print(ee)
27 [[[ 3  2  0]]
28 
29  [[ 1 -1  0]]
30 
31  [[ 0  5  1]]]

#另外还有函数np.hsplit

#条件查找与定位

 1 >>> print(a)
 2 [[-1  1  2  3  4  5  6  7]
 3  [ 8  9 -1  0  0  0  0 15]
 4  [16 17 18  0  0  0  0 23]]
 5 >>> np.where(a==0)#获得符合条件的行列位置
 6 (array([1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([3, 4, 5, 6, 3, 4, 5, 6], dtype=int64))
 7 >>> b[np.where(a==0)]#数组b在对应符合条件a位置上的数值
 8 array([11., 12., 13., 14., 19., 20., 21., 22.])
 9 >>> np.where(a==0,1,0)#等于0的位置,输出1,不等于0的位置,输出0
10 array([[0, 0, 0, 0, 0, 0, 0, 0],
11        [0, 0, 0, 1, 1, 1, 1, 0],
12        [0, 0, 0, 1, 1, 1, 1, 0]])
13 >>> np.argmax(b,axis=0)#沿0轴向,最大值所在位置
14 array([2, 2, 2, 2, 2, 2, 2, 2], dtype=int64)
15 >>> print(b)
16 [[ 0.  1.  2.  3.  4.  5.  6.  7.]
17  [ 8.  9. 10. 11. 12. 13. 14. 15.]
18  [16. 17. 18. 19. 20. 21. 22. 23.]]
19 >>> np.amax(b,axis=0)#沿0轴向,最大值
20 array([16., 17., 18., 19., 20., 21., 22., 23.])
21 >>> b[np.argmax(b,axis=0),np.arange(8)]
22 array([16., 17., 18., 19., 20., 21., 22., 23.])
23 >>> np.argmax(b,axis=1)#沿1轴向,最大值所在位置
24 array([7, 7, 7], dtype=int64)

#另外还有函数np.argmin,argmax,np.amin,amax

 1.2数轴交换numpy.swapaxes

首先简要介绍一下swapaxes函数,主要参考自博客https://blog.csdn.net/this_is_me_anyway/article/details/79695029,简要说swapaxes接受一对轴编号,对其进行交换。譬如arr.swapaxes(2,1)  就是将第三个维度和第二个维度交换。就python的索引来看,python和matlab不同的地方在于,python可以说是括号导向的,即根据由外到内的括号进行索引,matlab可以说是维度导向的。变换轴的顺序,做循环会更好一些。

 1 >>> a=np.arange(24)
 2 >>> print(a)
 3 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
 4 >>> a=np.arange(24).reshape(2,3,4)
 5 >>> a.shape
 6 (2, 3, 4)
 7 >>> b=np.swapaxes(np.swapaxes(a,0,2),0,1)#将a的第一个轴和第三个轴交换,又将得到的矩阵的第1个轴和第二个轴交换
 8 >>> b.shape
 9 (3, 4, 2)
10 >>> r,c=0,0
11 >>> print(a)
12 [[[ 0  1  2  3]
13   [ 4  5  6  7]
14   [ 8  9 10 11]]
15 
16  [[12 13 14 15]
17   [16 17 18 19]
18   [20 21 22 23]]]
19 >>> print(b)
20 [[[ 0 12]
21   [ 1 13]
22   [ 2 14]
23   [ 3 15]]
24 
25  [[ 4 16]
26   [ 5 17]
27   [ 6 18]
28   [ 7 19]]
29 
30  [[ 8 20]
31   [ 9 21]
32   [10 22]
33   [11 23]]]
34 >>> a[:,r,c]
35 array([ 0, 12])
36 >>> b[r,c,:]
37 array([ 0, 12])
38 >>> a[:,r,c]-b[r,c,:]
39 array([0, 0])

  1.3稀疏矩阵scipy.sparse.coo_matrix

>>> from scipy.sparse import coo_matrix
>>> row=np.array([0,3,1,0])
>>> col=np.array([0,3,1,2])#数据位置
>>> data=np.array([4,5,7,9])#数据内容
>>> sm = coo_matrix((data, (row, col)), shape=(4, 4)),第一个参数位置、
内容构成一个元组,第二个参数是矩阵大小
>>> sm#稀疏矩阵无法直接查看 <4x4 sparse matrix of type '<class 'numpy.int32'>' with 4 stored elements in COOrdinate format> >>> sm.row,sm.col (array([0, 3, 1, 0]), array([0, 3, 1, 2])) >>> sm.data array([4, 5, 7, 9]) >>> a=sm.toarray()#转为矩阵 >>> r,c=np.where(a>0)#顺序有变,但配对后行列号还是一致的 >>> d=a[np.where(a>0)]#提取数据内容 >>> print(a) [[4 0 9 0] [0 7 0 0] [0 0 0 0] [0 0 0 5]] >>> print(r) [0 0 1 3] >>> print(c) [0 2 1 3] >>> print(d) [4 9 7 5]

1.4线性代数

#矩阵的逆(inverse of a matrix)

1 >>> import numpy as np
2 >>> a=np.array([[1.,2.],[3.,4.]])
3 >>> b=np.linalg.inv(a)#对a求逆
4 >>> print(b)
5 [[-2.   1. ]
6  [ 1.5 -0.5]]
7 >>> np.dot(a,b)#內积为0
8 array([[1.00000000e+00, 1.11022302e-16],
9        [0.00000000e+00, 1.00000000e+00]])

#特征向量(eigenvalues)

 1 >>> a=np.diag((1,2,3))#对角矩阵
 2 >>> print(a)
 3 [[1 0 0]
 4  [0 2 0]
 5  [0 0 3]]
 6 >>> np.linalg.eig(a)#求特征向量
 7 (array([1., 2., 3.]), array([[1., 0., 0.],
 8        [0., 1., 0.],
 9        [0., 0., 1.]]))
10 >>> a=np.array([[1.,2.],[3.,4.]])
11 >>> np.linalg.eig(a)
12 (array([-0.37228132,  5.37228132]), array([[-0.82456484, -0.41597356],
13        [ 0.56576746, -0.90937671]]))

#点积(dot product)

 1 >>> a=np.arange(6).reshape(2,3)
 2 >>> b=np.arange(6).reshape(3,2)+1
 3 >>> print(a)
 4 [[0 1 2]
 5  [3 4 5]]
 6 >>> print(b)
 7 [[1 2]
 8  [3 4]
 9  [5 6]]
10 >>> np.dot(a,b)
11 array([[13, 16],
12        [40, 52]])

#內积(inner product)

关于內积,查询了百度知道的相关内容,以下引自网页:https://zhidao.baidu.com/question/188716142.html

矩阵的内积参照向量的内积的定义是:两个向量对应分量乘积之和。

比如: α=(1,2,3), β=(4,5,6)

则 α, β的内积等于 1*4 +2*5 + 3*6 = 32

α与α 的内积 = 1*1+2*2+3*3 = 14

 1 >>> a=np.arange(12).reshape(2,2,3)
 2 >>> b=np.arange(3)
 3 >>> print(a)
 4 [[[ 0  1  2]
 5   [ 3  4  5]]
 6 
 7  [[ 6  7  8]
 8   [ 9 10 11]]]
 9 >>> print(b)
10 [0 1 2]
11 >>> np.inner(a,b)
12 array([[ 5, 14],
13        [23, 32]])

#外积(outer product)

外积内容比较复杂,这里不贴了,可以格外查

 1 >>> np.ones((2,))
 2 array([1., 1.])
 3 >>> np.linspace(-3,3,3)
 4 array([-3.,  0.,  3.])
 5 >>> np.outer(np.ones((2,)),np.linspace(-3,3,3))
 6 array([[-3.,  0.,  3.],
 7        [-3.,  0.,  3.]])
 8 >>> np.outer(np.linspace(-3,3,3),np.ones(2))
 9 array([[-3., -3.],
10        [ 0.,  0.],
11        [ 3.,  3.]])

#方程组求解ax=b

1 >>> from scipy import linalg
2 >>> a=np.array([[3,2,0],[1,-1,0],[0,5,1]])
3 >>> b=np.array([2,4,-1])
4 >>> x=linalg.solve(a,b)
5 >>> x
6 array([ 2., -2.,  9.])
7 >>> np.dot(a,x)==b
8 array([ True,  True,  True])

1.5 NumPy应用实例

#NDVI计算,LANDSAT7,波段1234589

 1 >>> import osgeo.gdal as gdal
 2 >>> import numpy as np
 3 >>> ds_in=gdal.Open("landsat.tif")
 4 >>> im_width=ds_in.RasterXSize
 5 >>> im_height=ds_in.RasterYSize
 6 >>> im_bands=ds_in.RasterCount
 7 >>> im_geotrans=ds_in.GetGeoTransform()
 8 >>> im_proj=ds_in.GetProjection()
 9 >>> im_data=ds_in.ReadAsArray(0,0,im_width,im_height)
10 >>> ndvi=(im_data[3]-im_data[2])/(im_data[3]+im_data[2])#计算NDVI
11 >>> ndvi_=250.0*(ndvi-np.min(ndvi))/(np.max(ndvi)-np.min(ndvi))
12 >>> driver=gdal.GetDriverByName("GTiff")#创建图像
13 >>> ds_out=driver.Create("landsat_ndvi.tif",
im_width,im_height,1,gdal.GDT_Byte) 14 >>> ds_out.SetGeoTransform(im_geotrans) 15 0 16 >>> ds_out.SetProjection(im_proj) 17 0 18 >>> ds_out.GetRasterBand(1).WriteArray(ndvi_.astype(np.int16)) 19 0 20 >>> del ds_in#关闭 21 >>> del ds_out

猜你喜欢

转载自www.cnblogs.com/vividautumn/p/11623426.html
今日推荐