.arange()
The .arange()
function is a fundamental NumPy method that creates arrays of evenly spaced values within a specified interval. It serves as NumPy’s equivalent to Python’s built-in range()
function, but with enhanced capabilities for numerical computing. Unlike range()
which returns a list, numpy.arange()
returns a NumPy array that supports advanced mathematical operations and is optimized for numerical computations.
This function is extensively used in scientific computing, data analysis, and machine learning applications where you need to generate sequences of numbers for indexing, plotting, mathematical calculations, or creating coordinate systems. Its versatility makes it an essential tool for creating arrays for loops, generating test data, creating mathematical sequences, and establishing grids for visualization purposes.
Syntax
numpy.arange([start], stop, [step], dtype=None, *, like=None)
Parameters:
start
(optional): The starting value of the sequence. Default is 0.stop
(required): The end value of the sequence (exclusive).step
(optional): The spacing between consecutive values. Default is 1.dtype
(optional): The desired data type of the output array.like
(optional): Reference object to allow creation of arrays which are not NumPy arrays.
Returns:
ndarray
: Array of evenly spaced values within the given interval.
Example 1: Basic Integer Sequence
This example demonstrates the most fundamental use of numpy.arange()
to create a simple sequence of integers:
import numpy as np# Create a sequence from 0 to 9numbers = np.arange(10)print("Basic sequence:", numbers)# Create a sequence from 5 to 14custom_range = np.arange(5, 15)print("Custom range:", custom_range)# Create a sequence with step sizestepped_sequence = np.arange(0, 20, 3)print("Stepped sequence:", stepped_sequence)
The output of this code is:
Basic sequence: [0 1 2 3 4 5 6 7 8 9]Custom range: [5 6 7 8 9 10 11 12 13 14]Stepped sequence: [0 3 6 9 12 15 18]
This example shows how arange()
can generate sequences with different starting points and step sizes, making it perfect for creating index arrays or simple numerical sequences.
Example 2: Time Series Data Generation
This example demonstrates using numpy.arange()
for creating time-based sequences commonly used in data analysis and plotting:
import numpy as np# Generate hourly timestamps for a day (24 hours)hours = np.arange(0, 24, 1)print("Hours in a day:", hours)# Generate time points for a sine wave (0 to 2π with 0.1 intervals)time_points = np.arange(0, 2*np.pi, 0.1)sine_wave = np.sin(time_points)print("Time points:", time_points[:10]) # Show first 10 valuesprint("Sine values:", sine_wave[:10]) # Show corresponding sine values# Generate monthly data points for a yearmonths = np.arange(1, 13)quarterly_data = np.arange(1, 13, 3) # Quarterly intervalsprint("Monthly sequence:", months)print("Quarterly sequence:", quarterly_data)
The output of this code is:
Hours in a day: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]Time points: [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]Sine values: [0. 0.09983342 0.19866933 0.29552021 0.38941834 0.479425540.56464247 0.64421769 0.71735609 0.78332691]Monthly sequence: [ 1 2 3 4 5 6 7 8 9 10 11 12]Quarterly sequence: [1 4 7 10]
This example shows how arange()
is particularly useful for generating time-based sequences and creating data for mathematical functions, which is common in scientific computing and data visualization.
Codebyte Example: Financial Data Analysis
This example shows how numpy.arange()
can be used in financial analysis for creating price ranges, percentage calculations, and investment scenarios:
This practical example demonstrates how arange()
helps create structured datasets for financial modeling, allowing analysts to generate systematic ranges for price analysis, return calculations, and investment projections.
Frequently Asked Questions
1. What’s the difference between numpy.arange()
and Python’s range()
?
numpy.arange()
returns a NumPy array that supports mathematical operations and can handle floating-point steps, while range()
returns a range object that only works with integers and is primarily used for iteration.
2. Can numpy.arange()
work with floating-point numbers?
Yes, numpy.arange()
supports floating-point start, stop, and step values, making it ideal for creating decimal sequences that range()
cannot handle.
3. Can I create decreasing sequences with arange()
?
Yes, use a negative step value. For example, np.arange(10, 0, -1)
creates a decreasing sequence from 10 to 1.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours