Different ways to Create NumPy Arrays

NumPy arrays are the central data structure for applying the numerous useful operations defined in the NumPy library. Before you can start manipulating or transforming them, you’ll have to create them. In this tutorial, we’ll look at how some of the different ways to create numpy arrays.

There are a number of different ways to create a numpy array. You can use the numpy array() function to create one from an array-like object. You can also other built-in numpy functions to create more specific arrays depending on your use-case.

The numpy array() function is generally used to create a numpy array from other array-like objects. For instance, you can use this function to create a numpy array from a python list. See the example below:

import numpy as np
# create a 1D numpy array 
arr1 = np.array([1,2,3])
print("1D numpy array:\n", arr1)
print("Type:", type(arr1))
# create a 2D numpy array
arr2 = np.array([[1,2,3],[4,5,6]])
print("2D numpy array:\n", arr2)
print("Type:", type(arr2))

Output:

1D numpy array:
 [1 2 3]
Type: <class 'numpy.ndarray'>
2D numpy array:
 [[1 2 3]
 [4 5 6]]
Type: <class 'numpy.ndarray'>

In the above example, numpy arrays arr1 and arr2 are created from lists using the numpy array() function. Note that, to create a 2D array we had to pass a nested list to the array() function.

You can also use other array-like objects, such as tuples, etc. to create a numpy array using the array() function.

tup = ((1,2,3),(4,5,6))
print(np.array(tup))

Output:

[[1 2 3]
 [4 5 6]]

The numpy array() function is the standard way of creating numpy arrays. Numpy, however, also provides a bunch of other built-in functions to create certain numpy arrays from scratch. These arrays can come in handy depending upon the use-case. For instance, you may require an array of zeroes, or ones, or random numbers. All of these can be created using numpy built-in functions. Follow along to see how to make such arrays in numpy.

The numpy zeros() function is used to create an array of the specified shape filled with zeroes. See the example below.

import numpy as np
arr = np.zeros((2,3))
print("numpy array:\n", arr)
print("Type:", type(arr))

Output:

numpy array:
 [[0. 0. 0.]
 [0. 0. 0.]]
Type: <class 'numpy.ndarray'>

Here, the default dtype of the array items is numpy.float64, you can specify the desired data-type for the array via the dtype parameter.

You can create a numpy array of your desired shape filled with ones using the numpy ones() function. See the example below.

import numpy as np
arr = np.ones((2,3))
print("numpy array:\n", arr)
print("Type:", type(arr))

Output:

numpy array:
 [[1. 1. 1.]
 [1. 1. 1.]]
Type: <class 'numpy.ndarray'>

Here as well the default dtype of the array items is numpy.float64, you can specify the desired data-type for the array via the dtype parameter.

The numpy random.randint() function is used to create a numpy array filled with random integers in a given range. The following is its syntax:

arr = numpy.random.randint(low, high, size)

It returns a numpy array of the shape passed to the size parameter filled with integers from low (inclusive) to high (exclusive). See the example below.

import numpy as np
arr = np.random.randint(1, 5, (2,3))
print("numpy array:\n", arr)
print("Type:", type(arr))

Output:

numpy array:
 [[2 1 3]
 [4 2 4]]
Type: <class 'numpy.ndarray'>

There are other numpy methods like arange() and linspace() that can be used to create a numpy array with evenly spaced values between start and stop endpoints. The arange() function gives values in the interval [start, stop) and uses a step-size whereas the linspace() function provides more control on whether or not to include stop and uses number of samples instead of a step-size. See the example below.

import numpy as np
# array of evenly spaced numbers from 1 to 10 with a step-size of 2
arr1 = np.arange(1, 10, 2)
print("arr1: ", arr1)
# array of evenly spaced numbers from 1 to 10 and return 2 numbers
arr2 = np.linspace(1, 10, 2)
print("arr2: ", arr2)

Output:

arr1:  [1 3 5 7 9]
arr2:  [ 1. 10.]

You can see that np.arange(1, 10, 2) returned evenly spaced values between 1 to 10 with a step size of 2 whereas np.linspace(1, 10, 2) returned 2 evenly spaced values between 1 to 10.

These were some of the different ways to create a numpy array. For more, refer to this guide on array creation by numpy.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.