Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Different Ways to Create Numpy Arrays

Sep 15, 2018 • 7 Minute Read

Introduction

At the heart of a Numpy library is the array object or the ndarray object (n-dimensional array). You will use Numpy arrays to perform logical, statistical, and Fourier transforms. As part of working with Numpy, one of the first things you will do is create Numpy arrays. The main objective of this guide is to inform a data professional, you, about the different tools available to create Numpy arrays.

There are three different ways to create Numpy arrays:

  1. Using Numpy functions
  2. Conversion from other Python structures like lists
  3. Using special library functions

Using Numpy functions

Numpy has built-in functions for creating arrays. We will cover some of them in this guide.

Creating a One-dimensional Array

First, let’s create a one-dimensional array or an array with a rank 1. arange is a widely used function to quickly create an array. Passing a value 20 to the arange function creates an array with values ranging from 0 to 19.

      import Numpy as np
array = np.arange(20)
array
    

Output:

      array([0,  1,  2,  3,  4,
       5,  6,  7,  8,  9,
       10, 11, 12, 13, 14,
       15, 16, 17, 18, 19])
    

To verify the dimensionality of this array, use the shape property.

      array.shape
    

Output:

      (20,)
    

Since there is no value after the comma, this is a one-dimensional array. To access a value in this array, specify a non-negative index. As in other programming languages, the index starts from zero. So to access the fourth element in the array, use the index 3.

      array[3]
    

Output:

      3
    

Numpy Arrays are mutable, which means that you can change the value of an element in the array after an array has been initialized. Use the print function to view the contents of the array.

      array[3] = 100
print(array)
    

Output:

        0   1   2 100
   4   5   6   7
   8   9  10  11
   12  13  14  15
   16  17  18  19
    

Unlike Python lists, the contents of a Numpy array are homogenous. So if you try to assign a string value to an element in an array, whose data type is int, you will get an error.

      array[3] ='Numpy'
    

Output:

      ValueError: invalid literal for int() with base 10: 'Numpy'
    

Creating a Two-dimensional Array

Let's talk about creating a two-dimensional array. If you only use the arange function, it will output a one-dimensional array. To make it a two-dimensional array, chain its output with the reshape function.

      array = np.arange(20).reshape(4,5)
array
    

Output:

      array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
    

First, 20 integers will be created and then it will convert the array into a two-dimensional array with 4 rows and 5 columns. Let's check the dimensionality of this array.

      array.shape
    

Output:

      (4, 5)
    

Since we get two values, this is a two-dimensional array. To access an element in a two-dimensional array, you need to specify an index for both the row and the column.

      array[3][4]
    

Output:

      19
    

Creating a Three-dimensional Array and Beyond

To create a three-dimensional array, specify 3 parameters to the reshape function.

      array = np.arange(27).reshape(3,3,3)
array
    

Output:

      array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
    

Just a word of caution: The number of elements in the array (27) must be the product of its dimensions (3*3*3). To cross-check if it is a three-dimensional array, you can use the shape property.

      array.shape
    

Output:

      (3, 3, 3)
    

Also, using the arange function, you can create an array with a particular sequence between a defined start and end values

      np.arange(10, 35, 3)
    

Output:

      array([10, 13, 16, 19, 22, 25, 28, 31, 34])
    

Using Other Numpy Functions

Other than arange function, you can also use other helpful functions like zerosand ones to quickly create and populate an array.

Use the zeros function to create an array filled with zeros. The parameters to the function represent the number of rows and columns (or its dimensions).

      np.zeros((2,4))
    

Output:

      array([[0., 0., 0., 0.],
       [0., 0., 0., 0.]])
    

Use the ones function to create an array filled with ones.

      np.ones((3,4))
    

Output:

      array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
    

The empty function creates an array. Its initial content is random and depends on the state of the memory.

      np.empty((2,3))
    

Output:

      array([[0.65670626, 0.52097334, 0.99831087],
       [0.07280136, 0.4416958 , 0.06185705]])
    

The full function creates a n * n array filled with the given value.

      np.full((2,2), 3)
    

Output:

      array([[3, 3],
       [3, 3]])
    

The eye function lets you create a n * n matrix with the diagonal 1s and the others 0.

      np.eye(3,3)
    

Output:

      array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
    

The function linspace returns evenly spaced numbers over a specified interval. For example, the below function returns four equally spaced numbers between the interval 0 and 10.

      np.linspace(0, 10, num=4)
    

Output:

      array([ 0., 3.33333333, 6.66666667, 10.])
    

Conversion from Python Lists

Other than using Numpy functions, you can also create an array directly from a Python list. Pass a Python list to the array function to create a Numpy array:

      array = np.array([4,5,6])
array
    

Output:

      array([4, 5, 6])
    

You can also create a Python list and pass its variable name to create a Numpy array.

      list = [4,5,6]
list
    

Output:

      4
    
      array = np.array(list)
array
    

Output:

      array([4, 5, 6])
    

You can confirm that both the variables, array and list, are a of type Python list and Numpy array respectively.

      type(list)
    

list

      type(array)
    

Numpy.ndarray

To create a two-dimensional array, pass a sequence of lists to the array function.

      array = np.array([(1,2,3), (4,5,6)])
array
    

Output:

      array([[1, 2, 3],
       [4, 5, 6]])
    
      array.shape
    

Output:

      (2, 3)
    

Using Special Library Functions

You can also use special library functions to create arrays. For example, to create an array filled with random values between 0 and 1, use random function. This is particularly useful for problems where you need a random state to get started.

      np.random.random((2,2))
    

Output:

      array([[0.1632794 , 0.34567049],
       [0.03463241, 0.70687903]])
    

Conclusion

Creating and populating a Numpy array is the first step to using Numpy to perform fast numeric array computations. Armed with different tools for creating arrays, you are now well set to perform basic array operations.