Numpy的'linalg.solve'和'linalg.lstsq'没有给出与Matlab的'\'或mldivide相同的答案

KMi*_*ist 11 python matlab numpy

我正在尝试在Python上实现最小二乘曲线拟合算法,已经在Matlab上编写了它.但是,我无法获得正确的变换矩阵,问题似乎在解决步骤中发生.(编辑:我的变换矩阵使用Matlab非常准确,但完全不适用于Python.)

我在网上查看了很多资料,他们都表示要翻译Matlab的'mldivide',如果矩阵是正方形和非奇异的,你必须使用'np.linalg.solve',否则你必须使用'np.linalg.lstsq'.但我的结果并不匹配.

问题是什么?如果它与函数的实现有关,那么mumpivide在numpy中的正确翻译是什么?

我已附上以下两个版本的代码.除了解决部分之外,它们本质上是完全相同的实现.

Matlab代码:

%% Least Squares Fit

clear, clc, close all

% Calibration Data
scr_calib_pts = [0,0; 900,900; -900,900; 900,-900; -900,-900];
cam_calib_pts = [-1,-1; 44,44; -46,44; 44,-46; -46,-46];
cam_x = cam_calib_pts(:,1);
cam_y = cam_calib_pts(:,2);

% Least Squares Fitting
A_matrix = [];
for i = 1:length(cam_x)
    A_matrix = [A_matrix;1, cam_x(i), cam_y(i), ...
        cam_x(i)*cam_y(i), cam_x(i)^2, cam_y(i)^2];
end
A_star = A_matrix'*A_matrix
B_star = A_matrix'*scr_calib_pts
transform_matrix = mldivide(A_star,B_star)

% Testing Data
test_scr_vec = [200,400; 1600,400; -1520,1740; 1300,-1800; -20,-1600];
test_cam_vec = [10,20; 80,20; -76,87; 65,-90; -1,-80];
test_cam_x = test_cam_vec(:,1);
test_cam_y = test_cam_vec(:,2);

% Coefficients for Transform
coefficients = [];
for i = 1:length(test_cam_x)
    coefficients = [coefficients;1, test_cam_x(i), test_cam_y(i), ...
        test_cam_x(i)*test_cam_y(i), test_cam_x(i)^2, test_cam_y(i)^2];
end

% Mapped Points
results = coefficients*transform_matrix;

% Plotting
test_scr_x = test_scr_vec(:,1)';
test_scr_y = test_scr_vec(:,2)';
results_x = results(:,1)';
results_y = results(:,2)';

figure
hold on
load seamount
s = 50;
scatter(test_scr_x, test_scr_y, s, 'r')
scatter(results_x, results_y, s)
Run Code Online (Sandbox Code Playgroud)

Python代码:

# Least Squares fit

import numpy as np
import matplotlib.pyplot as plt

# Calibration data
camera_vectors = np.array([[-1,-1], [44,44], [-46,44], [44,-46], [-46,-46]])
screen_vectors = np.array([[0,0], [900,900], [-900,900], [900,-900], [-900,-900]])

# Separate axes
cam_x = np.array([i[0] for i in camera_vectors])
cam_y = np.array([i[1] for i in camera_vectors])

# Initiate least squares implementation
A_matrix = []
for i in range(len(cam_x)):
    new_row = [1, cam_x[i], cam_y[i], \
        cam_x[i]*cam_y[i], cam_x[i]**2, cam_y[i]**2]
    A_matrix.append(new_row)
A_matrix = np.array(A_matrix)
A_star = np.transpose(A_matrix).dot(A_matrix)

B_star = np.transpose(A_matrix).dot(screen_vectors)

print A_star
print B_star

try:
    # Solve version (Implemented)
    transform_matrix = np.linalg.solve(A_star,B_star)
    print "Solve version"
    print transform_matrix
except:
    # Least squares version (implemented)
    transform_matrix = np.linalg.lstsq(A_star,B_star)[0]
    print "Least Squares Version"
    print transform_matrix


# Test data
test_cam_vec = np.array([[10,20], [80,20], [-76,87], [65,-90], [-1,-80]])
test_scr_vec = np.array([[200,400], [1600,400], [-1520,1740], [1300,-1800], [-20,-1600]])

# Coefficients of quadratic equation
test_cam_x = np.array([i[0] for i in test_cam_vec])
test_cam_y = np.array([i[1] for i in test_cam_vec])    
coefficients = []
for i in range(len(test_cam_x)):
    new_row = [1, test_cam_x[i], test_cam_y[i], \
        test_cam_x[i]*test_cam_y[i], test_cam_x[i]**2, test_cam_y[i]**2]
    coefficients.append(new_row)
coefficients = np.array(coefficients)

# Transform camera coordinates to screen coordinates
results = coefficients.dot(transform_matrix)

# Plot points
results_x = [i[0] for i in results]
results_y = [i[1] for i in results]
actual_x = [i[0] for i in test_scr_vec]
actual_y = [i[1] for i in test_scr_vec]

plt.plot(results_x, results_y, 'gs', actual_x, actual_y, 'ro')
plt.show()
Run Code Online (Sandbox Code Playgroud)

编辑(根据建议):

# Transform matrix with linalg.solve

[[  2.00000000e+01   2.00000000e+01]
 [ -5.32857143e+01   7.31428571e+01]
 [  7.32857143e+01  -5.31428571e+01]
 [ -1.15404203e-17   9.76497106e-18]
 [ -3.66428571e+01   3.65714286e+01]
 [  3.66428571e+01  -3.65714286e+01]]

# Transform matrix with linalg.lstsq:

[[  2.00000000e+01   2.00000000e+01]
 [  1.20000000e+01   8.00000000e+00]
 [  8.00000000e+00   1.20000000e+01]
 [  1.79196935e-15   2.33146835e-15]
 [ -4.00000000e+00   4.00000000e+00]
 [  4.00000000e+00  -4.00000000e+00]]

% Transform matrix with mldivide:

   20.0000   20.0000
   19.9998    0.0002
    0.0002   19.9998
         0         0
   -0.0001    0.0001
    0.0001   -0.0001
Run Code Online (Sandbox Code Playgroud)

DrV*_*DrV 21

有趣的是,你会得到与完全不同的结果np.linalg.lstsqnp.linalg.solve.

x1 = np.linalg.lstsq(A_star, B_star)[0]
x2 = np.linalg.solve(A_star, B_star)
Run Code Online (Sandbox Code Playgroud)

两者都应该为方程Ax = B提供解决方案.但是,它们给出了两个完全不同的数组:

In [37]: x1    
Out[37]: 
array([[  2.00000000e+01,   2.00000000e+01],
       [  1.20000000e+01,   7.99999999e+00],
       [  8.00000001e+00,   1.20000000e+01],
       [ -1.15359111e-15,   7.94503352e-16],
       [ -4.00000001e+00,   3.99999999e+00],
       [  4.00000001e+00,  -3.99999999e+00]]


In [39]: x2
Out[39]: 
array([[  2.00000000e+01,   2.00000000e+01],
       [ -4.42857143e+00,   2.43809524e+01],
       [  2.44285714e+01,  -4.38095238e+00],
       [ -2.88620104e-18,   1.33158696e-18],
       [ -1.22142857e+01,   1.21904762e+01],
       [  1.22142857e+01,  -1.21904762e+01]])
Run Code Online (Sandbox Code Playgroud)

两者都应该给出线性方程组的准确(低到计算精度)解,对于非奇异矩阵,只有一个解.

那肯定是错的.让我们看看这两个候选人是否可以成为原始等式的解决方案:

In [41]: A_star.dot(x1)
Out[41]: 
array([[ -1.11249392e-08,   9.86256055e-09],
       [  1.62000000e+05,  -1.65891834e-09],
       [  0.00000000e+00,   1.62000000e+05],
       [ -1.62000000e+05,  -1.62000000e+05],
       [ -3.24000000e+05,   4.47034836e-08],
       [  5.21540642e-08,  -3.24000000e+05]])

In [42]: A_star.dot(x2)
Out[42]: 
array([[ -1.45519152e-11,   1.45519152e-11],
       [  1.62000000e+05,  -1.45519152e-11],
       [  0.00000000e+00,   1.62000000e+05],
       [ -1.62000000e+05,  -1.62000000e+05],
       [ -3.24000000e+05,   0.00000000e+00],
       [  2.98023224e-08,  -3.24000000e+05]])
Run Code Online (Sandbox Code Playgroud)

他们似乎给出了相同的解决方案,基本上与B_star应有的解决方案相同.这引导我们解释.使用简单的线性代数,我们可以预测A.(x1-x2)应该非常接近于零:

In [43]: A_star.dot(x1-x2)
Out[43]: 
array([[ -1.11176632e-08,   9.85164661e-09],
       [ -1.06228981e-09,  -1.60071068e-09],
       [  0.00000000e+00,  -2.03726813e-10],
       [ -6.72298484e-09,   4.94765118e-09],
       [  5.96046448e-08,   5.96046448e-08],
       [  2.98023224e-08,   5.96046448e-08]])
Run Code Online (Sandbox Code Playgroud)

确实如此.因此,似乎存在方程Ax = 0的非平凡解,解为x = x1-x2,这意味着矩阵是单数的,因此对于Ax = B存在无限多个不同的解.

因此问题不在于NumPy或Matlab,而是在矩阵本身.


但是,在这种矩阵的情况下,情况有点棘手.A_star上面的定义似乎是单数的(对于x <> 0,Ax = 0).另一方面,它的行列式是非零的,并不是单数.

在这种情况下A_star是矩阵的一个例子,该矩阵在数值上不稳定而不是单数.该solve方法通过使用简单的乘法逆来解决它.在这种情况下,这是一个糟糕的选择,因为反转包含非常大和非常小的值.这使得乘法容易出现舍入误差.通过查看矩阵的条件数可以看出:

In [65]: cond(A_star)
Out[65]: 1.3817810855559592e+17
Run Code Online (Sandbox Code Playgroud)

这是一个非常高的条件数,矩阵是病态的.

在这种情况下,使用逆来解决问题是一种糟糕的方法.您可能会看到,最小二乘方法可以提供更好的结果.但是,更好的解决方案是重新调整输入值,使x和x ^ 2处于相同的范围内.一个非常好的缩放是缩放-1和1之间的所有内容.


您可能会考虑的一件事是尝试使用NumPy的索引功能.例如:

cam_x = np.array([i[0] for i in camera_vectors])
Run Code Online (Sandbox Code Playgroud)

相当于:

cam_x = camera_vectors[:,0]
Run Code Online (Sandbox Code Playgroud)

你可以用A这种方式构建你的数组:

A_matrix = np.column_stack((np.ones_like(cam_x), cam_x, cam_y, cam_x*cam_y, cam_x**2, cam_y**2))
Run Code Online (Sandbox Code Playgroud)

无需创建列表列表或任何循环.


小智 5

矩阵A_matrix是6 x 5矩阵,因此A_star是奇异矩阵。结果,没有唯一的解决方案,并且两个程序的结果都是正确的。这对应于不确定性高的原始问题,而不是确定性高的问题。