NumPy Tutorial NumPy Statistics NumPy References

NumPy - Array Creation



The NumPy package provides a number of ways to create a new ndarray object. Below is the list of most commonly used functions for this purpose:

FunctionDescription
empty() Returns a new array of given shape and type, without initializing entries.
ones() Returns a new array of given shape and type, filled with ones.
zeros() Returns a new array of given shape and type, filled with zeros.

Lets discuss these functions in detail:

numpy.empty() function

The NumPy empty() function returns a new array of specified shape and data type, with uninitialized entries.

Syntax

numpy.empty(shape, dtype=float, order='C')

Parameters

shape Required. Specify shape of the returned array in form of int or tuple of ints.
dtype Optional. Specify the desired data-type for the array. Default: float
order Optional. Specify whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Example

In the example below, empty() function is used to create a 2 dimensional array of uninitialized (arbitrary) data of specified shape.

#array of uninitialized (arbitrary) data
import numpy as np
Arr = np.empty((2,2))
print(Arr)

The output of the above code will be:

[[  6.93360212e-310   6.93360212e-310]
 [  6.93359915e-310   6.93359743e-310]]

numpy.zeros() function

The NumPy zeros() function returns a new array of specified shape and data type, filled with zeros.

Syntax

numpy.zeros(shape, dtype=float, order='C')

Parameters

shape Required. Specify shape of the returned array in form of int or tuple of ints.
dtype Optional. Specify the desired data-type for the array. Default: float
order Optional. Specify whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Example

In the example below, zeros() function is used to create a 2 dimensional array of zeros of specified shape.

#array of zeros
import numpy as np
Arr = np.zeros((2,3))
print(Arr)

The output of the above code will be:

[[ 0.  0.  0.]
 [ 0.  0.  0.]]

Example

The data type of the returned array can be provided using dtype parameter.

#dtype=complex
import numpy as np
Arr = np.zeros((2,2), dtype=complex)
print(Arr)

The output of the above code will be:

[[ 0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j]]

numpy.ones() function

The NumPy ones() function returns a new array of specified shape and data type, filled with ones.

Syntax

numpy.ones(shape, dtype=float, order='C')

Parameters

shape Required. Specify shape of the returned array in form of int or tuple of ints.
dtype Optional. Specify the desired data-type for the array. Default: float
order Optional. Specify whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Example

In the example below, ones() function is used to create a 2 dimensional array of ones of specified shape.

#array of ones
import numpy as np
Arr = np.ones((2,3))
print(Arr)

The output of the above code will be:

[[ 1.  1.  1.]
 [ 1.  1.  1.]]

Example

The custom data type can also be used using dtype parameter.

#custom data type
import numpy as np
Arr = np.ones((2,2), dtype=[('x', 'i4'), ('y', 'i4')])
print(Arr)

The above code gives the following output:

[[(1, 1) (1, 1)]
 [(1, 1) (1, 1)]]