A Cheat Sheet on Generating Random Numbers in NumPy

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Windows核心编程(第5版)

Windows核心编程(第5版)

Jeffrey Richter、Christophe Nasarre / 葛子昂、周靖、廖敏 / 清华大学出版社 / 2008-9 / 99.00元

这是一本经典的Windows核心编程指南,从第1版到第5版,引领着数十万程序员走入Windows开发阵营,培养了大批精英。. 作为Windows开发人员的必备参考,本书是为打算理解Windows的C和C++程序员精心设计的。第5版全面覆盖Windows XP,Windows Vista和Windows Server 2008中的170个新增函数和Windows特性。书中还讲解了Windows......一起来看看 《Windows核心编程(第5版)》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具