numpy使用方法总结
一维数组: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]]])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]])区别:array是深拷贝,asarray是浅拷贝
start:起始值 stop:结束值 num:要生成的等间隔样例数量,默认为50 endpoint:是否包含结束值,默认为ture
np.linspace(0,10,11)step:步长,默认值为1
np.arange(0,10,2)创建等比数列 num:要生成的等比数列数量,默认为50
# 10的次方 np.logspace(0,2,4)loc:均值,μ scale:标准差 σ(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)方差σ^2
size:数量
x = np.random.normal(0, 1, 10000)返回指定形状的标准正态分布的数组
返回[0.0,1.0)内的一组均匀分布的数。
low: 采样下界,float类型,默认为0 high: 采样上界,float类型,默认为1 size: 数量
x = np.random.uniform(-1, 1, 10000)若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。
对象[:, :] — 先行后列
x = np.array([[1,2,3],[4,5,6]]) x[0,1] # 2行、列不进行互换 需要保持元素个数前后相同 返回一个新的数组
x = np.array([[1,2,3],[4,5,6]]) x.reshape([3,2]) x.reshape([-1,2]) # -1表示待计算在原数组上修改 需要保持元素个数前后相同 行、列不进行互换
数组的转置
转换为字符串类型
x = np.array([[1, 2, 3], [4, 5, 6]]) x.tostring()转换为byte类型
相当于and,全真为真
相当于or,全假为假
复合逻辑需要结合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)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=)
(1)数组的某一维度等长 (2)或者其中某一数组的某一维度为1
只能矩阵与矩阵(向量)相乘
既可以矩阵乘矩阵(向量),也可以矩阵与数字相乘
问题1:
# 输出结果 arr = np.arange(6).reshape(1, 2, 3) print(arr.transpose(2, 0, 1)) ''' out: [[[0 3]] [[1 4]] [[2 5]]] '''转自:transpose用法:
