# How to Create a Numpy array in Python

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 : import numpy as np

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

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

In : arr1 = np.array(data)

In : arr1
Out: 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 : arr1.ndim
Out: 1

In : arr1.shape
Out: (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 : data2 = [[1, 2], [3, 4]]

In : arr2 = np.array(data2)

In : arr2.ndim
Out: 2

In : arr2.shape
Out: (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 : arr3 = np.zeros(4)

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

In : arr4 = np.ones(5)

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

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

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

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

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

In : arr6
Out:
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 : arr7 = np.zeros((2, 3), dtype=np.int64)

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

In : arr7.dtype
Out: 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 : arr8 = np.ones((2, 3)) * 10

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

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

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

In : arr9
Out:
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 : arr9.fill(50)

In : arr9
Out:
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 : np.arange(0, 10, 1)
Out: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In : np.linspace(0, 10, 11)
Out: 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 : np.logspace(0, 2, 10)
Out:
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 : arr10 = np.full((2, 3), 10)

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

In : arr11 = np.ones_like(arr10)

In : arr11
Out:
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 : np.identity(3)
Out:
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 : np.eye(3, k=1)
Out:
array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])

In : np.eye(3, k=-1)
Out:
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 : np.diag([5, 7, 2, 10])
Out:
array([[ 5,  0,  0,  0],
[ 0,  7,  0,  0],
[ 0,  0,  2,  0],
[ 0,  0,  0, 10]])

### Related Posts –

Rating: 1 out of 5.