文章目录[隐藏]
numpy
- 矩阵等计算,还要了解基本的高数,概率,线代知识,这些都要用到。特别是矩阵的转换以及算法等等。
- 详细学习参考学习资料
NumPy 教程 | 菜鸟教程
查找xxxx的帮助文档
print(help(numpy.xxxx))
构造多维数组
numpy.array()
注意里面的值必须为同一类型,否则有类型转换;
eg:
# 构造数组
# 一维数组
vector = numpy.array([5,10,15,20])
# 二维数组,注意有两个括号
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)
print(matrix)
如何构造三维以上的数组是一个难点所在
mumpy/0.ipynb/13*14
查看xxx的结构/用于debug
print(xxxx.shape)
查询
print(xxxx[x,y])
print(xxxxx[,1]) # 取第一列
print(xxxx[:,0:2]) #第一和第二列
切片
print(xxxx[n:m])
判断值是否存在
xxx == m # xxx 中是否有m/会判断每一个值
整体值类型改变
v = numpy.array(["1","2","3"])
print(v.dtype)
print(v)
v = v.astype(float)
print(v.dtype)
print(v)
求极值
指定维度求值
# 对行求值
m = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
m.sum(axis=1)
# answer:
# array([ 30, 75, 120])
#对列求值
m = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
m.sum(axis=0)#answer:
#array([60, 75, 90])
变换为矩阵
import numpy as np
print(np.arange(15))
a = np.arange(15).reshape(3,5)
a
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14]])
求出array维度
print(xxx.ndim)
求出array元素个数
print(xxx.size)
初始化矩阵为0
import numpy as np
np.zeros((3,4))
# array([[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]])
指定类型
np.ones((2,3,4),dtype=np.int32)
# array([[[1, 1, 1, 1],
# [1, 1, 1, 1],
# [1, 1, 1, 1]],
# [[1, 1, 1, 1],
# [1, 1, 1, 1],
# [1, 1, 1, 1]]])
得出一个序列
np.arange(10,30,5)#该数>10 且< 30 从10开始每次加5
np.arange(10,30,5).reshape(4,3)# 注意元素个数是否够用
# array([[10, 15],
# [20, 25]])
随机模块
np.random.random((2,3)) # 第一个random是调用模块,第二个是调用函数,(2,3)是构造一个2*3的矩阵
# array([[0.05134094, 0.63073588, 0.14218974],
# [0.86727903, 0.95890848, 0.39738407]])
在一个区间上[x,y]平均间隔去取n个数
np.linspace[x,y,m]
np.linspace(2,3,5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])
数学运算
import numpy as np
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)
print("a - b " , a - b) # 对应位置相减
print("a - b - 1 :" , a - b - 1)
print("b**2" , b**2)
print("a < 35" , a < 35)
# [20 30 40 50]
# [0 1 2 3]
# a - b [20 29 38 47]
# a - b - 1 : [19 28 37 46]
# b**2 [0 1 4 9]
# a < 35 [ True True False False]
矩阵乘法
A = np.array([
[1,1],
[0,1]
])
B = np.array([
[2,0],
[3,4]
])
print('------A-------')
print(A)
print('------B-------')
print(B)
print('------A*B-------')
print(A*B) #对应位置相乘
print('------A.dot(B)-------')
print(A.dot(B)) # 矩阵乘法
print('------np.dot(A,B)-------')
print(np.dot(A,B)) # 也为矩阵乘法
# ------A-------
# [[1 1]
# [0 1]]
# ------B-------
# [[2 0]
# [3 4]]
# ------A*B-------
# [[2 0]
# [0 4]]
# ------A.dot(B)-------
# [[5 4]
# [3 4]]
# ------np.dot(A,B)-------
# [[5 4]
# [3 4]]
数学公式
e/平法等等
import numpy as np
B = np.arange(3)
print(B)
print(np.exp(B)) # e**B
print(np.sqrt(B)) # _/`B``
# [0 1 2]
# [1. 2.71828183 7.3890561 ]
# [0. 1. 1.41421356]
import numpy as np
a = np.floor(10*np.random.random((3,4))) # np.floor() //向下取整
print(a)
print('-------------')
print(a.ravel()) # 把矩阵拉成向量
print('-------------')
a.shape = (3,4) # 把向量拉成矩阵
#
# a.shape = (3,-1)
# -1帮你自动计算后一个维度的个数
#
print(a)
print('-------------')
print(a.T) # 矩阵转置
# [[3. 5. 8. 6.]
# [5. 6. 6. 7.]
# [1. 6. 2. 5.]]
# -------------
# [3. 5. 8. 6. 5. 6. 6. 7. 1. 6. 2. 5.]
# -------------
# [[3. 5. 8. 6.]
# [5. 6. 6. 7.]
# [1. 6. 2. 5.]]
# -------------
# [[3. 5. 1.]
# [5. 6. 6.]
# [8. 6. 2.]
# [6. 7. 5.]]
矩阵拼接
# 矩阵拼接
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print('----------a-----------')
print(a)
print('----------b-----------')
print(b)
print('----------------------')
print(np.hstack((a,b))) # 按行拼接
print('----------------------')
print(np.vstack((a,b))) # 按列拼接
# ----------a-----------
# [[2. 0.]
# [9. 7.]]
# ----------b-----------
# [[2. 0.]
# [6. 9.]]
# ----------------------
# [[2. 0. 2. 0.]
# [9. 7. 6. 9.]]
# ----------------------
# [[2. 0.]
# [9. 7.]
# [2. 0.]
# [6. 9.]]
矩阵数据切割
#数据分割
a = np.floor(10*np.random.random((2,12)))
print(a)
print('------------')
print(np.hsplit(a,3)) # 按行切分,3切分成3份,得到三个array值
print('------------')
print(np.hsplit(a,(3,4)))
# split a after the third and the fourth cloumn
# 在第三行和第四行后进行切割
print('------------')
a = np.floor(10*np.random.random((12,2)))
print(a)
print('-------------')
np.vsplit(a,3) # 按列切分
# [[4. 3. 3. 3. 7. 5. 7. 4. 6. 4. 6. 8.]
# [9. 9. 4. 8. 0. 4. 3. 5. 1. 9. 4. 4.]]
# ------------
# [array([[4., 3., 3., 3.],
# [9., 9., 4., 8.]]), array([[7., 5., 7., 4.],
# [0., 4., 3., 5.]]), array([[6., 4., 6., 8.],
# [1., 9., 4., 4.]])]
# ------------
# [array([[4., 3., 3.],
# [9., 9., 4.]]), array([[3.],
# [8.]]), array([[7., 5., 7., 4., 6., 4., 6., 8.],
# [0., 4., 3., 5., 1., 9., 4., 4.]])]
# ------------
# [[8. 2.]
# [3. 9.]
# [3. 5.]
# [5. 0.]
# [4. 3.]
# [2. 3.]
# [0. 2.]
# [5. 7.]
# [5. 5.]
# [7. 9.]
# [3. 8.]
# [0. 0.]]
# -------------
# [array([[8., 2.],
# [3., 9.],
# [3., 5.],
# [5., 0.]]), array([[4., 3.],
# [2., 3.],
# [0., 2.],
# [5., 7.]]), array([[5., 5.],
# [7., 9.],
# [3., 8.],
# [0., 0.]])]
复制
# 复制/有俩种方法
# 浅复制
c = a.view() # 浅复制,共用一套值
print(c is a)
c.shape = (2,6)
print('a.shape: ' ,a.shape)
print('c.shape: ' ,c.shape)
c[0,4] = 1234 # a 的值也变量,a和c共用了一套值
print(a)
print(id(a))
print(id(c))
# False
# a.shape: (3, 4)
# c.shape: (2, 6)
# [[ 0 1 2 3]
# [1234 5 6 7]
# [ 8 9 10 11]]
# 2540538182992
# 2540538442256
#
#
#
# 深复制
d = a.copy()
print(d is a)
d[0,0] = 9999
print('------d-------')
print(d)
print('------a-------')
print(a)
# False
# ------d-------
# [[9999 1 2 3]
# [1234 5 6 7]
# [ 8 9 10 11]]
# ------a-------
# [[ 0 1 2 3]
# [1234 5 6 7]
# [ 8 9 10 11]]
索引
#索引
import numpy as np
data = np.sin(np.arange(20).reshape(5,4))
print(data)
ind = data.argmax(axis = 0) # 按列来进行计算
print(ind) # 输出每一列的最大值所在的行(以0开始),索引
data_max = data[ind,range(data.shape[1])]
print(data_max)
# [[ 0. 0.84147098 0.90929743 0.14112001]
# [-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
# [ 0.98935825 0.41211849 -0.54402111 -0.99999021]
# [-0.53657292 0.42016704 0.99060736 0.65028784]
# [-0.28790332 -0.96139749 -0.75098725 0.14987721]]
# [2 0 3 1]
# [0.98935825 0.84147098 0.99060736 0.6569866 ]
在行和列进行扩展
# 扩展
import numpy as np
a = np.arange(0,40,10)
print(a)
b = np.tile(a,(3,5)) #构造一个三行五列的二维数组,每一个元素都是a
print(b)
# [ 0 10 20 30]
# [[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
# [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
# [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
排序
#排序
import numpy as np
a = np.array([[4,3,5],
[1,6,1],
[0,2,3]])
print(a)
print('------按列排序-------')
b = np.sort(a,axis = 0) #对二维数组排序,0为按列排序,1为按行排序
print(b)
#b
a.sort(axis = 1)
print('--------按行排序-----') #对二维数组排序,0为按列排序,1为按行排序
print(a)
print('################')
a = np.array([5,3,1,2])
j = np.argsort(a) # 索引,求最小值索引(编号)
print('-------最小值索引------')
print(j)
print('-------排序结果------')
print(a[j]) # 排序完之后的结果
# [[4 3 5]
# [1 6 1]
# [0 2 3]]
# ------按列排序-------
# [[0 2 1]
# [1 3 3]
# [4 6 5]]
# --------按行排序-----
# [[3 4 5]
# [1 1 6]
# [0 2 3]]
# ################
# -------最小值索引------
# [2 3 1 0]
# -------排序结果------
# [1 2 3 5]