Numpy Array From Numerical Ranges
In this chapter we will learn how to create arrays from numerical ranges.
### numpy.arange
numpy.arange function in the numpy package is used to create a numerical range and return an ndarray object, with the following function format:
numpy.arange(start, stop, step, dtype)
According to the range specified by start and stop and the step set by step, generate an ndarray.
Parameter description:
| Parameter | Description |
| --- | --- |
| `start` | Start value, default is `0` |
| `stop` | Stop value (not included) |
| `step` | Step, default is `1` |
| `dtype` | Data type of the returned `ndarray`, if not provided, the input data type will be used. |
### Instance
Generate an array with length 5 from 0 to 4:
## Instance
import numpy as np
x = np.arange(5)
print(x)
The output is as follows:
Set the return type to float:
## Instance
import numpy as np
x = np.arange(5, dtype = float)
print(x)
The output is as follows:
[0. 1. 2. 3. 4.]
Set the start value, stop value and step:
## Instance
import numpy as np
x = np.arange(10,20,2)
print(x)
The output is as follows:
### numpy.linspace
numpy.linspace function is used to create a one-dimensional array, the array is composed of an arithmetic progression, with the following format:
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Parameter description:
| Parameter | Description |
| --- | --- |
| `start` | Start value of the sequence |
| `stop` | Stop value of the sequence, if `endpoint` is `true`, this value is included in the sequence |
| `num` | Number of samples to generate with equal spacing, default is `50` |
| `endpoint` | When this value is `true`, the `stop` value is included in the sequence, otherwise not included, default is True. |
| `retstep` | If it is True, the spacing will be displayed in the generated array, otherwise not displayed. |
| `dtype` | Data type of `ndarray` |
The following instance uses three parameters, setting the start point to 1, stop point to 10, and the number of sequence elements to 10.
## Instance
import numpy as np
a = np.linspace(1,10,10)
print(a)
The output is:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Set the arithmetic progression with all elements being 1:
## Instance
import numpy as np
a = np.linspace(1,1,10)
print(a)
The output is:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Set endpoint to false, not including the stop value:
## Instance
import numpy as np
a = np.linspace(10, 20, 5, endpoint = False)
print(a)
The output is:
[10. 12. 14. 16. 18.]
If endpoint is set to true, it will include 20.
The following instance sets the spacing.
## Instance
import numpy as np
a =np.linspace(1,10,10,retstep= True)
print(a)
b =np.linspace(1,10,10).reshape([10,1])
print(b)
The output is:
(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
numpy.logspace function is used to create a one-dimensional array composed 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 means the subscript of log when taking logarithm.
| Parameter | Description |
| --- | --- |
| `start` | Start value of the sequence is: base ** start |
| `stop` | Stop value of the sequence is: base ** stop. If `endpoint` is `true`, this value is included in the sequence |
| `num` | Number of samples to generate with equal spacing, default is `50` |
| `endpoint` | When this value is `true`, the `stop` value is included in the sequence, otherwise not included, default is True. |
| `base` | Base of the logarithm log. |
| `dtype` | Data type of `ndarray` |
## Instance
import numpy as np
a = np.logspace(1.0, 2.0, num = 10)
print(a)
The output is:
[ 10. 12.91549665 16.68100537 21.5443469 27.82559402
35.938
YouTip