What is the purpose of the arange() function in NumPy?
a) To create an array with evenly spaced values within a given range
b) To randomly shuffle the elements of an array
c) To concatenate two arrays
d) To find the range of values in an array
Answer:
a) To create an array with evenly spaced values within a given range
Explanation:
The arange()
function in NumPy is used to create an array with evenly spaced values within a specified range. This function is similar to the Python built-in range()
function but returns a NumPy array instead of a list, and it supports floating-point increments.
import numpy as np
# Creating an array with values from 0 to 9
arr = np.arange(0, 10)
print(arr) # Output: [0 1 2 3 4 5 6 7 8 9]
In this example, the arange()
function creates an array of values from 0 to 9, with a default step size of 1.
The arange()
function is useful for generating sequences of numbers for use in simulations, data processing, and mathematical computations, especially when working with arrays that require specific intervals between elements.