Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_svd_update_all_features_updates_each_feature_once_in_any_order():
model = svd.SVD()
model.update_feature = MockThatTracksCallsWithoutRunning()
model.update_all_features()
assert model.update_feature.call_count == model.num_features
expected_calls = [call(feature) for feature in range(model.num_features)]
model.update_feature.assert_has_calls(expected_calls, any_order=True)
def test_svd_update_user_and_movie_modifies_matrices_as_expected():
from utils.data_io import get_user_movie_time_rating
model = svd.SVD()
initialize_model_with_simple_train_points_but_do_not_train(model)
for train_point in model.train_points:
user, movie, _, rating = get_user_movie_time_rating(train_point)
for feature in range(model.num_features):
error = model.calculate_prediction_error(user, movie, rating)
expected_users = np.copy(model.users)
expected_movies = np.copy(model.movies)
expected_user_change = (model.learn_rate * error *
model.movies[movie, feature])
expected_movie_change = (model.learn_rate * error *
model.users[user, feature])
expected_users[user, feature] += expected_user_change
expected_movies[movie, feature] += expected_movie_change
model.update_user_and_movie(user, movie, feature, error)
actual_users = model.users
actual_movies = model.movies
def test_svd_init_sets_default_learn_rate():
default_learn_rate = 0.001
model = svd.SVD()
assert model.learn_rate == default_learn_rate
def test_svd_train_sets_train_points_and_stats():
model = svd.SVD()
simple_train_points = make_simple_train_points()
simple_stats = make_simple_stats()
model.initialize_users_and_movies = MockThatAvoidsErrors()
model.update_all_features = MockThatAvoidsLongRunTime()
model.set_train_points = MockThatTracksCallsWithoutRunning()
model.set_stats = MockThatTracksCallsWithoutRunning()
model.train(simple_train_points, stats=simple_stats)
assert model.set_train_points.call_count == 1
assert model.set_stats.call_count == 1
def test_svd_train_more_sets_train_points_when_train_points_passed():
model = svd.SVD()
simple_train_points = make_simple_train_points()
model.initialize_users_and_movies = MockThatAvoidsErrors()
model.update_all_features = MockThatAvoidsLongRunTime()
model.set_train_points = MockThatTracksCallsWithoutRunning()
model.train_more(simple_train_points)
assert model.set_train_points.call_count == 1
def test_svd_init_sets_default_feature_initial_value_for_default_number():
from utils.constants import SVD_FEATURE_VALUE_INITIAL
default_num_features = 3
expected_feature_initial = SVD_FEATURE_VALUE_INITIAL
model = svd.SVD()
actual_feature_initial = model.feature_initial
assert float(actual_feature_initial) == expected_feature_initial
def test_svd_init_sets_default_feature_initial_value_for_custom_number():
from random import random
from utils.constants import SVD_FEATURE_VALUE_INITIAL
custom_number_of_features = random()
expected_feature_initial = SVD_FEATURE_VALUE_INITIAL
model = svd.SVD(num_features=custom_number_of_features)
actual_feature_initial = model.feature_initial
assert float(actual_feature_initial) == expected_feature_initial
def test_svd_set_train_points_sets_train_points_to_expected_matrix():
model = svd.SVD()
simple_train_points = make_simple_train_points()
model.set_train_points(simple_train_points)
np.testing.assert_array_equal(model.train_points, simple_train_points)
def test_svd_calculate_max_movie_returns_expected_number():
model = svd.SVD()
initialize_model_with_simple_train_points_but_do_not_train(model)
expected_max_movie = np.amax(model.train_points[:, MOVIE_INDEX]) + 1
actual_max_movie = model.calculate_max_movie()
assert actual_max_movie == expected_max_movie
__author__ = 'quinnosha'
import numpy as np
from algorithms.svd import SVD
from utils.c_interface import c_run_svd_plus_epoch
from utils.constants import SVD_FEATURE_VALUE_INITIAL
from utils.constants import MOVIE_INDEX, USER_INDEX, K_NEIGHBORS
class SVD_Plus(SVD):
def __init__(self, num_features=3, feature_initial=SVD_FEATURE_VALUE_INITIAL,
offset_learn_rate=0.007, feature_learn_rate=0.007, feedback_learn_rate=0.001,
offset_k_factor=0.005, feature_k_factor=0.015, feedback_k_factor=0.015):
self.num_features = num_features
self.feature_initial = feature_initial
self.offset_learn_rate = offset_learn_rate
self.feature_learn_rate = feature_learn_rate
self.feedback_learn_rate = feedback_learn_rate
self.offset_k_factor = offset_k_factor
self.feature_k_factor = feature_k_factor
self.feedback_k_factor = feedback_k_factor
self.users = np.array([])
self.movies = np.array([])
self.implicit_preference = np.array([])
self.explicit_feedback = np.array([])