Machine learning python for SVD decomposition

Source: Internet
Author: User

This article is a combination of the recommended algorithm and SVD in conjunction with machine learning combat.

Any matrix can be decomposed into the form of SVD.

In fact, the SVD meaning is to use the transformation of the feature space to map the data, the following will be devoted to the basic concept of SVD, first give a python, here first give a simple matrix, indicating the relationship between the user and the object

I have a doubt here myself?

For such a data = U (Z) Vt

Here's the true geometric meaning of U and V: the meaning of the book is that you map the item to the new feature space, and the transpose of V maps the user to the new feature space


The following is a code implementation, while SVD can also be used for dimensionality reduction, the operation of dimensionality reduction by preserving the value of the singular value of the comparison

#-*-coding:cp936-*-"Created on Mar 8, 2011@author:peter" from numpy import *from numpy import linalg as La #用到别名 # this Mainly in conjunction with the recommendation system to introduce SVD, so the data here can be regarded as a user's rating of the item Def loadexdata (): return[[0, 0, 0, 2, 2], [0, 0, 0, 3, 3], [0     , 0, 0, 1, 1], [1, 1, 1, 0, 0], [2, 2, 2, 0, 0], [5, 5, 5, 0, 0], [1, 1, 1, 0, 0]]           Def loadExData2 (): return[[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5], [0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 3], [0, 0, 0, 0, 4, 0, 0, 1, 0, 4, 0], [3, 3, 4, 0, 0, 0, 0, 2, 2, 0, 0], [5, 4, 5, 0, 0, 0, 0, 5, 5, 0,  0], [0, 0, 0, 0, 5, 0, 1, 0, 0, 5, 0], [4, 3, 4, 0, 0, 0, 0, 5, 5, 0, 1], [0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 4], [0, 0, 0, 2, 0, 2, 5, 0, 0, 1, 2], [0, 0, 0, 0, 5, 0, 0, 0, 0, 4, 0], [1, 0 , 0, 0, 0, 0, 0, 1, 2, 0, 0]] def ecludsim (INA,INB): Return 1.0/(1.0 + la.norm (INA-INB)) #计算向量的第二范式, equivalent to direct calculation of Euclidean distance D   EF Pearssim (INA,INB): If Len (INA) < 3:return 1.0 return 0.5+0.5*corrcoef (INA, inB, Rowvar = 0) [0][1] #corrcoef直接计算皮尔逊相关系数def Cossim (INA , InB): num = float (ina.t*inb) denom = La.norm (InA) *la.norm (InB) return 0.5+0.5* (num/denom) #计算余弦相似度 # Collaborative filtering algorithm #datam     User Data user Simmeas similarity calculation item Item def standest (Datamat, user, Simmeas, item): n = shape (Datamat) [1] #计算列的数量, number of items Simtotal = 0.0; Ratsimtotal = 0.0 for j in Range (n): userrating = datamat[user,j] Print (datamat[user,j]) if Userr ating = = 0:continue #如果用户u没有对物品j进行打分, then this judgment can be skipped overlap = Nonzero (Logical_and (datamat[:,item). A>0, Datamat[:,j]. a>0)) [0] #找到对物品 J and item are over-beaten user if len (overlap) = = 0:similarity = 0 else:similarity = Simmeas (datamat [Overlap,item], datamat[overlap,j]) #利用相似度计算两个物品之间的相似度 print ' The%d and%d Similarity is:%f '% (item, J, similarity) simtotal + = similarity ratsImtotal + = similarity * userrating #待推荐物品与用户打过分的物品之间的相似度 * User's rating of items if simtotal = = 0:return 0 Else:return ratsimtot al/simtotal# uses SVD to decompose, but here is directly using the library inside the function #如果自己实现一个SVD分解, I think is the matrix theory inside the solution knowledge is the same bar, but may be in the process of finding eigenvalues will compare Pain def svdest (Datamat, User, Simmeas, item): n = shape (Datamat) [1] simtotal = 0.0; Ratsimtotal = 0.0 U,SIGMA,VT = LA.SVD (datamat) #直接进行分解 Sig4 = Mat (Eye (4) *sigma[:4]) #arrange Sig4 into a diagonal ma Trix Xformeditems = datamat.t * U[:,:4] * sig4.i #create transformed items for j in range (n): userrating = d ATAMAT[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# The real recommendation function, followed by two functions is a method of similarity calculation and recommended Method Def recommend (Datamat, user, n=3, simmeas=Cossim, estmethod=standest): Unrateditems = Nonzero (Datamat[user,:]. a==0) [1] #find unrated items nonzero () [1] Returns the number of rows with a non-0 value, and returns a tuple if Len (unrateditems) = = 0:return ' You rated everything ' Itemscores = [] for item in Unrateditems:estimatedscore = Estmethod (datamat, user, Simmeas, item) I Temscores.append (item, Estimatedscore)) return sorted (Itemscores, Key=lambda jj:jj[1], reverse=true) [: N] #扩展的例子, Image compression using SVD # Print out the image def Printmat (Inmat, thresh=0.8): For I in range: for K in range: if float (Inmat[i,k]) > Thresh:print 1, else:print 0, print ' #最后发现重构出来的数据图是差不多的def imgcompre SS (Numsv=3, thresh=0.8): Myl = [] for line in open (' 0_5.txt '). ReadLines (): NewRow = [] for I in range ( : newrow.append (int (line[i))) Myl.append (newRow) Mymat = Mat (myl) #将数据读入了myMat当中 print "* Original matrix****** "Printmat (Mymat, thresh) u,sigma,vt = LA.SVD (mymat) Sigrecon = Mat (Zeros ((NUMSV, numsv)) #构建一个3 empty matrix for K in range (NUMSV): #construct diagonal matrix from vector SIGRECON[K,K] = sigma[k] Reconmat = u[:,:numsv]*sigrecon*vt[:numsv,:] print "****reconstructed matrix using% D singular values****** "% numsv Printmat (Reconmat, Thresh)

Through the results can be seen, before and after the reduction of dimensions of the picture is basically similar


Machine learning python for SVD decomposition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.