Exemplo n.º 1
0
def compress_image(filename, s):
    """Plots the original image found at 'filename' and the rank s approximation
    of the image found at 'filename.' States the number of entries used to store
    the original image and the approximation.

    Parameters:
        filename (str): Image file path.
        s (int): Rank of new image.
    """
    original = imread(filename) / 255

    #grayscale
    if len(original.shape) == 2:
        reduced, num = SVD.svd_approx(original, s)

        #plot
        plt.subplot(121)
        plt.imshow(original, cmap='gray')
        plt.axis('off')
        plt.title('Entries Stored: ' + str(original.size))

        plt.subplot(122)
        plt.imshow(reduced, cmap='gray')
        plt.axis('off')
        plt.title('Entries Stored: ' + str(num))

        plt.suptitle('Difference: ' + str(original.size - num))
        plt.show()

    #color
    else:
        R, num1 = SVD.svd_approx(original[:, :, 0], s)
        G, num2 = SVD.svd_approx(original[:, :, 1], s)
        B, num3 = SVD.svd_approx(original[:, :, 2], s)

        R = np.clip(R, 0, 1)
        G = np.clip(G, 0, 1)
        B = np.clip(B, 0, 1)

        num = num1 + num2 + num3
        reduced = np.dstack((R, G, B))

        #plot
        plt.subplot(121)
        plt.imshow(original)
        plt.axis('off')
        plt.title('Entries Stored: ' + str(original.size))

        plt.subplot(122)
        plt.imshow(reduced)
        plt.axis('off')
        plt.title('Entries Stored: ' + str(num))

        plt.suptitle('Difference: ' + str(original.size - num))
        plt.show()
Exemplo n.º 2
0
def cur_with_repeat(M, e):
    """
    CUR decomposition of M, allowing for repeat selection of columns with retained energy e
    """

    r = 50
    mean_val = np.squeeze(np.asarray(np.true_divide(M.sum(1),
                                                    (M != 0).sum(1))))
    for i in range(M.shape[0]):
        for j in range(M.shape[1]):
            if M[i, j] != 0:
                M[i, j] = M[i, j] - mean_val[i]

    temp = np.squeeze(np.asarray(np.sum(np.square(M), axis=0)))
    col_prob = temp / float(sum(temp))

    temp = np.squeeze(np.asarray(np.sum(np.square(M), axis=1)))
    row_prob = temp / float(sum(temp))
    rows = np.random.choice(len(row_prob), r, replace=True, p=row_prob)
    cols = np.random.choice(len(col_prob), r, replace=True, p=col_prob)

    R = (M[rows, :].T / np.sqrt(r * row_prob[rows])).T
    C = M[:, cols] / np.sqrt(r * col_prob[cols])
    W = M[rows[:, None], cols]
    S, V, D = SVD.svd_for_cur(W, 0.9)

    product = [D.T, l.matrix_power(l.pinv(V), 2), S.T]
    U = l.multi_dot(product)
    Y = l.multi_dot([C, U, R])

    Y = (Y.T + mean_val).T

    return C, U, R, Y
Exemplo n.º 3
0
def testSVD():
    dataset = load_dataset.loaddata('./data-new/train.txt', '0')
    trainset, testset = split_train_and_test(dataset, 10, 1, 1)
    # testset = load_dataset.loaddata('./data-new/test.txt','1')
    trainset = load_dataset.construct_trainset(dataset)
    svd = SVD.SVD()
    svd.fit(trainset)

    targets = [score for (_, _, score) in testset]
    items = [[userid, itemid] for (userid, itemid, score) in testset]
    # items = [[userid,itemid]for (userid,itemid) in testset]
    predictions = svd.predict_all(items)
    p = [score for (_, _, score) in predictions]

    # fid=open('result1.txt','w')
    #
    # index=0
    # for ruid,riid,score in predictions:
    #     fid.write(ruid+','+riid+',')
    #     fid.write(str(score))
    #     fid.write(',')
    #     fid.write(str(targets[index]))
    #     index+=1
    #     fid.write('\n')
    #
    #
    # fid.close()
    score = RMSE(p, targets)
    print(score)
Exemplo n.º 4
0
def test_ready(N, p):
    #tests ready(M)
    M = SVD.prepare(N, p)
    rMin, OptM = SVD.APIndexCode(M)
    size = ready(OptM)
    print(size)

#test_ready(10, .3)
Exemplo n.º 5
0
def imageToWFA(img,Maxerror):
	Maxerror = Maxerror/10
	img = img.convert('LA')
	img_array = numpy.array(img)
	n = 1
	currentState = n - 1
	images = [img_array]
	I = [1]
	F = [[0]]
	A = [[[0]],[[0]],[[0]],[[0]]]
	F[0][0] = getImageF(img_array)
	I[0] = 1
	while currentState < n:
		currentImage = images[currentState]	
		for j in range(4):
			subImageF = getImageF(currentImage,j)
			new_array = getSubImage(currentImage,j)
			MatrixA = getLinearMatrix(images)
			B = getLinearMatrix([new_array])
			x = SVD.calculWeights(MatrixA,B)
			item = 0
			for i in range(len(images)):
				valX = x[i][0]
				item += valX*getImageF(images[i])
			error = math.floor(abs(item-getImageF(new_array))*10000)/100
			stateExist = False	
			if error <= Maxerror:
				stateExist = True
				for xIndex in range(len(x)):
					A[j][currentState][xIndex] = x[xIndex][0]
			if subImageF != 0.0 and stateExist == False:
				n += 1
				J = 0	
				while J < 4:
					A[J].append([0])
					for H in range(len(A[J])):
						while len(A[J][H]) < n:
							A[J][H].append(0)
					J += 1	
				I.append(0)
				F.append([subImageF])
				A[j][currentState][n-1] = 1
				newImage = getSubImage(currentImage,j)
				images.append(newImage)		
		currentState += 1
	wfa = WFA(n,I,F,A)
	return wfa
Exemplo n.º 6
0
    def symop(self, matrixA, matrixB, title=""):
        a = SVD.SVDSuperimposer()
        a.set(matrixA[1], matrixB[1])
        x = []
        x.append(matrixA[0])
        x.append(matrixB[0])
        COM = self.get_center_of_mass(x)
        print "COMPARISON " + title
        print "Centroid: "
        print COM
        a.run()

        R = np.transpose(a.get_rot())
        print "Rotation Matrix:"
        print R
        print title + " Rotation Angle: " + self.getangle(R)
        angle = self.getangle(R)
        print title + " RMSD: " + a.get_rms()
        print '-----'
Exemplo n.º 7
0
def test(N):
    #test SVD decoding with APIC
    p = .3
    M = SVD.prepare(N, p)
    T = np.random.rand(N, 1)
    print(T)
    [Rmin, OptM] = SVD.APIndexCode(M)
    print(Rmin)
    X = SVDenc(OptM, T, Rmin)
    A = np.zeros((N, N))
    for i in range(N):
        for j in range(N):
            if M[i][j] > .001 and M[i][j] != 1:
                A[i][j] = T[j][0]
    message = []
    for i in range(N):
        t = SVDdec(OptM, X, i, A[i][:])
        message.append(t)
    print(message)
def calcula_superposicion_SVD(pdbh1,pdbh2,originalPDBname,fittedPDBname,test=False):
	""" Calcula matriz de rotacion que aplicada sobre coords1 minimiza RMSD respecto a coords2
	y crea archivo con formato PDB con la superposicion resultante.
	Emplea el algoritmo de 'Single Value Decomposition' del paquete SVD. """
	
	def calcula_centro(coords):
		centro = [0,0,0]
    		for coord in (coords): 
			for dim in range(0,3): centro[dim] += coord[dim]
    		for dim in range(0,3): centro[dim] /= len(coords)
		return centro
   
   	def calcula_coordenadas_centradas(coords,centro):
		ccoords,total = [],0
		for coord in (coords): 
			ccoords.append(coord)
			for dim in range(0,3): ccoords[total][dim] -= centro[dim]
			total+=1
		return ccoords
		
	def calcula_coordenadas_rotadas(coords,rotacion):
		rcoords = [0,0,0]            
		for i in range(0,3):               
			tmp = 0.0
			for j in range(0,3): tmp += coords[j] * rotacion[i][j]
			rcoords[i] = tmp
		return rcoords			
   
   	# escribe fichero PDB con coordenadas originales
	pdbfile = open(originalPDBname, 'w')
	print >> pdbfile, "HEADER %s\n" % pdbh1['file'],
	for res in (pdbh1['coords']): print >> pdbfile, res,
	print >> pdbfile, "TER\n",
	print >> pdbfile, "HEADER %s\n" % pdbh2['file'],
	for res in (pdbh2['coords']): print >> pdbfile, res,
	print >> pdbfile, "TER\n",
	pdbfile.close()	
	
	## prepara coordenadas de atomos CA alineados (equivalentes)
	coords1,coords2 = pdbh1['align_coords'],pdbh2['align_coords']
    	centro1 = calcula_centro(coords1) 
    	centro2 = calcula_centro(coords2) 
	ccoords1 = calcula_coordenadas_centradas(coords1,centro1) 
	ccoords2 = calcula_coordenadas_centradas(coords2,centro2) 
	
	## prepara matriz producto para descomposicion matricial SVD matriz = U.Sigma.V
	matriz = [[0,0,0],[0,0,0],[0,0,0]]
	peso = 1.0/len(ccoords1) # todos los residuos cuentan igual
	for i in range(0,3):
		for j in range(0,3):
			tmp = 0.0
			for k in range(0,len(ccoords1)): tmp += ccoords1[k][i] * ccoords2[k][j] * peso
			matriz[i][j]=tmp;
	if(test == True): 
		for i in range(0,3): print "mat %f %f %f\n" % (matriz[i][0],matriz[i][1],matriz[i][2]),		
   			
	## invoca descomposicion en valores singulares y comprueba matrix/determinante
	[U, Sigma, V] = SVD.svd( matriz )
	if(test==True): 
		for i in range(0,3): print "U %f %f %f\n" % (U[i][0],U[i][1],U[i][2]),
		for i in range(0,3): print "Vt %f %f %f\n" % (V[i][0],V[i][1],V[i][2]),
	
	rotacion = [[0,0,0],[0,0,0],[0,0,0]]
	for i in range(0,3):
		for j in range(0,3):
			rotacion[i][j]= U[j][0]*V[i][0] + U[j][1]*V[i][1] + U[j][2]*V[i][2]
				
	## evalua error de la superposicion
	rmsd = 0.0
	for n in range(0,len(coords1)):
		coords1_rot = calcula_coordenadas_rotadas(ccoords1[n],rotacion)
		for i in range(0,3):
			desv = ccoords2[n][i]-coords1_rot[i]
			rmsd += desv*desv
   	rmsd /= len(coords1)
	
	## imprime superposicion de todos los atomos en formato PDB
	pdbfile = open(fittedPDBname, 'w')
	
	# pdb superpuesto, coordenadas rotadas (1)
	print >> pdbfile, "HEADER %s (rotated)\n" % pdbh1['file'],
	print >> pdbfile, "REMARK Rotation matrix:\n",
	for i in range(0,3): print >> pdbfile, "REMARK %f %f %f\n" % \
				(rotacion[i][0],rotacion[i][1],rotacion[i][2]),
	print >> pdbfile, "REMARK centroid: %f %f %f\n" % (centro1[0],centro1[1],centro1[2]),
	print >> pdbfile, "REMARK partner centroid: %f %f %f\n" % \
		(centro2[0],centro2[1],centro2[2]),
	for res in (pdbh1['coords']): 
		for atomo in res.split("\n"):
			if(atomo == ''): break
			atcoords = extrae_coords_atomo(res,atomo[12:16]) 
			
			atcoords[0] -= centro1[0] # centralo
			atcoords[1] -= centro1[1]
			atcoords[2] -= centro1[2]
			
			coords_rot = calcula_coordenadas_rotadas(atcoords,rotacion)
			
			# trasladalo al pdb referencia
			atcoords[0] = centro2[0] + coords_rot[0] 
			atcoords[1] = centro2[1] + coords_rot[1]
			atcoords[2] = centro2[2] + coords_rot[2]
					
			print >> pdbfile, "%s%8.3f%8.3f%8.3f%s" % \
			(atomo[0:30],atcoords[0],atcoords[1],atcoords[2],atomo[54:])	
	print >> pdbfile, "TER\n",	
	
	# pdb de referencia, coordenadas originales (2)
	print >> pdbfile, "HEADER %s\n" % pdbh2['file'],
	for res in (pdbh2['coords']): print >> pdbfile, res,
	print >> pdbfile, "TER\n",
	
	pdbfile.close()	
	
	return sqrt(rmsd)
Exemplo n.º 9
0
def CompletionGradient(X,shape,ObservedList,R,Ls,alpha=0,XoriginalTensor=None,isProd=False):
    #X is Dense Vector with only observed elements

    #Observed must be given as list of coordinate tuples
    #TrueX is 3 dimensional array


    N = 3
    Xns = [critical.unfold(X,n,shape,ObservedList) for n in xrange(N)]
    print "unfolded"
    As = [zeros((shape[n],R)) for n in xrange(N)]
    #As = [SVD.getLeadingSingularVects(Xns[n],R) for n in xrange(N)]
    Rev = 100
    for i in xrange(Rev):
        Asadd = [SVD.getLeadingSingularVects(Xns[n],R) for n in xrange(N)]
        for k in xrange(N):
            As[k] += Asadd[k]
    for k in xrange(N):
        As[k] /= Rev 
        #As[k] += random.randn(shape[k],R)*0.01



    n,m,l=shape
    def lossfunc(U,V,W):
        #print "loss start"
        XO = critical.HadamardProdOfSparseTensor(U,V,W,ObservedList)
        loss = norm(XO - X)
        #print "loss end"
        return loss

    #Xinit = flattenAs(As)
    #print "start bfgs"


    J = alg.createUnitTensor(3,R)
    #Mask = getMaskTensor(ObservedList,shape)

    U,V,W = As

    Ls[:] = [L*alpha if not L==None else 0 for L in Ls]

    Lu,Lv,Lw=Ls
    beta = 1e-8
    isProd = False

    if not isProd:
        LuUse = alpha * Lu + beta * eye(n) 
        LvUse = alpha * Lv + beta * eye(m) 
        LwUse = alpha * Lw + beta * eye(l) 
    #print U.shape, V.shape, W.shape

    Xest = XoriginalTensor #memory consuming


    threshold = 0.5*const.ConvergenceThreshold_NewCompletion
    maxiter=700
    bfgs_maxiter=3
    errorold = inf
    errorTest = inf
    expandedX = 0

    Dw,Kw = separateLaplacian(Lw,l)
    Dv,Kv = separateLaplacian(Lv,m)
    Du,Ku = separateLaplacian(Lu,n)

    import itertools
    for steps in itertools.count():
        if isProd:
            DSu = alpha * trace(dot(W.T*Dw,W)) * trace(dot(V.T*Dv,V))
            KSu = alpha * trace(dot(W.T,dot(Kw,W))) * trace(dot(V.T,dot(Kv,V)))
            LuUse = DSu * Du - KSu * Ku + eye(n)

        #print "optimization of U"
        #print [U.shape,V.shape,W.shape]
        grad = lambda U:critical.Gradient(X,ObservedList,(U,V,W),LuUse,shape,R,0)
        loss = lambda U:lossfunc(U,V,W)
        U = LBFGS_matrix(loss,U,grad,maxiter=bfgs_maxiter)
        #print [U.shape,V.shape,W.shape]

        if isProd:
            DSv = alpha * trace(dot(U.T*Du,U)) * trace(dot(W.T*Dw,W))
            KSv = alpha * trace(dot(U.T,dot(Ku,U))) * trace(dot(W.T,dot(Kw,W)))
            LvUse = DSv * Dv - KSv * Kv + eye(m)
        #print "optimization of V"
        grad = lambda V:critical.Gradient(X,ObservedList,(U,V,W),LvUse,shape,R,1)
        loss = lambda V:lossfunc(U,V,W)
        V = LBFGS_matrix(loss,V,grad,maxiter=bfgs_maxiter)

        if isProd:
            DSw = alpha * trace(dot(U.T*Du,U)) * trace(dot(V.T*Dv,V))
            KSw = alpha * trace(dot(U.T,dot(Ku,U))) * trace(dot(V.T,dot(Kv,V)))
            LwUse = DSw * Dw - KSw * Kw + eye(l)
        #print "optimization of W"
        grad = lambda W:critical.Gradient(X,ObservedList,(U,V,W),LwUse,shape,R,2)
        loss = lambda W:lossfunc(U,V,W)
        W = LBFGS_matrix(loss,W,grad,maxiter=bfgs_maxiter)

        #grad = lambda (U,V,W)
        
        if False:
            XO = critical.HadamardProdOfSparseTensor(U,V,W,ObservedList)
            normrate = norm(XO) / norm(X)
            qubicnormrate = normrate ** (1.0 / 3)
            U /= qubicnormrate
            V /= qubicnormrate
            W /= qubicnormrate

        if steps % 20 == 1:
            Xest = alg.expand(J,[U,V,W])
            errorTest = norm(Xest - XoriginalTensor)
            #print U

        errorObserved = lossfunc(U,V,W)


        if steps % 20 == 1:
            print "iter:",steps," err:",errorTest ," oberr:",errorObserved, " diff:", errorObserved-errorold, "norm;", norm(Xest)

        faultThreshold = 1e4
        if errorObserved > faultThreshold or errorObserved != errorObserved:
            #やりなおし
            print "-----Try Again-----"
            print steps," steps, observation error=",errorObserved
            return CompletionGradient(X,shape,ObservedList,R,Ls,alpha,XoriginalTensor)

        if abs(errorObserved - errorold) < threshold or steps >= maxiter:
            expandedX = alg.expand(J,[U,V,W])
            print "estimation finished in ",(steps+1),"steps."
            break

        errorold = errorObserved

    return expandedX
Exemplo n.º 10
0
def classificationSVD(indice):
    scores = [0.] * 10
    for label in range(10):
        scores[label] = SVD.distance_de_base(label, indice, M)

    return np.argmin(scores), scores[np.argmin(scores)]
Exemplo n.º 11
0
# coding:utf8
'''
Created on 2018年3月7日
@author: XuXianda

'''
import SVD
from numpy import *
from numpy import linalg as la
matData = mat(SVD.loadExData2())
#我们试一下默认的推荐
print 'The default recommendation:'
print SVD.recommend(matData, 2)
#然后,我们试一下使用SVD的推荐效果
U, Sigma, VT = la.svd(matData)
print 'The recommendation using SVD:'
print SVD.recommend(matData, 2, estMethod=SVD.svdEst)
Exemplo n.º 12
0
def calcula_superposicion_SVD(pdbh1,
                              pdbh2,
                              originalPDBname,
                              fittedPDBname,
                              test=False):
    """ Calcula matriz de rotacion que aplicada sobre coords1 minimiza RMSD respecto a coords2
	y crea archivo con formato PDB con la superposicion resultante.
	Emplea el algoritmo de 'Single Value Decomposition' del paquete SVD. """
    def calcula_centro(coords):
        centro = [0, 0, 0]
        for coord in (coords):
            for dim in range(0, 3):
                centro[dim] += coord[dim]
        for dim in range(0, 3):
            centro[dim] /= len(coords)
        return centro

    def calcula_coordenadas_centradas(coords, centro):
        ccoords, total = [], 0
        for coord in (coords):
            ccoords.append(coord)
            for dim in range(0, 3):
                ccoords[total][dim] -= centro[dim]
            total += 1
        return ccoords

    def calcula_coordenadas_rotadas(coords, rotacion):
        rcoords = [0, 0, 0]
        for i in range(0, 3):
            tmp = 0.0
            for j in range(0, 3):
                tmp += coords[j] * rotacion[i][j]
            rcoords[i] = tmp
        return rcoords

# escribe fichero PDB con coordenadas originales

    pdbfile = open(originalPDBname, 'w')
    print >> pdbfile, "HEADER %s\n" % pdbh1['file'],
    for res in (pdbh1['coords']):
        print >> pdbfile, res,
    print >> pdbfile, "TER\n",
    print >> pdbfile, "HEADER %s\n" % pdbh2['file'],
    for res in (pdbh2['coords']):
        print >> pdbfile, res,
    print >> pdbfile, "TER\n",
    pdbfile.close()

    ## prepara coordenadas de atomos CA alineados (equivalentes)
    coords1, coords2 = pdbh1['align_coords'], pdbh2['align_coords']
    centro1 = calcula_centro(coords1)
    centro2 = calcula_centro(coords2)
    ccoords1 = calcula_coordenadas_centradas(coords1, centro1)
    ccoords2 = calcula_coordenadas_centradas(coords2, centro2)

    ## prepara matriz producto para descomposicion matricial SVD matriz = U.Sigma.V
    matriz = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    peso = 1.0 / len(ccoords1)  # todos los residuos cuentan igual
    for i in range(0, 3):
        for j in range(0, 3):
            tmp = 0.0
            for k in range(0, len(ccoords1)):
                tmp += ccoords1[k][i] * ccoords2[k][j] * peso
            matriz[i][j] = tmp
    if (test == True):
        for i in range(0, 3):
            print "mat %f %f %f\n" % (matriz[i][0], matriz[i][1],
                                      matriz[i][2]),

    ## invoca descomposicion en valores singulares y comprueba matrix/determinante
    [U, Sigma, V] = SVD.svd(matriz)
    if (test == True):
        for i in range(0, 3):
            print "U %f %f %f\n" % (U[i][0], U[i][1], U[i][2]),
        for i in range(0, 3):
            print "Vt %f %f %f\n" % (V[i][0], V[i][1], V[i][2]),

    rotacion = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    for i in range(0, 3):
        for j in range(0, 3):
            rotacion[i][
                j] = U[j][0] * V[i][0] + U[j][1] * V[i][1] + U[j][2] * V[i][2]

    ## evalua error de la superposicion
    rmsd = 0.0
    for n in range(0, len(coords1)):
        coords1_rot = calcula_coordenadas_rotadas(ccoords1[n], rotacion)
        for i in range(0, 3):
            desv = ccoords2[n][i] - coords1_rot[i]
            rmsd += desv * desv
    rmsd /= len(coords1)

    ## imprime superposicion de todos los atomos en formato PDB
    pdbfile = open(fittedPDBname, 'w')

    # pdb superpuesto, coordenadas rotadas (1)
    print >> pdbfile, "HEADER %s (rotated)\n" % pdbh1['file'],
    print >> pdbfile, "REMARK Rotation matrix:\n",
    for i in range(0, 3):        print >> pdbfile, "REMARK %f %f %f\n" % \
(rotacion[i][0],rotacion[i][1],rotacion[i][2]),
    print >> pdbfile, "REMARK centroid: %f %f %f\n" % (centro1[0], centro1[1],
                                                       centro1[2]),
    print >> pdbfile, "REMARK partner centroid: %f %f %f\n" % \
     (centro2[0],centro2[1],centro2[2]),
    for res in (pdbh1['coords']):
        for atomo in res.split("\n"):
            if (atomo == ''): break
            atcoords = extrae_coords_atomo(res, atomo[12:16])

            atcoords[0] -= centro1[0]  # centralo
            atcoords[1] -= centro1[1]
            atcoords[2] -= centro1[2]

            coords_rot = calcula_coordenadas_rotadas(atcoords, rotacion)

            # trasladalo al pdb referencia
            atcoords[0] = centro2[0] + coords_rot[0]
            atcoords[1] = centro2[1] + coords_rot[1]
            atcoords[2] = centro2[2] + coords_rot[2]

            print >> pdbfile, "%s%8.3f%8.3f%8.3f%s" % \
            (atomo[0:30],atcoords[0],atcoords[1],atcoords[2],atomo[54:])
    print >> pdbfile, "TER\n",

    # pdb de referencia, coordenadas originales (2)
    print >> pdbfile, "HEADER %s\n" % pdbh2['file'],
    for res in (pdbh2['coords']):
        print >> pdbfile, res,
    print >> pdbfile, "TER\n",

    pdbfile.close()

    return sqrt(rmsd)
Exemplo n.º 13
0

def centerData(X):
    X = X.copy()
    X -= np.mean(X, axis=0)
    return X


X_centered = centerData(X)
"""

Use SVD on the centered data

"""

U, D, V = SVD(X_centered)
"""
Calculate the principal components

"""

new_dim = 2

X_new = U[:, :new_dim] * D[:new_dim]

#we need to transpose X because the dimensions are on the columns in this case
#X_new = PCs.T.dot(X_centered.T)
names = loadtxt("short_names.txt",
                comments="#",
                dtype=str,
                delimiter="\n",
Exemplo n.º 14
0
import PCA_centra
import PCA_norm
import SVD
import ISOMAP

if __name__ == '__main__':
    resfile = open('../result/result.csv', 'w')
    resfile.write('algorithm, dataset, K, accuracy\n')
    PCA_centra.main(resfile)
    PCA_norm.main(resfile)
    SVD.main(resfile)
    ISOMAP.main(resfile)
    resfile.close()
    基于SVD的评分估计
    '''
    n=shape(dataMat)[1]
    simTotal=0.0
    ratSimTotal=0.0
    #奇异值分解,只利用包含90%能量值的奇异值
    U,Sigma,VT=la.svd(dataMat)
    #对奇异值构建对角矩阵
    Sig4=mat(eye(4)*Sigma[:4])
    #利用U矩阵将物品转换到低维空间
    xformedItems=dataMat.T*U[:,:4]*Sig4.I
    for j in range(n):
        userRating=dataMat[user,j]
        if userRating==0 or j==item:
            continue
        similarity=simMeas(xformedItems[item,:].T,xformedItems[j,:].T)
        print("the %d and %d similarity is: %f" % (item,j,similarity))
        simTotal+=similarity
        ratSimTotal+=similarity*userRating
    if simTotal==0:
        return 0
    else:
        return ratSimTotal/simTotal



if __name__=='__main__':
    myMat=mat(SVD.loadExData())
    myMat[0,1]=myMat[0,0]=myMat[1,0]=myMat[2,0]=4
    myMat[3,3]=2
    print(recommend(myMat,1,estMethod=svdEst,simMeas=SVD.pearsSim)) 
Exemplo n.º 16
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 13 13:26:54 2017
"""
import time
import SVD
import data
import CUR
import collaborative
import errors

start = time.time()
U, sigma, V, Y = SVD.svd_retained_energy(data.M.todense(), 1)
end = time.time()
svd_time = end - start
print("SVD")
errors.calc_error(Y)
print("Runtime: ", svd_time, " s")
print("\n\n")

start = time.time()
U, sigma, V, Y = SVD.svd_retained_energy(data.M.todense(), 0.9)
end = time.time()
svdret_time = end - start
print("SVD with 90% retained energy")
errors.calc_error(Y)
print("Runtime: ", svd_time, " s")
print("\n\n")

start = time.time()
Exemplo n.º 17
0
    def main(self, filetype):
        self.set = 1
        for pdb in self.pdbs:
            print "Autoloading detected PDB file: %s" % pdb
            print ", ".join([str(x) for x in pdb.list_chains()])
            commonAA = pdb.commonAA(
                123, 531
            )  #Only use amino acids 123-531 for superimposing and computing RMSD.
        f = open('input.txt', 'r')
        l = []
        matrices = []
        matrices.append([])
        for line in f:
            if line.strip() == "":
                matrices.append(l)
                l = []
            else:
                l.append(map(float, line.split(' ')))
        flag = 1
        while flag:
            towrite1 = []
            matrix_static = []
            matrix_moving = []
            towrite2 = []
            counter1 = 1
            counter2 = 1

            input2 = question("What subunit is the static reference? (eg 5A)")
            input2 = input2.strip()
            matrixID2 = int(re.findall(r'\d+', input2)[0])
            chainID2 = re.findall(r'\D+', input2)[0].upper()
            input2 = input2.upper()
            input1 = question("What subunit is moving? (eg 1A)")
            input1 = input1.strip()
            matrixID1 = int(re.findall(r'\d+', input1)[0])
            chainID1 = re.findall(r'\D+', input1)[0].upper()
            input1 = input1.upper()
            for pdb in self.pdbs:
                for atom in pdb.backbone:
                    if atom['chain'] == chainID1 and atom[
                            'res num'] in commonAA:
                        a = matrices[matrixID1] * np.transpose(atom.coord())
                        temp = Patom(str(atom), 1)
                        temp['x'] = float(a[0])
                        temp['y'] = float(a[1])
                        temp['z'] = float(a[2])
                        temp['line num'] = counter1
                        counter1 += 1
                        towrite1.append(temp)
                    if atom['chain'] == chainID2 and atom[
                            'res num'] in commonAA:
                        a = matrices[matrixID2] * np.transpose(atom.coord())
                        temp = Patom(str(atom), 1)
                        temp['x'] = float(a[0])
                        temp['y'] = float(a[1])
                        temp['z'] = float(a[2])
                        temp['line num'] = counter2
                        counter2 += 1
                        towrite2.append(temp)
            file = open("output/%s%s.pdb" % (filetype, input1), 'w')
            for i in towrite1:
                file.write(str(i) + '\n')
            file.close()
            file = open("output/%s%s.pdb" % (filetype, input2), 'w')
            for i in towrite2:
                file.write(str(i) + '\n')
            file.close()
            matrix_static = self.coordmatrix(towrite1)
            matrix_moving = self.coordmatrix(towrite2)
            a = SVD.SVDSuperimposer()
            static = [towrite1, matrix_static]
            moving = [towrite2, matrix_moving]
            self.symop(static, moving, "%s-to-%s" % (input1, input2))
            continue_check = question("Continue? (Y/n)",
                                      ['', 'y', 'Y', 'n', 'N'])
            if continue_check not in ['', 'y', 'Y']:
                flag = 0