用途
返回指定间隔上的等距数字,返回在间隔[start,stop]或[start, stop)上计算的平均间隔样本数。参数
start:数组, 序列的起始位置
stop:数组, 序列的终点位置
num: 非负整型, 默认是50
endpoint: bool, 如果为true,[start, stop], 否则[start, stop),默认为true
retstep: bool,默认为false, 如果是true, 返回的结果中包含步长
axis:int, 结果中存储样本的轴。仅当开始或停止类似于数组时才有效。默认值为0。返回值
endpoint=True, 返回:([start, stop), step) 或者 ([start, stop], step)
endpoint=False, 返回:[start, stop) 或者 [start, stop)
1. 导入numpy包
import numpy as np
2. 用法
2.1 start, end 都是数值不是数组
np.linspace(1,10)
array([ 1. , 1.18367347, 1.36734694, 1.55102041, 1.73469388,
1.91836735, 2.10204082, 2.28571429, 2.46938776, 2.65306122,
2.83673469, 3.02040816, 3.20408163, 3.3877551 , 3.57142857,
3.75510204, 3.93877551, 4.12244898, 4.30612245, 4.48979592,
4.67346939, 4.85714286, 5.04081633, 5.2244898 , 5.40816327,
5.59183673, 5.7755102 , 5.95918367, 6.14285714, 6.32653061,
6.51020408, 6.69387755, 6.87755102, 7.06122449, 7.24489796,
7.42857143, 7.6122449 , 7.79591837, 7.97959184, 8.16326531,
8.34693878, 8.53061224, 8.71428571, 8.89795918, 9.08163265,
9.26530612, 9.44897959, 9.63265306, 9.81632653, 10. ])
2.2 start, end 为数组
c = np.linspace((1,1),(9,10),5)
print(c)
[[ 1. 1. ]
[ 3. 3.25]
[ 5. 5.5 ]
[ 7. 7.75]
[ 9. 10. ]]
2.3 start, end为数组,axis=1
c = np.linspace((1,1),(9,10),5, axis=1)
print(c)
[[ 1. 3. 5. 7. 9. ]
[ 1. 3.25 5.5 7.75 10. ]]
2.4 返回值包含步长
np.linspace(1,10, 5, retstep=True)
(array([ 1. , 3.25, 5.5 , 7.75, 10. ]), 2.25)
2.5 返回值中不包含stop
np.linspace(1, 10, 5, endpoint=False)
array([1. , 2.8, 4.6, 6.4, 8.2])