NumPy Array Generation Functions
NumPy offers versatile functions to generate arrays with sequences of numbers or evenly spaced values. These tools help you create arrays for a variety of tasks, from simple ranges to precise intervals, without manual input.
Whether you need a range of integers, linearly spaced points, or logarithmically spaced samples, these functions provide efficient and reliable ways to generate the arrays you need for computations and data analysis.
- np.arange(): Generate arrays with regularly spaced values within a specified interval.
- np.linspace(): Create arrays of evenly spaced numbers over a specified interval.
- np.logspace(): Generate numbers spaced evenly on a logarithmic scale.
- np.geomspace(): Generate numbers spaced evenly on a geometric progression.
Generate Arrays with np.arange()
The np.arange() function creates arrays with regularly spaced values within a specified interval. It's similar to Pythonβs built-in range() function, but returns a NumPy array instead of a list.
You can specify the start, stop, and step values to control the range.
import numpy as np
# Create an array from 0 to 9
arr = np.arange(0, 10)
print(arr)
import numpy as np
# Create an array from 0 to 9
arr = np.arange(0, 10)
print(arr)
How It Works:
- 0: The start of the interval (inclusive).
- 10: The end of the interval (exclusive).
- np.arange(): Generates values in steps of 1 by default.
Output
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
You can also specify a custom step value:
# Create an array from 0 to 20, in steps of 5
arr_step = np.arange(0, 21, 5)
print(arr_step)
# Create an array from 0 to 20, in steps of 5
arr_step = np.arange(0, 21, 5)
print(arr_step)
Output
[ 0 5 10 15 20 ]
[ 0 5 10 15 20 ]
π‘ Tip: np.arange() works well for integers, but for non-integer steps (like 0.1), floating-point errors can occur. For more precise intervals, use np.linspace().
Create Arrays with np.linspace()
The np.linspace() function generates arrays of evenly spaced values over a specified interval. Unlike np.arange(), which uses a step size, linspace() lets you specify the exact number of values you want between two endpoints.
This is especially useful when working with floating-point ranges or plotting smooth curves.
import numpy as np
# Generate 5 values between 0 and 1 (inclusive)
arr = np.linspace(0, 1, 5)
print(arr)
import numpy as np
# Generate 5 values between 0 and 1 (inclusive)
arr = np.linspace(0, 1, 5)
print(arr)
How It Works:
- 0: The start of the interval (inclusive).
- 1: The end of the interval (inclusive by default).
- 5: The number of evenly spaced values to generate.
Output
[0. 0.25 0.5 0.75 1. ]
[0. 0.25 0.5 0.75 1. ]
By default, np.linspace() includes the endpoint. You can exclude the end by passing endpoint=False:
# Generate values excluding the endpoint
arr_exclude = np.linspace(0, 1, 5, endpoint=False)
print(arr_exclude)
# Generate values excluding the endpoint
arr_exclude = np.linspace(0, 1, 5, endpoint=False)
print(arr_exclude)
Output
[0. 0.2 0.4 0.6 0.8]
[0. 0.2 0.4 0.6 0.8]
π‘ Tip: linspace() is ideal for generating floating-point ranges, especially when precise spacing or a specific number of values is important.
Generate Numbers with np.logspace()
The np.logspace() function creates an array of numbers spaced evenly on a logarithmic scale. This is useful when you want values that grow exponentially β such as for plotting data on log-scale axes or modeling data with rapid growth.
Instead of linear spacing between values, logspace() places equal distance between exponents. You specify the base (commonly 10), a starting exponent, and an ending exponent.
import numpy as np
# Generate 5 values from 10^1 to 10^3
arr = np.logspace(1, 3, num=5)
print(arr)
import numpy as np
# Generate 5 values from 10^1 to 10^3
arr = np.logspace(1, 3, num=5)
print(arr)
How It Works:
- 1: The starting exponent (101 = 10).
- 3: The ending exponent (103 = 1000).
- num=5: The number of points to generate.
- By default, it uses base 10.
Output
[ 10. 31.6227766 100. 316.22776602 1000. ]
[ 10. 31.6227766 100. 316.22776602 1000. ]
You can change the base using the base argument:
# Use base 2 instead of base 10
arr_base2 = np.logspace(1, 4, num=4, base=2)
print(arr_base2)
# Use base 2 instead of base 10
arr_base2 = np.logspace(1, 4, num=4, base=2)
print(arr_base2)
Output
[ 2. 8. 32. 128.]
[ 2. 8. 32. 128.]
π‘ Tip: np.logspace() is perfect when working with logarithmic data, frequency bands, or exponential growth scenarios.
Generate Numbers with np.geomspace()
The np.geomspace() function generates numbers spaced evenly on a geometric progression. That means each value is multiplied by a constant ratio to get the next one β ideal for creating values that grow (or shrink) multiplicatively.
Unlike np.logspace(), which spaces values based on powers of a base, geomspace() directly spaces values based on a geometric ratio between the start and end.
import numpy as np
# Generate 4 values from 1 to 1000 (multiplicative spacing)
arr = np.geomspace(1, 1000, num=4)
print(arr)
import numpy as np
# Generate 4 values from 1 to 1000 (multiplicative spacing)
arr = np.geomspace(1, 1000, num=4)
print(arr)
Output
[ 1. 10. 100. 1000.]
[ 1. 10. 100. 1000.]
How It Works:
- 1: Starting value of the sequence.
- 1000: Ending value of the sequence.
- num=4: Total number of values to generate.
- Each number is a constant multiple of the previous one.
It also works for decreasing sequences (start > end):
# Geometric spacing in reverse (decreasing values)
arr_desc = np.geomspace(1000, 1, num=4)
print(arr_desc)
# Geometric spacing in reverse (decreasing values)
arr_desc = np.geomspace(1000, 1, num=4)
print(arr_desc)
Output
[1000. 100. 10. 1.]
[1000. 100. 10. 1.]
π‘ Tip: Use np.geomspace() when you need values that scale multiplicatively β it's especially useful for modeling exponential decay, frequency scales, or orders of magnitude.
Frequently Asked Questions
What is the difference between np.arange and np.linspace?
What is the difference between np.arange and np.linspace?
np.arange creates arrays with evenly spaced values within a specified interval using a step size, while np.linspace creates arrays with a specified number of evenly spaced samples between start and stop values.
How do I create logarithmically spaced arrays in NumPy?
How do I create logarithmically spaced arrays in NumPy?
You can use np.logspace to create arrays with values spaced evenly on a log scale between start and stop exponents.
What does np.geomspace do?
What does np.geomspace do?
np.geomspace generates numbers spaced evenly on a geometric progression (multiplicative) between start and stop values, useful for creating sequences with exponential growth.
Can I generate arrays with non-integer steps using np.arange?
Can I generate arrays with non-integer steps using np.arange?
Yes, np.arange supports non-integer step sizes, but due to floating-point precision issues, np.linspace is often preferred for non-integer steps.
How do I specify the number of samples in np.linspace?
How do I specify the number of samples in np.linspace?
You specify the number of samples using the num
parameter. For example, np.linspace(0, 10, num=5) generates 5 evenly spaced samples between 0 and 10.
What's Next?
Up next, weβll explore Indexing and Slicing in NumPy β essential techniques that let you access and manipulate subsets of your data with ease. You'll learn how to use basic indexing, slicing, and multidimensional access to work more efficiently with NumPy arrays.