使用numpy在python中执行varimax旋转

时间:2013-07-13 08:55:48

标签: python arrays numpy

我正在研究矩阵的主成分分析。我已经找到了如下所示的组件矩阵

A = np.array([[-0.73465832 -0.24819766 -0.32045055]
              [-0.3728976   0.58628043 -0.63433607]
              [-0.72617152  0.53812819 -0.22846634]
              [ 0.34042864 -0.08063226 -0.80064174]
              [ 0.8804307   0.17166265  0.04381426]
              [-0.66313032  0.54576874  0.37964986]
              [ 0.286712    0.68305196  0.21769803]
              [ 0.94651412  0.14986739 -0.06825887]
              [ 0.40699665  0.73202276 -0.08462949]])

我需要在此组件矩阵中执行varimax旋转,但无法找到旋转的确切方法和程度。大多数示例都显示在R.但是我需要python中的方法。

3 个答案:

答案 0 :(得分:10)

你可以在Python中找到很多例子。以下是我在Wikipedia上仅使用numpy找到的Python示例:

def varimax(Phi, gamma = 1, q = 20, tol = 1e-6):
    from numpy import eye, asarray, dot, sum, diag
    from numpy.linalg import svd
    p,k = Phi.shape
    R = eye(k)
    d=0
    for i in xrange(q):
        d_old = d
        Lambda = dot(Phi, R)
        u,s,vh = svd(dot(Phi.T,asarray(Lambda)**3 - (gamma/p) * dot(Lambda, diag(diag(dot(Lambda.T,Lambda))))))
        R = dot(u,vh)
        d = sum(s)
        if d/d_old < tol: break
    return dot(Phi, R)

答案 1 :(得分:7)

Wikipedia在python here中有一个例子!

取消示例并为 numpy 定制:

from numpy import eye, asarray, dot, sum, diag
from numpy.linalg import svd
def varimax(Phi, gamma = 1.0, q = 20, tol = 1e-6):
    p,k = Phi.shape
    R = eye(k)
    d=0
    for i in xrange(q):
        d_old = d
        Lambda = dot(Phi, R)
        u,s,vh = svd(dot(Phi.T,asarray(Lambda)**3 - (gamma/p) * dot(Lambda, diag(diag(dot(Lambda.T,Lambda))))))
        R = dot(u,vh)
        d = sum(s)
        if d_old!=0 and d/d_old < 1 + tol: break
    return dot(Phi, R)

答案 2 :(得分:2)

我已经查找了多次在堆栈溢出的python中进行因子分析的解决方案,我最近创建了自己的包fa-kit。即使这是一篇旧帖子,我也会抛出这个链接,以防将来有其他人通过谷歌来到这里。