<jupyter_start><jupyter_text>Model-Based Collaborative Filtering Systems SVD Matrix Factorization<jupyter_code># Import Dependencies
import numpy as np
import pandas as pd
import sklearn
from sklearn.decomposition import TruncatedSVD<jupyter_output><empty_output><jupyter_text>The MovieLens dataset was collected by the GroupLens Research Project at the University of Minnesota. You can download the dataset for this demostration at the following URL: https://grouplens.org/datasets/movielens/100k/         Preparing the data<jupyter_code># Loading the movie rating data set
columns = ['user_id', 'item_id', 'rating', 'timestamp']
rating_df = pd.read_csv('./data/u.data', sep='\t', names=columns)
rating_df.head()
# Loading the movie data set
columns = ['item_id', 'movie title', 'release date', 'video release date', 'IMDb URL', 'unknown', 'Action', 'Adventure',
          'Animation', 'Childrens', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Film-Noir', 'Horror',
          'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']

movies_df = pd.read_csv('./data/u.item', sep='|', names=columns, encoding='latin-1')
movie_names = movies_df[['item_id', 'movie title']]
movie_names.head()
# Join the two dataframes by item_id
joined_df = pd.merge(rating_df, movie_names, on='item_id')
joined_df.head()
# Check the number of times an item is rated
joined_df.groupby('item_id')['rating'].count().sort_values(ascending=False).head()
# Check the title of the most rated movie in the data
most_counted_movie = joined_df['item_id']==50
joined_df[most_counted_movie]['movie title'].unique()<jupyter_output><empty_output><jupyter_text>Building a Utility Matrix<jupyter_code># Create the rating crosstab to track user rating for each movie
rating_crosstab = joined_df.pivot_table(values='rating', index='user_id',
                                        columns='movie title', fill_value=0)
rating_crosstab.head()<jupyter_output><empty_output><jupyter_text>Transposing the Matrix<jupyter_code># Check the dimension of the crosstab matrix
rating_crosstab.shape
# Transposing the crosstable matrix
X = rating_crosstab.T
X.shape<jupyter_output><empty_output><jupyter_text>Decomposing the Matrix<jupyter_code># Using the SVD model to fit the data
SVD = TruncatedSVD(n_components=12, random_state=17)

resultant_matrix = SVD.fit_transform(X)
resultant_matrix.shape<jupyter_output><empty_output><jupyter_text>Generating a Correlation Matrix<jupyter_code># Creating a correlation matrix
corr_mat = np.corrcoef(resultant_matrix)
corr_mat.shape<jupyter_output><empty_output><jupyter_text>Isolating Star Wars from the Correlation Matrix<jupyter_code># Create a list of movie names from rating crosstab
movie_names = rating_crosstab.columns
movie_list = list(movie_names)

# Find and assign the index of Star Wars (1977)
star_wars = movie_list.index('Star Wars (1977)')
star_wars
# Calculate the correlation of all movies to Star Wars (1977)
corr_star_wars = corr_mat[1398]
corr_star_wars.shape<jupyter_output><empty_output><jupyter_text>Recommending a Highly Correlated Movie<jupyter_code># List all movie titles that is 90% correlated with Star Wars (1977)
list(movie_names[(corr_star_wars < 1.0) & (corr_star_wars > 0.9)])
# List all movie titles that is 95% correlated with Star Wars (1977)
list(movie_names[(corr_star_wars < 1.0) & (cor_star_wars > 9.5)])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11153619776339023, 'fraction_numerical': 0.01383166568569747, 'mean_word_length': 6.621076233183857, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '144376', 'license': 'MIT', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1117, 'n_words': 296, 'path': 'Model-Based Collaborative Filtering Systems.ipynb', 'repo_name': 'NormanLo4319/Recommendation_App', 'size': '3398'}