numpy.linspace 生成等差数组

来源:互联网 发布:上海日料放题 知乎 编辑:程序博客网 时间:2024/06/05 02:50

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

start:起始值 stop:结束值
num:生成的个数
endpoint True:包含 False:不包含 默认True
restep:显示相邻两数之差 默认不显示
dtype: 输出类型 默认不显示

同时,arange 是通过设置样本之间的差值来生成数组的。

import numpy as npx1 = np.linspace(2.0, 3.0, num=5)print x1x2 = np.linspace(2.0, 3.0, num=5, endpoint=False)print x2x3 = np.linspace(2.0, 3.0, num=5, retstep=True)print x3

结果:
[ 2. 2.25 2.5 2.75 3. ]
[ 2. 2.2 2.4 2.6 2.8]
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

图示:

import numpy as npimport matplotlib.pyplot as pltN = 8y = np.zeros(N)x1 = np.linspace(0, 10, N, endpoint=True)x2 = np.linspace(0, 10, N, endpoint=False)plt.plot(x1, y, "o")plt.plot(x2, y + 0.5, 'o')plt.ylim([-0.5, 1])plt.show()

这里写图片描述

0 0
原创粉丝点击