SciPy - orthogonal_procrustes() Function



The orthogonal_procrustes() method looks at two sets of data (A and B) and figures out how to turn, flip, and change the size of A to make it match B as as it can. If one set of data is bigger or smaller than the other, this method also works out the best way to adjust the size to make the two sets line up.

This method comes in handy when you need to compare or line up two sets of data in geometric or statistical spaces. It makes sure that when you change the data, you keep its original structure. This makes it useful for things like analyzing shapes working with images, and lining up features.

In the real world, people use the orthogonal_procrustes() method in areas like face recognition where it's key to line up facial features across different pictures. It's also used in natural language processing to map word embeddings from one language to another. It's an important tool to keep data consistent and lined up in many different fields.

In addition to using the orthogonal_procrustes(A, B) method directly, the transformation matrix R obtained from it can also be applied with the @ operator (matrix multiplication) to align one dataset to another.

Syntax

The syntax for the Scipy method is as follows −

.orthogonal_procrustes(A, B, check_finite=True)

Parameters

This method accepts the following parameters −

  • A − Input matrix of shape m,n, the source matrix

  • B − Input matrix of shape m,n, the target matrix

  • check_finite (boolean, optional) − If true, it checks the input matrices contain only finite numbers. Setting to False can improve performance for large datasets but may lead to issues if inputs contain NaN or inf.

Return Value

The orthogonal_procrustes() method returns the following −

  • R (ndarray, shape(n,n)) − The optimal orthogonal transformation matrix.

  • scale (float) − The optimal scaling factor.

Example 1

This example demonstrates aligning one matrix A to another B using the orthogonal_procrustes() method.

from scipy.linalg import orthogonal_procrustes
import numpy as np

# Define two matrices (3 rows and 2 columns)
A = np.array([[1, 0], [0, 1], [1, 1]])  # Shape (3, 2)
B = np.array([[2, 1], [-1, 2], [3, 4]])  # Shape (3, 2)

R, scale = orthogonal_procrustes(A, B)
transformed_A = A @ R  # Transformation
print("Optimal Orthogonal Transformation Matrix R:",R)
print("\nOptimal Scaling Factor:", scale)
print("\nTransformed A Matrix:",transformed_A)
print("\nTarget B Matrix:",print(B))

When we run above program, it produces following result

Optimal Orthogonal Transformation Matrix R:
[[ 0.96476382  0.26311741]
 [-0.26311741  0.96476382]]

Optimal Scaling Factor: 11.401754250991381

Transformed A Matrix:
[[ 0.96476382  0.26311741]
 [-0.26311741  0.96476382]
 [ 0.70164642  1.22788123]]

Target B Matrix:
[[ 2  1]
 [-1  2]
 [ 3  4]]

Example

This example demonstrates aligning A to B using the transformation matrix R.

from scipy.linalg import orthogonal_procrustes
import numpy as np

# Define matrices
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[2, 3], [6, 7], [10, 11]])

# Calculate the orthogonal transformation
R, scale = orthogonal_procrustes(A, B)

# Apply the transformation
transformed_A = A @ R

print("Transformed A Matrix:")
print(transformed_A)
print("\nTarget B Matrix:")
print(B)

Following is an output of the above code

Transformed A Matrix (with scaling):
[[1.10433584 1.94433597]
 [3.20727323 3.8358048 ]
 [5.31021062 5.72727362]]

Target B Matrix:
[[ 2  3]
 [ 6  7]
 [10 11]]
scipy_linalg.htm
Advertisements