Mostafa
Mostafa

Reputation: 1551

Fit a linear transformation in python

I have two sets of vectors x_i \in R^n and z_i \in R^m

I want to find a transformation matrix W such that W x_i approximates z_i,

i.e. I want to find W that minimizes: sum_i || W x_i − z_i ||^2

Is there a Python function that does this?

Upvotes: 5

Views: 4654

Answers (1)

eickenberg
eickenberg

Reputation: 14377

Using this kronecker product identity it becomes a classic linear regression problem. But even without that, it is just the transpose of a linear regression setup.

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

Using Z and X we can estimate W by solving argmin_W ||X^T W^T - Z^T||^2

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)

Upvotes: 4

Related Questions