<jupyter_start><jupyter_text>NMF = Not Monday night Football !<jupyter_code>import pandas as pd
import numpy as np
from sklearn.decomposition import NMF
import random
from random import randint
from matplotlib import pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>User Input<jupyter_code>#importing ratings and movies csv files
PATH2 = "ratings.csv"
PATH3 = "movies.csv"
ratings, movies_ind = pd.read_csv(PATH2), pd.read_csv(PATH3)
# create an empty array the length of number of movies in system
user_ratings = np.zeros(9724)
#format ratings dataframe
del ratings['timestamp']
ratings.set_index(['userId','movieId'], inplace=True)
ratings = ratings.unstack(0)
ratings_count = ratings.count(axis=1) #count the number of ratings for each movie as a measure of popularity
top = pd.DataFrame(ratings_count.sort_values(ascending = False).head(10)) #create a dataframe of the top 20 most popular movies
top.reset_index(inplace=True)
movies_ind.set_index('movieId',inplace=True)
top_movies_g = movies_ind.loc[top['movieId']]['title'].values<jupyter_output><empty_output><jupyter_text>Of the following movies, rate all that you have seen on a scale of 1-5.  If you have not seen a movie, rate 0.<jupyter_code>#creates a list of ratings for the prompted movies
user_input = []
for i in range(0,10):
    answer = int(input("How would you rate " + str(top_movies_g[i])))
    if answer > 5:
        answer = 5
    elif answer < 0:
        answer = 0
    user_input.append(answer)
movies_ind.reset_index(inplace=True)
top_movies_index = movies_ind.index[top['movieId']].values
# inputs user rating into large array (9,000+ count) at appropriate indexes
for i in range(0,10):
    user_ratings[top_movies_index[i]] = user_input[i]<jupyter_output><empty_output><jupyter_text>NMF Modeling<jupyter_code>ratings = ratings.fillna(0)
ratings = ratings["rating"]
ratings = ratings.transpose()
ratings.head(2)
R = pd.DataFrame(ratings)
# model assumes R ~ PQ'
model = NMF(n_components=5, init='random', random_state=10)
model.fit(R)

P = model.components_  # Movie feature
Q = model.transform(R)  # User features
query = user_ratings.reshape(1,-1)
t=model.transform(query)
# prediction movie ratings of input user
outcome = np.dot(t,P)
outcome=pd.DataFrame(outcome)
outcome = outcome.transpose()
outcome['movieId'] = movies_ind['movieId']
outcome = outcome.rename(columns={0:'rating'})
# top 100 ratings from predictions list
top = outcome.sort_values(by='rating',ascending=False).head(100)<jupyter_output><empty_output><jupyter_text>Selecting a Movie<jupyter_code># collects titles of the top movie predictions
top_movie_recs = movies_ind.loc[top['movieId']]['title'].values<jupyter_output><empty_output><jupyter_text>Selecting a Movie with Genre Input<jupyter_code>#importing genres
PATHG = "movie_genres_years.csv"
movie_genres = pd.read_csv(PATHG)
# creates list of genres
genres = movie_genres.columns.values[3:22]
# dictionary with keys equal to genre
b,c = {}, {}
for x in genres:
    key = x
    value = ''
    b[key],c[key] = value, value
# fills keys with list of movies that belong to respective genre
for x in genres:
    li = []
    for id in top['movieId']:
        if id in list(movie_genres.loc[movie_genres[x] == 1]['movieId']):
            li.append(movies_ind[movies_ind['movieId']==id]['title'].values)
    c[x] = li
#fills keys with random choice in the list of films within a genre
for x in genres:
    if len(c[x])>0:
        b[x] = c[x][randint(0, len(c[x])-1)][0]
    else:
        b[x] = ""
# add an option for not choosing a genre
genres_for_q = np.append(genres, 'none')
from fuzzywuzzy import process
genre_answer = process.extractOne(input("What genre of film would you like to watch?"),genres_for_q)
#picks a top movie of the selected genre
for x in genres:
    if genre_answer[0] == x:
        if len(b[x]) == 0:
            print('No ' +x+ ' recommedations')
        else:
            print('We recommend ' + b[x])
            
# if they don't want a specific genre
if genre_answer[0] == 'none':
        Select = top_movie_recs[randint(0, 4)]
        print('We recommend ' + Select)<jupyter_output>We recommend Interview with the Vampire: The Vampire Chronicles (1994)
{'fraction_non_alphanumeric': 0.11787072243346007, 'fraction_numerical': 0.014733840304182509, 'mean_word_length': 4.970212765957447, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '31274', 'license': 'MIT', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1423, 'n_words': 396, 'path': 'Very_Respectable_Code.ipynb', 'repo_name': 'julian801/JC_Movie_Recommender', 'size': '4208'}