Numpy的学习笔记
Numpy的学习笔记
#numpy
自用内容
简写np,矩阵的运算,
下面是输出矩阵:
import numpy as np
array = np.array([[1,2,3],[2,3,4]])
print(array)
#维度
print(“number of dim: “,array.ndim)
#形状 ?X ?的数组
print(“shape:”,array.shape)
#多少元素
print(“size:”,array.size)
[[1 2 3]
[2 3 4]]
number of dim: 2
shape: (2, 3)
size: 6
创建array,定义type
a = np.array([2,23,4],dtype = np.int64)
1
生成全部为0 和1的数组三行四列
zeroArray = np.zeros((3,4))
print(zeroArray)
oneArray = np.ones((3,4))
print(oneArray)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
1.生成有步长的数组:
2.生成0—11的数列
arangeArray = np.arange(10,20,2)
print(arangeArray)
numArray = np.arange(12).reshape((3,4))
print(numArray)
[10 12 14 16 18]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
生成线段,生成5点4段数列
linArray = np.linspace(1,10,6).reshape((2,3))
print(linArray)
[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
numpy基础运算
1.加减法
2.平方
3.三角函数的运算
aArray = np.array([10,20,30,40])
bArray = np.arange(4)
c= aArray – bArray
print(c)
d = c **2
print(d)
sinA = np.sin(10)
print(sinA)
[10 19 28 37]
[ 100 361 784 1369]
-0.5440211108893698
数组的逻辑运算
1.筛选出数组中比3小的数
2.等于3的数
3.数组中逐个相乘
print(bArray<3)
print(bArray == 3)
aShape = aArray.reshape(2,2)
bShape = bArray.reshape(2,2)
print(aShape)
print(bShape)
mutArray = aShape * bShape #逐个相乘
mut_dotArray = np.dot(aShape,bShape) #矩阵相乘
#或者
mut_Array = aShape.dot(bShape)
print(mutArray)
print(mut_dotArray)
[ True True True False]
[False False False True]
[[10 20]
[30 40]]
[[0 1]
[2 3]]
[[ 0 20]
[ 60 120]]
[[ 40 70]
[ 80 150]]
随机生成一个0-1的矩阵
求和np.sum()
*小值np.min()
*大值np.max()
axis = 1 行
axis = 0 列
randomArray = np.random.random((2,4))
print(randomArray)
print(np.sum(randomArray,axis = 1))
[[0.54661074 0.27343044 0.42980382 0.04123646]
[0.10769581 0.77549246 0.28787614 0.27292254]]
#*小值的索引
print(np.argmin(randomArray))
#max index
print(np.argmax(randomArray))
#mean
print(np.mean(randomArray))
print(randomArray.mean())
#median
print(np.median(randomArray))
#累加
print(np.cumsum(randomArray))
#累差
print(np.diff(randomArray))
#排序sort,逐行排序
print(np.sort(randomArray))
#矩阵反向:行列对换,转置
print(np.transpose(randomArray))
print(randomArray.T)
3
5
0.34188355032710077
0.34188355032710077
0.2806532854593555
[0.54661074 0.82004118 1.249845 1.29108145 1.39877726 2.17426972
2.46214586 2.7350684 ]
[[-0.27318031 0.15637338 -0.38856736]
[ 0.66779665 -0.48761633 -0.01495359]]
[[0.04123646 0.27343044 0.42980382 0.54661074]
[0.10769581 0.27292254 0.28787614 0.77549246]]
[[0.54661074 0.10769581]
[0.27343044 0.77549246]
[0.42980382 0.28787614]
[0.04123646 0.27292254]]
[[0.54661074 0.10769581]
[0.27343044 0.77549246]
[0.42980382 0.28787614]
[0.04123646 0.27292254]]
clip
让小于或大于设定的某个数,都近似于设定的数字
setA = np.arange(2,14).reshape(3,4)
print(setA)
#求列中的平均值
print(np.mean(setA,axis = 0))
print(np.clip(setA,5,9))
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[6. 7. 8. 9.]
[[5 5 5 5]
[6 7 8 9]
[9 9 9 9]]
index
print(setA)
#第3行的所有数
print(setA[2])
print(setA[2,:])
#第2行第三列
print(setA[1,2])
print(setA[2][1])
#第2列:
print(setA[:,1])
#取【7,8】,取左不取右
print(setA[1,1:3])
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[10 11 12 13]
[10 11 12 13]
[ 3 7 11]
[7 8]
for循环输出每行,row和column都只是参数名
for row in setA:
print(row)
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]
for循环输出每列
for column in setA.T:
print(column)
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
迭代出每一个数
for item in setA.flat:
print(item)
#第二行的每一个数
print(setA)
for itemR2 in setA[1].flat:
print(itemR2)
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
array的数据处理
setD = np.array([1,1,1])
setC = np.array([2,2,2])
setE = np.array([3,3])
print(np.vstack((setD,setC)))#vertical stack 变成二维上下合并
print(np.hstack((setD,setC)))#左右合并
[[1 1 1]
[2 2 2]]
[1 1 1 2 2 2]
一维数组变二维数组
#将一维数组增加了一个维度,变成二维数组(1X3)
print(setD[np.newaxis,:])
#变成(3×1)
print(setD[:,np.newaxis])
[[1 1 1]]
[[1]
[1]
[1]]
多个array的合并,任意维度
#左右合并 axis = 1
setRow = np.concatenate((setC[np.newaxis,:],setD[np.newaxis,:]),axis = 1)
print(setRow)
#上下合并 axis = 0
setColumn = np.concatenate((setC[np.newaxis,:],setD[np.newaxis,:]),axis = 0)
print(setColumn)
[[2 2 2 1 1 1]]
[[2 2 2]
[1 1 1]]
array的分割:
myArr = np.arange(12).reshape(3,4)
print(myArr)
#左右分割
print(np.split(myArr,2,axis = 1))
print(np.hsplit(myArr,2))
#左右不等量分割,分出三个不等量的矩阵
print(np.array_split(myArr,3,axis = 1))
#上下分割
print(np.vsplit(myArr,3))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
Numpy 的copy 和deep copy
在numpyArray中,改变了赋值的数,被赋值的数也会跟着改变
#copy,仅copy初始值,不copy后面可能会改变的
print(setC)
setCopy = setC.copy()
#1.更改数组中的某一个值,得是整数
setC[0] = 3
print(setC)
#2.同时改变几个数字:
setC[1:3] = [4,6]
print(setC)
print(setCopy)
[2 2 2]
[3 2 2]
[3 4 6]
[2 2 2]
1
2
3
4