Open In App

How to create a NumPy 1D-array with equally spaced numbers in an interval?

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using np.arange() Function
  2. Using np.arange() and intervals as parameters
  3. Using arange() and dtype
  4. Using np.linspace() Function
  5. Using step size
  6. 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]


Last Updated : 28 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads