本文将为您提供关于numpy.random.seed(0)做什么?的详细介绍,我们还将为您解释numpy.random.seed()的使用的相关知识,同时,我们还将为您提供关于java–使用Rando
本文将为您提供关于numpy.random.seed(0) 做什么?的详细介绍,我们还将为您解释numpy.random.seed()的使用的相关知识,同时,我们还将为您提供关于java – 使用Random.setSeed的重要性是什么?、numpy : random.rand 和 random.random 的区别、numpy random、numpy random 模块的实用信息。
本文目录一览:- numpy.random.seed(0) 做什么?(numpy.random.seed()的使用)
- java – 使用Random.setSeed的重要性是什么?
- numpy : random.rand 和 random.random 的区别
- numpy random
- numpy random 模块
numpy.random.seed(0) 做什么?(numpy.random.seed()的使用)
np.random.seed
以下来自 Scikit-Learn 教程的代码做了什么?我对 NumPy
的随机状态生成器的东西不是很熟悉,所以我非常感谢外行人对此的解释。
np.random.seed(0)indices = np.random.permutation(len(iris_X))
答案1
小编典典np.random.seed(0)
使随机数可预测
>>> numpy.random.seed(0) ; numpy.random.rand(4)array([ 0.55, 0.72, 0.6 , 0.54])>>> numpy.random.seed(0) ; numpy.random.rand(4)array([ 0.55, 0.72, 0.6 , 0.54])
随着种子重置(每次),每次都会出现 相同 的一组数字。
如果未重置随机种子,则每次调用都会出现 不同的数字:
>>> numpy.random.rand(4)array([ 0.42, 0.65, 0.44, 0.89])>>> numpy.random.rand(4)array([ 0.96, 0.38, 0.79, 0.53])
(伪)随机数的工作原理是从一个数字(种子)开始,将其乘以一个大数,添加一个偏移量,然后对该总和取模。然后将得到的数字用作生成下一个“随机”数字的种子。当你设置种子(每次)时,它每次都做同样的事情,给你同样的数字。
如果您想要看似随机的数字,请不要设置种子。但是,如果您的代码使用要调试的随机数,那么在每次运行之前设置种子会非常有帮助,这样代码每次运行时都会执行相同的操作。
要为每次运行获取最多的随机数,请调用numpy.random.seed()
.
这将导致
numpy 将种子设置为从/dev/urandom
或其 Windows 模拟获得的随机数,或者,如果这些都不可用,它将使用时钟。
有关使用种子生成伪随机数的更多信息,请参阅wikipedia。
java – 使用Random.setSeed的重要性是什么?
setSeed
.我们为什么要使用这种方法?
我们不能在不使用setSeed的情况下使用Random吗?使用setSeed的主要目的是什么?
解决方法
例如,我想为数据库中的每一行计算一个随机变量.我希望程序可以重现,但我希望行之间的随机性.为此,我将随机数种子设置为每行的主键.这样,当我再次运行程序时,我得到了相同的结果,但在行之间,随机变量是伪随机的.
numpy : random.rand 和 random.random 的区别
相同点:两个函数都是在 [0, 1) 的均匀分布中产生随机数。
不同点:参数传递不同。random.random( )接收一个单独的元组,而random.rand( )接收分开的参数
np.random.seed(1)
rand = np.random.rand(2, 2)
print(rand)
''''''
[[4.17022005e-01 7.20324493e-01]
[1.14374817e-04 3.02332573e-01]]
''''''
np.random.seed(1)
random = np.random.random((2, 2))
print(random)
''''''
[[4.17022005e-01 7.20324493e-01]
[1.14374817e-04 3.02332573e-01]]
''''''
两个函数功能完全一样,numpy为什么这么做,这是有历史原因的。可能是为了使 Matlab 用户更容易学习 python+numpy 的组合。把其中一个函数去掉,所带来的麻烦远大于好处,因为有很多现存的代码使用了函数的不同版本。
numpy random
np.random.seed () 用法:
np.random.seed(5)
print(np.random.permutation(np.array([i for i in range(9)])))
np.random.seed(5)
print(np.random.permutation(np.array([i for i in range(9)])))
[2 4 8 7 1 0 5 6 3]
[2 4 8 7 1 0 5 6 3]
np.random.seed(5)
print(np.random.permutation(np.array([i for i in range(9)])))
np.random.seed(5)
print(np.random.permutation(np.array([i for i in range(10,19)])))
[2 4 8 7 1 0 5 6 3]
[12 14 18 17 11 10 15 16 13]
np.random.choice () 的用法:
import numpy as np
# 参数意思分别 是从a 中以概率P,随机选择3个, p没有指定的时候相当于是一致的分布
a1 = np.random.choice(a=5, size=3, replace=False, p=None)
print(a1)
# 非一致的分布,会以多少的概率提出来
a2 = np.random.choice(a=5, size=3, replace=False, p=[0.2, 0.1, 0.3, 0.4, 0.0])
print(a2)
# replacement 代表的意思是抽样之后还放不放回去,如果是False的话,那么出来的三个数都不一样,如果是
# True的话, 有可能会出现重复的,因为前面的抽的放回去了。
np.random.permutation () 的用法:
随机排列一个序列,返回一个排列的序列
>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12])
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8],
[0, 1, 2],
[3, 4, 5]])
np.random.uniform(low=0.0, high=1.0, size=None):
从一个均匀分布 [low,high) 中随机采样,注意定义域是左闭右开,即包含 low,不包含 high.
np.random.normal(loc=0.0, scale=1.0, size=None):
正态分布
np.random.rand()
随即生成 [0,1) 之间的数。
np.random.randn()
- randn 函数返回一个或一组样本,具有标准正态分布。
numpy random 模块
一下方法都要加np.random.前缀
1.简单随机数据
name | describe |
---|---|
rand(d0, d1, …, dn) | Random values in a given shape. |
randn(d0, d1, …, dn) | Return a sample (or samples) from the “standard normal” distribution. |
randint(low[, high, size, dtype]) | Return random integers from low (inclusive) to high (exclusive). |
random_integers(low[, high, size]) | Random integers of type np.int between low and high, inclusive. |
random_sample([size]) | Return random floats in the half-open interval [0.0, 1.0). |
random([size]) | Return random floats in the half-open interval [0.0, 1.0). |
ranf([size]) | Return random floats in the half-open interval [0.0, 1.0). |
sample([size]) | Return random floats in the half-open interval [0.0, 1.0). |
choice(a[, size, replace, p]) | Generates a random sample from a given 1-D array |
bytes(length) | Return random bytes. |
2.生成随机分布
name | describe |
---|---|
beta(a, b[, size]) | Draw samples from a Beta distribution. |
binomial(n, p[, size]) | Draw samples from a binomial distribution. |
chisquare(df[, size]) | Draw samples from a chi-square distribution. |
dirichlet(alpha[, size]) | Draw samples from the Dirichlet distribution. |
exponential([scale, size]) | Draw samples from an exponential distribution. |
f(dfnum, dfden[, size]) | Draw samples from an F distribution. |
gamma(shape[, scale, size]) | Draw samples from a Gamma distribution. |
geometric(p[, size]) | Draw samples from the geometric distribution. |
gumbel([loc, scale, size]) | Draw samples from a Gumbel distribution. |
hypergeometric(ngood, nbad, nsample[, size]) | Draw samples from a Hypergeometric distribution. |
laplace([loc, scale, size]) | Draw samples from the Laplace or double exponential distribution with specified logistic([loc, scale, size]) Draw samples from a logistic distribution. |
lognormal([mean, sigma, size]) | Draw samples from a log-normal distribution. |
logseries(p[, size]) | Draw samples from a logarithmic series distribution. |
multinomial(n, pvals[, size]) | Draw samples from a multinomial distribution. |
multivariate_normal(mean, cov[, size]) | Draw random samples from a multivariate normal distribution. |
negative_binomial(n, p[, size]) | Draw samples from a negative binomial distribution. |
noncentral_chisquare(df, nonc[, size]) | Draw samples from a noncentral chi-square distribution. |
noncentral_f(dfnum, dfden, nonc[, size]) | Draw samples from the noncentral F distribution. |
normal([loc, scale, size]) | Draw random samples from a normal (Gaussian) distribution. |
pareto(a[, size]) | Draw samples from a Pareto II or Lomax distribution with specified shape. |
poisson([lam, size]) | Draw samples from a Poisson distribution. |
power(a[, size]) | Draws samples in [0, 1] from a power distribution with positive exponent a - 1. |
rayleigh([scale, size]) | Draw samples from a Rayleigh distribution. |
standard_cauchy([size]) | Draw samples from a standard Cauchy distribution with mode = 0. |
standard_exponential([size]) | Draw samples from the standard exponential distribution. |
standard_gamma(shape[, size]) | Draw samples from a standard Gamma distribution. |
standard_normal([size]) | Draw samples from a standard Normal distribution (mean=0, stdev=1). |
standard_t(df[, size]) | Draw samples from a standard Student’s t distribution with df degrees of freedom. |
triangular(left, mode, right[, size]) | Draw samples from the triangular distribution over the interval [left, right]. |
uniform([low, high, size]) | Draw samples from a uniform distribution. |
vonmises(mu, kappa[, size]) | Draw samples from a von Mises distribution. |
wald(mean, scale[, size]) | Draw samples from a Wald, or inverse Gaussian, distribution. |
weibull(a[, size]) | Draw samples from a Weibull distribution. |
zipf(a[, size]) | Draw samples from a Zipf distribution. |
3.重排
name | describe |
---|---|
shuffle(x) | Modify a sequence in-place by shuffling its contents. |
permutation(x) | Randomly permute a sequence, or return a permuted range. |
关于numpy.random.seed(0) 做什么?和numpy.random.seed()的使用的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于java – 使用Random.setSeed的重要性是什么?、numpy : random.rand 和 random.random 的区别、numpy random、numpy random 模块的相关信息,请在本站寻找。
本文标签: