当前位置: 首页 > 面试题库 >

使用NumPy进行分析?

上官波鸿
2023-03-14
问题内容

procrustesNumPy /
SciPy或相关库中是否有类似Matlab的函数?

以供参考。Procrustes分析的目的是对齐2组点(换句话说,是2个形状),以通过消除缩放,平移和旋转扭曲分量来最小化它们之间的平方距离。

Matlab中的示例:

X = [0 1; 2 3; 4 5; 6 7; 8 9];   % first shape
R = [1 2; 2 1];                  % rotation matrix
t = [3 5];                       % translation vector
Y = X * R + repmat(t, 5, 1);     % warped shape, no scale and no distortion
[d Z] = procrustes(X, Y);        % Z is Y aligned back to X
Z

Z =

  0.0000    1.0000
  2.0000    3.0000
  4.0000    5.0000
  6.0000    7.0000
  8.0000    9.0000

NumPy中的相同任务:

X = arange(10).reshape((5, 2))
R = array([[1, 2], [2, 1]])
t = array([3, 5])
Y = dot(X, R) + t
Z = ???

注意:我只对对齐的形状感兴趣,因为平方误差(d在Matlab代码中可变)很容易从2个形状中计算出来。


问题答案:

我不知道Python中是否存在任何预先存在的实现,但是很容易查看使用的MATLAB代码edit procrustes.m并将其移植到Numpy:

def procrustes(X, Y, scaling=True, reflection='best'):
    """
    A port of MATLAB's `procrustes` function to Numpy.

    Procrustes analysis determines a linear transformation (translation,
    reflection, orthogonal rotation and scaling) of the points in Y to best
    conform them to the points in matrix X, using the sum of squared errors
    as the goodness of fit criterion.

        d, Z, [tform] = procrustes(X, Y)

    Inputs:
    ------------
    X, Y    
        matrices of target and input coordinates. they must have equal
        numbers of  points (rows), but Y may have fewer dimensions
        (columns) than X.

    scaling 
        if False, the scaling component of the transformation is forced
        to 1

    reflection
        if 'best' (default), the transformation solution may or may not
        include a reflection component, depending on which fits the data
        best. setting reflection to True or False forces a solution with
        reflection or no reflection respectively.

    Outputs
    ------------
    d       
        the residual sum of squared errors, normalized according to a
        measure of the scale of X, ((X - X.mean(0))**2).sum()

    Z
        the matrix of transformed Y-values

    tform   
        a dict specifying the rotation, translation and scaling that
        maps X --> Y

    """

    n,m = X.shape
    ny,my = Y.shape

    muX = X.mean(0)
    muY = Y.mean(0)

    X0 = X - muX
    Y0 = Y - muY

    ssX = (X0**2.).sum()
    ssY = (Y0**2.).sum()

    # centred Frobenius norm
    normX = np.sqrt(ssX)
    normY = np.sqrt(ssY)

    # scale to equal (unit) norm
    X0 /= normX
    Y0 /= normY

    if my < m:
        Y0 = np.concatenate((Y0, np.zeros(n, m-my)),0)

    # optimum rotation matrix of Y
    A = np.dot(X0.T, Y0)
    U,s,Vt = np.linalg.svd(A,full_matrices=False)
    V = Vt.T
    T = np.dot(V, U.T)

    if reflection is not 'best':

        # does the current solution use a reflection?
        have_reflection = np.linalg.det(T) < 0

        # if that's not what was specified, force another reflection
        if reflection != have_reflection:
            V[:,-1] *= -1
            s[-1] *= -1
            T = np.dot(V, U.T)

    traceTA = s.sum()

    if scaling:

        # optimum scaling of Y
        b = traceTA * normX / normY

        # standarised distance between X and b*Y*T + c
        d = 1 - traceTA**2

        # transformed coords
        Z = normX*traceTA*np.dot(Y0, T) + muX

    else:
        b = 1
        d = 1 + ssY/ssX - 2 * traceTA * normY / normX
        Z = normY*np.dot(Y0, T) + muX

    # transformation matrix
    if my < m:
        T = T[:my,:]
    c = muX - b*np.dot(muY, T)

    #transformation values 
    tform = {'rotation':T, 'scale':b, 'translation':c}

    return d, Z, tform


 类似资料:
  • 在我正在开发的应用程序中,我需要知道一串单词是否是名词短语、动词短语等。我知道NP和VP既不是依赖项,也不是位置。我也知道要做到这一点,我可能需要某种分块工具,但我找不到任何开源工具。 在SyntaxNet输出的“她真的很喜欢可爱的黑狗”一句中: 我注意到NP“可爱的黑狗”已经放在了自己的树节点中: 所以我想知道是否有任何方法可以将SyntaxNet用作chunker?

  • 我正在使用我有一个类,如下所示: 现在我想做的是: 筛选出senderId无效的记录(使用映射) 下面是我的代码: 这给我带来了一个错误: 错误:(105,90)java:找不到适用于groupingBy(共享[…]的方法gMode,java。util。作用函数)方法java。util。流动收藏家。groupingBy(java.util.function.function)不适用(无法推断类型变

  • 本文向大家介绍使用Python Pandas进行数据分析,包括了使用Python Pandas进行数据分析的使用技巧和注意事项,需要的朋友参考一下 在本教程中,我们将看到使用Python pandas库进行的数据分析。图书馆的熊猫都是用C语言编写的。因此,我们在速度上没有任何问题。它以数据分析而闻名。我们在熊猫中有两种类型的数据存储结构。它们是Series和DataFrame。让我们一一看。 1.

  • 在这篇文章我们看到了如何使用iNalyzer对iOS应用进行静态分析。本文我们将看看如何用iNalyer对iOS应用进行运行时分析。我们能够在运行时调用方法,能够在应用的某个特殊时间找出特定实例变量的值,基本上能做我们用Cycript做的所有事情。 在这篇文章当中,我们成功的用Doxygen生成了html文件,并且打开它看到了关于这个应用的类信息和其他信息。我们将使用Firefox浏览器进行运行时

  • iNalyzer允许我们查看类信息,执行运行时分析和其他一些事情。基本上它把解密应用、导出类信息这些事情自动化了,并且更好的展示了出来。我们也可以像Cycript那样挂钩运行的进程。iNalyzer由AppSec Labs开发和维护,它的官方地址在这。iNalyzer同时也已经开源了,gitub地址在这。 在用iNalyzer之前,有些依赖的软件需要先安装。请确保Graphviz 和Doxygen

  • 问题内容: 我想使用itext生成pdf。我会在某些时候添加内容以进行分页。我需要插入几个单独的conenidos依赖源,所以我要求用户在单独的页面上插入。有任何想法吗??? 问题答案: 调用告诉iText将后续对象放置在新页面上。仅当您放置下一个对象时,才会真正创建新页面。另外,仅在当前页面不为空白时创建一个新页面;否则,仅创建一个新页面。否则,它将被忽略;您可以用来克服这一点。 请参见下面的链