Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create a Sequence of Linearly Increasing Values with NumPy Arrange?
NumPy is a Python library widely used for numerical computations and scientific data analysis. One of the most commonly used functions is numpy.arange(), which creates a sequence of linearly increasing values with a given start, stop, and step size. This tutorial examines how to use numpy.arange() to produce sequences of linearly increasing values.
Syntax
numpy.arange([start, ]stop, [step, ], dtype=None)
Parameters
start Starting value of the sequence (optional, default is 0)
stop End value of the sequence (not included)
step Spacing between values (optional, default is 1)
dtype Data type of the output array (optional)
Basic Usage with Integer Values
Here's a simple example that creates sequences of linearly increasing integer values ?
import numpy as np
# Create sequences with different stop values
print("0 to 9:", np.arange(10))
print("0 to 19:", np.arange(20))
print("1 to 10:", np.arange(1, 11))
0 to 9: [0 1 2 3 4 5 6 7 8 9] 0 to 19: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] 1 to 10: [ 1 2 3 4 5 6 7 8 9 10]
Using Float Values with Custom Step Size
You can create sequences with floating-point numbers and custom step sizes ?
import numpy as np
# Create sequences with float values and step 0.5
print("1.5 to 5 (step 0.5):", np.arange(1.5, 5, 0.5))
print("0 to 3 (step 0.3):", np.arange(0, 3, 0.3))
print("2 to 10 (step 1.5):", np.arange(2, 10, 1.5))
1.5 to 5 (step 0.5): [1.5 2. 2.5 3. 3.5 4. 4.5] 0 to 3 (step 0.3): [0. 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7] 2 to 10 (step 1.5): [2. 3.5 5. 6.5 8. 9.5]
Using Larger Step Sizes
Create sequences with larger step sizes for specific intervals ?
import numpy as np
# Create sequences with step size 4
print("1 to 17 (step 4):", np.arange(1, 18, 4))
print("0 to 25 (step 5):", np.arange(0, 26, 5))
print("10 to 50 (step 10):", np.arange(10, 51, 10))
1 to 17 (step 4): [ 1 5 9 13 17] 0 to 25 (step 5): [ 0 5 10 15 20 25] 10 to 50 (step 10): [10 20 30 40 50]
Specifying Data Type
Control the data type of the resulting array using the dtype parameter ?
import numpy as np
# Different data types
int_array = np.arange(1, 6, dtype=int)
float_array = np.arange(1, 6, dtype=float)
print("Integer array:", int_array)
print("Float array:", float_array)
print("Int array dtype:", int_array.dtype)
print("Float array dtype:", float_array.dtype)
Integer array: [1 2 3 4 5] Float array: [1. 2. 3. 4. 5.] Int array dtype: int64 Float array dtype: float64
Key Points
The stop value is never included in the result
Use positive step values for increasing sequences
Use negative step values for decreasing sequences
Floating-point arithmetic may cause precision issues with non-integer steps
Conclusion
numpy.arange() is a powerful function for creating sequences of linearly spaced values. It's essential for numerical computations, array indexing, and generating data ranges in scientific applications.
