Table of Contents
ToggleCreating arrays from numerical ranges in NumPy refers to generating arrays that contain sequences of numbers within a specified range. NumPy provides several functions to create such arrays, they are as follows −
numpy.arange() Functionnumpy.linspace() Functionnumpy.logspace() Functionnumpy.meshgrid() FunctionThe numpy.arange() function creates an array by generating a sequence of numbers based on specified start, stop, and step values. It is similar to Python’s built-in range() function but returns a NumPy array. Following is the syntax −
numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)
Where,
In the following example, we are using the numpy.arange() function to generate an array starting from 0 up to (but not including) 10 −
# Open Compiler
import numpy as np
# Create an array from 0 to 9
arr = np.arange(10)
print("Array using arange():", arr)
Output:
Following is the output obtained −
Array using arange(): [0 1 2 3 4 5 6 7 8 9]
In here, we are generating an array starting from 1, up to (but not including) 10, with a step of 2 using the numpy.arange() function −
# Open Compiler
import numpy as np
# Create an array from 1 to 9 with a step of 2
arr = np.arange(1, 10, 2)
print("Array with start, stop, and step:", arr)
Output:
This will produce the following result −
Array with start, stop, and step: [1 3 5 7 9]
The numpy.linspace() function generates an array with evenly spaced values over a specified interval. It is useful when you need a specific number of points between two values.
This function is similar to the arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. Following is the syntax −
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Where,
In this example, we are using the numpy.linspace() function to generate an array of 10 evenly spaced values from 0 to 1 (inclusive by default) −
# Open Compiler
import numpy as np
# Create an array of 10 evenly spaced values from 0 to 1
arr = np.linspace(0, 1, 10)
print("Array using linspace():", arr)
Output:
Following is the output of the above code −
Array using linspace(): [0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
In here, we are generating an array of 10 evenly spaced values from 0 to just below 1, excluding the endpoint using the numpy.linspace() function −
# Open Compiler
import numpy as np
# Create an array
arr = np.linspace(0, 1, 10, endpoint=False)
print("Array with endpoint=False:", arr)
Output:
The output obtained is as shown below −
Array with endpoint=False: [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
Now, we are generating an array of 10 evenly spaced values from 0 to 1 (inclusive by default) and also returns the step size −
# Open Compiler
import numpy as np
# Create an array
arr, step = np.linspace(0, 1, 10, retstep=True)
print("Array with step size:", arr)
print("Step size:", step)
Output:
After executing the above code, we get the following output −
Array with step size: [0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
Step size: 0.1111111111111111
The numpy.logspace() function generates an array with values that are evenly spaced on a log scale. This is useful for generating values that span several orders of magnitude. Following is the syntax −
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)
Where,
In the example below, we are using the numpy.logspace() function to generate an array of 10 values evenly spaced on a logarithmic scale from 21 to 210 with base 2 −
# Open Compiler
import numpy as np
# Create an array
arr = np.logspace(1, 10, 10, base=2)
print("Array with base 2:", arr)
Output:
We get the output as shown below −
Array with base 2: [ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.]
The numpy.meshgrid() function generates coordinate matrices from coordinate vectors. This is useful for creating grids of points for evaluating functions over a 2D or 3D space. Following is the syntax −
numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
Where,
In the example below, we are using the numpy.meshgrid() function to generate coordinate matrices “X” and “Y” from 1D arrays “x” and “y”, where X represents the x-coordinates and Y represents the y-coordinates of a 2D grid −
# Open Compiler
import numpy as np
# Create 1D arrays for x and y coordinates
x = np.arange(1, 4)
y = np.arange(1, 3)
# Generate coordinate matrices
X, Y = np.meshgrid(x, y)
print("X grid:")
print(X)
print("Y grid:")
print(Y)
Output:
Following is the output obtained −
X grid:
[[1 2 3]
[1 2 3]]
Y grid:
[[1 1 1]
[2 2 2]]
Now, we are generating coordinate matrices X, Y, and Z from 1D arrays x, y, and z for a 3D grid using matrix indexing (‘ij’) −
# Open Compiler
import numpy as np
# Create 1D arrays for x, y, and z coordinates
x = np.arange(1, 4)
y = np.arange(1, 3)
z = np.arange(1, 3)
# Generate coordinate matrices
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
print("X grid:")
print(X)
print("Y grid:")
print(Y)
print("Z grid:")
print(Z)
Output:
After executing the above code, we get the following output −
X grid:
[[[1 1]
[1 1]]
[[2 2]
[2 2]]
[[3 3]
[3 3]]]
Y grid:
[[[1 1]
[2 2]]
[[1 1]
[2 2]]
[[1 1]
[2 2]]]
Z grid:
[[[1 2]
[1 2]]
[[1 2]
[1 2]]
[[1 2]
[1 2]]]
Key Takeaway: Master creating NumPy arrays from numerical ranges at Vista Academy!
