1
- import numpy as np
2
- import matplotlib .pyplot as plt
3
-
4
- class CollaborativeFiltering :
5
- def fit (self , X , y , dimension , learning_rate , epochs ):
6
- '''
7
- Parameters
8
- ----------
9
- X : shape (data_number , 2)
10
- Training data, column 1 is user id, column 2 is item id
11
- y : shape (data_number, 1 )
12
- Rating
13
- learning_rate : learning rate
14
- epochs : The number of epochs
15
- '''
16
- data_number = X .shape [0 ]
17
- user_id = X [:, 0 ]
18
- item_id = X [:, 1 ]
19
-
20
- self .__user_items = np .unique (user_id )
21
- self .__item_items = np .unique (item_id )
22
-
23
- user_number = len (self .__user_items )
24
- item_number = len (self .__item_items )
25
-
26
- self .__user_vector = np .random .uniform (size = (user_number , dimension ))
27
- self .__user_bias = np .zeros ((user_number , 1 ))
28
- self .__item_vector = np .random .uniform (size = (item_number , dimension ))
29
- self .__item_bias = np .zeros ((item_number , 1 ))
30
-
31
- loss = []
32
- for _ in range (epochs ):
33
- index = np .random .randint (0 , data_number )
34
-
35
- user_index = np .flatnonzero (self .__user_items == user_id [index ])
36
- item_index = np .flatnonzero (self .__item_items == item_id [index ])
37
-
38
- r = (self .__user_vector [user_index ].dot (self .__item_vector [item_index ].T ) + self .__user_bias [user_index ] + self .__item_bias [item_index ] - y [index ])
39
-
40
- loss .append (r .ravel () ** 2 )
41
-
42
- user_vector_new = self .__user_vector [user_index ] - learning_rate * r * self .__item_vector [item_index ]
43
- self .__user_bias [user_index ] -= learning_rate * r
44
- item_vector_new = self .__item_vector [item_index ] - learning_rate * r * self .__user_vector [user_index ]
45
- self .__item_bias [item_index ] -= learning_rate * r
46
-
47
- self .__user_vector [user_index ] = user_vector_new
48
- self .__item_vector [item_index ] = item_vector_new
49
-
50
- plt .plot (loss )
51
- plt .show ()
52
-
53
- def predict (self , X ):
54
- '''
55
- Parameters
56
- ----------
57
- X : shape (data_number , 2)
58
- Predicting data, column 1 is user id, column 2 is item id
59
-
60
- Returns
61
- -------
62
- y : shape (data_number, 1 )
63
- Predicted rating per sample.
64
- '''
65
- data_number = X .shape [0 ]
66
- user_id = X [:, 0 ]
67
- item_id = X [:, 1 ]
68
-
69
- y = np .zeros (( data_number , 1 ) )
70
- for i in range (data_number ):
71
- user_index = np .flatnonzero (self .__user_items == user_id [i ])
72
- item_index = np .flatnonzero (self .__item_items == item_id [i ])
73
- y [i ] = self .__user_vector [user_index ].dot (self .__item_vector [item_index ].T ) + self .__user_bias [user_index ] + self .__item_bias [item_index ]
74
-
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+
4
+ class CollaborativeFiltering :
5
+ def fit (self , X , y , dimension , learning_rate , epochs ):
6
+ '''
7
+ Parameters
8
+ ----------
9
+ X : shape (n_samples , 2)
10
+ Training data, column 1 is user id, column 2 is item id
11
+ y : shape (n_samples, )
12
+ Rating
13
+ learning_rate : learning rate
14
+ epochs : The number of epochs
15
+ '''
16
+ n_samples = X .shape [0 ]
17
+ user_id = X [:, 0 ]
18
+ item_id = X [:, 1 ]
19
+
20
+ self .__user_items = np .unique (user_id )
21
+ self .__item_items = np .unique (item_id )
22
+
23
+ n_users = len (self .__user_items )
24
+ n_items = len (self .__item_items )
25
+
26
+ self .__user_vector = np .random .uniform (size = (n_users , dimension ))
27
+ self .__user_bias = np .zeros ((n_users , 1 ))
28
+ self .__item_vector = np .random .uniform (size = (n_items , dimension ))
29
+ self .__item_bias = np .zeros ((n_items , 1 ))
30
+
31
+ loss = []
32
+ for _ in range (epochs ):
33
+ index = np .random .randint (0 , n_samples )
34
+
35
+ user_index = np .flatnonzero (self .__user_items == user_id [index ])
36
+ item_index = np .flatnonzero (self .__item_items == item_id [index ])
37
+
38
+ r = (self .__user_vector [user_index ].dot (self .__item_vector [item_index ].T ) + self .__user_bias [user_index ] + self .__item_bias [item_index ] - y [index ])
39
+
40
+ loss .append (r .ravel () ** 2 )
41
+
42
+ user_vector_new = self .__user_vector [user_index ] - learning_rate * r * self .__item_vector [item_index ]
43
+ self .__user_bias [user_index ] -= learning_rate * r
44
+ item_vector_new = self .__item_vector [item_index ] - learning_rate * r * self .__user_vector [user_index ]
45
+ self .__item_bias [item_index ] -= learning_rate * r
46
+
47
+ self .__user_vector [user_index ] = user_vector_new
48
+ self .__item_vector [item_index ] = item_vector_new
49
+
50
+ plt .plot (loss )
51
+ plt .show ()
52
+
53
+ def predict (self , X ):
54
+ '''
55
+ Parameters
56
+ ----------
57
+ X : shape (n_samples , 2)
58
+ Predicting data, column 1 is user id, column 2 is item id
59
+
60
+ Returns
61
+ -------
62
+ y : shape (n_samples, )
63
+ Predicted rating per sample.
64
+ '''
65
+ n_samples = X .shape [0 ]
66
+ user_id = X [:, 0 ]
67
+ item_id = X [:, 1 ]
68
+
69
+ y = np .zeros (n_samples )
70
+ for i in range (n_samples ):
71
+ user_index = np .flatnonzero (self .__user_items == user_id [i ])
72
+ item_index = np .flatnonzero (self .__item_items == item_id [i ])
73
+ y [i ] = self .__user_vector [user_index ].dot (self .__item_vector [item_index ].T ) + self .__user_bias [user_index ] + self .__item_bias [item_index ]
74
+
75
75
return y
0 commit comments