Statement: This article is reprinted from the Wang machine, only as a learning use, not as a commercial use, if there is infringement please private letter to me, the shortest time for you to deal with!

I still recommend the system xiaobai, so this topic understanding is quite shallow, but it must be very good to understand. That’s the right way to start learning something new.

introduce

1

The background of the story consists of four children and five animations, with each child scoring each animation

  • One is least liked
  • A score of 5 is favorite

2

Yo-yo thought Peppa Pig was ok, giving it a 3. Let’s create a 4 by 5 matrix

  • Each row represents a child’s score for all the animations
  • Each column represents the score scored by all the children for an animation

When Yo-yo scores Peppa Pig, she adds 3 points to row 1, column 1 of the matrix.

3

Assuming that four children have finished scoring five animations, which scoring matrix is the most realistic?

  • On the far left, it’s not like a child’s preferences are so uniform
  • On the far right, unlike, say, a child’s preferences are not so disconnected
  • The ones in the middle are the most similar, and there are certain rules to children’s preferences

4

Youyou and Ya ya, who are about the same age, spend a lot of time watching cartoons together, and they have surprisingly similar tastes in animation (five movies give the same score). They love Peppa Pig, they love The Lion King, and they hate Frozen, the Incredibles, Martha and the Bear.

Rule 1: Different users may have similar preferences.

5

Joy likes to watch cartoons with “characters” and gives high marks to “Frozen” and “The Incredibles”; Ya Ya likes to watch animations with “animals”. She gives high marks to Peppa Pig and The Lion King. Duo Duo is still young, he likes all animations, he gives high marks to all animations (duo Duo gives the same score as Lele and Ya Ya give).

Rule 2: The preferences of one user may include the preferences of many other users.

6

“Martha and the Bear” was an average of “The Lion King” and “Frozen.” That’s probably because “Lion King” contains only animals, “Frozen” is mostly people, and “Martha and the Bear” contains both animals and people in equal numbers.

Rule 3: One animated content may contain multiple other animated content.

7

Let’s start with the simplest recommendation system.

When everyone gives all movies a 3, ask Ya Ya how much you should give the Incredibles?

Judging from the rating matrix, everyone likes every movie the same, hence the prediction that Yaya will give the Incredibles a 3.

8

Let’s look at a slightly more complicated recommendation system.

Ask Ya Ya how much you should give “Martha and the Bear”?

Judging from the first and third rows of the rating matrix, Yo-yo and Ya-ya share the same preferences, so it is predicted that Ya-ya will give “Martha and The Bear” a 2, the same as Yo-yo.

9

The example above is too simple, if there are lots of kids, if there are lots of animations, how are we going to learn all the rules from the rating matrix?

Answer: Find the hidden features!

Going back to the example above, if features are categories of animation, then there are two eigenvalues, characters and animals. So depending on how kids like these two features, like the one above,

  • The longandYa yaI like animals, I don’t like people
  • leleI like people, I don’t like animals
  • A lot ofAll like

According to the proportion of these two features in specific cartoons, see the figure below

We can factor a 4 by 5 rating matrix into

  • A 4 by 2 kid-feature matrix
  • A 2 × 5 feature-animation matrix

As follows.

10

So what we’re going to do is we’re going to talk about a score Matrix Factorization as a product of two non-negative matrices, which is the technical term for non-negative Matrix Factorization, and sciKit-learn is a machine learning package.

import numpy as np
from sklearn.decomposition import NMF
Copy the code

The above data were used to try it out. Although the matrices were not exactly the same, the ones with 0 in them were still 0. The other elements only differed by one scaling factor.

In fact, it is impossible for every child to give a score for every animation, so NMF cannot be used directly. Only those missing values can realize the value of the recommendation system. We should predict the ungiven score according to the existing score, and then decide whether to recommend.

11

The actual situation, fight? They’re not graded, and then

  • Let the child-feature matrix be U
  • Let the feature-animation matrix be V

Use the following error function (only Ri,j that are not missing are considered)

Inner product of Ri,j -- U row I and V column jCopy the code

How to solve? Gradient descent! R4 = 5, R4 = 5, R4 = 5