解决numpy / scipy中的3D最小二乘

时间:2018-09-21 08:22:47

标签: python numpy scipy linear-algebra least-squares

对于大约100的整数K,我有2 * K var remoteCode = 'http://remote-site.com/test.js'; //location of your remote file var script = app.doScript("do shell script \"curl 'remoteCode'\"".replace("remoteCode", remoteCode), ScriptLanguage.APPLESCRIPT_LANGUAGE); // Set script args app.scriptArgs.setValue("scriptArg1", "Hello"); // Set environmental vars $.setenv("envVar1", "World"); // Run the "remote" script app.doScript(script, ScriptLanguage.JAVASCRIPT); 个数组:(n, n)X_1, ..., X_K

我想同时执行K个最小二乘,即找到n×n的矩阵Y_1, ..., Y_K最小化k的平方和:A\sum_k norm(Y_k - A.dot(X_k), ord='fro') ** 2不能依赖于{{ 1}})。

我正在寻找使用numpy或scipy实现此目的的简便方法。 我知道我要最小化的函数是A中的二次形式,所以我可以手工完成,但是我正在寻找一种现成的方法。有一个吗?

3 个答案:

答案 0 :(得分:0)

如果passwordMatchValidator(this.formGroup.get('password'),this.formGroup.get('confirmPassword'))很小,类似的事情就会起作用。

n

注意:import numpy as np from scipy.optimize import minimize K = 5 n = 10 X = np.random.random_sample((K, n, n)) Y = np.random.random_sample((K, n, n)) def opt(A): A = np.reshape(A, (n, n)) # maybe need to transpose X.dot(a) ? # if axis is a 2-tuple, it specifies the axes that hold 2-D matrices, # and the matrix norms of these matrices are computed. return np.sum(np.linalg.norm(Y - X.dot(A), ord='fro', axis=(1, 2)) ** 2.0) A_init = np.random.random_sample((n, n)) print(minimize(opt, A_init )) 默认使用的优化算法是本地的。

答案 1 :(得分:0)

我无法帮助python,但是这里是数学解决方案,以防万一。 我们力求最小化

E = Sum { Tr (Y[j]-A*X[j])*(Y[j]-A*X[j])'}

一些代数的结果

E = Tr(P-A*Q'-Q*A'+A*R*A')
where
P = Sum{ Y[j]*Y[j]'}
Q = Sum{ Y[j]*X[j]'}
R = Sum{ X[j]*X[j]'}

如果R是可逆的,则代数产生更多

E = Tr( (A-Q*S)*R*(A-Q*S)') + Tr( P - Q*S*Q')
where S = inv( R)

(A-Q*S)*R*(A-Q*S)' is positive definite, 

我们通过取A = Q * S来最小化E。

在这种情况下,算法将是:

compute Q
compute R
solve A*R = Q for A (eg by finding the cholesky factors of R)

如果R是不可逆的,我们应该对S使用广义逆,而不是普通逆。

答案 2 :(得分:0)

事实上,答案很简单,我只需要通过水平堆叠Y_k(创建Y)和X_k(创建X)来创建更大的矩阵Y和X。然后,我可以解决一个常规的二维最小二乘问题:最小化norm(Y - A.dot(X))