numpy&pandas
- 安装numpy&pandas
- array创建
- numpy的基础运算
- numpy的索引
- numpy的 array 合并
- numpy的 array分割
- numpy的 copy & deep copy
安装numpy&pandas
import numpy as np
array=np.array([[1,2,3],[2,3,4]])# 将数组转化为矩阵
print(array) #打印矩阵
print('number of dim:',array.ndim) #几维
print('shape:',array.shape) #大小,几行几列
print('size:',array.size) #元素的大小
结果:
[[1 2 3][2 3 4]]
number of dim: 2
shape: (2, 3)
size: 6Process finished with exit code 0
array创建
import numpy as np
a=np.array([2,23,4],dtype=np.int) #创建时使用列表 并且确定类型
print(a.dtype)b=np.zeros((3,4))
print(b)c=np.ones((3,4),dtype=np.int)
print(c)d=np.empty((3,4))
print(d)e=np.arange(10,100,10)
print(e)f=np.arange(12).reshape((3,4))
print(f)g=np.linspace(10,100,10)
print(g)h=np.linspace(10,100,10).reshape((2,5))
print(h)
int32
[[0. 0. 0. 0.][0. 0. 0. 0.][0. 0. 0. 0.]]
[[1 1 1 1][1 1 1 1][1 1 1 1]]
[[0. 0. 0. 0.][0. 0. 0. 0.][0. 0. 0. 0.]]
[10 20 30 40 50 60 70 80 90]
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
[ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[[ 10. 20. 30. 40. 50.][ 60. 70. 80. 90. 100.]]Process finished with exit code 0
numpy的基础运算
1.加减乘除 求和
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
c=a-b #加减
print(a,b,c)d=a**2 #平方d
e=10*np.sin(a) #三角函数
print(d,e)print(b)
print(b<3) #哪些值小于3#矩阵运算
f=np.array([[10,20],[30,40]])
g=np.arange(4).reshape((2,2))
print(f)
print(g)
#
h=f*g
print('逐个相乘结果:\n',h)h_dot=np.dot(f,g)
h_dot2=f.dot(g)
print('矩阵相乘结果:\n',h_dot)
print('矩阵相乘结果2:\n',h_dot2)
[10 20 30 40] [0 1 2 3] [10 19 28 37]
[ 100 400 900 1600] [-5.44021111 9.12945251 -9.88031624 7.4511316 ]
[0 1 2 3]
[ True True True False]
[[10 20][30 40]]
[[0 1][2 3]]
逐个相乘结果:[[ 0 20][ 60 120]]
矩阵相乘结果:[[ 40 70][ 80 150]]
矩阵相乘结果2:[[ 40 70][ 80 150]]Process finished with exit code 0
#求和 最大最小值
import numpy as np
a=np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.sum(a,axis=1)) #axis=1 是在列中求和,axis=0是在行中求和
print(np.min(a))
print(np.min(a,axis=0))
print(np.max(a))
[[0.85663257 0.60903081 0.32588806 0.17383597][0.82810401 0.86504537 0.35610231 0.95728784]]
4.9719269475592105
[1.96538742 3.00653953]
0.17383597454524058
[0.82810401 0.60903081 0.32588806 0.17383597]
0.9572878423829763Process finished with exit code 0
2.其他运算
import numpy as np
A =np.arange(2,14).reshape((3,4))
print(A)
#最小/大值的索引
print(np.argmin(A))
print(np.argmax(A))#平均值
print(np.mean(A))
print(A.mean())
print(np.average(A))
#print(A.average()) #这个版本不适用
#中位数
print('中位数:',np.median(A),"\n")#累计相加
print(A)
print('累计相加:\n',np.cumsum(A),'\n')
#累差
print(A)
print('累差:\n',np.diff(A),'\n')print('行列:\n',np.nonzero(A),'\n')A=np.arange(14,2,-1).reshape((3,4))
print(A)
print('排序:\n',np.sort(A),'\n') #逐行排序print(A)
print('转置')
print(np.transpose(A))
print(A.T)
print((A.T).dot(A))print(A)
print('截取给定的数')
print('给定小于5的数等于5,大于9的数等于9\n',np.clip(A,5,9))print('axis=1 是在列中求和,axis=0是在行中求和')
print('求行中的平均值:')
print(np.mean(A,axis=0))
[[ 2 3 4 5][ 6 7 8 9][10 11 12 13]]
0
11
7.5
7.5
7.5
中位数: 7.5 [[ 2 3 4 5][ 6 7 8 9][10 11 12 13]]
累计相加:[ 2 5 9 14 20 27 35 44 54 65 77 90] [[ 2 3 4 5][ 6 7 8 9][10 11 12 13]]
累差:[[1 1 1][1 1 1][1 1 1]] 行列:(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64)) [[14 13 12 11][10 9 8 7][ 6 5 4 3]]
排序:[[11 12 13 14][ 7 8 9 10][ 3 4 5 6]] [[14 13 12 11][10 9 8 7][ 6 5 4 3]]
转置
[[14 10 6][13 9 5][12 8 4][11 7 3]]
[[14 10 6][13 9 5][12 8 4][11 7 3]]
[[332 302 272 242][302 275 248 221][272 248 224 200][242 221 200 179]]
[[14 13 12 11][10 9 8 7][ 6 5 4 3]]
截取给定的数
给定小于5的数等于5,大于9的数等于9[[9 9 9 9][9 9 8 7][6 5 5 5]]
axis=1 是在列中求和,axis=0是在行中求和
求行中的平均值:
[10. 9. 8. 7.]
numpy的索引
import numpy as np
A=np.arange(3,15).reshape((3,4))
print(A,"\n")
print(A[2])
print(A[2][2])
print(A[2,2]) #与上面同样功能
print(A[2,:]) #第二行所有数
print(A[:,1]) #第一列所有数
print('A:\n',A)print('第二行,第一列到第二列:')
print(A[2,1:3],'\n') #第二行,第一列到第二列print(A)
print('打印每一行:')
for row in A:print(row)
print('打印每一列:')
for column in A.T:print(column)print('打印出每个项目:')
print(A.flatten()) #返回一个array,flat是一个迭代器
for item in A.flat:print(item)
[[ 3 4 5 6][ 7 8 9 10][11 12 13 14]] [11 12 13 14]
13
13
[11 12 13 14]
[ 4 8 12]
A:[[ 3 4 5 6][ 7 8 9 10][11 12 13 14]]
第二行,第一列到第二列:
[12 13] [[ 3 4 5 6][ 7 8 9 10][11 12 13 14]]
打印每一行:
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
打印每一列:
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
打印出每个项目:
[ 3 4 5 6 7 8 9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14Process finished with exit code 0
numpy的 array 合并
import numpy as np
A=np.array([1,1,1])
B=np.array([2,2,2])C=np.vstack((A,B)) #vertical stack
D=np.hstack((A,B)) #horizontal stack
print(C)
print(D)
print(A.shape,C.shape,D.shape)
print(A.T)
print(A.T.shape) #么有改成列,要怎么改成列呢?看下面# print(A[np.newaxis,:]) #在行加一个维度
# print(A[:,np.newaxis]) #在列加一个维度
print(A[:,np.newaxis]) #在列加一个维度
print(B[:,np.newaxis])C= np.concatenate((A,B,B,A),axis=0)
print(C)
[[1 1 1][2 2 2]]
[1 1 1 2 2 2]
(3,) (2, 3) (6,)
[1 1 1]
(3,)
[[1][1][1]]
[[2][2][2]]
[1 1 1 2 2 2 2 2 2 1 1 1]Process finished with exit code 0
numpy的 array分割
import numpy as npA =np.arange(12).reshape((3,4))
print(A)print(np.split(A,3,axis=0))
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]Process finished with exit code 0
但是机器学习里经常需要实现不等量分割:
import numpy as npA =np.arange(12).reshape((3,4))
print(A)print(np.array_split(A,3,axis=1))
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
[array([[0, 1],[4, 5],[8, 9]]), array([[ 2],[ 6],[10]]), array([[ 3],[ 7],[11]])]Process finished with exit code 0
另外一种用法:
import numpy as npA =np.arange(12).reshape((3,4))
print(A)#print(np.array_split(A,3,axis=1))print(np.vsplit(A,3))
print(np.hsplit(A,2))
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],[4, 5],[8, 9]]), array([[ 2, 3],[ 6, 7],[10, 11]])]Process finished with exit code 0
numpy的 copy & deep copy
import numpy as np
a=np.arange(4)
print(a)
b=a
c=a
d=b
a[0]=11
print(a)
print(b is a)
print(b,c,d)
#改变其中任意一个 都会改变
d[1:3]=[22,23]
print(b,c,d)#deep copy
b=a.copy() #把a的值赋给b,但是不关联
a[3]=45
print(a,b)
print(b is a)
[0 1 2 3]
[11 1 2 3]
True
[11 1 2 3] [11 1 2 3] [11 1 2 3]
[11 22 23 3] [11 22 23 3] [11 22 23 3]
[11 22 23 45] [11 22 23 3]
FalseProcess finished with exit code 0