History PCA论文分析及代码实现(未完待续)

import numpy as np
import time

def generate_X_i(A, sigma, B, p):
    A = np.matrix(A),
    k = A.shape[1],
    w = np.random.multivariate_normal(mean=np.zeros(p), cov=pow(sigma, 2) * np.identity(p), size=B),
    z = np.random.multivariate_normal(np.zeros(k), np.identity(k), B),
    x = np.matrix(z).dot(A.T) + w,
    return x

def distance(mat1, mat2):
    return(np.abs(1 - np.linalg.norm(mat1.T.dot(mat2), 2))),

def ours(U, X, k, B, maxIter):
    n, d = X.shape
    V = np.random.randn(d, k)
    S = np.zeros((k, k))

    totoalTime, timeList, iterList, accList = 0, [], [], []

    allTrace = pow(np.linalg.norm(X, ord="fro"), 2)
    c = 0.1

    totalSample = 0
    totalSample_prev = 0
    maxIter = 5 # It is the value of m

    t = np.random.permutation(n)
    X = X[t, :]
    XT = X.T
    nblock = int(n/B)
    maxIter_b = nblock * maxIter

    for i in range(1, maxIter_b + 1):
        timebegin = time.time()
        if i <= nblock:
            nowid = i
        elif nblock == 1:
            nowid = 1
        else:
            nowid = np.random.randint(1, high=nblock, size=1)[0]
            
        my_begin = (nowid - 1) * B + 1
        my_end = nowid * B + 1
    
        if nowid == nblock:
            my_end = n
        
        XnowT = XT[:, my_begin:my_end]
        Xnow = XnowT.T
        newV = V
        
        for j in range(maxIter):
            newV = (XnowT.dot(Xnow.dot(newV))) / float(i) + V.dot(S*((i-1)/float(i))).dot(V.T.dot(newV))
            newV, _ = np.linalg.qr(newV) # QR decomposition

        aa = newV.T.dot(XnowT)
        bb = V.T.dot(newV)
        S = aa.dot(aa.T)/float(i) + (bb.T.dot(S*(i-1)/float(i))).dot(bb)
        V = newV

        totalTime = totalTime + time.time() - timebegin
        totalSample = totalSample + B

        if (totalSample == B or totalSample > totalSample_prev + n/10):
            timeList.append(totalTime)
            iterList.append(totalSample)
            acc = distance(V, U)
            accList.append(acc)
            totalsample_prev = totalSample

    return timeList, accList, iterList, V

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值