Python3----Numpy总结

Python–Numpy

1. 导包
import numpy as np
2. 创建一个数组Array(不同于List
array1 = np.array([1,2,3,4,5])# 数组当中存储相同的数据类型,不同于一般的列表
print(array1)
[1 2 3 4 5]
print(array1.dtype) # 查看当前数组中存储数据的数据类型
int32
array2 = np.array([1.,2,3,4])
print(array2) #If types do not match,Numpy will upcast if possbile(here,integers are upcast to flating point)
[1. 2. 3. 4.]
array3 = np.array([1,2,3,4],dtype = 'float64') #在定义时将数据类型转换
print(array3.dtype)
float64
np.array([range(i,i+3) for i in [2,3,4]]) # 可创建多维数组
array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])
3. 使用numpy创建一些特殊的数组
np.zeros(10,dtype='float32') #创建一个长度为10的,数据类型为float32 的数组
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
np.zeros([2,3],dtype='int32') #创建一个两行三列的数组
array([[0, 0, 0],
       [0, 0, 0]])
np.ones([3,3],dtype='float32') #数组的形状可以用元组代替列表
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)
np.full((2,2),3.14) #创建一个全部由3.14来填充的数组
array([[3.14, 3.14],
       [3.14, 3.14]])
np.arange(1,10,2) #创建一个线性序列,从1开始,10结束,步长为2,且不包含10
array([1, 3, 5, 7, 9])
np.linspace(0,1,5) #创建一个从0到1包含5个等间距点的数组
array([0.  , 0.25, 0.5 , 0.75, 1.  ])
np.random.random((3,3)) #create a 3x3 array of uniformly distributed,random values between 0 and 1
array([[0.96106772, 0.53449777, 0.99360549],
       [0.02162571, 0.90131666, 0.16706767],
       [0.27723283, 0.88013812, 0.96710278]])
np.random.normal(0,1,(3,3)) #with mean 0 and standard deviation 1,标准正态分布
array([[-0.00746501,  0.7445069 , -0.9031185 ],
       [-0.16900041,  0.69525934, -2.18961853],
       [-1.55475733, -0.33409044,  0.75753941]])
np.random.randint(1,10,(3,3)) #创建一个int数组,值在1,10之间
array([[5, 8, 5],
       [4, 4, 2],
       [3, 9, 4]])
np.eye(4) #创建一个单位矩阵
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
np.empty(2) #创建一个空数组,保存的值为当前内存中的值,未经初始化
array([5.43230922e-312, 7.29112202e-304])
4. Array的元素属性
np.random.seed(0) #创建随机数种子,保证实验的可重复性
x1 = np.random.randint(1,10,size=(2,3,5)) #创建一个3维数组
print(x1)
[[[6 1 4 4 8]
  [4 6 3 5 8]
  [7 9 9 2 7]]

 [[8 8 9 2 6]
  [9 5 4 1 4]
  [6 1 3 4 9]]]
print("x1 ndim:",x1.ndim) #x1 的维数
print("x1 shape",x1.shape)#x1 的形状
print("x1 size",x1.size) #x1 数组内的个数
x1 ndim: 3
x1 shape (2, 3, 5)
x1 size 30
5. Array 元素取值
element = np.arange(1,10,2)
print(element[0]) #取数组第一个元素
print(element[-1]) #取数组倒数第一个元素,即最后一个元素
1
9
elements = np.random.randint(1,10,(3,3))
print(elements)
[[8 4 3]
 [8 3 1]
 [1 5 6]]
print(elements[1][1]) #第二行第二列
3
print(elements[1,1]) #作用同上
3
elements[1,1] = 12
print(elements)
[[ 8  4  3]
 [ 8 12  1]
 [ 1  5  6]]
elements[0,0] = 3.14 #Numpy 会自动的修改数据类型。Don't be caught by this behavior!
print(elements)
[[ 3  4  3]
 [ 8 12  1]
 [ 1  5  6]]
6. Array 切片 Slicing
slicing = np.arange(10)
print(slicing[:3]) #前包后不包
print(slicing[8:]) 
print(slicing[6:-1]) #[6:-1)
[0 1 2]
[8 9]
[6 7 8]
print(slicing[::2]) #步长为2取值
print(slicing[::-1]) #反序
[0 2 4 6 8]
[9 8 7 6 5 4 3 2 1 0]
mutl = np.random.randint(1,10,[3,3])
print(mutl)
[[8 4 7]
 [8 3 1]
 [4 6 5]]
print(mutl[:1,:1]) #取第一行第一列的值
[[8]]
print(mutl[:1,::2]) #取第1行,步长为2的列的值
[[8 7]]
print(mutl[0,:]) #取第一行的所有列
print(mutl[0])
[8 4 7]
[8 4 7]
list1 = range(10)
for i in list1:
    print(i,end =" ")
print("")
tmp = list1[3]
tmp = 20
for i in list1:
    print(i,end =" ")
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
array1 = np.arange(10).reshape(2,5) # 改变数组的形状shape
print(array1)
tmp = array1[:,1]
print(tmp)
tmp[0] = 20
print(array1) #当我们用变量接收数组的切片结果时,我们改变变量的值,如同改变原有数组中的值
[[0 1 2 3 4]
 [5 6 7 8 9]]
[1 6]
[[ 0 20  2  3  4]
 [ 5  6  7  8  9]]
tmp2 = array1[:,1].copy()
print(tmp2)
[20  6]
tmp[0] = 1
print(array1)
[[0 1 2 3 4]
 [5 6 7 8 9]]
7. Array数组的拼接
x = np.array([1,2,3])
y = np.array([3,2,1])
np.concatenate([x,y]) # a tuple or a list
array([1, 2, 3, 3, 2, 1])
z = np.array([99,99,99])
np.concatenate([x,y,z])
array([ 1,  2,  3,  3,  2,  1, 99, 99, 99])
grid = np.arange(1,7).reshape(2,3)
print(grid)
[[1 2 3]
 [4 5 6]]
np.concatenate([grid,grid]) #concatenate along the first axis(zero-indexed)
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
np.concatenate([grid,grid],axis = 1) # concatenate along the second axis(zero-indexed)
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])
np.vstack([grid,grid]) #竖直拼接
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
np.hstack([grid,grid]) #水平拼接
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])
  1. 轴这个概念是为不止一个维度的数组准备的;
  2. axis = 0,第一个维度,垂直向下贯穿所有行数据,数据聚合时,注意贯穿,合并数组时,注意垂直向下;
  3. axis = 1,第二个维度,水平贯穿所有列数据,注意的点如上;
x = np.arange(10)
print(x)
[0 1 2 3 4 5 6 7 8 9]
x1,x2,x3 = np.split(x,[1,3]) #数组分段[:1][1:3][3:]下标
print(x1,x2,x3)
[0] [1 2] [3 4 5 6 7 8 9]
y = np.arange(1,13).reshape(3,4)
print(y)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
y1,y2 = np.vsplit(y,[1])
print(y1,"\n",y2)
[[1 2 3 4]] 
 [[ 5  6  7  8]
 [ 9 10 11 12]]
y3,y4 = np.hsplit(y,[1])
print(y3,"\n",y4)
[[1]
 [5]
 [9]] 
 [[ 2  3  4]
 [ 6  7  8]
 [10 11 12]]
print(y)
print(y.sum(axis=0)) #列的和,即贯穿所有行
print(y.sum(axis=1)) #行的和
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
[15 18 21 24]
[10 26 42]
8. Array数学函数
print(y.min(),y.max()) #最大、最小值
1 12 3.452052529534663
print(y.var(),y.std()) #方差、标准差
11.916666666666666 3.452052529534663
9. Rules of Broadcasting
  • If the two arrays differ in their number of dimensions,the shape of the one with fewer dimensions is padded with ones on its leading (left)side;
  • If the shape of the two arrays does not match in any dimension,the array with shape equal to 1 in that dimension is stretched to match the other shape;
  • If in any dimension the sizes disagree and neither is equal to 1,an error is raised.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值