栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python之Numpy的random模块

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python之Numpy的random模块

import numpy as np

(0, 1)间的随机数(组)

print(np.random.random())   # 0.9712291171155641
print(np.random.random(3))  # [0.54426685 0.10859501 0.66425772]

随机选择choice

print(np.random.choice([2, 3, 5, 7], 5))    # [7 7 3 5 3]

指定范围的随机整数(组矩阵)

print(np.random.randint(2, 10))         # 7
print(np.random.randint(2, 10, 3))      # [5 3 5]
print(np.random.randint(2, 10, (2, 2)))     # [[2 7]
#                                              [3 2]]

服从正态分布的随机整数

np.random.normal(loc=μ,scale=δ,size=shape)正态分布
mdt = np.random.normal(0, 1, 100)  # 产生服从标准正态分布的100个随机数
# Numpy Functions: min() max() mean() median() std()(标准差)
print(f'min: {mdt.min(): .2f}, max: {mdt.max(): .2f}, mean: {mdt.mean(): .2f}, std: {mdt.std(): .2f}')
# min: -2.67, max:  2.88, mean: -0.01, std:  1.02

复制(?)

 # (repeating sequences) np.tile np.repeat
print(np.tile([1, 3, 2], 2))    # [1 3 2 1 3 2]
print(np.repeat([1, 3, 2], 2))  # [1 1 3 3 2 2]

结合matplotlib

import matplotlib.pyplot as plt
plt.hist(mdt, color="grey", bins=21)
plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/725190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号