
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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]]