numpy的使用

本文深入探讨了NumPy库中随机数生成的方法,包括标准正态分布、均匀分布及离散分布的随机数生成,同时介绍了如何使用np.argmax获取数组中最大值的索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np

np.random

numpy.random.normal(loc=0.0, scale=1.0, size=None)

输出均值为0,标准差为1的一个随机值

#产生的随机数服从标准正态分布0-1,2行4列矩阵
np.random.randn(2,4)

结果:

[[-0.08302365  0.36937819  0.08568024 -1.56085425]
 [ 0.16679172  0.10654604 -1.96022107 -1.9178003 ]]

#产生的随机数位于[0, 1)中

np.random.rand(2,4)

结果:

[[0.10653075 0.75118002 0.37745255 0.65212225]
 [0.76288377 0.08645488 0.00689437 0.90927803]]

np.random.randint

random.randint(1,20) 结果为:13

np.random.randint(1,20) 结果为:1

np.random.randint(1,20,size=3)  结果为:array([19, 18, 4])

np.random.randint(1,20,size=(3,4)) 结果为:array([[ 4, 8, 7, 13], [ 8, 19, 16, 1], [ 4, 4, 2, 18]])


np.expand_dims:

np.array([[[1,2,3],[4,5,6]]]),生成(1,2,3)维的矩阵
np.expand_dims(a, axis=0),表示在0位置添加数据,转换结果如下:
b = np.expand_dims(a, axis=0)
b:array([[[[1, 2, 3],
         [4, 5, 6]]]])
b.shape:(1, 1, 2, 3)

np.argmax:返回沿轴最大值的索引值

A = np.arange(6).reshape(1,6) 结果为:array([[0, 1, 2, 3, 4, 5]])

np.argmax(A) 结果为:6

E = np.array([[1,2,3],[2,1,4],[3,6,1]]) 结果为:

[[1 2 3]
 [2 1 4]
 [3 6 1]]

np.argmax(E, axis=0)  #axis=0表示沿行比较,结果为:array([2, 2, 1], dtype=int64)

np.argmax(E, axis=1) 结果为:#axis=1表示沿列比较,array([2, 2, 1], dtype=int64)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值