numpy使用方法总结
一 模块导入
import numpy as np
二 N维数组(ndarray)
2.1 ndarray的属性
# 创建二位数组
x = np.array([[1,2,3],[3,4,5],[6,7,8]])
# 数组x的形状
x.shape # (3,3)
# 数组的大小
x.size # 9
# 数组的维数
x.ndim # 2
# 数组的元素长度
x.itemsize # 8个字节
# 数组元素类型
x.dtype # 'int64'
2.2 ndarray的形状
一维数组:1个[ ]
二维数组:2个[ ]
三维数组:3个[ ],是二维数组的叠加
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6]])
c = np.array([[[1, 2, 3], [4, 5, 9]], [[1, 2, 3], [4, 1, 2]]])
2.3 ndarray的类型
# 查看数组的类型
type(x.dtype)
# 指定数组类型
a = np.array([1, 2, 3], dtype=np.float32)
三 数组的基本操作
3.1 生成0或1的数组
np.ones(shape, dtype)
np.ones_like(a, dtype)
np.zeros(shape, dtype)
np.zeros_like(a, dtype)
score = np.ones([2,3],dtype="int64")
# out:array([[1, 1, 1], [1, 1, 1]])
zeros = np.zeros_like(score)
# out:array([[0, 0, 0], [0, 0, 0]])
3.2 在现有数组基础上生成
a = np.array([[1,2,3])
# array和asarray方法
a1 = np.array(a)
a2 = np.asarray(a)
区别:array是深拷贝,asarray是浅拷贝
**3.3 生成固定范围的数组
np.linspace (start, stop, num, endpoint)
start:起始值
stop:结束值
num:要生成的等间隔样例数量,默认为50
endpoint:是否包含结束值,默认为ture
np.linspace(0,10,11)
np.arange(start,stop, step, dtype)
step:步长,默认值为1
np.arange(0,10,2)
np.logspace(start,stop, num)
创建等比数列
num:要生成的等比数列数量,默认为50
# 10的次方
np.logspace(0,2,4)
**3.4 生成随机数组
(1)正态分布
np.random.randn(d0, d1, …, dn)
* np.random.normal(loc=0.0, scale=1.0, size=None)
loc:均值,μ
scale:标准差 σ(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)方差σ^2
size:数量
x = np.random.normal(0, 1, 10000)
np.random.standard_normal(size=None)
返回指定形状的标准正态分布的数组
(2)均匀分布
np.random.rand(d0, d1, …, dn)
返回[0.0,1.0)内的一组均匀分布的数。
*np.random.uniform(low=0.0, high=1.0, size=None)
low: 采样下界,float类型,默认为0
high: 采样上界,float类型,默认为1
size: 数量
x = np.random.uniform(-1, 1, 10000)
np.random.randint(low, high=None, size=None, dtype=‘l’)
若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。
3.5 数组的索引、切片
对象[:, :] — 先行后列
x = np.array([[1,2,3],[4,5,6]])
x[0,1] # 2
3.6 形状修改
ndarray.reshape(shape, order)
行、列不进行互换
需要保持元素个数前后相同
返回一个新的数组
x = np.array([[1,2,3],[4,5,6]])
x.reshape([3,2])
x.reshape([-1,2]) # -1表示待计算
ndarray.resize(new_shape)
在原数组上修改
需要保持元素个数前后相同
行、列不进行互换
ndarray.T
数组的转置
3.7 类型修改
ndarray.astype(type)
x.astype(np.int32)
ndarray.tostring([order])
转换为字符串类型
x = np.array([[1, 2, 3], [4, 5, 6]])
x.tostring()
ndarray.tobytes([order])
转换为byte类型
3.8 数组的去重
np.unique()
x = np.array([[1, 2, 3], [4, 5, 6]])
np.unique(x)
四 ndarray运算
4.1 逻辑运算
# 条件为真返回True 否则为False
x = np.array([1, 2, 3])
x > 2
# out: array([ False, False, True])
# BOOL赋值, 将满足条件的设置为指定的值
x[x > 2] = 1
# out: array([ 1, 2, 1])
4.2 通用判断函数
(1)np.all()
相当于and,全真为真
(2)np.any()
相当于or,全假为假
4.3 三元运算符
np.where()
# 大于60的赋值1,否则赋值0
np.where(temp[:4,:] > 60, 1, 0)
复合逻辑需要结合np.logical_and和np.logical_or使用
# 同时满足大于60和小于90,赋值为1,否则赋值为0
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
# 满足大于90或小于60,赋值为1,否则赋值为0
np.where(np.logical_or(temp > 90, temp < 60), 1, 0)
4.4 统计运算
axis表示按行或者按列统计,不填则统计全部
(1)min(a, axis)
(2)max(a, axis])
(3)median(a, axis) ——>中位数
(4)mean(a, axis, dtype)——>平均值
(5)std(a, axis, dtype)——>标准差
(6)var(a, axis, dtype)——>方差
求最大值最小值对应的下标
np.argmax(temp, axis=)
np.argmin(temp, axis=)
五 数组运算
5.1 数组与数
arr = np.array([1, 2, 3])
# 数组中每个数都加1
arr + 1
# out: array([2, 3, 4])
# 数组中每个数都除2
arr / 2
# out: array([0.5, 1, 1.5])
5.2 数组与数组
广播机制
(1)数组的某一维度等长
(2)或者其中某一数组的某一维度为1
六 numpy中的矩阵运算
(1)np.matmul
只能矩阵与矩阵(向量)相乘
(2)np.dot
既可以矩阵乘矩阵(向量),也可以矩阵与数字相乘
补充
问题1:
# 输出结果
arr = np.arange(6).reshape(1, 2, 3)
print(arr.transpose(2, 0, 1))
''' out:
[[[0 3]]
[[1 4]]
[[2 5]]] '''
转自:transpose用法: