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

import numpy as np x = np.arange(5) print (x)

The output result is as follows:

[0  1  2  3  4]

Set the return type to float:

Example

import numpy as np # set the dtype x = np.arange(5, dtype = float) print (x)

The output result is as follows:

[0.  1.  2.  3.  4.]

Set the start value, end value, and step size:

Example

import numpy as np x = np.arange(10,20,2) print (x)

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

import numpy as np a = np.linspace(1,10,10) print(a)

The output result is:

[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

Set an arithmetic sequence with all elements being 1:

Example

import numpy as np a = np.linspace(1,1,10) print(a)

The output result is:

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Set endpoint to false to exclude the end value:

Example

import numpy as np a = np.linspace(10, 20, 5, endpoint = False) print(a)

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

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)

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

import numpy as np # The default base is 10 a = np.logspace(1.0, 2.0, num = 10) print (a)

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

import numpy as np a = np.logspace(0,9,10,base=2) print (a)

The output is as follows:

[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]
Other Extensions