A simple Python-based referral system

Source: Internet
Author: User
Tags true true

defloadexdata ():return[[1,1,1, 0,0], [2,2,2, 0,0], [1,1,1, 0,0], [5,5,5, 0,0], [1,1,0,2,2], [0,0,0,3,3], [0,0,0,The]]defloadExData2 ():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]]  fromNumPyImport* fromNumPyImportLinalg as La#Euclidean distancedefEuclidsim (INA,INB):return1.0/(1.0+la.norm (ina-InB))#Pearson correlation coefficientdefPearssim (INA,INB):ifLen (InA) <3:return1.0return0.5+0.5*corrcoef (ina,inb,rowvar=0) [0][1]#Cosine similarity degreedefCossim (INA,INB): Num=float (ina.t*InB) Denom=la.norm (InA) *la.norm (InB)return0.5+0.5* (num/denom)#recommendation engine based on similarity of items (user estimates under the standard similarity calculation method)defstandest (datamat,user,simmeas,item):#Number of itemsN=shape (Datamat) [1]    #two variables to calculate the estimated scoring valuesimtotal=0.0;ratsimtotal=0.0#traverse all items and compare it to all items     forJinchrange (n):#user's rating of an itemuserrating=Datamat[user,j]ifUserrating==0:Continue        #Logical_and: The matrix is run logically with each element, and the return value is true,false for each element        #Datamat[:,item]. A>0: An element greater than 0 in the item column        #Datamat[:,j]. A: Elements greater than 0 in column J        #The row subscript (a vector) of the element that is both greater than 0 in Overlap:datamat[:,item],datamat[:,j]Overlap=nonzero (Logical_and (Datamat[:,item). A>0, Datamat[:,j]. A>0)) [0]Print(j)Print("------overlap------")        Print(overlap)ifLen (overlap) ==0:similarity=0#calculate the similarity of overlap matrices        Else: similarity=Simmeas (Datamat[overlap,item], datamat[overlap,j])Print("Datamat[overlap,item:")        Print(Datamat[overlap,item])Print("datamat[overlap,j:")        Print(Datamat[overlap,j])Print('The %d and%d similarity is:%f'%(item,j,similarity))#Cumulative total similarity (not quite understood)#Suppose A score is unknown, A, B similarity 0.9,b score 5,;a C similarity 0.8,c rating 4.#then according to the formula a score = (0.9*5+0.8*4)/(0.9+0.8)#equivalent to a weighted average (if divided by 2), but because the weights of the 2 ratings are different, the sum of the similarity should be divided bysimtotal+=Similarity#ratsimtotal = similarity * Element valueRatsimtotal+=similarity*userratingPrint("ratsimtotal+=similarity*userrating:")        Print(ratsimtotal)ifSimtotal==0:return0Else:returnratsimtotal/Simtotal#produce the highest n recommended results for a user#user indicates the number of users to recommenddefRecommend (datamat,user,n=3,simmeas=cossim,estmethod=standest):#Create a matrix of non-graded items for a given userUnrateditems=nonzero (Datamat[user,:]. a==0) [1]#element equal to 0 in the first user line#print (Datamat[user,:]. a==0)----[[True True True] ..., true False true]]#for a two-dimensional array B2,nonzero (B2), a tuple of length 2 is obtained. Its No. 0 element is the subscript of the No. 0 axis of an element with a value not 0 in array A, and the 1th element is the subscript of the 1th axis, so the values of b2[0,0], b[0,2], and b2[1,0] are not 0 from the following results:##>>> b2 = Np.array ([[True, False, True], [True, False, false]])#>>> Np.nonzero (B2)#(Array ([0, 0, 1], Dtype=int64), array ([0, 2, 0], Dtype=int64))       ifLen (unrateditems) ==0:return 'You rated everything'    #A list of projected scores for a non-rated itemitemscores=[]     forIteminchUnrateditems:#predict scores by Standest () method for each non-rated item        Print("Item------------")        Print(item) Estimatedscore=Estmethod (Datamat,user,simmeas,item)#Store item numbers and estimated scores in the listItemscores.append ((item,estimatedscore))#sorted sort function, key is sorted by keyword, lambda is implicit function, fixed notation,    #JJ means to sort the progenitor, jj[1] According to JJ's second column sort, reverse=true, descending; [: n] Top n    returnSorted (itemscores,key=LambdaJj:jj[1],reverse=True) [: N]#using SVD to improve the recommended effect#estimation of scoring based on SVDdefsvdest (datamat,user,simmeas,item):#Number of itemsN=shape (Datamat) [1] Simtotal=0.0;ratsimtotal=0.0#SVD decomposition is: u*s*vu,sigma,vt=LA.SVD (Datamat)#after decomposition, use only 90% of the singular value of energy, stored in the numpy arraySig4=mat (Eye (4) *sigma[:4])    #converting items into low-dimensional space using the U matrixxformeditems=datamat.t*u[:,:4]*sig4.i forJinchrange (N): userrating=Datamat[user,j]ifUserrating==0orJ==item:ContinueSimilarity=Simmeas (Xformeditems[item,:]. T, Xformeditems[j,:]. T)Print('The %d and%d similarity is:%f'%(item,j,similarity)) Simtotal+=Similarity Ratsimtotal+=similarity*userratingifSimtotal==0:return0Else:returnratsimtotal/Simtotalif __name__=='__main__': Mymat=Mat (LoadExData2 ())Print(Recommend (mymat,2))

  

A simple Python-based referral system

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.