NumPy Tutorial NumPy Statistics NumPy References

NumPy - matlib.empty() function



The NumPy matlib.empty() function returns a matrix of given shape and type, without initializing entries.

Syntax

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

Parameters

shape Required. Specify shape of the matrix.
dtype Optional. Specify the desired data-type for the matrix.
order Optional. Specify whether to store the result. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Return Value

Returns a matrix of uninitialized (arbitrary) data with the given shape, dtype, and order.

Example: Create a matrix of uninitialized entry

In the example below, matlib.empty() function is used to create a matrix of uninitialized (arbitrary) entries of specified shape.

import numpy as np
import numpy.matlib

mat = np.matlib.empty((2,2))
print(mat)

The output of the above code will be:

[[1.58262349e-316 0.00000000e+000]
 [6.21064510e+175 6.78850084e+199]]

Example: matlib.empty() with scalar or length one

If shape has length one i.e. (N,), or is a scalar N, the returned matrix will be a single row matrix of shape (1,N). Consider the following example.

import numpy as np
import numpy.matlib

mat1 = np.matlib.empty(2)
print("mat1 is:", mat1)

mat2 = np.matlib.empty((3,))
print("mat2 is:", mat2)

The output of the above code will be:

mat1 is: [[-5.73021895e-300  6.92584906e-310]]
mat2 is: [[6.92584906e-310 6.92584906e-310 0.00000000e+000]]

Example: matlib.empty() function with dtype parameter

The matlib.empty() function can be used with dtype parameter to provide the data type of the elements of the matrix. In the example below, data type of the matrix is int.

import numpy as np
import numpy.matlib

mat = np.matlib.empty((2,2), dtype=int)
print(mat)

The output of the above code will be:

[[       13537456               0]
 [139962566194416              49]]

❮ NumPy - Functions