NumPy Creating Arrays from Numeric Ranges
In this chapter, we will learn how to create arrays from numeric ranges.
numpy.arange
You can use the arange function in the numpy package to create numeric ranges and return an ndarray object. The function format is as follows:
numpy.arange(start, stop, step, dtype)
Generate an ndarray based on the range specified by start and stop and the step size set by step.
Parameter description:
| Parameter | Description |
|---|---|
start |
Start value, default is0 |
stop |
End value (not included) |
step |
Step size, default is1 |
dtype |
Returnndarraydata type; if not provided, the type of the input data will be used. |
Example
Generate an array from 0 to 4 with length 5:
Example
The output result is as follows:
[0 1 2 3 4]
Set the return type to float:
Example
The output result is as follows:
[0. 1. 2. 3. 4.]
Set the start value, end value, and step size:
Example
The output result is as follows:
[10 12 14 16 18]
numpy.linspace
The numpy.linspace function is used to create a one-dimensional array, which is composed of an arithmetic sequence. The format is as follows:
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Parameter description:
| Parameter | Description |
|---|---|
start |
The start value of the sequence |
stop |
The end value of the sequence, ifendpointistruethis value is included in the sequence. |
num |
The number of samples to generate with equal spacing; default is50 |
endpoint |
When this value istruethen the sequence contains thestopvalue; otherwise it is not included. Default is True. |
retstep |
If True, the spacing will be displayed in the generated array; otherwise it will not be displayed. |
dtype |
ndarraydata type |
The following example uses three parameters: set the start point to 1, the end point to 10, and the number of items to 10.
Example
The output result is:
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Set an arithmetic sequence with all elements being 1:
Example
The output result is:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Set endpoint to false to exclude the end value:
Example
The output result is:
[10. 12. 14. 16. 18.]
If endpoint is set to true, 20 will be included.
The following example sets the spacing.
Example
The output result 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
The numpy.logspace function is used to create a geometric sequence. The format is as follows:
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
The base parameter means the base of the log when taking a logarithm.
| Parameter | Description |
|---|---|
start |
The start value of the sequence is: base ** start |
stop |
The end value of the sequence is: base ** stop. Ifendpointistruethis value is included in the sequence. |
num |
The number of samples to generate with equal spacing; default is50 |
endpoint |
When this value istruethen the sequence containsstopvalue; otherwise it is not included. Default is True. |
base |
The base of the logarithm log. |
dtype |
ndarraydata type |
Example
The output result is:
[ 10. 12.91549665 16.68100537 21.5443469 27.82559402 35.93813664 46.41588834 59.94842503 77.42636827 100. ]
Set the base of the logarithm to 2:
Example
The output is as follows:
[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]Other Extensions