Numpy基本操作

Python科学计算-Numpy

Numpy-多维数组(上)

1.创建numpy数组

import numpy as np
v1 = np.array([1,2,3,4])
print(v1)
type(v1)
print(v1.shape)
print(v1.size)
v2 = np.array([[1,2],[3,4]])
type(v2)
print(v2.shape)
print(v2.size)
[1 2 3 4]
(4,)
4
(2, 2)
4
x = np.arange(0,10,1)
print(x)
x = np.arange(-1,1,0.1)
print(x)
[0 1 2 3 4 5 6 7 8 9]
[ -1.00000000e+00  -9.00000000e-01  -8.00000000e-01  -7.00000000e-01
  -6.00000000e-01  -5.00000000e-01  -4.00000000e-01  -3.00000000e-01
  -2.00000000e-01  -1.00000000e-01  -2.22044605e-16   1.00000000e-01
   2.00000000e-01   3.00000000e-01   4.00000000e-01   5.00000000e-01
   6.00000000e-01   7.00000000e-01   8.00000000e-01   9.00000000e-01]
x = np.random.rand(5,5) #uniform random numbers in [0,1]
print(x)
[[ 0.3084717   0.26712706  0.78337614  0.15270855  0.03677639]
 [ 0.7858553   0.3741689   0.64712306  0.15857866  0.98599454]
 [ 0.10074502  0.0929277   0.7856723   0.34105588  0.19593979]
 [ 0.46291147  0.56004176  0.47740925  0.38668993  0.84500669]
 [ 0.00214992  0.72528686  0.46597411  0.41659678  0.79489281]]

Numpy-多维数组(下)

1. 操作numpy数组常用函数

import numpy as np
x = np.arange(0,10,0.5)
print(x)
[ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5  5.   5.5  6.   6.5  7.
  7.5  8.   8.5  9.   9.5]
mask = (5 < x)*(x < 7.5)
print(mask)
"""
where函数能将索引掩码转换成索引位置:
"""
indices = np.where(mask)
print(indices)
[False False False False False False False False False False False  True
  True  True  True False False False False False]
(array([11, 12, 13, 14], dtype=int64),)
"""
diag函数能够提取出数组的对角线
"""
A = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
print(A)
print(np.diag(A))
print(np.diag(A,-1))
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
[ 1  6 11 16]
[ 5 10 15]
"""
take函数与高级索引(fancy indexing)用法相似
"""
v2 = np.arange(-3,3)
print(v2)
row_indices = [1,3,5]
print(v2[row_indices]) #fancy indexing
print(v2.take(row_indices))
"""
但是take也可以用在list和其它对象上:
"""
v3 = np.take([-3,-2,-1,0,1,2],row_indices)
print(v3)
[-3 -2 -1  0  1  2]
[-2  0  2]
[-2  0  2]
[-2  0  2]
"""
choose选取多个数组的部分组成新的数组  
"""
which = [0,1,0,1]
choices = [[-2,-2,-2,-2], [5,5,5,5]]
new_array = np.choose(which, choices)
print(new_array)
[-2  5 -2  5]

2. 线性代数

"""
标量计算
"""
v1 = np.arange(0,4)
print(v1)
print(v1 * 2)
print(v1 + 2)
[0 1 2 3]
[0 2 4 6]
[2 3 4 5]
"""
数组-数组运算
"""
A = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]])
print(A)
print(A * A)
print(A.shape, v1.shape)
print(A * v1)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[[  0   1   4   9]
 [ 16  25  36  49]
 [ 64  81 100 121]
 [144 169 196 225]]
(4, 4) (4,)
[[ 0  1  4  9]
 [ 0  5 12 21]
 [ 0  9 20 33]
 [ 0 13 28 45]]
"""
使用dot函数进行矩阵-矩阵,矩阵-向量,数量积乘法
"""
print(np.dot(A,A))
print(np.dot(v1,A))
print(np.dot(A,v1))
print(np.dot(v1,v1))
[[ 56  62  68  74]
 [152 174 196 218]
 [248 286 324 362]
 [344 398 452 506]]
[56 62 68 74]
[14 38 62 86]
14
"""
将数组对象映射到matrix类型
"""
M = np.matrix(A)
print(M)
V = np.matrix(v1).T
print(V)
print(M.shape, V.shape)
print(M * V)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[[0]
 [1]
 [2]
 [3]]
(4, 4) (4, 1)
[[14]
 [38]
 [62]
 [86]]
C = np.matrix([[1j,2j],[3j,4j]])
print(C)
[[ 0.+1.j  0.+2.j]
 [ 0.+3.j  0.+4.j]]
"""
共轭和共轭转置
"""
conju = np.conjugate(C)
print(conju)   #共轭
print(conju.H) #共轭转置

"""
real与imag能够分别得到复数的实部与虚部
"""
print(np.real(conju))
print(np.imag(conju))

"""
angle与abs可以分别得到幅角和绝对值
"""
print(np.angle(conju+1))
print(np.abs(conju))
[[ 0.-1.j  0.-2.j]
 [ 0.-3.j  0.-4.j]]
[[ 0.+1.j  0.+3.j]
 [ 0.+2.j  0.+4.j]]
[[ 0.  0.]
 [ 0.  0.]]
[[-1. -2.]
 [-3. -4.]]
[[-0.78539816 -1.10714872]
 [-1.24904577 -1.32581766]]
[[ 1.  2.]
 [ 3.  4.]]
from scipy.linalg import *
"""
矩阵求逆inv()
"""
C = np.matrix([[1j,2j],[3j,4j]])
print(C)
print(inv(C))

"""
行列式linalg.det
"""
print(det(C))
[[ 0.+1.j  0.+2.j]
 [ 0.+3.j  0.+4.j]]
[[-0.+2.j   0.-1.j ]
 [ 0.-1.5j  0.+0.5j]]
(2-0j)
import numpy as np

A = np.arange(0,100).reshape(10,10)
print(A)
print(np.shape(A))
print('*' * 50)

"""
平均值 
"""
print(np.mean(A)) #对所有元素求均值
print(np.mean(A, axis=0))  #对各列求均值
#print(np.mean(B, axis=1)) #对各行求均值
print('*' * 50)

"""
标准差与方差 
"""
print(np.std(A[:,2]), np.var(A[:,2]))
print('*' * 50)

"""
最小值与最大值
"""
print(A[:,2].min(), A[:,2].max())
print('*' * 50)

"""
总和, 总乘积与对角线和
"""
d = np.arange(0,100).reshape(10,10)
print(d)
print(np.sum(d))   #求元素的总和
print(np.trace(d)) #矩阵对角线的和
print(d.flatten()) #创建一个高阶数组的向量版本
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]
(10, 10)
**************************************************
49.5
[ 45.  46.  47.  48.  49.  50.  51.  52.  53.  54.]
**************************************************
28.7228132327 825.0
**************************************************
2 92
**************************************************
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]
4950
495
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99]
"""
增加一个新维度: newaxis,将一个向量转换成列矩阵和行矩阵
"""
import numpy as np

V = np.array([1,2,3])
print(V)
print(V.shape)
print('*' * 50)

print(V[:, np.newaxis]) #将一个向量转换成列矩阵
print(V[:, np.newaxis].shape) #列矩阵
print('*' * 50)

print(V[np.newaxis,:])  #将一个向量转换成行矩阵
print(V[np.newaxis,:].shape) #行矩阵
print('*' * 50)

"""
叠加与重复数组: 函数 repeat, tile, vstack, hstack, 与 concatenate
能帮助我们以已有的矩阵为基础创建规模更大的矩阵
"""

"""
title与repeat
"""

a = np.array([[1,2],[3,4]])
print(np.repeat(a, 3)) #每个元素重复三次
print(np.tile(a, 3))  #tile矩阵三次
print('*' * 50)

"""
concatenate “串联”
"""
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))  #串联一行
print(np.concatenate((a, b.T), axis=1))  #串联一列
print('*' * 50)

"""
hstack与vstack
"""
print(np.vstack((a,b)))
print(np.hstack((a,b.T)))

[1 2 3]
(3,)
**************************************************
[[1]
 [2]
 [3]]
(3, 1)
**************************************************
[[1 2 3]]
(1, 3)
**************************************************
[1 1 1 2 2 2 3 3 3 4 4 4]
[[1 2 1 2 1 2]
 [3 4 3 4 3 4]]
**************************************************
[[1 2]
 [3 4]
 [5 6]]
[[1 2 5]
 [3 4 6]]
**************************************************
[[1 2]
 [3 4]
 [5 6]]
[[1 2 5]
 [3 4 6]]
import numpy as np
"""
浅拷贝与深拷贝
"""
A = np.array([[1,2], [3,4]])
print(A)
B = A  #浅拷贝
B[0,0] = 10  #更改B后,A也会随之改变
print(B)
print(A)
print('*' * 30)

B = np.copy(A)
B[0,0] = -5  #更改B后,A不会随之改变
print(B)
print(A)
print('*' * 30)

"""
遍历数组元素
"""
V = np.array([1,2,3,4])
for element in V:
    print(element)
M = np.array([[1,2],[3,4]])
for row in M:
    print("row", row)
    for element in row:
        print(element)
        
"""
当我们需要遍历数组并且更改元素内容的时候,
可以使用 enumerate 函数同时获取元素与对应的序号:
"""
for row_idx, row in enumerate(M):
    print("row_idx", row_idx, "row", row)
    
    for col_idx, element in enumerate(row):
        print("col_idx", col_idx, "element", element)
        M[row_idx, col_idx] = element ** 2
print(M)
print('*' * 30)
[[1 2]
 [3 4]]
[[10  2]
 [ 3  4]]
[[10  2]
 [ 3  4]]
******************************
[[-5  2]
 [ 3  4]]
[[10  2]
 [ 3  4]]
******************************
1
2
3
4
row [1 2]
1
2
row [3 4]
3
4
row_idx 0 row [1 2]
col_idx 0 element 1
col_idx 1 element 2
row_idx 1 row [3 4]
col_idx 0 element 3
col_idx 1 element 4
[[ 1  4]
 [ 9 16]]
******************************
"""
数组与条件判断
"""
import numpy as np

M = np.array([[1,4],[9,16]])
if(M > 5).any():
    print("在M元素中至少一个元素大于5")
else:
    print("在M元素中没有元素大于5")

M = np.array([[1,4],[9,16]])
if(M > 5).all():
    print("在M元素所有元素都大于5")
else:
    print("在M元素中不是所有元素元素大于5")

"""
类型转换: 对某些元素数据类型进行转换生成新的数组,使用 astype 函数
"""
print(M.dtype)
M2 = M.astype(float)
print(M2)
M3 = M.astype(bool)
print(M3)
在M元素中至少一个元素大于5
在M元素中不是所有元素元素大于5
int32
[[  1.   4.]
 [  9.  16.]]
[[ True  True]
 [ True  True]]
posted @ 2020-06-08 11:11  BrianX  阅读(253)  评论(0编辑  收藏  举报