Using numpy.asmatrix() function (4 examples)

Updated: February 27, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python, providing support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. One of the functions provided by NumPy is asmatrix(), which is used to interpret the input as a matrix. Unlike arrays, matrices are restricted to 2D and have specific operations that follow the rules of linear algebra.

This tutorial provides an in-depth look at the numpy.asmatrix() function, walking you through its usage with four progressively advanced examples. By the end, you’ll have a solid understanding of how to use asmatrix() in your Python projects.

Syntax & Parameters

Syntax:

numpy.asmatrix(data, dtype=None)

Parameters:

  • data: Input data. This can be a sequence, a nested sequence, or another array-like object.
  • dtype: Data type of the output matrix. If not specified, the data type is inferred from the input data.

Returns:

  • A matrix representation of the input data.

Example 1: Basic Usage of numpy.asmatrix()

import numpy as np

# Create a numpy array
arr = np.array([[1, 2], [3, 4]])

# Convert array to matrix
mat = np.asmatrix(arr)

print(mat)

Output:

[[1 2]
 [3 4]]

The numpy.asmatrix() function converts the given array into a matrix. This is especially useful when you need to perform linear algebra operations, which are more naturally expressed with matrices.

Example 2: Modifying the Original Array

import numpy as np

arr = np.array([[5, 6], [7, 8]])
mat = np.asmatrix(arr)

# Modify the original array
arr[0, 0] = 10

print(mat)

Output:

[[10  6]
 [ 7  8]]

When you modify the original array after converting it to a matrix with numpy.asmatrix(), the changes will reflect in the matrix. This is because asmatrix() does not create a deep copy but rather returns a view of the input array as a matrix.

Example 3: Performing Matrix Operations

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

A_mat = np.asmatrix(A)
B_mat = np.asmatrix(B)

# Perform matrix multiplication
result = A_mat * B_mat

print(result)

Output:

[[19 22]
 [43 50]]

This example illustrates how to perform matrix operations, specifically matrix multiplication, using asmatrix(). Matrices support direct multiplication using the * operator, adhering to the rules of linear algebra, unlike ndarray which would use the @ operator or the dot() function for such operations.

Example 4: Working with Slices

import numpy as np

arr = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
mat = np.asmatrix(arr)

# Creating a submatrix by slicing
sub_matrix = mat[:2, 1:3]

print(sub_matrix)

Output:

[[8 7]
 [5 4]]

When slicing a matrix obtained with numpy.asmatrix(), the result is also a matrix. This feature is beneficial when working with parts of a larger matrix, as it allows you to perform matrix operations on the slices directly.

Conclusion

In this tutorial, we explored the numpy.asmatrix() function and its utility through four examples. Understanding how to use this function effectively can enhance your data manipulation capabilities, especially when dealing with linear algebra operations. Remember, the choice between using arrays and matrices depends on the requirements of your project and your preference for certain mathematical conventions.