NumPy Tutorial NumPy Statistics NumPy Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

NumPy - Array from Numerical Ranges



The NumPy package contains a number of functions which can be used to create an array from a given numerical range. Below is the list of most commonly used functions for this purpose:

FunctionDescription
arange() Return evenly spaced values within a given interval.
linspace() Returns evenly spaced numbers over a specified interval.
logspace() Return numbers spaced evenly on a log scale.

Lets discuss these functions in detail:

numpy.arange() function

The numpy.arange() function returns evenly spaced values within a given interval. The values are generated in the range [start, stop) with specified step size.

Syntax

numpy.arange(start, stop, step, dtype=None)

Parameters

start Optional. Specify start of the interval (inclusive). The default value is 0.
stop Required. Specify end of the interval (exclusive).
step Optional. Specify the step size. The default value is 1.
dtype Optional. Specify the type of the output array. If dtype is not given, infer the data type from the other input arguments.

Example:

The example below shows how to use arange() function to create arrays.

import numpy as np

#creating array using range [10,55) 
#and step size = 5
Arr1 = np.arange(10,55,5)
print("Arr1 is:", Arr1)

#creating array using range [10,16) 
#and step size = 1 and dtype=float
Arr2 = np.arange(10,16,dtype=float)
print("Arr2 is:", Arr2)

#reshaping the Arr2
Arr3 = Arr2.reshape(2,3)
print("Arr3 is:\n", Arr3)

The output of the above code will be:

Arr1 is: [10 15 20 25 30 35 40 45 50]
Arr2 is: [10. 11. 12. 13. 14. 15.]
Arr3 is:
[[10. 11. 12.]
 [13. 14. 15.]]

numpy.linspace() function

The numpy.linspace() function returns evenly spaced values over a specified interval. The values are generated in the range [start, stop] with specified number of samples. The endpoint of the interval can optionally be excluded.

Syntax

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

Parameters

start Required. Specify the starting value of the sequence.
stop Required. Specify the end value of the sequence, unless endpoint is False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.
num Optional. Specify the number of samples to generate. Default is 50. Must be non-negative.
endpoint Optional. Specify boolean value. If True, stop is the last sample. Otherwise, it is not included. Default is True.
retstep Optional. Specify boolean value. If True, return (samples, step), where step is the spacing between samples.
dtype Optional. Specify type of the output array. If dtype is not given, infer the data type from the other input arguments.

Example:

In the example below, linspace() function is used with different parameters to create arrays.

import numpy as np

#creating 5 samples points in the given range
Arr1 = np.linspace(1,5,num=5)
print("Arr1 is:", Arr1)

#when endpoint=False, (num+1) samples are  
#generated and returns samples without last
Arr2 = np.linspace(1,5,num=5,endpoint=False)
print("Arr2 is:", Arr2)

#when retstep=True, returns (samples, step)
Arr3 = np.linspace(1,5,num=5,retstep=True)
print("Arr3 is:", Arr3)

The output of the above code will be:

Arr1 is: [1. 2. 3. 4. 5.]
Arr2 is: [1.  1.8 2.6 3.4 4.2]
Arr3 is: (array([1., 2., 3., 4., 5.]), 1.0)

numpy.logspace() function

The numpy.logspace() function returns numbers spaced evenly on a log scale. The values are generated in the range [base ** start, base ** stop] with specified number of samples. The endpoint of the interval can optionally be excluded.

Syntax

numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

Parameters

start Required. Specify the starting value of the sequence. The starting value is base ** start.
stop Required. Specify the end value of the sequence (end value is base ** end), unless endpoint is False. In that case, num + 1 values are spaced over the interval in log-space, of which all but the last (a sequence of length num) are returned.
num Optional. Specify the number of samples to generate. Default is 50. Must be non-negative.
endpoint Optional. Specify boolean value. If True, stop is the last sample. Otherwise, it is not included. Default is True.
base Optional. Specify base of the log space. The step size between the elements in ln(samples) / ln(base) (or log_base(samples)) is uniform. Default is 10.0.
dtype Optional. Specify type of the output array. If dtype is not given, infer the data type from the other input arguments.

Example:

In the example below, logspace() function is used with different parameters to create arrays.

import numpy as np

#creating 5 samples points in the given range
Arr1 = np.logspace(1,2,num=5)
print("Arr1 is:", Arr1)

#when endpoint=False, (num+1) samples are  
#generated and returns samples without last 
Arr2 = np.logspace(1,2,num=5,endpoint=False)
print("Arr2 is:", Arr2)

#using base=2.0
Arr3 = np.logspace(1,2,num=5,base=2.0)
print("Arr3 is:", Arr3)

The output of the above code will be:

Arr1 is: [10.     17.7827941   31.6227766   56.23413252  100.       ]
Arr2 is: [10.     15.84893192  25.11886432  39.81071706  63.09573445]
Arr3 is: [2.      2.37841423   2.82842712   3.36358566   4.         ]

5