经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » NumPy » 查看文章
numpy.linspace函数具体使用详解
来源:jb51  时间:2019/5/28 8:56:10  对本文有异议

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

在指定的间隔内返回均匀间隔的数字。

返回num均匀分布的样本,在[start, stop]。

这个区间的端点可以任意的被排除在外。

Parameters(参数):

 

start : scalar(标量)

The starting value of the sequence(序列的起始点).

stop : scalar

序列的结束点,除非endpoint被设置为False,在这种情况下, the sequence consists of all but the last of num + 1 evenly spaced samples(该序列包括所有除了最后的num+1上均匀分布的样本(感觉这样翻译有点坑)), 以致于stop被排除.当endpoint is False的时候注意步长的大小(下面有例子).

num : int, optional(可选)

生成的样本数,默认是50。必须是非负。

endpoint : bool, optional

如果是真,则一定包括stop,如果为False,一定不会有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(推断这个输入用例从其他的输入中).

New in version 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(只有当retstep设置为真的时候才会存在)

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)

.arange使用的是步长,而不是样本的数量

logspace

Samples uniformly distributed in log space. 

当endpoint被设置为False的时候

  1. >>> import numpy as np
  2. >>> np.linspace(1, 10, 10)
  3. array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
  4. >>> np.linspace(1, 10, 10, endpoint = False)
  5. array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
  6.  
  7. In [4]: np.linspace(1, 10, 10, endpoint = False, retstep= True)
  8. Out[4]: (array([ 1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1]), 0.9)
  9.  

官网的例子 

Examples

  1. >>> >>> np.linspace(2.0, 3.0, num=5)
  2. array([ 2. , 2.25, 2.5 , 2.75, 3. ])
  3. >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
  4. array([ 2. , 2.2, 2.4, 2.6, 2.8])
  5. >>> np.linspace(2.0, 3.0, num=5, retstep=True)
  6. (array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

Graphical illustration:

  1. >>> >>> import matplotlib.pyplot as plt
  2. >>> N = 8
  3. >>> y = np.zeros(N)
  4. >>> x1 = np.linspace(0, 10, N, endpoint=True)
  5. >>> x2 = np.linspace(0, 10, N, endpoint=False)
  6. >>> plt.plot(x1, y, 'o')
  7. [<matplotlib.lines.Line2D object at 0x...>]
  8. >>> plt.plot(x2, y + 0.5, 'o')
  9. [<matplotlib.lines.Line2D object at 0x...>]
  10. >>> plt.ylim([-0.5, 1])
  11. (-0.5, 1)
  12. >>> plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号