# -*- coding: utf-8 -*-
"""
Created on Wed May 23 16:38:58 2018
@author: Loulch C.C
"""
from numpy import np
"""
np.random.rand(d0, d1, ..., dn)
Random values in a given shape.
随机生成一给定形状的n维数组,dtype=ndarray
Create an array of the given shape and populate it with
random samples from a uniform distribution
over ``[0, 1)``.
创建一个给定形状的数组,并用从[0, 1)均匀分布中用随机样本填充它。
Parameters
----------
d0, d1, ..., dn : int, optional
The dimensions of the returned array, should all be positive.
If no argument is given a single Python float is returned.
由于每个参数指定的是对应维数的长度,所以应该是正数,若没有参数给定,返回一个浮点数
Returns
-------
out : ndarray, shape ``(d0, d1, ..., dn)``
Random values.
输出类型是n维数组
Notes
-----
This is a convenience function. If you want an interface that
takes a shape-tuple as the first argument, refer to
np.random.random_sample .
"""
np.random.rand()
#0.0004465318551577502
np.random.rand(3,2,2)
# array([[[0.33349897, 0.07612993],
# [0.86019193, 0.38230735]],
#
# [[0.46780106, 0.55711774],
# [0.44259951, 0.67058414]],
#
# [[0.673428 , 0.90087974],
# [0.70143593, 0.77487263]]])
"""
np.random.random_sample(size=None)
Return random floats in the half-open interval [0.0, 1.0).
在前开后比的区间[0.0, 1.0)返回一个浮点数
Results are from the "continuous uniform" distribution over the
stated interval. To sample :math:`Unif[a, b), b > a` multiply
the output of `random_sample` by `(b-a)` and add `a`::
(b - a) * random_sample() + a
结果是在规定的间隔中从“连续均匀”分布中取值。
Parameters
----------
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
输入是整数或整数型元组,若用默认参数None,返回一个浮点数
Returns
-------
out : float or ndarray of floats
Array of random floats of shape `size` (unless ``size=None``, in which
case a single float is returned).
"""
np.random.random_sample(2)
#0.6028707437254377
np.random.random_sample((3,2,2))
#array([[[0.64652491, 0.65078447],
# [0.56590835, 0.90413143]],
#
# [[0.7356992 , 0.83405403],
# [0.79705633, 0.93500609]],
#
# [[0.06099415, 0.88193611],
# [0.18557931, 0.25294683]]])
"""
np.random.randn(d0, d1, ..., dn)
Return a sample (or samples) from the "standard normal" distribution.
返回一个服从正态分布的样本,注意输入参数不能是元组
If positive, int_like or int-convertible arguments are provided,
`randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
with random floats sampled from a univariate "normal" (Gaussian)
distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
floats, they are first converted to integers by truncation). A single
float randomly sampled from the distribution is returned if no
argument is provided.
This is a convenience function. If you want an interface that takes a
tuple as the first argument, use `numpy.random.standard_normal` instead.
Parameters
----------
d0, d1, ..., dn : int, optional
The dimensions of the returned array, should be all positive.
If no argument is given a single Python float is returned.
由于每个参数指定的是对应维数的长度,所以应该是正数,若没有参数给定,返回一个浮点数
Returns
-------
Z : ndarray or float
A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from
the standard normal distribution, or a single such float if
no parameters were supplied.
从标准正态分布中得到样本为浮点型,形状为(d0, d1, ..., dn)的数组,或者如果没有
指定参数,返回一个浮点数。
See Also
--------
random.standard_normal : Similar, but takes a tuple as its argument.
random.standard_normal和np.random.randn功能相似,但输入参数是元组
Notes
-----
For random samples from :math:`N(\mu, \sigma^2)`, use:
``sigma * np.random.randn(...) + mu``
"""
np.random.randn()
#1.1959465924275063
np.random.randn(3,2,2)
#array([[[ 0.17616821, 0.71155665],
# [-0.19112895, 0.43024993]],
#
# [[ 1.00172339, 0.04432509],
# [ 0.35799857, -0.09941122]],
#
# [[ 0.93664108, 0.12910895],
# [-0.07058416, 0.78139332]]])
"""
np.random.standard_normal(size=None)
Draw samples from a standard Normal distribution (mean=0, stdev=1).
返回一个服从正态分布的样本
Parameters
----------
size : int or tuple of ints, optional 输入参数size是整数或整数型元组
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
Returns
-------
out : float or ndarray 返回一个浮点数和n维数组
Drawn samples.
"""
np.random.standard_normal()
# -0.2502682581746278
np.random.standard_normal(8)
#array([-0.56042949, -0.49409946, -1.54906932, 0.83449251, 0.81294393,
# 0.8207554 , 0.08094755, 0.35935617])
np.random.standard_normal(size=(3, 2, 2))
#array([[[-0.2995678 , -1.51831344],
# [ 0.76354373, 1.91338345]],
#
# [[ 1.26671755, -0.63408626],
# [-0.20556799, 0.04380744]],
#
# [[-0.82423503, -0.42493808],
# [ 0.29523695, -1.13595518]]])
np.random下的4个随机函数
最新推荐文章于 2024-05-02 15:46:55 发布