vectorspace_orthonormalization:将一组线性独立的向量转换为一组正交归一基向量
一个将一组线性无关向量转换为一组正交归一基向量的函数。
> 来自 mlxtend.math 的 vectorspace_orthonormalization
概述
vectorspace_orthonormalization
函数使用 Gram-Schmidt 过程 [1] 将一组线性独立的向量转换为一组正交标准基向量。
参考文献
- [1] https://zh.wikipedia.org/wiki/Gram%E2%80%93Schmidt%E8%BF%90%E7%BB%B4
示例 1 - 将一组向量转换为正交标准基底
请注意,要将一组线性无关的向量转换为一组正交归一基向量,vectorspace_orthonormalization
函数期望这些向量以矩阵的列形式排列(这里是 NumPy 数组)。请记住,vectorspace_orthonormalization
函数也适用于非线性独立的向量集;然而,结果向量集将不会是正交归一的。检查输入集中所有向量是否线性独立的一个简单方法是使用 numpy.linalg.det
(行列式)函数。
import numpy as np
from mlxtend.math import vectorspace_orthonormalization
a = np.array([[2, 0, 4, 12],
[0, 2, 16, 4],
[4, 16, 6, 2],
[2, -12, 4, 6]])
s = ''
if np.linalg.det(a) == 0.0:
s = ' not'
print('Input vectors are%s linearly independent' % s)
vectorspace_orthonormalization(a)
Input vectors are linearly independent
array([[ 0.40824829, -0.1814885 , 0.04982278, 0.89325973],
[ 0. , 0.1088931 , 0.99349591, -0.03328918],
[ 0.81649658, 0.50816781, -0.06462163, -0.26631346],
[ 0.40824829, -0.83484711, 0.07942048, -0.36063281]])
请注意,按比例相同地缩放输入值应该不会改变结果:
vectorspace_orthonormalization(a/2)
array([[ 0.40824829, -0.1814885 , 0.04982278, 0.89325973],
[ 0. , 0.1088931 , 0.99349591, -0.03328918],
[ 0.81649658, 0.50816781, -0.06462163, -0.26631346],
[ 0.40824829, -0.83484711, 0.07942048, -0.36063281]])
但是,在发生线性依赖的情况下(如下例中,第二列是第一列的线性组合),依赖向量之一的向量元素将变为零。(对于一对线性依赖向量,列索引较大的那个将被置为零。)
a[:, 1] = a[:, 0] * 2
vectorspace_orthonormalization(a)
array([[ 0.40824829, 0. , 0.04155858, 0.82364839],
[ 0. , 0. , 0.99740596, -0.06501108],
[ 0.81649658, 0. , -0.04155858, -0.52008861],
[ 0.40824829, 0. , 0.04155858, 0.21652883]])
API
vectorspace_orthonormalization(ary, eps=1e-13)
Transforms a set of column vectors to a orthonormal basis.
Given a set of orthogonal vectors, this functions converts such column vectors, arranged in a matrix, into orthonormal basis vectors.
Parameters
-
ary
: array-like, shape=[num_vectors, num_vectors]An orthogonal set of vectors (arranged as columns in a matrix)
-
eps
: float (default: 1e-13)A small tolerance value to determine whether the vector norm is zero or not.
Returns
-
arr
: array-like, shape=[num_vectors, num_vectors]An orthonormal set of vectors (arranged as columns)
Examples
For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/math/vectorspace_orthonormalization/