numpy 随机数使用

2019-06-09  本文已影响0人  dongshangtong

有两个额外的方法, any 和 all ,对布尔数组尤其有用。 any 用来测试一个数组中是否有一个或更多的 True ,而 all用来测试所有的值是否为 True

1. np.random.rand()函数

通过本函数可以返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1)

参数可以一个或两个 ,具体看实例

np.random.rand(2)

# array([0.78255266, 0.28622747])

np.random.rand(2,4)

# array([[0.16415752, 0.18633667, 0.69753378, 0.1878069 ],
       [0.09026839, 0.37546257, 0.99291378, 0.4874447 ]])

2. np.random.randn()函数

(1)通过本函数可以返回一个或一组服从标准正态分布的随机样本值。
(2) np.random.randn()函数所产生的随机样本基本上取值主要在-1.96~+1.96之间,当然也不排除存在较大值的情形,只是概率较小而已

标准正态分布曲线.gif

3. np.random.randint()函数

numpy.random.randint(low, high=None, size=None,
dtype=’l’)

参数说明

low—–为最小值
high—-为最大值
size—–为数组维度大小
dtype—为数据类型,默认的数据类型是np.int。

作用:

随机整数或整型数组,范围区间为[low,high),包含low,不包含high;
high没有填写时,默认生成随机数的范围是[0,low)

np.random.randint(1,5,size=(3,5))

# array([[3, 1, 3, 4, 4],
       [1, 2, 4, 1, 3],
       [4, 4, 4, 4, 3]])

np.random.randint(6)
# 4

np.random.randint(6,10)

# 6

np.random.randint(3,5,size=(2,3))

# array([[4, 3, 3],
       [4, 4, 3]])

4. np.random.random(size = 5)

返回随机的浮点数,在半开区间 [0.0, 1.0)。

  一个参数是表示 size = 多少, size 可以不写。
np.random.random(size = 5)

# array([0.85627943, 0.1482212 , 0.49979544, 0.27535643, 0.16568008])

np.random.random_sample(5) 
# array([0.55904618, 0.09636442, 0.25522611, 0.69723003, 0.4561148 ])

# 第一个表示维度
np.random.random_sample((2,2,4,))

# array([[[0.68660694, 0.62919759, 0.81648022, 0.90615015],
        [0.78381849, 0.8284301 , 0.69204844, 0.72662745]],

       [[0.93989798, 0.95939636, 0.49894923, 0.87598416],
        [0.10462743, 0.32110368, 0.13064537, 0.51795068]]])

5. np.random.random_integers

生成闭区间[low,high]上离散均匀分布的整数值;若high=None,则取值区间变为[1,low]

np.random.random_integers(5,size=(2,3))

# array([[3, 5, 1],
       [3, 2, 3]])

seed( )是拿来确定随机数生成的,如果使用相同的seed( )值,则每次生成的随机数相同

# 第一次测试
np.random.seed(2)  # 第一设种子为seed(2)
for i in range(5):
    print(np.random.random())

print("*"*40)
np.random.seed(2)  # 第二设种子为seed(2)
for i in range(5):
    print(np.random.random())

# 两次打印结果是:

0.43599490214200376
0.025926231827891333
0.5496624778787091
0.4353223926182769
0.42036780208748903
****************************************
0.43599490214200376
0.025926231827891333
0.5496624778787091
0.4353223926182769
0.42036780208748903

# 第二次测试, 我们随便改一个seed.

np.random.seed(2)  # 第一设种子为seed(2)
for i in range(5):
    print(np.random.random())

print("*"*40)
np.random.seed(4)  # 第二设种子为seed(4) 和上次不一样
for i in range(5):
    print(np.random.random())

# 我们观察结果,两次结果不一样
0.43599490214200376
0.025926231827891333
0.5496624778787091
0.4353223926182769
0.42036780208748903
****************************************
0.9670298390136767
0.5472322491757223
0.9726843599648843
0.7148159936743647
0.6977288245972708

我们可以得出结论:
输出相同。即seed(2)中的随机数是确定的、按顺序生成的。 不使用seed,则每次运行的结果都不同

查看numpy 的一些属性

arr2 = np.random.randint(3,7,size=[3,5])

#array([[6, 5, 6, 6, 5],
       [6, 4, 6, 5, 4],
       [3, 4, 4, 4, 6]])

arr2.ndim  # 维度
# 2
arr2.shape  #各维度长度
# (3, 5)
arr2.size    # 总长度
# 15
arr2.dtype   ##元素类型
# dtype('int64')

  1. 变形
arr3 = np.random.randint(1,12,size=(3,5))

# array([[ 1,  8,  3,  7,  4],
       [ 8,  9,  3,  7,  8],
       [11,  8, 10,  3,  8]])

arr3.T
# 变形结果
#array([[ 1,  8, 11],
       [ 8,  9,  8],
       [ 3,  3, 10],
       [ 7,  7,  3],
       [ 4,  8,  8]])


# 行倒序

arr3[::-1]

# array([[ 2,  4,  2,  4,  7],
       [ 3,  2, 11,  9,  9],
       [ 9,  5,  5,  1,  8]])

# 列倒序

arr3[:,::-1]

# array([[ 8,  1,  5,  5,  9],
       [ 9,  9, 11,  2,  3],
       [ 7,  4,  2,  4,  2]])

# 前部倒序
arr3[::-1,::-1]

array([[ 7,  4,  2,  4,  2],
       [ 9,  9, 11,  2,  3],
       [ 8,  1,  5,  5,  9]])

显示图像


import matplotlib.pyplot as plt

img2 = plt.imread('maomi.jpg')

plt.imshow(img2)

image.png

将上面图片上下翻转

plt.imshow(img2[::-1])
image.png

左右翻转

plt.imshow(img2[:,::-1])
image.png

上下左右翻转

plt.imshow(img2[::-1,::-1]) 

668A2E45-1C7A-4D60-84E4-9BBD511786AB.png

rgb颜色翻转

plt.imshow(img2[:,:,::-1])
image.png
上一篇 下一篇

猜你喜欢

热点阅读