本次学习使用spyder编译器
1、数组类型
--------------------------------------------------------------分割线----------------------------------------------------------
类型转换
import numpy as np
a = np.array([0,12,5,20])
np.asarray(a,dtype=str) #asarray!一个函数!用法
a.astype(dtype=float) #astype!一个方法!用法
a
#上述函数与方法都会返回一个新数组,可被赋值
array(['0', '12', '5', '20'], dtype='<U11')
array([ 0., 12., 5., 20.])
array([ 0, 12, 5, 20]) #值得注意的是,原数组a仍不变(为整数类型)
--------------------------------------------------------------分割线----------------------------------------------------------
2、数组操作(重点)
数组排序
import numpy as np
a = np.array([1,3,5,4,87,54,102,505,12,311,57])
np.sort(a,axis=-1) #sort()从小到大排序,原数组顺序不变
np.argsort(a) #argsort()返回从小到大的排列在数组中的索引位置
array([ 1, 3, 4, 5, 12, 54, 57, 87, 102, 311, 505])
array([ 0, 1, 3, 2, 8, 5, 10, 4, 6, 9, 7], dtype=int64)
--------------------------------------------------------------分割线----------------------------------------------------------
数组计算
import numpy as np
a = np.array([1,3,5,4,87,54,102,505,12,311,57])
b = np.array([1,6,10,8,174,108,204,1010,24,622,114])
np.sum(a) or a.sum() #数组求和
np.max(a) or a.max() #数组元素求最大值
np.min(a) or a.min() #数组元素求最小值
np.mean(a) or a.mean() #数组元素求平均值
np.std(a) or a.std() #数组元素求标准差
np.cov(a,b) #数组元素求协方差
1141
505
1
103.72727272727273
153.06747823284337
array([[ 25772.61818182, 51555.50909091],
[ 51555.50909091, 103131.65454545]])
--------------------------------------------------------------分割线----------------------------------------------------------
多维数组操作
import numpy as np
a = np.arange(6)
a.shape = 2,3 #shape方法会修改原数组的值
a
a.shape
array([[0, 1, 2],
[3, 4, 5]])
(2, 3)
import numpy as np
a = np.arange(6)
a.reshape(2,3) #reshape方法不会改变原数组的值
a
array([0, 1, 2, 3, 4, 5])
--------------------------------------------------------------分割线----------------------------------------------------------
import numpy as np
a = np.arange(6)
a = a.reshape(2,3) #reshape有返回值,可被赋值
a
a.T #数组转置,原数组a不变
a.transpose() #数组转置,原数组a不变
array([[0, 1, 2],
[3, 4, 5]])
array([[0, 3],
[1, 4],
[2, 5]])
array([[0, 3],
[1, 4],
[2, 5]])
---------------------------------------------------------------分割线--------------------------------------------------------
数组连接
import numpy as np
x = np.array([[0,1,2],[10,11,12]])
y = np.array([[50,51,52],[60,61,62]])
z = np.concatenate((x,y),axis=0) #数组连接,concatenate((),axis)) axis默认为零,也可为1
p = np.concatenate((x,y),axis=1) #axis为1时,按列拼接
print(x.shape)
print(y.shape)
z
p
(2, 3)
(2, 3)
array([[ 0, 1, 2],
[10, 11, 12],
[50, 51, 52],
[60, 61, 62]])
array([[ 0, 1, 2, 50, 51, 52],
[10, 11, 12, 60, 61, 62]])
---------------------------------------------------------------分割线--------------------------------------------------------
import numpy as np
x = np.array([[0,1,2],[10,11,12]])
y = np.array([[50,51,52],[60,61,62]])
np.vstack((x,y)) #垂直堆叠vertical stack
np.hstack((x,y)) #横向堆叠horizontal stack
np.dstack((x,y)) #维度修改dimensional stack
array([[ 0, 1, 2],
[10, 11, 12],
[50, 51, 52],
[60, 61, 62]])
array([[ 0, 1, 2, 50, 51, 52],
[10, 11, 12, 60, 61, 62]])
array([[[ 0, 50],
[ 1, 51],
[ 2, 52]],
[[10, 60],
[11, 61],
[12, 62]]])