矩阵分解推荐系统python代码
时间: 2023-11-09 07:02:24 浏览: 167
以下是使用Python实现矩阵分解推荐系统的代码:
```python
import numpy as np
from sklearn.metrics import mean_squared_error
from scipy.sparse.linalg import svds
class MatrixFactorization:
def __init__(self, R, K, learning_rate, reg_param, epochs, verbose=False):
self.R = R
self.num_users, self.num_items = R.shape
self.K = K
self.learning_rate = learning_rate
self.reg_param = reg_param
self.epochs = epochs
self.verbose = verbose
def fit(self):
self.P = np.random.normal(scale=1./self.K, size=(self.num_users, self.K))
self.Q = np.random.normal(scale=1./self.K, size=(self.num_items, self.K))
self.b_u = np.zeros(self.num_users)
self.b_i = np.zeros(self.num_items)
self.b = np.mean(self.R[np.where(self.R != 0)])
self.samples = [
(i, j, self.R[i, j])
for i in range(self.num_users)
for j in range(self.num_items)
if self.R[i, j] > 0
]
training_process = []
for i in range(self.epochs):
np.random.shuffle(self.samples)
self.sgd()
mse = self.mse()
training_process.append((i, mse))
if self.verbose:
if (i+1) % 10 == 0:
print("Iteration: %d ; error = %.4f" % (i+1, mse))
return training_process
def mse(self):
xs, ys = self.R.nonzero()
predicted = self.full_matrix()
error = 0
for x, y in zip(xs, ys):
error += pow(self.R[x, y] - predicted[x, y], 2)
return np.sqrt(error)
def sgd(self):
for i, j, r in self.samples:
prediction = self.get_rating(i, j)
e = (r - prediction)
self.b_u[i] += self.learning_rate * (e - self.reg_param * self.b_u[i])
self.b_i[j] += self.learning_rate * (e - self.reg_param * self.b_i[j])
self.P[i, :] += self.learning_rate * (e * self.Q[j, :] - self.reg_param * self.P[i,:])
self.Q[j, :] += self.learning_rate * (e * self.P[i, :] - self.reg_param * self.Q[j,:])
def get_rating(self, i, j):
prediction = self.b + self.b_u[i] + self.b_i[j] + self.P[i, :].dot(self.Q[j, :].T)
return prediction
def full_matrix(self):
return self.b + self.b_u[:,np.newaxis] + self.b_i[np.newaxis:,] + self.P.dot(self.Q.T)
# 使用示例
R = np.array([
[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4],
])
mf = MatrixFactorization(R, K=2, learning_rate=0.01, reg_param=0.01, epochs=100, verbose=True)
mf.fit()
print(mf.full_matrix())
```
阅读全文