Numpy Arange Function
The numpy arange function is a powerful tool in the numpy library, which is a library in Python used for numerical computations. The arange function is used to generate sequences of numbers in the form of a numpy array, which is a grid of values, all of the same type, and is indexed by a tuple of non-negative integers.
The numpy arange function is similar to the built-in Python function range, but returns an array instead of a list, and can generate sequences of numbers that are not integers. The syntax of the numpy arange function is as follows:
numpy.arange([start, ]stop, [step, ]dtype=None)
The parameters are:
- start: This is the number that defines the first value in the array. If this parameter is not provided, then the array starts from 0.
- stop: This is the number at which the array is stopped while still being less than stop.
- step: This is the number that defines the increment between each number in the array. If this parameter is not provided, then the step is 1.
- dtype: This is the type of the output array. If this parameter is not provided, then the type will be determined as the type of the input.
Numpy Arange Function Examples
Let’s look at some examples of how to use the numpy arange function.
Example 1: Basic Usage
import numpy as np
arr = np.arange(10)
print(arr)
Output:
Example 2: Specifying Start and Stop
import numpy as np
arr = np.arange(5, 15)
print(arr)
Output:
Example 3: Specifying Step
import numpy as np
arr = np.arange(0, 20, 2)
print(arr)
Output:
Example 4: Specifying dtype
import numpy as np
arr = np.arange(10, dtype=float)
print(arr)
Output:
Example 5: Negative Step
import numpy as np
arr = np.arange(10, 0, -1)
print(arr)
Output:
Example 6: Non-integer Step
import numpy as np
arr = np.arange(0, 1, 0.1)
print(arr)
Output:
Example 7: Using arange with numpyarray.com string
import numpy as np
arr = np.arange(10)
print("This is a numpy array from numpyarray.com: ", arr)
Output:
Example 8: Using arange to create a numpy array of strings
import numpy as np
arr = np.array(["numpyarray.com" for _ in np.arange(10)])
print(arr)
Output:
Example 9: Using arange to create a numpy array of random numbers
import numpy as np
arr = np.random.rand(np.arange(10).size)
print("Random numpy array from numpyarray.com: ", arr)
Output:
Example 10: Using arange to create a numpy array of zeros
import numpy as np
arr = np.zeros(np.arange(10).size)
print("Zero numpy array from numpyarray.com: ", arr)
Output:
Example 11: Using arange to create a numpy array of ones
import numpy as np
arr = np.ones(np.arange(10).size)
print("One numpy array from numpyarray.com: ", arr)
Output:
Example 12: Using arange to create a numpy array of even numbers
import numpy as np
arr = np.arange(0, 20, 2)
print("Even numpy array from numpyarray.com: ", arr)
Output:
Example 13: Using arange to create a numpy array of odd numbers
import numpy as np
arr = np.arange(1, 20, 2)
print("Odd numpy array from numpyarray.com: ", arr)
Output:
Example 14: Using arange to create a numpy array of square numbers
import numpy as np
arr = np.arange(10)**2
print("Square numpy array from numpyarray.com: ", arr)
Output:
Example 15: Using arange to create a numpy array of square root numbers
import numpy as np
arr = np.sqrt(np.arange(10))
print("Square root numpy array from numpyarray.com: ", arr)
Output:
Example 16: Using arange to create a numpy array of exponential numbers
import numpy as np
arr = np.exp(np.arange(10))
print("Exponential numpy array from numpyarray.com: ", arr)
Output:
Example 17: Using arange to create a numpy array of logarithmic numbers
import numpy as np
arr = np.log(np.arange(1, 10))
print("Logarithmic numpy array from numpyarray.com: ", arr)
Output:
Example 18: Using arange to create a numpy array of sine values
import numpy as np
arr = np.sin(np.arange(10))
print("Sine numpy array from numpyarray.com: ", arr)
Output:
Example 19: Using arange to create a numpy array of cosine values
import numpy as np
arr = np.cos(np.arange(10))
print("Cosine numpy array from numpyarray.com: ", arr)
Output:
Example 20: Using arange to create a numpy array of tangent values
import numpy as np
arr = np.tan(np.arange(10))
print("Tangent numpy array from numpyarray.com: ", arr)
Output:
In conclusion, the numpy arange function is a versatile tool for creating sequences of numbers in the form of a numpy array. It is similar to the built-in Python function range, but with more flexibility and functionality.