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
說明:
- 參數
low
和參數high
使用類似於random函數的使用方法 - size用法和上面random函數介紹的一樣,建議使用元組
- 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