Creating Arrays from Numerical Ranges in NumPy
In this section, we will learn how to create arrays from numerical ranges.
numpy.arange
The arange
function in the NumPy package creates a numerical range and returns an ndarray object. The function format is as follows:
numpy.arange(start, stop, step, dtype)
This function generates an ndarray based on the specified range from start
to stop
and the step size step
.
Parameter Description:
Parameter | Description |
---|---|
start | The starting value, default is 0 |
stop | The ending value (not included) |
step | The step size, default is 1 |
dtype | The data type of the returned ndarray. If not provided, it will use the type of the input data. |
Example
Generate an array from 0 to 5:
import numpy as np
x = np.arange(5)
print(x)
Output:
[0 1 2 3 4]
Set the return type to float:
import numpy as np
# Set dtype
x = np.arange(5, dtype=float)
print(x)
Output:
[0. 1. 2. 3. 4.]
Set the starting value, ending value, and step size:
import numpy as np
x = np.arange(10, 20, 2)
print(x)
Output:
[10 12 14 16 18]
numpy.linspace
The numpy.linspace
function is used to create a one-dimensional array consisting of an arithmetic progression. The format is as follows:
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Parameter Description:
Parameter | Description |
---|---|
start | The starting value of the sequence |
stop | The ending value of the sequence. If endpoint is true, this value is included in the sequence. |
num | The number of samples to generate, default is 50 |
endpoint | If true, the sequence includes the stop value; otherwise, it does not. Default is True. |
retstep | If true, the spacing between values is displayed in the output; otherwise, it is not. |
dtype | The data type of the ndarray |
The following example uses three parameters: start at 1, end at 10, and generate 10 numbers.
import numpy as np
a = np.linspace(1, 10, 10)
print(a)
Output:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Set the sequence to all 1s:
import numpy as np
a = np.linspace(1, 1, 10)
print(a)
Output:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Set endpoint to false to exclude the stop value:
import numpy as np
a = np.linspace(10, 20, 5, endpoint=False)
print(a)
Output:
[10. 12. 14. 16. 18.]
If endpoint is set to true, it will include 20.
The following example sets the spacing:
import numpy as np
a = np.linspace(1, 10, 10, retstep=True)
print(a)
# Extended example
b = np.linspace(1, 10, 10).reshape([10, 1])
print(b)
Output:
(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]
[ 6.]
[ 7.]
[ 8.]
[ 9.]
[10.]]
numpy.logspace
The numpy.logspace
function is used to create an array of a geometric progression. The format is as follows:
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
The base
parameter indicates the base of the logarithm.
Parameter | Description |
---|---|
start | The starting value of the sequence: base ** start |
stop | The end value of the sequence is: base ** stop. If endpoint is true, this value is included in the sequence. |
num | The number of samples to generate with equal steps, default is 50. |
endpoint | When this value is true, the stop value is included in the sequence, otherwise it is not, default is True. |
base | The base of the logarithm. |
dtype | The data type of the ndarray. |
Example
import numpy as np
# The default base is 10
a = np.logspace(1.0, 2.0, num=10)
print(a)
Output result:
[ 10. 12.91549665 16.68100537 21.5443469 27.82559402
35.93813664 46.41588834 59.94842503 77.42636827 100. ]
Setting the base of the logarithm to 2:
Example
import numpy as np
a = np.logspace(0, 9, 10, base=2)
print(a)
Output:
[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]