Collaborative filtering algorithm based on item


Now recommend movies to all users

First, we need to calculate the similarity between movies. The similarity uses Pearson correlation to find the similarity between movies.


Find the degree of acquaintance as shown in the table below


For example, if we want to recommend a movie to A, then according to the similarity between the movies that A has watched and other movies, a weighted score is performed to obtain the movie to be recommended to A.

For example, the table is the movies we want to recommend to all users, where 0 means that the user has seen movies and does not need to be recommended.


From the table, we can see that the highest recommended movie for A is 4, that is, the movie Xunlongjue is recommended to user A.

The code looks like this:

Movie similarity code:

[plain]  view plain copy  
  1. load('score.mat');  
  2. [rowsize,colsize] = size(A);  
  3. sim = zeros(colsize);  
  4. %% Pearson correlation coefficient for movie similarity  
  5. for i = 1:colsize-1  
  6.     for j = i+1:colsize  
  7.         sum1=0;  
  8.         sum2=0;  
  9.         sum3 = 0;  
  10.         sum4=0;  
  11.         sum5=0;  
  12.         temp = A(:,i)&A(:,j);  
  13.         index = find(temp);  
  14.         for k = 1:size(index,1)  
  15.             sum1 = sum1 + A(index(k),i)*A(index(k),j);  
  16.             sum2 = sum2 + A(index(k),i);  
  17.             sum3 = sum3 + A(index(k),j);  
  18.             sum4 = sum4 + A(index(k),i)^2;  
  19.             sum5 = sum5 + A(index(k),j)^2;  
  20.         end  
  21.         sum23 = (sum2 * sum3)/k;  
  22.         sum42 = sum4 - sum2^2/k;  
  23.         sum53 = sum5 - sum3^2/k;  
  24.         if (sum42~=0&&sum53~=0)  
  25.             sim(i,j)=(sum1 - sum23)/sqrt(sum42 * sum53);  
  26.         end  
  27.     end  
  28. end  


Recommend movie rating codes to users:

[plain]  view plain copy  
  1. %% recommend movie ratings to users  
  2. predict_score = zeros(rowsize,colsize);  
  3. for i = 1:rowsize  
  4.     %% Find the index of the movies the user has rated  
  5.     find_temp = find(A(i,:));  
  6.     %% found the index of movies that the user has not rated  
  7.     ufind_temp = find(A(i,:)==0);  
  8.     %% predicts that the user has not rated the movie's rating value  
  9.     for j = 1:size(ufind_temp,2)  
  10.         %% Use a user to multiply all rated movies by the similarity of unrated movies  
  11.         for k=1:size(find_temp,2)  
  12.             predict_score(i,ufind_temp(j)) = predict_score(i,ufind_temp(j)) + A(i,find_temp(k))*sim(find_temp(k),ufind_temp(j));  
  13.         end  
  14.     end  
  15. end  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324899731&siteId=291194637