栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用NumPy进行分析?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用NumPy进行分析?

我不知道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    tforma 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636900.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号