A Cheat Sheet on Generating Random Numbers in NumPy

栏目: IT技术 · 发布时间: 4年前

内容简介:To begin with, we’ll first import the NumPy module by runningThis method generates random numbers in a given shape. Some common examples are given below. Note that the numbers specified in theBoth methods are used to generate random numbers from the standa

To begin with, we’ll first import the NumPy module by running import numpy as np , allowing us to access all relevant functions in the module.

1. rand(d0, d1, ..., dn)

This method generates random numbers in a given shape. Some common examples are given below. Note that the numbers specified in the rand() function correspond to the number of dimensions of the array that is to be generated. A special case is that when no numbers are specified in the function, a random value will be generated.

Random Numbers With rand()

2. randn(d0, d1, …, dn) and standard_normal([size])

Both methods are used to generate random numbers from the standard normal distribution , the curves of which are shown in the figure below. The red curve shows you the standard normal distribution with a mean of 0 and standard deviation of 1.

Standard Normal Distribution Shown By Read Curve (Source: Wikipedia )

Both methods are to draw numbers randomly from the distribution to generate arrays of the defined shape. Their usages are about the same, except that for the standard_normal() method, we need to use a tuple to set the shape size. Some common examples are given below.

Random Numbers With randn() and standard_normal()

One thing to note is that because we know that these methods are to create a sample of numbers from the standard normal distribution, and thus we can verify if this is the case shown in the figure below.

Histogram of Random Numbers Generated With randn()

Related to these two methods, there is another method called normal([loc, scale, size]) , using which we can generate random numbers from the normal distribution specified by loc and scale parameters.

3. randint (low[, high, size, dtype])

This method generates random integers in the shape defined by size from low (inclusive) to high (exclusive) in the discrete uniform distribution. Optionally, we can also set the dtype as int64 , int , or something else, with np.int being the default value.

One thing to note is that when we don’t set the high argument, the numbers will be generated in the range of [0, low ).

Random Numbers With randint()

4. random_sample ([size]), random ([size]), ranf ([size]), and sample ([size])

All of these functions are to generate random floats in the shape defined by size in the range of [0.0, 1,0), which is a continuous uniform distribution. Using random_sample() as an example, the relevant use cases are shown below.

One thing to note that as these random numbers are drawn from the continuous uniform distribution of [0.0, 1.0), and thus the mean of these numbers is around 0.5, which is the half of the sum of 0.0 and 1.0.

Random Numbers With random_sample()

Related to these four methods, there is another method called uniform([low, high, size]) , using which we can generate random numbers from the half-open uniform distribution specified by low and high parameters.

5. choice (a[, size, replace, p])

This method generates a random sample from a given 1-D array specified by the argument a . However, if a is set as an int, the method will run as if a were an ndarray generated from np.arange(a) . Some examples using this method are shown below.

The size argument specifies the shape of the ndarray that is returned, while the p argument is a list of floats indicating the probability of the elements in a for being drawn. It should be noted that if you set the p argument, the sum of the probability values has to be 1.0.

Random Numbers With choice()

6. shuffle (x) and permutation (x)

Both methods are used to permute the sequence randomly. The major difference between them is that the shuffle() method modifies the sequence in-place and returns None , while the permutation() method generates a new ndarray of the same shape after the modification. Let’s see some examples below.

One thing to note is that when we permute a multi-dimensional array, it only works on its first axis as shown in the last two examples. In other words, the contents of the subarrays in a multi-dimensional array stay the same.

Random Numbers With shuffle() and permutation()

以上所述就是小编给大家介绍的《A Cheat Sheet on Generating Random Numbers in NumPy》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java多线程编程实战指南(设计模式篇)

Java多线程编程实战指南(设计模式篇)

黄文海 / 电子工业出版社 / 2015-10 / 59.00

随着CPU 多核时代的到来,多线程编程在充分利用计算资源、提高软件服务质量方面扮演了越来越重要的角色。而 解决多线程编程中频繁出现的普遍问题可以借鉴设计模式所提供的现成解决方案。然而,多线程编程相关的设计模式书籍多采用C++作为描述语言,且书中所举的例子多与应用开发人员的实际工作相去甚远。《Java多线程编程实战指南(设计模式篇)》采用Java(JDK1.6)语言和UML 为描述语言,并结合作者多......一起来看看 《Java多线程编程实战指南(设计模式篇)》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试