Numpy
一、定义
一个基于python的扩展库
提供高维数组对象ndarray,运算速度碾压python List
提供了各种高级数据编程工具,如矩阵运算、向量运算、快速筛选、IO操作、傅里叶变换、线性代数、随机数等
二、属性
- ndim:维度
- shape:形状(各维度的长度)
- size:总长度
- dtype:元素类型
三、初始numpy
1、创建 ndarray
import numpy as np
arr1 = np.array([1,2,3])
type(arr1)
>>输出结果:<class 'numpy.ndarray'>
arr1.ndim
>>输出结果:1
arr1.shape
>>输出结果:(3,)
arr1.size
>>输出结果:3
arr1.dtype
>>输出结果:int32
arr2 = np.array([[1,2,3],[2,3,4]])
arr2.ndim, arr2.shape, arr2.size, arr2.dtype
>>输出结果:
2 (2, 3) 6 int32
2.强制类型统一
numpy设计初衷是用于运算的,所以对数据类型进行统一优化。
注意:
- numpy 默认 ndarray 的所有元素的类型是相同的。
- 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str > float > int 。
list1 = [1,2,3, 4.12]
print(list1)
>>输出结果:[1,2,3, 4.12]
arr1 = np.array(list1)
print(arr1)
>>输出结果:[1. 2. 3. 4.12]
arr1.dtype
>>输出结果:float64
3. numpy 中的数据类型
名称 描述
bool_ 布尔型数据类型(True 或者 False)
int_ 默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc 与 C 的 int 类型一样,一般是 int32 或 int 64
intp 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8 字节(-128 to 127)
int16 整数(-32768 to 32767)
int32 整数(-2147483648 to 2147483647)
int64 整数(-9223372036854775808 to 9223372036854775807)
uint8 无符号整数(0 to 255)
uint16 无符号整数(0 to 65535)
uint32 无符号整数(0 to 4294967295)
uint64 无符号整数(0 to 18446744073709551615)
float_ float64 类型的简写
float16 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_ complex128 类型的简写,即 128 位复数
complex64 复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128 复数,表示双 64 位浮点数(实数部分和虚数部分)
4. 使用 numpy 的 routines 函数创建 ndarray
4.1 np.ones(shape, dtype=None, order=‘C’),shape: 形状,使用元组表示
arr1 = np.ones(shape=(2,3))
print(arr1)
>>输出结果:
[[1. 1. 1.]
[1. 1. 1.]]
arr1 = np.ones(shape=(3,))
print(arr2)
>>输出结果:
[1., 1., 1.]
arr3 = np.ones(shape=(1,3))
print(arr3)
>>输出结果:
[[1., 1., 1.]]
arr4 = np.ones(shape=(3,1))
print(arr4)
>>输出结果:
[[1.],
[1.],
[1.]]
arr5 = np.ones(shape=(3,2), dtype=np.int8)
print(arr5。dtype)
>>输出结果:
int8
4.2 np.zeros(shape, dtype=float, order=‘C’)
arr = np.zeros(shape=(3,1),dtype=np.uint8)
print(arr,arr.dtype)
>>输出结果:
[[0]
[0]
[0]] uint8
4.3 np.full(shape, fill_value, dtype=None, order=‘C’)
arr = np.full(shape=(3,3), fill_value=6)
print(arr)
>>输出结果:
[[6 6 6]
[6 6 6]
[6 6 6]]
4.4 np.eye(N, M=None, k=0, dtype=float)
N表示行数,M表示列数,k表示对角线的偏移量
arr1 = np.eye(N=3)
print(arr1)
>>输出结果:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
arr2 = np.eye(N=3,M=4)
print(arr2)
>>输出结果:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
arr3 = np.eye(N=3,k=1)
print(arr3)
>>输出结果:
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
4.5 np.linspace(start, stop, num endpoint=True, retstep=False, dtype=None)
start:起始值,stop:终止值,num:生成数量,endpoint:是否包含终值,默认包含,retstep:是否显示步长,默认不显示
arr1 = np.linspace(0, 10, 11)
print(arr1)
>>输出结果:
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
arr2 = np.linspace(0, 100, 10, endpoint=False)
print(arr2)
>>输出结果:
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90.]
arr3 = np.linspace(0, 10, 11, retstep=True)
print(arr3)
>>输出结果:
(array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
4.6 np.arange(start, stop, step, dtype=None)
arr = np.arange(0, 100, step=10)
print(arr)
>>输出结果:
[ 0 10 20 30 40 50 60 70 80 90]
4.7 np.random.randint(low, high=None, size=None, dtype=‘l’)
arr1 = np.random.randint(low=0, high=100, size=(3,5))
print(arr1)
>>输出结果:
[[71 77 21 50 22]
[97 2 76 15 88]
[39 65 35 83 31]]
arr2 = np.random.randint(0,10, size=(10,))
print(arr2)
>>输出结果:
[7 4 1 0 0 8 8 8 9 3]
4.8 正态分布函数
4.8.1 np.random.normal( loc , scale , size ) 普通正态分布
loc:分布的均值(中心),scale:分布的标准差(宽度),size:数据的维度
arr1 = np.random.normal(loc=175, scale=10, size=(10,))
print(arr1)
>>输出结果:
[181.82099125 165.95232532 174.28611612 175.88692564 190.99557938
188.72141845 172.99607932 181.86017743 179.41932145 177.87545193]
4.8.2 np.random.randn(d0, d1, …, dn) 标准正态分布
loc = 0 ,scale = 1 的正太分布,dn 表示数据的维度
arr1 = np.random.rand(4,2)
print(arr1)
>>输出结果:
[[0.72600143 0.00204256]
[0.77456368 0.70902766]
[0.13168928 0.37441316]
[0.36387004 0.78058689]]
arr2 = np.random.rand(4,3,2)
print(arr2)
>>输出结果:
[[[0.33233046 0.09854491]
[0.51708553 0.34565501]
[0.92549466 0.47242487]]
[[0.6345973 0.26929633]
[0.18913436 0.86976257]
[0.24877992 0.70578262]]
[[0.02646144 0.12935062]
[0.40492351 0.12528527]
[0.19876852 0.97161948]]
[[0.09079952 0.87203089]
[0.13185315 0.11456373]
[0.91589346 0.41653486]]]
4.9 np.random.random(size=None)
生成0到1的随机数,左闭右开
arr = np.random.random(size=(10,5))
print(arr)
>>输出结果:
[[0.6399623 0.2894182 0.68055697 0.31633504 0.96599415]
[0.51333444 0.3878091 0.50374029 0.17746228 0.11390883]
[0.5477178 0.60228294 0.8993186 0.03540799 0.7171814 ]
[0.17864351 0.79998941 0.97899478 0.749328 0.88348865]
[0.90883659 0.7320521 0.7149262 0.26934021 0.66994562]
[0.85799539 0.14949797 0.42199284 0.43718203 0.27460366]
[0.51228035 0.81401212 0.37627402 0.21548691 0.83943411]
[0.61688753 0.78208082 0.00106351 0.28748206 0.69535803]
[0.5425586 0.12124555 0.76270666 0.70387453 0.21285709]
[0.73174531 0.32765208 0.97659109 0.81806137 0.3291994 ]]
4.10 np.random.permutation(10)
生成随机索引
arr = np.random.permutation(5)
print(arr)
>>输出结果:
[2 4 3 0 1]
4.11 np.random.seed(num)
在创建随机数组时,如果在np.random.seed()中指定一个参数值,如np.random.seed(2),则每次生成的随机数组都相同
np.random.seed(2) # 添加一个种子
arr = np.random.randint(0, 10, size=5)
print(arr)
>>输出结果:
[8 8 6 2 8]
练习题
-
创建一组形状为(3,4)的二维数组,取值范围为(-5,5)
np.random.randint(-5, 5, size=(3,4))
-
创建一组等差数列,步长为3, 数组长度为5
np.linspace(0, 15, 5, endpoint=False) np.arange(0, 15, step=3)
-
构造一个3维取值范围为0-1的随机数组,数据类型为np.float64,形状为(100,100,3)
data = np.random.random(size=(100, 100, 3))
-
已知Π在numpy中是一个预制的常量,可以使用np.pi访问。计算出将一个圆等分成8份的弧度的代码
np.linspace(0, 2*np.pi, num=8, endpoint=False)
5. ndarray的读写操作
5.1 索引访问
arr4 = np.random.randint(0