- 快速
- 方便
- 有科学计算的基础库
NumPy(Numerical Python)是 Python 科学计算的基础包,它是一个开源的 Python 扩展库,用来支持大数据量的高维数组和矩阵运算,比 Python 自身的嵌套列表(该结构也可以用来表示矩阵)结构要高效的多。
3、numpy的数据类型
import random import numpy as np # 使用numpy 生成数组,得到ndarray 的数据类型 t1 = np.array([1,2,3]) print(t1) # [1 2 3] print(type(t1)) #5、numpy数组的形状t2 = np.array(range(10)) print(t2) # [0 1 2 3 4 5 6 7 8 9] print(type(t2)) # t3 = np.arange(10) print(t3) # [0 1 2 3 4 5 6 7 8 9] print(type(t3)) # t4 = np.arange(2, 10, 2) print(t4) # [2 4 6 8] print(type(t4)) # # 查看数据类型 print(t1.dtype) # int32 print(t2.dtype) # int32 print(t3.dtype) # int32 print(t4.dtype) # int32 # 创建numpy 时指定 数据类型 t5 = np.array(range(1,4),dtype="int8") print(t5) # [1 2 3] print(t5.dtype) # int8 t5 = np.array(range(1,4),dtype=float) print(t5) # [1. 2. 3.] print(t5.dtype) # float64 t5 = np.array(range(1,4),dtype=int) print(t5) # [1 2 3] print(t5.dtype) # int32 t5 = np.array([1,1,0,1,0],dtype=bool) print(t5) # [ True True False True False] print(t5.dtype) # bool 布尔类型 # 修改数据类型 t6 = t5.astype("int8") print(t6) # [1 1 0 1 0] print(t6.dtype) # int8 # numpy 中的小数 t7 = np.array([random.random() for i in range(3)]) print(t7) # [0.94210459 0.37335804 0.38199125] print(t7.dtype) # float64 # 保留小数 t8 = np.round(t7,2) print(t8)
https://www.bilibili.com/video/BV1hx411d7jb?p=15
https://www.bilibili.com/video/BV1hx411d7jb?p=14
https://www.bilibili.com/video/BV1hx411d7jb?p=15
https://www.runoob.com/numpy/numpy-tutorial.html



