How to Create a Numpy array in Python

Spread the love

There are various ways to create a numpy array, let’s look at them one by one.

Create a Numpy array from a python list –

You can use np.array function to create a numpy array from python lists or any other sequence objects.

To create a numpy array first we have to import the numpy library. By convention numpy library is imported under the alias np.

In [1]: import numpy as np

Then we will use the np.array function to create a numpy array from a python list.

In [2]: data = [5, 7, 3, 8.5, 10]

In [3]: arr1 = np.array(data)

In [4]: arr1
Out[4]: array([ 5. ,  7. ,  3. ,  8.5, 10. ])

We can check the number of dimensions this array has using the ndim attribute and the shape of the using the shape attribute.

In [5]: arr1.ndim
Out[5]: 1

In [6]: arr1.shape
Out[6]: (5,)

Since this is a one-dimensional array, the ndim is 1. And the shape of the array is a tuple which tells you the number of elements in each dimensions. Since we have 5 element and one dimension, we get the value of (5,).

Create a numpy array from list of list –

To create a two-dimensional array we can use a list of list or nested list in python.

In [7]: data2 = [[1, 2], [3, 4]]

In [8]: arr2 = np.array(data2)

In [9]: arr2.ndim
Out[9]: 2

In [10]: arr2.shape
Out[10]: (2, 2)

Create a numpy array with constant values –

To create a numpy array with zeros and ones, we can use the np.zeros and np.ones functions respectively. The first argument to this functions is an integer or a tuple that describes the number of elements along each dimensions of the array.

To create a one dimensional array with ones and zeros, we can use

In [11]: arr3 = np.zeros(4)

In [12]: arr3
Out[12]: array([0., 0., 0., 0.])

In [13]: arr4 = np.ones(5)

In [14]: arr4
Out[14]: array([1., 1., 1., 1., 1.])

To create a two dimensional arrays of ones and zeros, we will write

In [16]: arr5 = np.zeros((2, 3))

In [17]: arr5
Out[17]: 
array([[0., 0., 0.],
       [0., 0., 0.]])

In [18]: arr6 = np.ones((4, 5))

In [19]: arr6
Out[19]: 
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

Like other array generating functions, the np.ones and np.zeros takes an optional keyword argument that specifies the data type of the elements in an array.

In [20]: arr7 = np.zeros((2, 3), dtype=np.int64)

In [21]: arr7
Out[21]: 
array([[0, 0, 0],
       [0, 0, 0]], dtype=int64)

In [22]: arr7.dtype
Out[22]: dtype('int64')

To create an array with any constant value, we have to first create an array filled with ones and then multiply that array with the value that we want.

Let’s say I want to create an array all filled with 10s.

In [23]: arr8 = np.ones((2, 3)) * 10

In [24]: arr8
Out[24]: 
array([[10., 10., 10.],
       [10., 10., 10.]])

Numpy also provide a function np.full for doing this.

In [25]: arr9 = np.full((2, 3), 10)

In [26]: arr9
Out[26]: 
array([[10, 10, 10],
       [10, 10, 10]])

A existed array can also be filled with constant values using the np.fill function which takes an array and a value as an argument.

In [30]: arr9.fill(50)

In [31]: arr9
Out[31]: 
array([[50, 50, 50],
       [50, 50, 50]])

Create numpy array with incremental sequences –

To create an evenly spaced values between a starting and ending value, we can use the np.arange and np.linspace functions in numpy. Both functions takes three arguments where the first two are the start and end value. The third argument of np.arange is the increment whereas for np.linspace it is the total number of points in the array.

In [2]: np.arange(0, 10, 1)
Out[2]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [3]: np.linspace(0, 10, 11)
Out[3]: array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

Please note that np.arange don’t include the end value 10 but np.linspace does which can be changed using the endpoint keyword argument.

Create a numpy array with logarithmic sequences –

You can also create a logarithmic sequence using the np.logspace. The increments in np.logspace is logarithmically distributed and the first two arguments.

For example to create logarithmically distributed array with 10 values from 10**0=1 and 10**2=100.

In [4]: np.logspace(0, 2, 10)
Out[4]: 
array([  1.        ,   1.66810054,   2.7825594 ,   4.64158883,
         7.74263683,  12.91549665,  21.5443469 ,  35.93813664,
        59.94842503, 100.        ])

Create a numpy array with properties of other arrays-

We can also create a numpy array that shares the properties and data type of another array using the np.ones_like, np.zeros_like, np.full_like.

In [5]: arr10 = np.full((2, 3), 10)

In [6]: arr10
Out[6]: 
array([[10, 10, 10],
       [10, 10, 10]])

In [7]: arr11 = np.ones_like(arr10)

In [8]: arr11
Out[8]: 
array([[1, 1, 1],
       [1, 1, 1]])

Create Matrix arrays –

Numpy also provides various functions to create commonly used matrices. The np.identity function generates a square matrix with 1 on the diagonals and zeros elsewhere.

In [9]: np.identity(3)
Out[9]: 
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

The np.eye function also let’s you generate a array with ones on a diagonal but it also let’s you specify the offset from the diagonal.

In [10]: np.eye(3, k=1)
Out[10]: 
array([[0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])

In [11]: np.eye(3, k=-1)
Out[11]: 
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])

To construct a matrix with different values along the diagonal, we can use the np.diag function.

In [12]: np.diag([5, 7, 2, 10])
Out[12]: 
array([[ 5,  0,  0,  0],
       [ 0,  7,  0,  0],
       [ 0,  0,  2,  0],
       [ 0,  0,  0, 10]])

Related Posts –

  1. Numpy array data types with examples
  2. Numpy array attributes with examples

Rating: 1 out of 5.

Leave a Reply