text_html_output_1.png
from numpy.linalg import norm

from scipy.sparse import coo_matrix

from surprise import Dataset,Reader,SVD

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

from surprise import Dataset, Reader, SVD
reader = Reader()
ratings = pd.read_csv('../input/the-movies-dataset/ratings_small.csv')

rows = ratings.userId.unique()
columns = ratings.movieId.unique()
columns = np.sort(columns)

myData = np.array([0.0 for i in range(671 * 9066)])
mydf = pd.DataFrame(myData.reshape(671, -1))

mydf.columns = columns
mydf.index = rows
mydf

for i in range(100004):
    mydf.loc[ratings.loc[i, 'userId'], ratings.loc[i, 'movieId']] = ratings.loc[i, 'rating']
mydf

user_list = list(mydf.index)
movie_list = list(mydf.columns)

from scipy.sparse import coo_matrix
R = coo_matrix(mydf.values)

M, N = R.shape
K = 3
P = np.random.rand(M, K)
Q = np.random.rand(K, N)

R.data
R.row
R.col

from numpy.linalg import norm
def error(R, P, Q, lamda=0.02):
    ratings = R.data
    rows = R.row
    cols = R.col
    e = 0
    for ui in range(len(ratings)):
        rui = ratings[ui]
        u = rows[ui]
        i = cols[ui]
        if rui > 0:
            e = e + pow(rui - np.dot(P[u, :], Q[:, i]), 2) + lamda * (pow(norm(P[u, :]), 2) + pow(norm(Q[:, i]), 2))
    return e

rmse = np.sqrt(error(R, P, Q) / len(R.data))
rmse