Array From Numerical Ranges using Numpy

Array From Numerical Ranges using Numpy

In this chapter, we will see how to create an array from numerical ranges.

numpy.arange

This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows −

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

The constructor takes the following parameters.

Sr.No. Parameter & Description
1 start
The start of an interval. If omitted, defaults to 0
2 stop
The end of an interval (not including this number)
3 step
Spacing between values, default is 1
4 dtype
Data type of resulting ndarray. If not given, data type of input is used

start
The start of an interval. If omitted, defaults to 0
stop
The end of an interval (not including this number)
step
Spacing between values, default is 1
dtype
Data type of resulting ndarray. If not given, data type of input is used
The following examples show how you can use this function.

Example 1

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

Its output would be as follows −

[0 1 2 3 4 5 6]

Example 2

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

Here, the output would be −

[0. 1. 2. 3. 4. 5. 6. 7.]

Example 3

# start and stop parameters set
x = np.arange(10,45,3)
print(x)

Its output is as follows −

[10 13 16 19 22 25 28 31 34 37 40 43]

numpy.linspace

This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. The usage of this function is as follows −

numpy.linspace(start, stop, num, endpoint, retstep, dtype)

The constructor takes the following parameters.

Sr.No. Parameter & Description
1 start
The starting value of the sequence
2 stop
The end value of the sequence, included in the sequence if endpoint set to true
3 num
The number of evenly spaced samples to be generated. Default is 50
4 endpoint
True by default, hence the stop value is included in the sequence. If false, it is not included
5 retstep
If true, returns samples and step between the consecutive numbers
6 dtype
Data type of output ndarray

start
The starting value of the sequence
stop
The end value of the sequence, included in the sequence if endpoint set to true
num
The number of evenly spaced samples to be generated. Default is 50
endpoint
True by default, hence the stop value is included in the sequence. If false, it is not included
retstep
If true, returns samples and step between the consecutive numbers
dtype
Data type of output ndarray
The following examples demonstrate the use linspace function.

Example 1

import numpy as np
x = np.linspace(10,20,9)
print(x)

Its output would be −

[10.   11.25 12.5  13.75 15.   16.25 17.5  18.75 20.  ]

Example 2

# endpoint set to false
import numpy as np
x = np.linspace(10,20, 8, endpoint = False)
print(x)

The output would be −

[10.   11.25 12.5  13.75 15.   16.25 17.5  18.75]

Example 3

# find retstep value
import numpy as np
x = np.linspace(1,3,8, retstep = True)
print(x)
# retstep here is 0.25

Now, the output would be −

(array([1.        , 1.28571429, 1.57142857, 1.85714286, 2.14285714,
2.42857143, 2.71428571, 3.        ]), 0.2857142857142857)

numpy.logspace

This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10.

numpy.logspace(start, stop, num, endpoint, base, dtype)

Following parameters determine the output of logspace function.

Sr.No. Parameter & Description
1 start
The starting point of the sequence is basestart
2 stop
The final value of sequence is basestop
3 num
The number of values between the range. Default is 50
4 endpoint
If true, stop is the last value in the range
5 base
Base of log space, default is 10
6 dtype
Data type of output array. If not given, it depends upon other input arguments

start
The starting point of the sequence is basestart
stop
The final value of sequence is basestop
num
The number of values between the range. Default is 50
endpoint
If true, stop is the last value in the range
base
Base of log space, default is 10
dtype
Data type of output array. If not given, it depends upon other input arguments
The following examples will help you understand the logspace function.

Example 1

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

Its output would be as follows −

[ 10. 12.91549665 16.68100537 21.5443469 27.82559402
35.93813664 46.41588834 59.94842503 77.42636827 100. ]

Example 2

# set base of log space to 2
import numpy as np
a = np.logspace(1,9,num = 10, base = 2)
print(a)

Now, the output would be −

[  2.           3.70349885   6.85795186  12.69920842  23.51575188
43.54528001  80.63494719 149.31571707 276.49529316 512.        ]
Array From Existing Data using Numpy (Prev Lesson)
(Next Lesson) Indexing & Slicing using Numpy