np.arange vs np.linspace
Both np.arange and np.linspace are used to create arrays with evenly spaced values, but they have different ways of specifying the intervals and the number of elements.
np.arange takes start, stop, and step as arguments:
import numpy as np
array = np.arange(start, stop, step)
start: The starting value of the range.
stop: The end value of the range (exclusive).
step: The difference between each consecutive value in the array.
np.linspace takes start, stop, and num as arguments:
import numpy as np
array = np.linspace(start, stop, num)
start: The starting value of the range.
stop: The end value of the range (inclusive).
num: The number of evenly spaced values to generate.
Here's a comparison of the two functions:
import numpy as np
# Using np.arange
array1 = np.arange(0, 10, 2)
print(array1) # Output: [0 2 4 6 8]
# Using np.linspace
array2 = np.linspace(0, 10, 5)
print(array2) # Output: [ 0. 2.5 5. 7.5 10. ]
In summary, np.arange is useful when you want to specify the step size between values, while np.linspace is useful when you want to specify the total number of values in the array.
标签:value,stop,arange,start,linspace,np,NumPy From: https://www.cnblogs.com/chucklu/p/17241002.html