numpy 函数一:linspace
接触 numpy 遇到的第一个函数可能就是 linspace 函数,但是对于我们这种没有学过 matlab 的人来说,根本不知道这是什么。
所以只能自己查资料。
词典显示:
线性等分向量
线性平分矢量
线性平分向量
那么怎么用呢?
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Return evenly spaced numbers over a specified interval.
Returns `num` evenly spaced samples, calculated over the
interval [`start`, `stop` ].
The endpoint of the interval can optionally be excluded.
Parameters
----------
start : scalar
The starting value of the sequence.
样本起始值
stop : scalar
The end value of the sequence, unless `endpoint` is set to False.
样本终止值
In that case, the sequence consists of all but the last of ``num + 1``
evenly spaced samples, so that `stop` is excluded. Note that the step
size changes when `endpoint` is False.
num : int, optional
样本个数
Number of samples to generate. Default is 50.
endpoint : bool, optional
If True, `stop` is the last sample. Otherwise, it is not included.
Default is True.
如果为真,则最后一个值(stop对应的值)包含在样本中
retstep : bool, optional
If True, return (`samples`, `step`), where `step` is the spacing
between samples.
如果为真,返回样本及步长
dtype : dtype, optional
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.
样本的数据类型
.. versionadded:: 1.9.0
Returns
-------
samples : ndarray
There are `num` equally spaced samples in the closed interval
``[start, stop]`` or the half-open interval ``[start, stop)``
(depending on whether `endpoint` is True or False).
step : float
Only returned if `retstep` is True
Size of spacing between samples.
See Also
--------
arange : Similar to `linspace`, but uses a step size (instead of the
number of samples).
logspace : Samples uniformly distributed in log space.
例:
import numpy as np
import scipy as sp
import pylab as pl
# 从0到4pi之间,取100个样点
x = np.linspace(0,4*np.pi,100)
pl.plot(x,np.sin(x))
pl.show()
结果: