官方文档: https://numpy.org/doc/stable/reference/arrays.html
1.数组属性
数组属性反映了数组本身固有的信息。
属性名字 | 属性解释 |
---|---|
ndarray.shape | 数组维度的元组 |
ndarray.ndim | 数组维数 |
ndarray.size | 数组中的元素数量 |
ndarray.itemsize | 一个数组元素的长度(字节) |
ndarray.dtype | 数组元素的类型 |
2.数组生成
-
生成0和1数组
- np.empty(shape[, dtype, order]) empty_like(a[, dtype, order, subok])
eye(N[, M, k, dtype, order]) - np.identity(n[, dtype])
- np.ones(shape[, dtype, order])
- np.ones_like(a[, dtype, order, subok])
- np.zeros(shape[, dtype, order]) zeros_like(a[, dtype, order, subok])
full(shape, fill_value[, dtype, order]) - np.full_like(a, fill_value[, dtype, order, subok])
- np.empty(shape[, dtype, order]) empty_like(a[, dtype, order, subok])
-
从现有数组生成
- np.array(object[, dtype, copy, order, subok, ndmin])
- 从现有的数组中创建
- np.asarray(a[, dtype, order])
- 相当于建立索引
- np.asanyarray(a[, dtype, order]) ascontiguousarray(a[, dtype])
- np.asmatrix(data[, dtype])
- np.copy(a[, order])
- np.array(object[, dtype, copy, order, subok, ndmin])
-
生成固定范围的数组
- np.linspace (start, stop, num, endpoint, retstep, dtype)
- 生成等间隔的序列
- start 起始值
- stop 终止值
- num 个数默认50
- endpoint 是否包含stop的值 默认为True
- retstep True 返回样例
- dtype 数据类型
- np.arange(start,stop, step, dtype)
- np.logspace(start,stop, num, endpoint, base, dtype)
- np.linspace (start, stop, num, endpoint, retstep, dtype)
-
生成随机数组
-
均匀分布
-
np.random.rand(d0, d1, …, dn)
- [0, 1]之间
-
np.random.uniform(low=0.0, high=1.0, size=None)
- [low, high)之间 小数
-
np.random.randint(low, high=None, size=None, dtype=‘l’)
- 整数
-
正态分布
-
np.random.randn(d0, d1, …, dn)
- 标准正态分布
-
np.random.normal(loc=0.0, scale=1.0, size=None)
- loc 均值 scale 标准差
-
np.random.standard_normal(size=None)
- 指定形状的标准正态分布
-
3.形状改变
- ndarray.reshape() 返回转换后的数组
- ndarray.resize() 替换原来的数组
- ndarray.T 转置 行列互换
4.类型修改
- ndarray.astype()
- ndarray.tostring()
- ndarray.tobytes()
5.数组去重
- np.unique()
6.逻辑运算
import numpy as np
stock_change = np.random.normal(0, 1, (8, 10))
stock_change = stock_change[0:5, 0:5]
# 值大于0.5 设置为1
stock_change[stock_change > 0.5] = 1
-
np.all() 所有
-
np.any() 任意
-
np.where(条件, 为真处理, 为假处理) 三元运算符
- np.where(stock_change > 0.5, 1, 0) 值大于0.5 的设为1, 否则为0
- 复合逻辑
- 多个条件 and : np.logical_and()
- 多个条件 or : np.logical_or()
- 多个条件也可以直接将条件用括号(), 中间用& | 表示条件的与或
- 如 np.where((条件1) & (条件2))
7.统计指标
- ndarray.min() 最小值
- ndarray.argmin() 最小值的位置
- ndarray.max() 最大值
- ndarray.argmax() 最大值的位置
- ndarray.mean() 平均值
- ndarray.std() 标准差
- ndarray.var() 方差
- np.median(ndarray) 中位数 np才有这个方法
- 多维数组 通过axis 指定基于某个纬度 计算 a.min(axis=1)
8. 广播
- 两个array 的形状 倒过来 从尾部开始比较, 对应数字相等 或其中任意一个为1 才可以广播
- (2, 5) 和 (5,)
- (2, 3, 2, 4) 和 (1, 2, 1)
9. 矩阵运算
-
矩阵必须是2维的, 相乘必须满足形状 (m,n) * (n, l) = (m, l)
-
np.mat() 将数组转换为矩阵, 矩阵必须是2维的, 转成了matrix类型
-
矩阵相乘 必须要注意纬度, 可以利用reshape()转换
- matrix类型 直接用 * 相乘
- array类型 np.dot() 和 np.matmul()
10.合并与分割
-
np.concatenate() 通过axis=0 / 1 控制 水平/垂直合并
-
np.hstack() 水平合并
-
np.vstack() 垂直合并
-
np.split()
- np.split(x, 3) 平均切分3份
- 指定切分点 np.split(x, [2, 7]) 索引为2 和7 作为切分点
11.文件读写
12. 输出样式
# 浮点精度为4,完整输出
np.set_printoptions(precision=4, suppress=True, threshold=np.nan)