Scipy Newton Krylov期望平方矩阵 [英] Scipy Newton Krylov Expects Square Matrix

查看:174
本文介绍了Scipy Newton Krylov期望平方矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scipy.optimize.newton_krylov()解决最小二乘优化问题,即找到x使得(Ax-b)** 2 =0.我的理解是A必须与m> n,b必须是mx1,x将是nx1.当我尝试运行优化时,出现错误:

I am trying to use scipy.optimize.newton_krylov() to solve a least-squares optimization problem, i.e. finding x such that (Ax - b)**2 = 0. My understanding is that A has to be mxn with m>n, b has to be mx1, and x will be nx1. When I try to run the optimization, I get an error:

ValueError: expected square matrix, but got shape=(40, 6)

大概这个错误与雅可比行列式的计算有关,而不是我的输入矩阵A?但是,如果是这样,如何更改为函数提供的值以解决此问题?任何建议将不胜感激.

Presumably this error concerns the computation of the Jacobian and not my input matrix A? But if so, how can I change the values I am providing to the functions to resolve this problem? Any advice would be appreciated.

以下代码重现该错误:

import numpy as np
from scipy.optimize import newton_krylov

A = np.random.uniform(0, 1, (40,6))
b = np.arange(40)
x0 = np.ones(6)

def F(x):
    return (A.dot(x) - b)**2

x = newton_krylov(F, np.ones(6))

推荐答案

作为 newton_krylov 的文档字符串说明,它找到函数F(x)的根.函数F必须接受一维数组,并返回与输入大小相同的一维数组.例如,如果x的长度为3,则F(x)必须返回长度为3的数组.在这种情况下,newton_krylov尝试求解F(x) = [0, 0, 0].

As the docstring of newton_krylov explains, it finds a root of a function F(x). The function F must accept a one-dimensional array, and return a one-dimensional array of the same size as the input. If, for example, x has length 3, F(x) must return an array with length 3. In that case, newton_krylov attempts to solve F(x) = [0, 0, 0].

您得到的错误是newton_krylov尝试使用数值计算的 Jacobian矩阵<F的/a>带有一个函数,该函数期望矩阵为正方形.您的函数F具有形状为(40,6)的雅可比矩阵,因为输入的长度为6,输出的长度为40.

The error that you got is the result of newton_krylov attempting to use the numerically computed Jacobian matrix of F with a function that expects the matrix to be square. Your function F has a Jacobian matrix with shape (40, 6), because the input has length 6 and the output has length 40.

就其本身而言,newton_krylov不是用于解决最小二乘问题的正确函数.最小二乘问题是最小化问题,而不是寻根问题. (诸如newton_krylov之类的求解器可能用于实现最小化算法,但我认为您对使用现有解决方案感兴趣,而不是编写自己的解决方案.)

By itself, newton_krylov is not the right function to use for solving a least-squares problem. A least-squares problem is a minimization problem, not a root-finding problem. (A solver such as newton_krylov might be used to implement a minimization algorithm, but I assume you are interested in using an existing solution rather than writing your own.)

您说您想解决最小二乘问题,但是然后您说即找到x使得(Ax-b)** 2 = 0".我认为这只是您描述中的草率,因为这不是最小二乘问题.最小二乘问题是找到x以使sum((Ax - b)**2)最小化. (通常,不会有一个x使得平方和等于为零.)

You say you want to solve a least-squares problem, but then you say "i.e. finding x such that (Ax - b)**2 = 0." I assume that was just a bit a sloppiness in your description, because that is not the least-squares problem. The least-squares problem is to find x such that sum((Ax - b)**2) is minimized. (In general, there won't be an x that makes the sum of squares equal to zero.)

因此,假设您确实想找到x以使sum((Ax - b)**2)最小化,则可以使用

So, assuming you really want to find x such that sum((Ax - b)**2) is minimized, you can use scipy.linalg.lstsq.

例如:

In [54]: from scipy.linalg import lstsq

In [55]: A = np.random.uniform(0, 1, (40,6))

In [56]: b = np.arange(40)

In [57]: x, res, rank, s = lstsq(A, b)

In [58]: x
Out[58]: 
array([  5.07513787,   1.83858547,  18.07818853,   9.28805475,
         6.13019155,  -0.7045539 ])

这篇关于Scipy Newton Krylov期望平方矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆