Python学习笔记-数据分析-Numpy04-随机数

Numpy随机数
1、随机数是数据分析中很重要的一个辅助工具。里面包含正太分布、均匀分布等等很多类型。通过随机数可以生成我们想要的符合某个特性的数据,以便我们学习新的工具包或者测试某个算法。
2、这里主要使用到numpy.random。它有比较多的方法,这里只介绍一些常用的。
import numpy as np

# 这里导入matplotlib模块,主要是为了用图表来辅助分析
import matplotlib.pyplot as plt
# 魔法函数,每次运行自动生成图表
% matplotlib inline 
一、随机数的生成— —.normal()
# .normal()会生成一个标准的正态分布样本。
# 生成一个标准正太分布的2×2样本值
srn = np.random.normal(size=(2,2))
srn1 = np.random.normal(size = (3))
print(srn, type(srn))
print(srn1, type(srn1))

运行结果如下:

[[-0.41595877  1.11532727]
 [-0.70863366 -0.99436532]] <class 'numpy.ndarray'>
[-0.96559852 -1.12385658 -1.00368828] <class 'numpy.ndarray'>
二、随机数的生成— —.rand()
# 通过.rand()这个函数:生成符合均匀分布的一个[0,1)之间的随机浮点数或N维浮点数组
# 生产一个随机浮点数样本。
sr1 = np.random.rand()
print(sr1, type(sr1))

# 生产一个有3个随机浮点的一维数组
sr2 = np.random.rand(3)
print(sr2, type(sr2))

# 生产一个3*3的二维随机浮点数组
sr3 = np.random.rand(3,3)
print(sr3, type(sr3))

# 用图表来演示一下均匀分布
sr_t1 = np.random.rand(500)
sr_t2 = np.random.rand(500)
plt.scatter(sr_t1, sr_t2)

运作结果如下:

0.8483121400402119 <class 'float'>
[0.09368106 0.77192309 0.14391224] <class 'numpy.ndarray'>
[[0.83304206 0.95253513 0.31348246]
 [0.15265572 0.60699124 0.90215839]
 [0.2633082  0.00708486 0.92831763]] <class 'numpy.ndarray'>
<matplotlib.collections.PathCollection at 0x2455afb6f60>

在这里插入图片描述

三、随机数的生成— —.randn()

通过.randn()这个函数生成符合正态分布的一个浮点数或N维浮点数组。由于randn和normal都是生成符合正态分布的样本数据,所以下面我简单的说一下对randn()和normal()的理解,有什么不对的地方希望能够得到指正:
1、normal是通过(loc=0.0, scale=1.0, size=None)这三个参数来调整生成的不同“均值、标准差和维度”样本数据。
2、randn没有可以调整“均值和标准差”的参数,所以生成的样本数据或样本数组的数据都是在[0,1)之间。

# normal举例
# 注意生成的样本数据的范围
a = np.random.normal(size=4)
a1 = np.random.normal(loc=3, scale=5, size=4)
print(a)
print(a1)
print('-----------------------------------')
# randn举例
# 注意生成的样本数据和样本数组的数据
b = np.random.randn(2)
b1 = np.random.randn(2,3)
print(b)
print(b1)
print('-----------------------------------')

# 用图表来演示一下正态分布
srrn1 = np.random.randn(500)
srrn2 = np.random.randn(500)
plt.scatter(srrn1, srrn2)

运行结果如下:

[ 0.18514025  0.11667968 -1.17234504 -1.32040683]
[ 4.53035666  0.5300141  -0.48759922 -7.33107027]
-----------------------------------
[ 1.02453864 -2.26215368]
[[ 0.44707795  1.99943163 -1.0518808 ]
 [-0.48363184 -0.03158104  0.58360637]]
-----------------------------------
<matplotlib.collections.PathCollection at 0x2455b055828>

在这里插入图片描述

四、随机数的生成— —.randint()
# 通过.randint()这个函数生成一个整数或N维整数数组.
# 该函数要求至少有一个参数。
# 该函数有4个参数:low, high=None, size=None, dtype='l'。要想使用这个参数则至少要包含一个(low)这个参数。
# 如果high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数,且high必须大于low,dtype参数:只能是int类型  

# low=3:生成1个[0,3)之间随机整数 
a = np.random.randint(3)
print(a)
print('-----------------------------------')
# low=5,size=5 :生成5个[0,5)之间随机整数
b = np.random.randint(5,size=5)
print(b)
print('-----------------------------------')
# low=2,high=6,size=5:生成5个[2,6)之间随机整数
c = np.random.randint(2,6,size=5)
print(c)
print('-----------------------------------')
# low=3,size=(3,3):生成一个3x3整数数组,取数范围:[0,2)随机整数   
d = np.random.randint(3,size=(3,3))
print(d)
print('-----------------------------------')
# low=2,high=6,size=(2,3):生成一个2*3整数数组,取值范围:[2,6)随机整数
e = np.random.randint(2,6,(2,3))
print(e)

运行结果如下:

1
-----------------------------------
[1 0 4 2 2]
-----------------------------------
[2 4 4 4 3]
-----------------------------------
[[2 2 2]
 [2 1 2]
 [0 0 0]]
-----------------------------------
[[2 5 3]
 [4 2 2]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值