Python NumPy matlib.empty() Function

NumPy matlib.empty() Function:

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

Syntax:

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

Parameters

shape: This is required. This is a tuple that specifies the shape of the matrix.

dtype: This is optional. The represents the data type of the matrix

order: This is optional. It Specifies whether or not the result should be stored. C (C-style) and F (Fortran-style) are two possible values. ‘C’ is the default value. That means it is the matrix’s insertion order.

Return Value:

A matrix of uninitialized (arbitrary) data with the specified shape, dtype, and order is returned.

NumPy matlib.empty() Function in Python

Example1

The matlib.empty() method is used to construct a matrix of uninitialized (arbitrary) elements of a given shape.

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword
  • Pass the shape of the matrix as an argument to the matlib.empty() function of numpy module to create a matrix of random values.
  • Store it in a variable.
  • Print the given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix as an argument to the matlib.empty() function
# of numpy module to create a matrix of random values.
# Store it in a variable.
gvn_matrx = np.matlib.empty((2,2))
# Print the given matrix
print(gvn_matrx)

Output:

[[5.e-324 5.e-324]
 [5.e-324 0.e+000]]

Example2

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

Approach:

  • Import numpy module using the import keyword.
  • Import matlib function of numpy module using the import keyword
  • Pass the shape of the matrix (here with only length one) as an argument to the matlib.empty() function of numpy module to create a matrix of random values.
  • Store it in a variable.
  • Print the given first matrix.
  • Pass the shape of the matrix (here with only length one, ) as an argument to the matlib.empty() function of numpy module to create a matrix of random values.
  • Store it in another variable.
  • Print the given second matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(here with only length one) as an argument to the matlib.empty() function
# of numpy module to create a matix of random values.
# Store it in a variable.
gvn_matrx1 = np.matlib.empty((3))
# Print the given first matrix
print("The given first matrix = ",gvn_matrx1)
# Pass the shape of the matrix(here length one) as an argument to the matlib.empty() function
# of numpy module to create a matrix of random values.
# Store it in another variable.
gvn_matrx2 = np.matlib.empty((2,))
# Print the given second matrix
print("The given second matrix = ",gvn_matrx2)

Output:

The given first matrix = [[5.e-324 5.e-324 5.e-324]]
The given second matrix = [[4.66938826e-310 0.00000000e+000]]

Example3: With giving dtype

The dtype option can be used with the matlib.empty() function to give the data type of the matrix elements. Here, the data type of the matrix is int.

# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix, datatype as int as an argument to the matlib.empty() function
# of numpy module to create a matrix of with random integer values.
# Store it in a variable.
gvn_matrx = np.matlib.empty((3,3), dtype=int)
# Print the given matrix
print(gvn_matrx)

Output:

[[ 94509478290608 468151435310 498216206433]
 [ 450971566188 197568495714 468151435365]
 [ 498216206448 7813572927730745465 8317708060514805100]]