39

python-numpy-03-创建特定形式数组

 3 years ago
source link: http://www.niefajun.com/archives/python-numpy-03-创建特定形式数组
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1.np.arange创建指定步长

函数定义:

def arange(start=None, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
    """
    arange([start,] stop[, step,], dtype=None)
    
        Return evenly spaced values within a given interval.
    
        Values are generated within the half-open interval ``[start, stop)``
        (in other words, the interval including `start` but excluding `stop`).
        For integer arguments the function is equivalent to the Python built-in
        `range` function, but returns an ndarray rather than a list.
    
        When using a non-integer step, such as 0.1, the results will often not
        be consistent.  It is better to use `numpy.linspace` for these cases.
    
        Parameters
        ----------
        start : number, optional
            Start of interval.  The interval includes this value.  The default
            start value is 0.
        stop : number
            End of interval.  The interval does not include this value, except
            in some cases where `step` is not an integer and floating point
            round-off affects the length of `out`.
        step : number, optional
            Spacing between values.  For any output `out`, this is the distance
            between two adjacent values, ``out[i+1] - out[i]``.  The default
            step size is 1.  If `step` is specified as a position argument,
            `start` must also be given.
        dtype : dtype
            The type of the output array.  If `dtype` is not given, infer the data
            type from the other input arguments.
    
        Returns
        -------
        arange : ndarray
            Array of evenly spaced values.
    
            For floating point arguments, the length of the result is
            ``ceil((stop - start)/step)``.  Because of floating point overflow,
            this rule may result in the last element of `out` being greater
            than `stop`.
    
        See Also
        --------
        numpy.linspace : Evenly spaced numbers with careful handling of endpoints.
        numpy.ogrid: Arrays of evenly spaced numbers in N-dimensions.
        numpy.mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.
    """
    pass

说明: numpy.arange 函数和经常使用的 range 函数非常的类似,只是多增加了一个dtype参数,dtype参数的作用和 numpy.array 里面介绍的作用是一致的。

range()和arange()只所以这么灵活,一方面是python的灵活的参数机制;另一方面是对接收的参数数目进行判断,根据参数数目的不同执行不同的操作。

示例代码:

# 指定终点
a = np.arange(10)
print(a)
print('--' * 20)

# 指定起点、终点
b = np.arange(1, 10)
print(b)
print('--' * 20)

# 指定起点、终点、步长
c = np.arange(1, 10, 2)
print(c)
print('--' * 20)

# 指定起点、终点、步长、dtype类型
d = np.arange(1, 10, 2, float)
print(d)
print('--' * 20)

# 小数的情况也能使用numpy,实际情况这样使用的比较少
e = np.arange(0.1, 1.0, 0.1, float)
print(e)

运行结果:

[0 1 2 3 4 5 6 7 8 9]
----------------------------------------
[1 2 3 4 5 6 7 8 9]
----------------------------------------
[1 3 5 7 9]
----------------------------------------
[1. 3. 5. 7. 9.]
----------------------------------------
[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

2.np.random.random创建随机数

用于创建值范围在[0.0, 1.0)区间的随机数组

函数定义:

def random(size=None): # real signature unknown; restored from __doc__
    """
    random(size=None)
    
            Return random floats in the half-open interval [0.0, 1.0). Alias for
            `random_sample` to ease forward-porting to the new random API.
    """
    pass

通过介绍可以知道,random是random_sample的别名。我们再来看一下random_sample函数。

def random_sample(size=None): # real signature unknown; restored from __doc__
    """
    random_sample(size=None)
    
            Return random floats in the half-open interval [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
    
            .. note::
                New code should use the ``random`` method of a ``default_rng()``
                instance instead; see `random-quick-start`.
    
            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.
    
            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).
   
    """
    pass

示例代码:

import numpy as np

a1 = np.random.random(size=1)
a2 = np.random.random(size=(1,))
a3 = np.random.random_sample(size=(1,))
print(a1)
print("~~" * 10)
print(a2)
print("~~" * 10)
print(a3)
print('--' * 20)

b1 = np.random.random(size=(2, 3))
b2 = np.random.random_sample(size=(2, 3))
print(b1)
print("~~" * 10)
print(b2)
print("--" * 20)

运行结果:

[0.12406671]
~~~~~~~~~~~~~~~~~~~~
[0.51463238]
~~~~~~~~~~~~~~~~~~~~
[0.89463238]
----------------------------------------
[[0.10907993 0.16789092 0.43668195]
 [0.79106801 0.22137333 0.01017769]]
~~~~~~~~~~~~~~~~~~~~
[[0.65803265 0.11789976 0.56492191]
 [0.74975911 0.09096749 0.05589122]]
----------------------------------------

程序说明:通过运行结果我们可以看到a1、a2、a3这三个结构一致,说明传递参数最终是以元组的形式进行解析的,另外一个就是random和random_sample效果一致。

为了程序规规范性,建议创建ndarray数组过程指定参数size以元组的形式传递。

3.np.random.randint创建随机整数

主要用于创建指定区间范围的整数数据类型数组

函数定义:

def randint(low, high=None, size=None, dtype=None): # real signature unknown; restored from __doc__
    """
    randint(low, high=None, size=None, dtype=int)
    
            Return random integers from `low` (inclusive) to `high` (exclusive).
    
            Return random integers from the "discrete uniform" distribution of
            the specified dtype in the "half-open" interval [`low`, `high`). If
            `high` is None (the default), then results are from [0, `low`).
    
            .. note::
                New code should use the ``integers`` method of a ``default_rng()``
                instance instead; see `random-quick-start`.
    
            Parameters
            ----------
            low : int or array-like of ints
                Lowest (signed) integers to be drawn from the distribution (unless
                ``high=None``, in which case this parameter is one above the
                *highest* such integer).
            high : int or array-like of ints, optional
                If provided, one above the largest (signed) integer to be drawn
                from the distribution (see above for behavior if ``high=None``).
                If array-like, must contain integer values
            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.
            dtype : dtype, optional
                Desired dtype of the result. Byteorder must be native.
                The default value is int.
    
                .. versionadded:: 1.11.0
    
            Returns
            -------
            out : int or ndarray of ints
                `size`-shaped array of random integers from the appropriate
                distribution, or a single such random int if `size` not provided. 
    """
    pass

说明:

  1. 参数 low 和参数 high 使用类似于random函数的使用方法
  2. size用法和上面random函数介绍的一样,建议使用元组
  3. dtype函数用于指定数据类型,注意: 因为randint本身已经指定整数类型的范围,所以不能指定非整形数据类型。

示例代码:

import numpy as np

# 指定终点
a1 = np.random.randint(10)
print(a1)
print('--' * 20)

# 指定起点、终点
b1 = np.random.randint(1, 10)
print(b1)
print('--' * 20)

# 指定起点、终点、大小
c1 = np.random.randint(1, 10, size=(2, 3))
print(c1)
print('--' * 20)

# 指定起点、终点、大小、数据类型
d1 = np.random.randint(1, 10, size=(2, 3), dtype=np.uint8)
print(d1)
print('--' * 20)

运行结果:

9
----------------------------------------
9
----------------------------------------
[[9 8 6]
 [1 1 5]]
----------------------------------------
[[6 3 8]
 [9 9 5]]
----------------------------------------

4.创建正态分布数组

4.1 np.random.randn创建标准正太分布

用于创建符合标准正态分布(期望为0,方差为1)

函数定义

def randn(*dn): # known case of numpy.random.mtrand.randn
    """
    randn(d0, d1, ..., dn)
    
            Return a sample (or samples) from the "standard normal" distribution.
    
            .. note::
                This is a convenience function for users porting code from Matlab,
                and wraps `standard_normal`. That function takes a
                tuple to specify the size of the output, which is consistent with
                other NumPy functions like `numpy.zeros` and `numpy.ones`.
    
            .. note::
                New code should use the ``standard_normal`` method of a ``default_rng()``
                instance instead; see `random-quick-start`.
    
            If positive int_like 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. A single float randomly sampled
            from the distribution is returned if no argument is provided.
    
            Parameters
            ----------
            d0, d1, ..., dn : int, optional
                The dimensions of the returned array, must be non-negative.
                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.
    """
    pass

4.2 np.random.common指定方差和期望

用于创建指定期望和方差正态分布数据的数组

函数定义

def normal(loc=0.0, scale=1.0, size=None): # real signature unknown; restored from __doc__
    """
    normal(loc=0.0, scale=1.0, size=None)
    
            Draw random samples from a normal (Gaussian) distribution.
    
            The probability density function of the normal distribution, first
            derived by De Moivre and 200 years later by both Gauss and Laplace
            independently [2]_, is often called the bell curve because of
            its characteristic shape (see the example below).
    
            The normal distributions occurs often in nature.  For example, it
            describes the commonly occurring distribution of samples influenced
            by a large number of tiny, random disturbances, each with its own
            unique distribution [2]_.
    
            .. note::
                New code should use the ``normal`` method of a ``default_rng()``
                instance instead; see `random-quick-start`.
    
            Parameters
            ----------
            loc : float or array_like of floats
                Mean ("centre") of the distribution.
            scale : float or array_like of floats
                Standard deviation (spread or "width") of the distribution. Must be
                non-negative.
            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.  If size is ``None`` (default),
                a single value is returned if ``loc`` and ``scale`` are both scalars.
                Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
    
            Returns
            -------
            out : ndarray or scalar
                Drawn samples from the parameterized normal distribution.
    
            See Also
            --------
            scipy.stats.norm : probability density function, distribution or
                cumulative density function, etc.
            Generator.normal: which should be used for new code.
    
            Notes
            -----
            The probability density for the Gaussian distribution is
    
            .. math:: p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }}
                             e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },
    
            where :math:`\mu` is the mean and :math:`\sigma` the standard
            deviation. The square of the standard deviation, :math:`\sigma^2`,
            is called the variance.
    
            The function has its peak at the mean, and its "spread" increases with
            the standard deviation (the function reaches 0.607 times its maximum at
            :math:`x + \sigma` and :math:`x - \sigma` [2]_).  This implies that
            normal is more likely to return samples lying close to the mean, rather
            than those far away.
    
            References
            ----------
            .. [1] Wikipedia, "Normal distribution",
                   https://en.wikipedia.org/wiki/Normal_distribution
            .. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability,
                   Random Variables and Random Signal Principles", 4th ed., 2001,
                   pp. 51, 51, 125.
    """
    pass

示例代码:

import numpy as np

a1 = np.random.randn(2)
a2 = np.random.normal(0, 1, 2)
print(a1)
print('~~' * 10)
print(a2)
print('--' * 20)

b1 = np.random.randn(2, 3)
b2 = np.random.normal(0, 1, (2, 3))
print(b1)
print('~~' * 10)
print(b2)

运行结果:

[-0.08968467  0.19935229]
~~~~~~~~~~~~~~~~~~~~
[-2.70345057  0.31810813]
----------------------------------------
[[ 0.26098236  0.59379753 -0.70686308]
 [-0.78541554 -0.27910239 -0.15193886]]
~~~~~~~~~~~~~~~~~~~~
[[-0.92466689  0.580677    0.80772163]
 [ 2.17103711 -0.11340317 -0.06021829]]

备注:

更多精彩博客,请访问:聂发俊的技术博客 博客内容修改和更新,只更新个人博客。

对应视频教程,请访问: python400

完整markdown笔记,请访问: python400_learn_github


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK