我有两组向量 x_i\in R^n 和 z_i\in R^m
我想找到一个变换矩阵W 使得 W x_i 近似于 z_i,
即我想找到最小化的 W:sum_i || W x_i − z_i ||^2
是否有执行此操作的 Python 函数?
最佳答案
使用 this kronecker product identity它成为一个经典的线性回归问题。但即使没有它,它也只是线性回归设置的转置。
import numpy as np
m, n = 3, 4
N = 100 # num samples
rng = np.random.RandomState(42)
W = rng.randn(m, n)
X = rng.randn(n, N)
Z_clean = W.dot(X)
Z = Z_clean + rng.randn(*Z_clean.shape) * .001
使用 Z
和 X
我们可以通过求解 argmin_W ||X^T W^T - Z^T||^2 来估计 W
W_est = np.linalg.pinv(X.T).dot(Z.T).T
from numpy.testing import assert_array_almost_equal
assert_array_almost_equal(W, W_est, decimal=3)
关于python - 在 python 中拟合线性变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980159/