gpt4 book ai didi

python - 通过求解普鲁斯特问题的线性投影矩阵

转载 作者:行者123 更新时间:2023-12-01 07:19:10 26 4
gpt4 key购买 nike

我有两个矩阵:

target = np.array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3]])

source = np.array([[11, 11, 11, 11, 11],
[22, 22, 22, 22, 22],
[33, 33, 33, 33, 33]])

我想创建一个转换矩阵,将矩阵投影到目标矩阵。

我发现 Scipy 库提供了一个函数来做到这一点:

from scipy.spatial import procrustes
mtx1, mtx2, disparity = procrustes(target, source)

基于 documentation ,它说:

enter image description here

因此,mtx2 是投影矩阵。

如果我有另一个数据,并且我想使用 Scipy 用于将矩阵投影到目标矩阵的“学习变换矩阵”将它们投影到目标矩阵,该怎么办? 目标一个?

如何使用 Scipy 做到这一点?

最佳答案

您需要修改该函数才能返回转换矩阵 (R)。

删除注释后的源代码如下所示:

def procrustes(data1, data2):
mtx1 = np.array(data1, dtype=np.double, copy=True)
mtx2 = np.array(data2, dtype=np.double, copy=True)

if mtx1.ndim != 2 or mtx2.ndim != 2:
raise ValueError("Input matrices must be two-dimensional")
if mtx1.shape != mtx2.shape:
raise ValueError("Input matrices must be of same shape")
if mtx1.size == 0:
raise ValueError("Input matrices must be >0 rows and >0 cols")

# translate all the data to the origin
mtx1 -= np.mean(mtx1, 0)
mtx2 -= np.mean(mtx2, 0)

norm1 = np.linalg.norm(mtx1)
norm2 = np.linalg.norm(mtx2)

if norm1 == 0 or norm2 == 0:
raise ValueError("Input matrices must contain >1 unique points")

# change scaling of data (in rows) such that trace(mtx*mtx') = 1
mtx1 /= norm1
mtx2 /= norm2

# transform mtx2 to minimize disparity
R, s = orthogonal_procrustes(mtx1, mtx2)
mtx2 = np.dot(mtx2, R.T) * s # HERE, the projected mtx2 is estimated.

# measure the dissimilarity between the two datasets
disparity = np.sum(np.square(mtx1 - mtx2))

return mtx1, mtx2, disparity, R

来源:https://github.com/scipy/scipy/blob/v1.3.0/scipy/spatial/_procrustes.py#L17-L132

关于python - 通过求解普鲁斯特问题的线性投影矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com