text_plain_output_1.png
import numpy as np # linear algebra

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

import scipy.sparse as sparse

books = pd.read_csv('../input/Books.csv', encoding='ISO 8859-1')
users = pd.read_csv('../input/Users.csv', encoding='ISO 8859-1')
book_ratings = pd.read_csv('../input/BookRatings.csv', encoding='ISO 8859-1', low_memory=False)

book_ratings['UserID'] = book_ratings.UserID.astype(int)
grouped_cleaned = book_ratings.groupby(['UserID', 'ISBN']).sum().reset_index()
grouped_cleaned = grouped_cleaned.query('BookRating > 0')

grouped_cleaned.shape

import scipy.sparse as sparse
from scipy.sparse.linalg import spsolve
users = list(np.sort(grouped_cleaned.UserID.unique()))
books = list(grouped_cleaned.ISBN.unique())
ratings = list(grouped_cleaned.BookRating)

rows = grouped_cleaned.UserID.astype('category', categories=users).cat.codes
cols = grouped_cleaned.ISBN.astype('category', categories=books).cat.codes
ratings_sparse = sparse.csr_matrix((ratings, (rows, cols)), shape=(len(users), len(books)))

matrix_size = ratings_sparse.shape[0] * ratings_sparse.shape[1]
num_ratings = len(ratings_sparse.nonzero()[0])
sparsity = 100 * (1 - num_ratings / matrix_size)
sparsity