This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import pandas as pd | |
import numpy as np | |
from scipy.sparse import csr_matrix, save_npz, load_npz, vstack, hstack, lil_matrix | |
import implicit | |
import pickle | |
from implicit.evaluation import train_test_split, precision_at_k, mean_average_precision_at_k | |
def load_data(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sparse_matrices(df): | |
'''creates the sparse user-item and item-user matrices''' | |
# using a scalar value (40) to convert ratings from a scale (1-5) to a like/click/view (1) | |
alpha = 40 | |
sparse_user_item = csr_matrix( ([alpha]*len(df['movie_id']), (df['user_id'], df['movie_id']) )) | |
# transposing the item-user matrix to create a user-item matrix | |
sparse_item_user = sparse_user_item.T.tocsr() | |
# save the matrices for recalculating user on the fly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model(): | |
'''computes p@k and map@k evaluation metrics and saves model''' | |
sparse_item_user = load_npz("sparse_item_user.npz") | |
train, test = train_test_split(sparse_item_user, train_percentage=0.8) | |
model = implicit.als.AlternatingLeastSquares(factors=100, | |
regularization=0.1, iterations=20, calculate_training_loss=False) | |
model.fit(train) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def map_movies(movie_ids): | |
'''takes a list of movie_ids and returns a list of dictionaries with movies information''' | |
df = pd.read_csv('ml-1m/movies.dat', delimiter='::', header=None, | |
names=['movie_id', 'title', 'genre'], engine='python') | |
# add years to a new column 'year' and remove them from the movie title | |
df['year'] = df['title'].str[-5:-1] | |
df['title'] = df['title'].str[:-6] | |
# creates an ordered list of dictionaries with the movie information for all movie_ids |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def most_similar_items(item_id, n_similar=10): | |
'''computes the most similar items''' | |
with open('model.sav', 'rb') as pickle_in: | |
model = pickle.load(pickle_in) | |
similar, _ = zip(*model.similar_items(item_id, n_similar)[1:]) | |
return map_movies(similar) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recommend(user_id): | |
'''recommend N items to user''' | |
sparse_user_item = load_npz("sparse_user_item.npz") | |
with open('model.sav', 'rb') as pickle_in: | |
model = pickle.load(pickle_in) | |
recommended, _ = zip(*model.recommend(user_id, sparse_user_item)) | |
return recommended, map_movies(recommended) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recalculate_user(user_ratings): | |
'''adds new user and its liked items to sparse matrix and returns recalculated recommendations''' | |
alpha = 40 | |
m = load_npz('sparse_user_item.npz') | |
n_users, n_movies = m.shape | |
ratings = [alpha for i in range(len(user_ratings))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
<form action="/contact" id="contact-form" method="POST" enctype="text/plain"> | |
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> | |
<div class="row"> | |
<div class="input-field col s6"> | |
<input type="text" class="validate" name ="first-name" id="first-name" pattern="[a-zA-Z]*" required/> | |
<label for="first-name">First name</label> | |
<span class="helper-text" data-error="Only text is accepted." data-success=""></span> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
{% block links %} | |
<!-- Bootstrap CSS --> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | |
<!-- jQuery script--> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> | |
<!--Import materialize.css--> | |
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" media="screen,projection"/> | |
<!--Let browser know website is optimized for mobile--> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#contact-form-button').click(function(event){ | |
// Prevent redirection with AJAX for contact form | |
var form = $('#contact-form'); | |
var form_id = 'contact-form'; | |
var url = form.prop('action'); | |
var type = form.prop('method'); | |
var formData = getContactFormData(form_id); | |
// submit form via AJAX | |
send_form(form, form_id, url, type, modular_ajax, formData); |
OlderNewer