Create arrays in Numpy


Reading time: 20 minutes | Coding time: 2 minutes

Creating and managing arrays is one of the fundamental and commonly used task in scientific computing and while using Numpy. Array is the mst fundamental and useful way of managing data and representing it in a multi-dimensional space.

Numpy is a Python library which adds support for several mathematical operations and makes working with multi-dimensional data easy. This is widely used in the scientific computing domain.

We will take a look at the various ways to can take to create an array in Numpy.

The Numpy functions that we have covered are:

  • arange()
  • zeros()
  • ones()
  • empty()
  • full()
  • eye()
  • linspace()
  • random()

Import Numpy as:

import numpy as np

Create a one dimensional array (1D)

arange() function

The arange() function will take an integer parameter N and return a one dimensional (1D) array with integers from 0 to N-1. Hence, the size of the array will be N.

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

Output:

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

You can check the shape of your array as:

array.shape

which will return:

(10,)

Additionally, you can reshape your array to any dimension provided the total number of elements remain the same. The elements are repacked in row major order.

For example, we can reshape our array to a 3 dimensional array as:

new_array = array.reshape(1,5,2)
new_array.shape
new_array

which gives the following output:

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

Using zeros function

The zeros function takes in an array shape and returns an array with the shape and filled with 0.

np.zeros((3,5))

Output will be:

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

ones function

The ones function takes in an array shape and returns an array with the shape and filled with 1.

np.ones((2,2,2))

Output:

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

       [[1., 1.],
        [1., 1.]]])

empty function

The empty function takes in an array shape and returns an array with the shape and filled with random numeric data.

np.empty((4,5))

Output:

array([[6.91956081e-310, 6.91956081e-310, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000],
       [0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000],
       [0.00000000e+000, 0.00000000e+000, 5.53353523e-322,
        5.53353523e-322, 0.00000000e+000],
       [6.91956157e-310, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000]])

full function

The full function takes in:

  • an array shape
  • an element used to fill the array

and returns an array with the shape and filled with the element provided.

np.full((2,2), 10)

Output:

array([[10, 10],
       [10, 10]])

eye function

The eye function takes in an array shape and returns an array with the shape and filled with 1 in the diagonal elements and rest elements are 0.

np.eye(4,4)

Output:

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

linspace function

The linspace function takes in:

  • number of elements in an array
  • starting and ending elements

and returns a 1 dimensional (1D) array with the specified number of elements uniformly distributed between the starting and ending elements.

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

Output:

array([ 0. ,  2.5,  5. ,  7.5, 10. ])

Random function

The random function takes in an array shape and returns an array with the shape and filled with random data which is between 0 and 1.

np.random.random((4,4))

Output:

array([[0.10358096, 0.43395544, 0.28328384, 0.36643843],
       [0.87050695, 0.09189959, 0.23660321, 0.29122672],
       [0.03201984, 0.0940262 , 0.02701679, 0.83124005],
       [0.69896299, 0.19106267, 0.35888434, 0.09041747]])