python-重点工具(Numpy)

本文最后更新于:July 14, 2022 pm

Numpy基础数据结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy  as np

ar = np.array([[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]) #数组没有逗号
print(ar)

print(ar.ndim) #几维数组

print(ar.shape)

print(ar.size) #总共元素的个数

print(type(ar),ar.dtype)

print(ar.itemsize) #字节大小、

print(ar.data)
1
2
3
4
5
6
7
8
9
10
#  如何创建数组

ar1 = np.array(range(10))
ar2 = np.arange(10)
ar3 = np.array([1,2,3,4,5])
print(ar1)
print(ar2)
print(ar3)

print(np.random.rand(10).reshape(2,5))
1
2
3
4
5
6
7
#linspace():创建num个均匀间隔的样本

print(np.linspace(10,20,num = 21))

print(np.linspace(10,20,num = 21,endpoint = True)) #最后是否被包含

print(np.linspace(10,20,num = 21,retstep = True)) #元素本身和步长
1
2
3
4
5
6
7
8
#   zeros()   zeros_like()      ///  ones()    ones_like()-----和前边一样,只不过是用“1”来填充

print(np.zeros((3,5)), ) #用“0”来填充

ar = np.array([list(range(10)),list(range(10,20))])
print(ar)
print(type(ar))
print(np.zeros_like(ar))
1
2
3
#eye()

print(np.eye(5)) #矩阵的数组

Numpy通用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ar1 = np.arange(10)
ar2 = np.ones((5,2))

print(ar1)
print(ar1.T)
print(ar2)
print(ar2.T)

ar3 = ar1.reshape(2,5)
ar4 = np.zeros((4,6)).reshape(3,8)
ar5 = np.reshape(np.arange(12),(3,4))

print(ar3)
print(ar4)
print(ar5)

ar6 = np.resize(np.arange(5),(3,4))
print(ar6)
1
2
3
4
5
6
7
8
9
10
11
#数组的复制
ar1 = np.arange(10)
ar2 = ar1
print(ar1 is ar2)
ar1[2] = 100
print(ar1,ar2)

s = (np.arange(10))
print(np.resize(s,(2,6))) #形成了新的数组
print(s.resize(2,6)) #改变了原来的数组
print(s)
1
2
3
4
5
6
#数组类型转变  :  astype()

ar1 = np.arange(10,dtype = float)
ar2 = ar1.astype(np.int64)
print(ar1,ar1.dtype)
print(ar2,ar2.dtype)
1
2
3
4
5
6
7
8
9
#数组的堆叠
a = np.arange(5)
b = np.arange(5,10)

print(np.hstack((a,b))) #横向连接
print(np.vstack((a,b))) #纵向连接

print(np.stack((a,b))) #默认是纵向连接
print(np.stack((a,b),axis=1))
1
2
3
4
#数组的拆分
ar = np.arange(16).reshape(4,4)
print(np.hsplit(ar,2)[0]) #横向拆分
print(np.vsplit(ar,2)[0]) #纵向拆分

Numpy的索引及切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ar = np.arange(20)
print(ar)
print(ar[4])
print(ar[3:6])
print('-----')

ar = np.arange(16).reshape(4,4)
print(ar,'数组的轴数是%i'%ar.ndim)
print(ar[2],'数组轴数为%i'%ar[2].ndim)
print(ar[2][1])
print(ar[1:3])
print(ar[2,2])
print(ar[:2,1:])
print('-------')

ar1 = np.arange(8).reshape(2,2,2)
print(ar1,'数组的轴数是%i'%ar1.ndim)
print(ar1[0],'数组轴数为%i'%ar1[0].ndim)
print(ar1[0][0],'数组轴数为%i'%ar1[0][0].ndim)
print(ar1[0][0][1],'数组轴数为%i'%ar1[0][0][1].ndim)
1
2
3
4
5
6
7
8
9
#布尔型索引及切片(只保留True的值)
ar = np.arange(12).reshape(3,4)
print(ar)
i = np.array([True,False,True])
j = np.array([True,True,False,False])
print(i)
print(j)
print(ar[i])
print(ar[ar>5])

Numpy随机数

1
2
3
#随机数生成
samples = np.random.normal(size=(4,4))*100 #0-1之间的数字,在后面*100就会变成0-100之间的随机数
print(samples)
1
2
3
4
5
6
7
8
9
10
11
12
a = np.random.rand()
print(a)

print( np.random.rand(4))
print( np.random.rand(2,4))

data1 = ( np.random.rand(500))
data2 = ( np.random.rand(500)) #rand后边加一个n就会生成一个正态分布的图形

import matplotlib.pyplot as plt # 会生成一个均匀分布的散点图
% matplotlip inline
plt.scatter(data1,data2)
1
2
3
4
5
#随机整数
print(np.random.randint(2))
print(np.random.randint(2,10))
print(np.random.randint(2,size=10))
print(np.random.randint(2,size=(2,5)))

Numpy数据的输入和输出

1
2
3
4
5
6
7
#  存储数组数据 .npy文件
import os
os.chdir('C:\\Users\\86155\\Desktop\\')

ar = np.random.rand(5,5)
print(ar)
np.save('arraydata.npy',ar)
1
2
3
4
#读取数组数据 .npy文件

ar_load = np.load('arraydata.npy')
print(ar_load)