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:
- load('score.mat');
- [rowsize,colsize] = size(A);
- sim = zeros(colsize);
- %% Pearson correlation coefficient for movie similarity
- for i = 1:colsize-1
- for j = i+1:colsize
- sum1=0;
- sum2=0;
- sum3 = 0;
- sum4=0;
- sum5=0;
- temp = A(:,i)&A(:,j);
- index = find(temp);
- for k = 1:size(index,1)
- sum1 = sum1 + A(index(k),i)*A(index(k),j);
- sum2 = sum2 + A(index(k),i);
- sum3 = sum3 + A(index(k),j);
- sum4 = sum4 + A(index(k),i)^2;
- sum5 = sum5 + A(index(k),j)^2;
- end
- sum23 = (sum2 * sum3)/k;
- sum42 = sum4 - sum2^2/k;
- sum53 = sum5 - sum3^2/k;
- if (sum42~=0&&sum53~=0)
- sim(i,j)=(sum1 - sum23)/sqrt(sum42 * sum53);
- end
- end
- end
- %% recommend movie ratings to users
- predict_score = zeros(rowsize,colsize);
- for i = 1:rowsize
- %% Find the index of the movies the user has rated
- find_temp = find(A(i,:));
- %% found the index of movies that the user has not rated
- ufind_temp = find(A(i,:)==0);
- %% predicts that the user has not rated the movie's rating value
- for j = 1:size(ufind_temp,2)
- %% Use a user to multiply all rated movies by the similarity of unrated movies
- for k=1:size(find_temp,2)
- predict_score(i,ufind_temp(j)) = predict_score(i,ufind_temp(j)) + A(i,find_temp(k))*sim(find_temp(k),ufind_temp(j));
- end
- end
- end