How is the arange function used in numpy?

The arange function in numpy is used to create an array with evenly spaced values, and its syntax is as follows:

numpy.arange(start, stop, step, dtype)

Explanation of parameters:

  1. Starting value: Default is 0.
  2. termination value (exclusive), must be specified.
  3. Step: Default value is 1.
  4. dtype: The default data type of the array is None.

This function returns a one-dimensional numpy array that contains elements incrementally increasing from a start value to an end value (excluding) with a specified step size.

For example, you can create an integer array from 0 to 9 using the arange function.

import numpy as np

arr = np.arange(10)
print(arr)

The output result is:

[0 1 2 3 4 5 6 7 8 9]

You can also specify a starting value, ending value, and step size to create different arrays. For example, creating an array from 1 to 9 with a step size of 2:

import numpy as np

arr = np.arange(1, 10, 2)
print(arr)

The output is:

[1 3 5 7 9]
广告
Closing in 10 seconds
bannerAds