In this article we look at various ways of creating an array in Numpy with some examples.
First the basic syntax of the array() function which is probably the easiest and most common way.
# Syntax of numpy.array() numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
Create a Single Dimension Array in NumPy
You can create a single-dimensional array using a list of numbers.
We will use the numpy.array() function which is the most basic way to create a NumPy array from other array-like objects such as a list or tuple.
# Import numpy module import numpy as np # Create a 1D array arr1=np.array([1,2,3,4,5]) print("My 1D array:\n",arr1)
You will see this
My 1D array:
[1 2 3 4 5]
Create a Multi-Dimensional Array in NumPy
A list of lists will create a 2D Numpy array, you can also create N-dimensional arrays. Let’s look at how you can create a 2D array by using numpy.array() function.
# Import numpy module import numpy as np arr1 = np.array([[1,2,3],[4,5,6]]) print("My 2D numpy array:\n", arr1) print("Type:", type(arr1))
You will see this
My 2D numpy array:
[[1 2 3]
[4 5 6]]
Create an Array Using the arange() Function
NumPy provides the arange() function which returns an array.
Following is the syntax of arange().
# Syntax of arange() numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)
This function returns evenly spaced values within a given interval. In other words, this returns a list of values from start and stop value by incrementing 1. If step is specified, it increments the value by a given step.
# Import numpy module import numpy as np # Create a sequence of integers # from 0 to 10 with step of 2 arr= np.arange(0, 10, 2) print ("A sequential array with steps of 2:\n", arr)
You will see this
A sequential array with steps of 2:
[0 2 4 6 8]
Using linspace() Function
linspace() returns evenly spaced values within a given interval.
Like the arange() function this can also be used to create a NumPy array.
In this function, we have control over where to start the Numpy array, where to stop, and the number of values to return between the start and stop.
# Syntax of linspace() numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
This function takes arguments start, stop and num (the number of elements) to be outputted. These number of elements would be linearly spaced in the range mentioned. For example,
# Import numpy module import numpy as np arr= np.linspace(0, 30, 4) print(arr)
You will see this
[ 0. 10. 20. 30.]
Create Empty Array using the empty() Function
You can use the numpy.empty() function to create an empty NumPy array, pass it a shape tuple.
# Syntax of the empty function empty(shape, dtype)
Lets see this now
# Import numpy module import numpy as np arr = (3, 3) # 3 rows and 3 columns arr1 = np.empty(arr) print(" Array with values:\n",arr1)
This will give something like this, the numbers will be random
Array with values:
[[0.00000000e+000 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 4.74303020e-321]
[1.05700854e-307 2.56761491e-312 0.00000000e+000]]
Create a NumPy Array filled with Zeros
Lets say you want the array to be filled with zeros rather than random values like above.
We can the zeros() function to create an array of a specified shape that is filled with the value zero (0). Lets see this now
# Import numpy module import numpy as np arr = np.zeros((3,3)) print("numpy array:\n", arr)
This will give something like this
numpy array:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Create a NumPy Array with Ones
Similar to the zeros example above
We can use the numpy.ones() function this time. Lets do that.
# Import numpy module import numpy as np # Use ones() and create an array arr = np.ones((3,3)) print("numpy array:\n", arr)
This time when run you will see the following
numpy array:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Conclusion
In this article, I have explained how to create a NumPy array in different ways with examples.