How to create a NumPy 1D-array with equally spaced numbers in an interval?
Last Updated :
28 Dec, 2023
At times, we need to make arrays of different types like in AP(equally spaced series of numbers), GP(exponentially spaced series of numbers), or HP(reciprocally spaced series of numbers) to solve various problems, especially while solving some scientific or astronomical problem, to reduce the calculations. Python is one of the best languages when it comes to the pre-implemented codes, and is present there almost every time, at the cost of processing speed.
NumPy has built-in methods called np.arange() and np.linspace() which are capable of creating an array of a given integer type(in bytes), with equal spacing between the numbers. In this article, we will see how to create an array with evenly spaced values in NumPy.
Creating a NumPy Array with Equally Spaced Numbers
Below are the ways by which we can see how to create an array with evenly spaced values in NumPy:
- Using np.arange() Function
- Using np.arange() and intervals as parameters
- Using arange() and dtype
- Using np.linspace() Function
- Using step size
- Using np.linspace() Function and specific datatype
Creating a Sequential Array from 0 to a Given Number
In this example, a 1D array named myArray
is created using numpy.arange()
with a single argument, generating numbers from 0 up to (but not including) 8. The resulting array is then printed.
Python3
import numpy as np
myArray = np.arange(8)
print(myArray)
Output:
[0 1 2 3 4 5 6 7]
Create an Equally Spaced Array with a Given Interval
In this example, the numpy.arange()
function generates a 1D array named myThirdArray
starting from 2, ending just before 12, with a step size of 2 between consecutive elements. The resulting array is then displayed.
Python3
import numpy as np
myThirdArray = np.arange(2, 12, 2)
print(myThirdArray)
Output:
[ 2 4 6 8 10]
Creating a Sequential Array Using numpy.arange() and dtype
We use dtype especially in the cases when we want to deal with images or some other sort of computation.
Python3
import numpy as np
myForthArray = np.arange(5, 101, 10, np.int32)
print(myForthArray)
Output:
[ 5 15 25 35 45 55 65 75 85 95]
Equally Spaced Numbers Using np.linspace() Function
In this example, numpy.linspace()
creates a 1D array arr
with 9 evenly spaced numbers starting from -2 and ending at 2, inclusive. The array is then printed.
Python3
import numpy as np
arr = np.linspace(1, 10, num=8)
print(arr)
Output:
[ 1. 2.28571429 3.57142857 4.85714286 6.14285714 7.42857143 8.71428571 10.]
Creating Equally Spaced Number with Step Size
In this example, numpy.linspace()
generates a 1D array arr
with 11 evenly spaced numbers from 0 to 10. Additionally, it returns the step size between consecutive numbers, which is then printed alongside the array.
Python3
import numpy as np
arr, step = np.linspace(0, 10, num=11, retstep=True)
print(arr)
print(f"Step size: {step}")
Output:
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Step size: 1.0
Creating Equally Spaced Number Array Using a Specific Data Type
In this example, numpy.linspace()
produces a 1D integer array arr
with 11 evenly spaced integers from 0 to 10, inclusive. The resulting integer array is then printed.
Python3
import numpy as np
arr = np.linspace(0, 10, num=11, dtype=int)
print(arr)
Output:
[ 0 1 2 3 4 5 6 7 8 9 10]
Similar Reads
How to normalize an NumPy array so the values range exactly between 0 and 1? In this article, we will cover how to normalize a NumPy array so the values range exactly between 0 and 1. Normalization is done on the data to transform the data to appear on the same scale across all the records. After normalization, The minimum value in the data will be normalized to 0 and the ma
3 min read
How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem
2 min read
How to create a constant matrix in Python with NumPy? A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th
4 min read
How to normalize an array in NumPy in Python? Normalizing an array in NumPy refers to the process of scaling its values to a specific range, typically between 0 and 1. For example, an array like [1, 2, 4, 8, 10] can be normalized to [0.0, 0.125, 0.375, 0.875, 1.0], where the smallest value becomes 0, the largest becomes 1 and all other values a
4 min read
How to create a list of uniformly spaced numbers using a logarithmic scale with Python? In this article, we will create a list of uniformly spaced numbers using a logarithmic scale. It means on a log scale difference between two adjacent samples is the same. The goal can be achieved using two different functions from the Python Numpy library. Functions  Used:numpy.logspace: This functi
3 min read