📜  Python中的 numpy.linspace()

📅  最后修改于: 2022-05-13 01:55:42.421000             🧑  作者: Mango

Python中的 numpy.linspace()

numpy.linspace()函数在间隔中均匀地返回数字空间。类似于 numpy.arange()函数,但它使用样本编号而不是 step。
句法 :

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

参数 :

-> start  : [optional] start of interval range. By default start = 0
-> stop   : end of interval range
-> restep : If True, return (samples, step). By deflut restep = False
-> num    : [int, optional] No. of samples to generate
-> dtype  : type of output array

返回 :

-> ndarray
-> step : [float, optional], if restep = True

代码 1:解释 linspace函数

Python
# Python Programming illustrating
# numpy.linspace method
 
import numpy as geek
 
# restep set to True
print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n")
 
# To evaluate sin() in long range
x = geek.linspace(0, 2, 10)
print("A\n", geek.sin(x))


Python
# Graphical Representation of numpy.linspace()
import numpy as geek
import pylab as p
 
# Start = 0
# End = 2
# Samples to generate = 10
x1 = geek.linspace(0, 2, 10, endpoint = False)
y1 = geek.ones(10)
 
p.plot(x1, y1, '*')
p.xlim(-0.2, 1.8)


Python
# Graphical Representation of numpy.linspace()
import numpy as geek
import pylab as p
 
# Start = 0
# End = 2
# Samples to generate = 15
x1 = geek.linspace(0, 2, 15, endpoint = True)
y1 = geek.zeros(15)
 
p.plot(x1, y1, 'o')
p.xlim(-0.2, 2.1)


输出 :

B
 (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

A
 [ 0.          0.22039774  0.42995636  0.6183698   0.77637192  0.8961922
  0.9719379   0.99988386  0.9786557   0.90929743]

代码 2:使用 matplotlib 模块的 numpy.linspace() 的图形表示 - pylab

Python

# Graphical Representation of numpy.linspace()
import numpy as geek
import pylab as p
 
# Start = 0
# End = 2
# Samples to generate = 10
x1 = geek.linspace(0, 2, 10, endpoint = False)
y1 = geek.ones(10)
 
p.plot(x1, y1, '*')
p.xlim(-0.2, 1.8)

输出 :

代码 3:使用 pylab 对 numpy.linspace() 进行图形表示

Python

# Graphical Representation of numpy.linspace()
import numpy as geek
import pylab as p
 
# Start = 0
# End = 2
# Samples to generate = 15
x1 = geek.linspace(0, 2, 15, endpoint = True)
y1 = geek.zeros(15)
 
p.plot(x1, y1, 'o')
p.xlim(-0.2, 2.1)

输出 :