def test_fit(self): rec = ExplicitItemFinder() data = load_movielens_mini() true_sc = np.where(data.score <= 3, 0, 1) data.binarize_score() rec.fit(data, event_index=(0, 1)) # check whether score info is correctly set assert_allclose(rec.score_domain, [0, 1, 1]) assert_allclose(rec.score, true_sc) assert_equal(rec.n_score_levels, 2) # error checker data.score_domain = (0, 5, 1) with assert_raises(ValueError): rec.fit(data) data.score_domain = (0, 1, 1) data.n_score_levels = 3 with assert_raises(ValueError): rec.fit(data) data.n_score_levels = 2 data.score = np.zeros(30) with assert_raises(ValueError): rec.fit(data) data.score = np.ones(30) with assert_raises(ValueError): rec.fit(data) data.score = np.repeat(np.arange(3), 10) with assert_raises(ValueError): rec.fit(data)
def test_class(self): # setup data = load_movielens_mini() # __init__() rec = Recommender(random_state=1234) self.assertEqual(rec.n_otypes, 0) self.assertIsNone(rec.n_objects) self.assertIsNone(rec.eid) self.assertIsNone(rec.iid) self.assertEqual(rec.random_state, 1234) self.assertIsNone(rec._rng) self.assertDictEqual(rec.fit_results_, {}) # _set_object_info() rec._set_object_info(data) self.assertEqual(rec.n_otypes, 2) assert_array_equal(rec.n_objects, [8, 10]) assert_array_equal(rec.eid, data.eid) assert_array_equal(rec.iid, data.iid) # to_eid() self.assertEqual(rec.to_eid(0, 0), 1) self.assertEqual(rec.to_eid(1, 0), 1) # to_iid() self.assertEqual(rec.to_iid(0, 1), 0) self.assertEqual(rec.to_iid(1, 1), 0)
def test_binarize_score(self): data = load_movielens_mini() data.binarize_score(2) assert_array_equal(data.score_domain, [0, 1, 1]) assert_array_equal( data.score, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) assert_equal(data.n_score_levels, 2) data = load_movielens_mini() data.binarize_score() assert_array_equal( data.score, [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1])
def test_binarize_score(self): data = load_movielens_mini() data.binarize_score(2) assert_array_equal(data.score_domain, [0, 1, 1]) assert_array_equal(data.score, [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]) assert_equal(data.n_score_levels, 2) data = load_movielens_mini() data.binarize_score() assert_array_equal(data.score, [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ])
def test_filter_event(self): from kamrecsys.data import EventWithScoreData # load movie_lens data = load_movielens_mini() # filter events filter_cond = np.arange(data.n_events) % 3 == 0 filtered_data = super( EventWithScoreData, data).filter_event(filter_cond) assert_array_equal( filtered_data.event[:, 0], [1, 5, 3, 4, 0, 0, 0, 2, 2, 0]) assert_array_equal( filtered_data.event[:, 1], [1, 3, 6, 5, 7, 6, 4, 0, 7, 2]) assert_array_equal( filtered_data.to_eid(0, filtered_data.event[:, 0]), data.to_eid(0, data.event[filter_cond, 0])) assert_array_equal( filtered_data.to_eid(1, filtered_data.event[:, 1]), data.to_eid(1, data.event[filter_cond, 1])) assert_array_equal( filtered_data.event_feature['timestamp'], [875636053, 877889130, 891351328, 879362287, 878543541, 875072484, 889751712, 883599478, 883599205, 878542960]) assert_array_equal(filtered_data.eid[0], [1, 5, 6, 7, 8, 10]) assert_array_equal(filtered_data.eid[1], [1, 2, 3, 4, 5, 7, 8, 9]) assert_equal( filtered_data.iid[0], {1: 0, 5: 1, 6: 2, 7: 3, 8: 4, 10: 5}) assert_equal( filtered_data.iid[1], {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 7: 5, 8: 6, 9: 7}) assert_equal( filtered_data.feature[0]['zip'], [u'85711', u'15213', u'98101', u'91344', u'05201', u'90703']) assert_equal( filtered_data.feature[1]['name'], [u'Toy Story (1995)', u'GoldenEye (1995)', u'Four Rooms (1995)', u'Get Shorty (1995)', u'Copycat (1995)', u'Twelve Monkeys (1995)', u'Babe (1995)', u'Dead Man Walking (1995)']) # dummy event data data = EventData() data.set_event(np.tile(np.arange(5), (2, 2)).T) filtered_data = data.filter_event( [True, False, True, True, False, False, True, True, False, False]) assert_equal(filtered_data.n_events, 5) assert_array_equal( filtered_data.event, [[0, 0], [2, 2], [3, 3], [1, 1], [2, 2]])
def test_filter_event(self): data = load_movielens_mini() filtered_data = data.filter_event(data.score > 3) assert_allclose(filtered_data.score, [ 4., 4., 4., 5., 4., 5., 5., 5., 4., 5., 4., 5., 5., 4., 5., 4., 4., 4., 4., 4., 4. ]) assert_allclose(filtered_data.to_eid(0, filtered_data.event[:, 0]), [ 10, 5, 10, 1, 7, 7, 9, 7, 10, 1, 2, 1, 7, 6, 7, 6, 1, 9, 1, 6, 10 ])
def test_filter_event(self): data = load_movielens_mini() filtered_data = data.filter_event(data.score > 3) assert_allclose( filtered_data.score, [4., 4., 4., 5., 4., 5., 5., 5., 4., 5., 4., 5., 5., 4., 5., 4., 4., 4., 4., 4., 4.]) assert_allclose( filtered_data.to_eid(0, filtered_data.event[:, 0]), [10, 5, 10, 1, 7, 7, 9, 7, 10, 1, 2, 1, 7, 6, 7, 6, 1, 9, 1, 6, 10])
def test_class(self): import numpy as np from kamrecsys.datasets import load_movielens_mini from kamrecsys.mf.pmf import EventScorePredictor data = load_movielens_mini() recommender = EventScorePredictor(C=0.1, k=2, tol=1e-03, random_state=1234) self.assertDictEqual( vars(recommender), {'C': 0.1, 'n_otypes': 0, 'bu_': None, 'bi_': None, 'k': 2, 'p_': None, 'q_': None, '_coef': None, 'f_loss_': np.inf, 'iid': None, 'i_loss_': np.inf, 'eid': None, 'tol': 1e-03, 'n_objects': None, '_dt': None, 'mu_': None, 'opt_outputs_': None, 'maxiter': 200, 'random_state': 1234, '_rng': None}) recommender.fit(data, disp=False) self.assertAlmostEqual(recommender.i_loss_, 0.74652578358324106, delta=1e-5) self.assertAlmostEqual(recommender.f_loss_, 0.025638738121075231, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 7)), 3.9873641434545979, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 9)), 4.9892118821609106, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 11)), 3.6480799850368273, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 7)), 3.6336318795279228, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 9)), 4.2482001235634943, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 11)), 3.7236984083417841, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 7)), 3.4141968145802597, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 9)), 3.9818882049478654, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 11)), 3.4710520150321895, delta=1e-5) x = np.array([ [1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose( recommender.predict(x), [3.98736414, 4.98921188, 3.64807999, 3.63363188, 4.24820012, 3.72369841, 3.41419681, 3.9818882, 3.47105202], rtol=1e-5)
def test_class(self): data = load_movielens_mini() data.binarize_score() rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose(rec.fit_results_['initial_loss'], 0.7665586528005817, rtol=1e-5) assert_allclose(rec.fit_results_['final_loss'], 0.07714505638740253, rtol=1e-5) # raw_predict assert_allclose(rec.raw_predict(np.array([[0, 6]])), 0.999880367504, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[8, 8]])), 0.976596331684, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[2, 10]])), 0.886485084449, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [0.999880367504, 0.976596331684, 0.886485084449], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 0.999880367504, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 0.985475882857, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 0.851425801703, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 0.890682628776, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 0.976596331684, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 0.932930852359, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 0.998741832467, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 0.962962765249, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 0.886485084449, rtol=1e-5) # multiple prediction x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(rec.predict(x), [ 0.999880367504, 0.985475882857, 0.851425801703, 0.890682628776, 0.976596331684, 0.932930852359, 0.998741832467, 0.962962765249, 0.886485084449 ], rtol=1e-5)
def test_class(self): rec = ExplicitItemFinder() data = load_movielens_mini() true_sc = np.where(data.score <= 3, 0, 1) data.binarize_score() rec.fit(data, event_index=(0, 1)) # get_score() assert_allclose(rec.get_score(), true_sc) # remove_data rec.remove_data() self.assertIsNone(rec.score) assert_allclose(rec.score_domain, [0, 1, 1]) self.assertEqual(rec.n_score_levels, 2)
def test_class(self): data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose(rec.fit_results_['initial_loss'], 0.7291206184050988, rtol=1e-5) assert_allclose(rec.fit_results_['final_loss'], 0.013777730279425669, rtol=1e-5) # raw_predict assert_allclose(rec.raw_predict(np.array([[0, 6]])), 3.997101590073, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[8, 8]])), 4.24151516373, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[2, 10]])), 3.498237262002, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [3.997101590073, 4.24151516373, 3.498237262002], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 3.997101590073, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 4.977093755711, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 3.62779086784, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 3.683330861026, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 4.24151516373, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 3.70802937382, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 3.521554946725, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 4.000964107588, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 3.498237262002, rtol=1e-5) # multiple prediction x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(rec.predict(x), [ 3.997101590073, 4.977093755711, 3.62779086784, 3.683330861026, 4.24151516373, 3.70802937382, 3.521554946725, 4.000964107588, 3.498237262002 ], rtol=1e-5)
def test_class(self): # setup data = load_movielens_mini() rec = EventRecommender() rec._set_object_info(data) # _set_event_info rec._set_event_info(data) self.assertEqual(rec.s_event, 2) assert_array_equal(rec.event_otypes, [0, 1]) self.assertEqual(rec.n_events, 30) assert_array_equal( rec.event[0, :], [rec.to_iid(0, 5), rec.to_iid(1, 2)]) assert_array_equal( rec.event[-1, :], [rec.to_iid(0, 10), rec.to_iid(1, 9)]) ts = sorted(rec.event_feature['timestamp']) assert_array_equal(ts[:2], [874965758, 875071561]) assert_array_equal(ts[-2:], [891352220, 891352864]) self.assertIsNone(rec.event_index) # fit rec.fit(data, event_index=(1, 0)) assert_array_equal(rec.event_index, [1, 0]) # get_event ev, n_objects = rec.get_event() assert_array_equal(ev[0, :], [rec.to_iid(1, 2), rec.to_iid(0, 5)]) assert_array_equal(ev[-1, :], [rec.to_iid(1, 9), rec.to_iid(0, 10)]) assert_array_equal(n_objects, [10, 8]) # predict self.assertEqual(rec.predict([0, 0]).ndim, 0) self.assertEqual(rec.predict([[0, 0]]).ndim, 0) self.assertEqual(rec.predict([[0, 0], [0, 1]]).ndim, 1) assert_array_equal(rec.predict([[0, 0], [0, 1]]).shape, (2, )) # remove_data rec.remove_data() self.assertEqual(rec.n_events, 30) self.assertIsNone(rec.event) self.assertIsNone(rec.event_feature) assert_array_equal(rec.event_index, (1, 0))
def test_class(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose(rec.fit_results_['initial_loss'], 1.3111240714555008, rtol=1e-5) assert_allclose(rec.fit_results_['final_loss'], 0.21148605953543897, rtol=1e-5) # raw_predict assert_allclose(rec.raw_predict(np.array([[0, 6]])), 0.98444253128, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[8, 8]])), 0.225589860753, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[2, 10]])), 0.090758250123, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [0.98444253128, 0.225589860753, 0.090758250123], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 0.98444253128, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 0.924884069088, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 0.952482724921, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 0.618744630907, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 0.225589860753, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 0.2295914768, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 0.00080319875342, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 0.0396585424477, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 0.0907582501228, rtol=1e-5) # multiple prediction x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(rec.predict(x), [ 0.98444253128, 0.924884069088, 0.952482724921, 0.618744630907, 0.225589860753, 0.2295914768, 0.00080319875342, 0.0396585424477, 0.0907582501228 ], rtol=1e-5)
def test_class(self): data = load_movielens_mini() data.binarize_score() rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose( rec.fit_results_['initial_loss'], 23.7963271809711, rtol=1e-5) assert_allclose( rec.fit_results_['final_loss'], 3.48393252113573, rtol=1e-5) # raw_predict assert_allclose( rec.raw_predict(np.array([[0, 6]])), 0.989201290366165, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[8, 8]])), 0.959375735295748, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[2, 10]])), 0.824034860051334, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [0.989201290366, 0.959375735296, 0.824034860051], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 0.989201290366165, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 0.974756390738565, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 0.816803762429987, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 0.854574774389645, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 0.959375735295748, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 0.900999255222352, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 0.888811789653219, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 0.9634835048284, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 0.824034860051334, rtol=1e-5) # multiple prediction x = np.array([ [1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose( rec.predict(x), [0.989201290366, 0.974756390739, 0.81680376243, 0.85457477439, 0.959375735296, 0.900999255222, 0.888811789653, 0.963483504828, 0.824034860051], rtol=1e-5)
def test_class(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose( rec.fit_results_['initial_loss'], 103.98807319757, rtol=1e-5) assert_allclose( rec.fit_results_['final_loss'], 13.8730554159163, rtol=1e-5) # raw_predict assert_allclose( rec.raw_predict(np.array([[0, 6]])), 0.997028296278715, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[8, 8]])), 0.131044842032382, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[2, 10]])), 0.058370496087627, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [0.9970282963, 0.131044842, 0.0583704961], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 0.997028296278715, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 0.944998324514573, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 0.964923027550838, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 0.624248406054208, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 0.131044842032382, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 0.174497501438718, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 4.07951069077663e-05, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 0.00230569601442113, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 0.058370496087627, rtol=1e-5) # multiple prediction x = np.array([ [1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose( rec.predict(x), [9.9702829628e-01, 9.4499832451e-01, 9.6492302755e-01, 6.2424840605e-01, 1.3104484203e-01, 1.7449750144e-01, 4.0795106908e-05, 2.3056960144e-03, 5.8370496088e-02], rtol=1e-5)
def test_class(self): # setup data = load_movielens_mini() rec = EventRecommender() rec._set_object_info(data) # _set_event_info rec._set_event_info(data) self.assertEqual(rec.s_event, 2) assert_array_equal(rec.event_otypes, [0, 1]) self.assertEqual(rec.n_events, 30) assert_array_equal( rec.event[0, :], [rec.to_iid(0, 5), rec.to_iid(1, 2)]) assert_array_equal( rec.event[-1, :], [rec.to_iid(0, 10), rec.to_iid(1, 9)]) ts = sorted(rec.event_feature['timestamp']) assert_array_equal(ts[:2], [874965758, 875071561]) assert_array_equal(ts[-2:], [891352220, 891352864]) self.assertIsNone(rec.event_index) # fit rec.fit(data, event_index=(1, 0)) assert_array_equal(rec.event_index, [1, 0]) # get_event ev, n_objects = rec.get_event() assert_array_equal(ev[0, :], [rec.to_iid(1, 2), rec.to_iid(0, 5)]) assert_array_equal(ev[-1, :], [rec.to_iid(1, 9), rec.to_iid(0, 10)]) assert_array_equal(n_objects, [10, 8]) # predict self.assertEqual(rec.predict([0, 0]).ndim, 0) self.assertEqual(rec.predict([[0, 0]]).ndim, 0) self.assertEqual(rec.predict([[0, 0], [0, 1]]).ndim, 1) assert_array_equal(rec.predict([[0, 0], [0, 1]]).shape, (2,)) # remove_data rec.remove_data() self.assertEqual(rec.n_events, 30) self.assertIsNone(rec.event) self.assertIsNone(rec.event_feature) assert_array_equal(rec.event_index, (1, 0))
def test_class(self): data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec.fit(data) assert_allclose( rec.fit_results_['initial_loss'], 11.6412258667768, rtol=1e-5) assert_allclose( rec.fit_results_['final_loss'], 0.683795023344032, rtol=1e-5) # raw_predict assert_allclose( rec.raw_predict(np.array([[0, 6]])), 3.98981537208499, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[8, 8]])), 4.25466828602487, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[2, 10]])), 3.45221824936513, rtol=1e-5) assert_allclose( rec.raw_predict(np.array([[0, 6], [8, 8], [2, 10]])), [3.9898153721, 4.254668286, 3.4522182494], rtol=1e-5) # single prediction assert_allclose(rec.predict((1, 7)), 3.98981537208499, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 4.96457075887952, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 3.64937487633011, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 3.56199613750802, rtol=1e-5) assert_allclose(rec.predict((3, 9)), 4.25466828602487, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 3.74991933999025, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 3.27152744041802, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 3.99212459353293, rtol=1e-5) assert_allclose(rec.predict((5, 11)), 3.45221824936513, rtol=1e-5) # multiple prediction x = np.array([ [1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose( rec.predict(x), [3.9898153721, 4.9645707589, 3.6493748763, 3.5619961375, 4.254668286, 3.74991934, 3.2715274404, 3.9921245935, 3.4522182494], rtol=1e-5)
def test_class(self): data = load_movielens_mini() rec = ScorePredictor() # fit() rec.fit(data, event_index=(0, 1)) assert_allclose(rec.score_domain, [1., 5., 1.]) assert_allclose(rec.score, true_sc) assert_equal(rec.n_score_levels, 5) # get_score() assert_allclose(rec.get_score(), true_sc) # remove_data rec.remove_data() self.assertIsNone(rec.score) assert_allclose(rec.score_domain, [1., 5., 1.]) assert_equal(rec.n_score_levels, 5)
def test__get_event_array(self): rec = ImplicitItemFinder() data = load_movielens_mini() data.filter_event( np.logical_and(data.event[:, 0] < 5, data.event[:, 1] < 5)) rec.fit(data) ev, n_objects = rec.get_event_array(sparse_type='array') assert_array_equal(ev[:5, :5], [[1, 1, 1, 1, 1], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 0]]) ev2, n_objects = rec.get_event_array('csr') assert_array_equal(ev, ev2.todense()) ev2, n_objects = rec.get_event_array('csc') assert_array_equal(ev, ev2.todense()) ev2, n_objects = rec.get_event_array('lil') assert_array_equal(ev, ev2.todense())
def test__get_event_array(self): rec = ImplicitItemFinder() data = load_movielens_mini() data.filter_event( np.logical_and(data.event[:, 0] < 5, data.event[:, 1] < 5)) rec.fit(data) ev, n_objects = rec.get_event_array(sparse_type='array') assert_array_equal( ev[:5, :5], [[1, 1, 1, 1, 1], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 0]]) ev2, n_objects = rec.get_event_array('csr') assert_array_equal(ev, ev2.todense()) ev2, n_objects = rec.get_event_array('csc') assert_array_equal(ev, ev2.todense()) ev2, n_objects = rec.get_event_array('lil') assert_array_equal(ev, ev2.todense())
def test_loss(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) n_objects = data.n_objects ev = sparse.coo_matrix((np.ones(data.n_events, dtype=int), (data.event[:, 0], data.event[:, 1])), shape=n_objects) ev = ev.tocsr() rec._init_coef(ev, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose(rec.loss(rec._coef, ev, n_objects), 1.3111240714555008, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose(rec.loss(rec._coef, ev, n_objects), 0.69314718055994518, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose(rec.loss(rec._coef, ev, n_objects), 3.1817153484891296, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose(rec.loss(rec._coef, ev, n_objects), 2.516800033025768, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose(rec.loss(rec._coef, ev, n_objects), 7.466231131678393, rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() data.binarize_score() rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -2.69422502892223, rtol=1e-5) assert_allclose( grad[1:5], [1.4860354254, 0.5813833517, -0.2378488265, -0.4549466355], rtol=1e-5) assert_allclose( grad[15:19], [-1.4192072182, -0.4856324281, -1.793371146, 1.1698467502], rtol=1e-5) assert_allclose( grad[19:23], [2.218012237, -2.8022576986, 0.7577517736, -0.189979212], rtol=1e-5) assert_allclose( grad[-4:], [-0.252934635, 2.569059726, 1.5578077993, -0.4423181343], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -6.0, rtol=1e-5) assert_allclose( grad[1:5], [0., 0., 0., -1.], rtol=1e-5) assert_allclose( grad[15:19], [-1., -0.5, -2., 0.5], rtol=1e-5) assert_allclose( grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose( grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 8.799214472271457, rtol=1e-5) assert_allclose( grad[1:5], [5.0330714908, 1.0866142982, 1.0866142982, 1.0732285963], rtol=1e-5) assert_allclose( grad[15:19], [2.0598428945, 1.0799214472, 0.0732285963, 2.0799214472], rtol=1e-5) assert_allclose( grad[19:23], [5.0330714908, 5.0330714908, 1.0866142982, 1.0866142982], rtol=1e-5) assert_allclose( grad[-4:], [0.0732285963, 0.0732285963, 2.0799214472, 2.0799214472], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 8.323751110862577, rtol=1e-5) assert_allclose( grad[1:5], [4.7101163359, 0.9545317778, 0.952133346, 0.9486417943], rtol=1e-5) assert_allclose( grad[15:19], [2.0612948847, 1.117042198, 0.1220469429, 2.1435828557], rtol=1e-5) assert_allclose( grad[19:23], [0.9920232672, 4.8101163359, 0.2389063556, 1.0445317778], rtol=1e-5) assert_allclose( grad[-4:], [-0.0089765286, 0.0420469429, 0.9967914278, 2.0535828557], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 8.999701753193309, rtol=1e-5) assert_allclose( grad[1:5], [5.0798783079, 1.0699742535, 1.0599646235, 1.0499642981], rtol=1e-5) assert_allclose( grad[15:19], [2.1899607439, 1.2049810091, 0.2199797513, 2.2349854785], rtol=1e-5) assert_allclose( grad[19:23], [11.0797319135, 2.4999606492, 2.3379421381, 0.9999954835], rtol=1e-5) assert_allclose( grad[-4:], [0.2059342372, 0.0799797513, 6.2829547023, 2.0899854785], rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() data.binarize_score() rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -0.0898075009641, rtol=1e-5) assert_allclose( grad[1:5], [0.0498308105, 0.0196757414, -0.0076319979, -0.0152389619], rtol=1e-5) assert_allclose( grad[15:19], [-0.0472451789, -0.0160642908, -0.060056816, 0.0394887188], rtol=1e-5) assert_allclose( grad[19:23], [0.0732353188, -0.0916441815, 0.0231358636, -0.0058694524], rtol=1e-5) assert_allclose( grad[-4:], [-0.0099831228, 0.084085786, 0.0506473455, -0.0145630614], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -0.2, rtol=1e-5) assert_allclose(grad[1:5], [0., 0., 0., -0.0333333333], rtol=1e-5) assert_allclose( grad[15:19], [-0.0333333333, -0.0166666667, -0.0666666667, 0.0166666667], rtol=1e-5) assert_allclose(grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose(grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 0.293307149076, rtol=1e-5) assert_allclose( grad[1:5], [0.1662875682, 0.0347389951, 0.0347389951, 0.0342928051], rtol=1e-5) assert_allclose( grad[15:19], [0.0671799483, 0.0345159001, 0.0009594717, 0.0678492334], rtol=1e-5) assert_allclose( grad[19:23], [0.1662875682, 0.1662875682, 0.0347389951, 0.0347389951], rtol=1e-5) assert_allclose( grad[-4:], [0.0009594717, 0.0009594717, 0.0678492334, 0.0678492334], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 0.277458370362, rtol=1e-5) assert_allclose( grad[1:5], [0.1570038779, 0.0316695778, 0.0314414819, 0.0311769487], rtol=1e-4) assert_allclose( grad[15:19], [0.0663394591, 0.0347162214, 0.0014015648, 0.068637947], rtol=1e-5) assert_allclose( grad[19:23], [0.0323267015, 0.1588557297, 0.0072228044, 0.0333362444], rtol=1e-5) assert_allclose(grad[-4:], [ -5.9551391499e-04, -7.9916718879e-05, 3.2930084632e-02, 6.6971280375e-02 ], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 0.29999005844, rtol=1e-5) assert_allclose( grad[1:5], [0.1681440917, 0.0346287714, 0.0344432652, 0.0342580692], rtol=1e-5) assert_allclose( grad[15:19], [0.0701838766, 0.0371289966, 0.0040733991, 0.0710180345], rtol=1e-5) assert_allclose( grad[19:23], [0.3648799527, 0.0818505402, 0.0733684416, 0.0318517013], rtol=1e-5) assert_allclose( grad[-4:], [0.0038126227, 0.0014808065, 0.206424416, 0.0683328493], rtol=1e-5)
def test_get_score_levels(self): data = load_movielens_mini() score_levels = data.get_score_levels() assert_allclose(score_levels, [1., 2., 3., 4., 5])
def test_loss(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) n_objects = data.n_objects ev = sparse.coo_matrix( (np.ones(data.n_events, dtype=int), (data.event[:, 0], data.event[:, 1])), shape=n_objects) ev = ev.tocsr() rec._init_coef(ev, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose( rec.loss(rec._coef, ev, n_objects), 103.98807319757, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose( rec.loss(rec._coef, ev, n_objects), 55.4517744447956, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose( rec.loss(rec._coef, ev, n_objects), 253.23722787913, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose( rec.loss(rec._coef, ev, n_objects), 200.293169308728, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose( rec.loss(rec._coef, ev, n_objects), 593.059407200938, rtol=1e-5)
def test_loss(self): # setup data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 11.6412258667768, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 235.5, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 38.2, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 18.7025, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 947.15920616, rtol=1e-5)
def test_loss(self): # setup data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 0.7291206184050988, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 15.7, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 2.4166666666666665, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 1.14175, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 62.720038744000014, rtol=1e-5)
def test_loss(self): # setup data = load_movielens_mini() data.binarize_score(3) rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 23.7963271809711, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 20.7944154167983, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 47.9014604546737, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 36.467883082757, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose( rec.loss(rec._coef, ev, sc, n_objects), 113.246948248756, rtol=1e-5)
def test_class(self): import numpy as np data = load_movielens_mini() rec = MultinomialPLSA(k=2, random_state=1234, tol=1e-8) # import logging # logging.getLogger('kamrecsys').addHandler(logging.StreamHandler()) rec.fit(data) assert_equal(rec.fit_results_['n_users'], 8) assert_equal(rec.fit_results_['n_items'], 10) assert_equal(rec.fit_results_['n_events'], 30) assert_(rec.fit_results_['success']) assert_equal(rec.fit_results_['status'], 0) assert_allclose(rec.fit_results_['initial_loss'], 5.41836900049, rtol=1e-5) assert_allclose(rec.fit_results_['final_loss'], 5.17361298499, rtol=1e-5) assert_equal(rec.fit_results_['n_iterations'], 38) assert_allclose(rec.score_levels, [1, 2, 3, 4, 5], rtol=1e-5) # output size assert_array_equal( rec.raw_predict(data.event[0].reshape(1, 2)).shape, (1, )) assert_array_equal(rec.raw_predict(data.event).shape, (30, )) rec.use_expectation = False assert_array_equal( rec.raw_predict(data.event[0].reshape(1, 2)).shape, (1, )) assert_array_equal(rec.raw_predict(data.event).shape, (30, )) rec.use_expectation = True # known user and item assert_allclose(rec.raw_predict(np.array([[0, 6]])), 3.64580117249, rtol=1e-5) assert_allclose(rec.raw_predict(np.array([[2, 8]])), 3.62184516985, rtol=1e-5) assert_allclose(rec.predict((1, 7)), 3.64580117249, rtol=1e-5) assert_allclose(rec.predict((1, 9)), 3.6587422493, rtol=1e-5) assert_allclose(rec.predict((5, 7)), 3.60707987724, rtol=1e-5) assert_allclose(rec.predict((5, 9)), 3.62184516985, rtol=1e-5) # known user and unknown item assert_allclose(rec.raw_predict(np.array([[2, 10]])), 3.62387542269, rtol=1e-5) assert_allclose(rec.predict((1, 11)), 3.66032199689, rtol=1e-5) assert_allclose(rec.predict((5, 12)), 3.62387542269, rtol=1e-5) # unknown user and known item assert_allclose(rec.raw_predict(np.array([[8, 6]])), 3.60821491793, rtol=1e-5) assert_allclose(rec.predict((3, 7)), 3.60821491793, rtol=1e-5) assert_allclose(rec.predict((11, 9)), 3.62304301551, rtol=1e-5) # unknown user and item assert_allclose(rec.raw_predict(np.array([[8, 10]])), 3.62507437787, rtol=1e-5) assert_allclose(rec.predict((3, 11)), 3.62507437787, rtol=1e-5) x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(rec.predict(x), [ 3.64580117249, 3.6587422493, 3.66032199689, 3.60821491793, 3.62304301551, 3.62507437787, 3.60707987724, 3.62184516985, 3.62387542269 ], rtol=1e-5) rec.use_expectation = False assert_allclose(rec.predict(x), [4., 5., 5., 4., 4., 4., 4., 4., 4.], rtol=1e-5)
def test_class(self): import numpy as np from kamrecsys.datasets import load_movielens_mini from kamrecsys.mf.pmf import EventScorePredictor data = load_movielens_mini() recommender = EventScorePredictor(C=0.1, k=2, tol=1e-03, random_state=1234) self.assertDictEqual( vars(recommender), { 'C': 0.1, 'n_otypes': 0, 'bu_': None, 'bi_': None, 'k': 2, 'p_': None, 'q_': None, '_coef': None, 'f_loss_': np.inf, 'iid': None, 'i_loss_': np.inf, 'eid': None, 'tol': 1e-03, 'n_objects': None, '_dt': None, 'mu_': None, 'opt_outputs_': None, 'maxiter': 200, 'random_state': 1234, '_rng': None }) recommender.fit(data, disp=False) self.assertAlmostEqual(recommender.i_loss_, 0.74652578358324106, delta=1e-5) self.assertAlmostEqual(recommender.f_loss_, 0.025638738121075231, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 7)), 3.9873641434545979, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 9)), 4.9892118821609106, delta=1e-5) self.assertAlmostEqual(recommender.predict((1, 11)), 3.6480799850368273, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 7)), 3.6336318795279228, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 9)), 4.2482001235634943, delta=1e-5) self.assertAlmostEqual(recommender.predict((3, 11)), 3.7236984083417841, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 7)), 3.4141968145802597, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 9)), 3.9818882049478654, delta=1e-5) self.assertAlmostEqual(recommender.predict((5, 11)), 3.4710520150321895, delta=1e-5) x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(recommender.predict(x), [ 3.98736414, 4.98921188, 3.64807999, 3.63363188, 4.24820012, 3.72369841, 3.41419681, 3.9818882, 3.47105202 ], rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) n_objects = data.n_objects ev = sparse.coo_matrix((np.ones(data.n_events, dtype=int), (data.event[:, 0], data.event[:, 1])), shape=n_objects) ev = ev.tocsr() rec._init_coef(ev, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 0.369344884225, rtol=1e-5) assert_allclose( grad[1:5], [-0.0163795867, 0.0766087539, 0.0494371859, 0.0523347865], rtol=1e-5) assert_allclose( grad[15:19], [-0.0079542759, 0.0300129557, 0.0221887768, 0.0444588993], rtol=1e-5) assert_allclose( grad[19:23], [-0.00388247, -0.0106748979, 0.047492578, -0.0228685915], rtol=1e-5) assert_allclose( grad[-4:], [0.0284519191, 0.0318876738, 0.0234999572, -0.0061334269], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 0.125, atol=1e-5) assert_allclose(grad[1:5], [-0.0625, 0.0375, 0.0375, 0.0125], rtol=1e-5) assert_allclose(grad[15:19], [-0.025, 0.0125, 0., 0.0125], rtol=1e-5) assert_allclose(grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose(grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 0.618307149076, rtol=1e-5) assert_allclose( grad[1:5], [0.0010152455, 0.1010152455, 0.1010152455, 0.0760152455], rtol=1e-5) assert_allclose( grad[15:19], [0.0261825668, 0.0636825668, 0.0511825668, 0.0636825668], rtol=1e-5) assert_allclose( grad[19:23], [0.0010152455, 0.0010152455, 0.1010152455, 0.1010152455], rtol=1e-5) assert_allclose( grad[-4:], [0.0511825668, 0.0511825668, 0.0636825668, 0.0636825668], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 0.603873544619, rtol=1e-5) assert_allclose( grad[1:5], [-0.0036235458, 0.0968966988, 0.0973867802, 0.0728492687], rtol=1e-4) assert_allclose( grad[15:19], [0.0262076293, 0.0640570655, 0.051891369, 0.0647118918], rtol=1e-5) assert_allclose( grad[19:23], [0.0002012168, -0.0017716939, 0.0202682287, 0.0985633655], rtol=1e-5) assert_allclose( grad[-4:], [0.0246493882, 0.0504098876, 0.030967057, 0.0630452251], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 0.624990535043, rtol=1e-5) assert_allclose( grad[1:5], [0.0014799603, 0.1012948868, 0.101109805, 0.0759247155], rtol=1e-5) assert_allclose( grad[15:19], [0.0285178333, 0.066295708, 0.0540735689, 0.0668514181], rtol=1e-5) assert_allclose(grad[19:23], [0.0055522045, 0.00185136, 0.2222005993, 0.0468513936], rtol=1e-5) assert_allclose( grad[-4:], [0.1678131716, 0.0514809763, 0.2142578479, 0.0641662329], rtol=1e-5)
def test_class(self): import numpy as np from kamrecsys.datasets import load_movielens_mini from kamrecsys.tm.plsa_multi import EventScorePredictor data = load_movielens_mini() rcmdr = EventScorePredictor(tol=1e-8, k=2, random_state=1234) self.assertDictEqual( vars(rcmdr), { "k": 2, "tol": 1e-8, "maxiter": 100, "alpha": 1.0, "random_state": 1234, "_rng": None, "iid": None, "eid": None, "n_objects": None, "n_otypes": 0, "n_score_levels_": 0, "i_loss_": np.inf, "f_loss_": np.inf, "n_iter_": 0, "pZ_": None, "pYgZ_": None, "pRgZ_": None, "pXgZ_": None, "n_events_": 0, "n_users_": 0, "n_items_": 0, "_q": None, "score_levels_": None, "use_expectation": True, }, ) # import logging # logging.getLogger('kamrecsys').addHandler(logging.StreamHandler()) rcmdr.fit(data) self.assertAlmostEqual(rcmdr.i_loss_, 5.41836900049, delta=1e-5) self.assertAlmostEqual(rcmdr.f_loss_, 5.17361298499, delta=1e-5) assert_allclose(rcmdr.score_levels_, [1, 2, 3, 4, 5], rtol=1e-5) # known user and item self.assertAlmostEqual(rcmdr.predict((1, 7)), 3.64580117249, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((1, 9)), 3.6587422493, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 7)), 3.60707987724, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 9)), 3.62184516985, delta=1e-5) # known user and unknown item self.assertAlmostEqual(rcmdr.predict((1, 11)), 3.66032199689, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 12)), 3.62387542269, delta=1e-5) # unknown user and known item self.assertAlmostEqual(rcmdr.predict((3, 7)), 3.60821491793, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((11, 9)), 3.62304301551, delta=1e-5) # unknown user and item self.assertAlmostEqual(rcmdr.predict((3, 11)), 3.62507437787, delta=1e-5) x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose( rcmdr.predict(x), [ 3.64580117249, 3.6587422493, 3.66032199689, 3.60821491793, 3.62304301551, 3.62507437787, 3.60707987724, 3.62184516985, 3.62387542269, ], rtol=1e-5, ) rcmdr.use_expectation = False assert_allclose(rcmdr.predict(x), [4.0, 5.0, 5.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0], rtol=1e-5)
def test_loss(self): # setup data = load_movielens_mini() data.binarize_score(3) rec = LogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 0.7665586528005817, rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 0.69314718055994495, rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 1.5567153484891236, rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 1.1832627694252338, rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) assert_allclose(rec.loss(rec._coef, ev, sc, n_objects), 3.6444649416252157, rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() rec = ImplicitLogisticPMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) n_objects = data.n_objects ev = sparse.coo_matrix( (np.ones(data.n_events, dtype=int), (data.event[:, 0], data.event[:, 1])), shape=n_objects) ev = ev.tocsr() rec._init_coef(ev, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 29.54759073801552, rtol=1e-5) assert_allclose( grad[1:5], [-1.3585150835, 6.1190706839, 3.9453452394, 4.1675236607], rtol=1e-5) assert_allclose( grad[15:19], [-0.6724531834, 2.3829809039, 1.7510280667, 3.5386563872], rtol=1e-5) assert_allclose( grad[19:23], [-0.3332963261, -0.7966485582, 3.730424055, -1.8144337114], rtol=1e-5) assert_allclose( grad[-4:], [2.2257145558, 2.5006539103, 1.8384101853, -0.4847956693], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 10.0, atol=1e-5) assert_allclose( grad[1:5], [-5., 3., 3., 1.], rtol=1e-5) assert_allclose( grad[15:19], [-2., 1., 0., 1.], rtol=1e-5) assert_allclose( grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose( grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 49.46457192605721, rtol=1e-5) assert_allclose( grad[1:5], [0.0330714908, 8.0330714908, 8.0330714908, 6.0330714908], rtol=1e-5) assert_allclose( grad[15:19], [2.0464571926, 5.0464571926, 4.0464571926, 5.0464571926], rtol=1e-5) assert_allclose( grad[19:23], [0.0330714908, 0.0330714908, 8.0330714908, 8.0330714908], rtol=1e-5) assert_allclose( grad[-4:], [4.0464571926, 4.0464571926, 5.0464571926, 5.0464571926], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 48.309883569540034, rtol=1e-5) assert_allclose( grad[1:5], [-0.2898836641, 7.7469210915, 7.7813127826, 5.813497054], rtol=1e-4) assert_allclose( grad[15:19], [2.0195733081, 5.0427133869, 4.0646428559, 5.085469863], rtol=1e-5) assert_allclose( grad[19:23], [-0.0079767328, -0.1898836641, 1.5973842183, 7.8369210915], rtol=1e-5) assert_allclose( grad[-4:], [1.962321428, 3.9846428559, 2.4677349315, 4.995469863], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, n_objects) assert_allclose(grad[0], 49.999242803470324, rtol=1e-5) assert_allclose( grad[1:5], [0.0798783079, 8.0698872383, 8.0598955089, 6.0499031689], rtol=1e-5) assert_allclose( grad[15:19], [2.1899451815, 5.204952933, 4.2199595873, 5.2349652997], rtol=1e-5) assert_allclose( grad[19:23], [0.2997319135, 0.0999606492, 17.6277516462, 3.6999633395], rtol=1e-5) assert_allclose( grad[-4:], [13.3258685412, 4.0799595873, 17.0428870887, 5.0899652997], rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -0.6389059157188512, rtol=1e-5) assert_allclose( grad[1:5], [1.0788090999, 0.464982617, -0.7963940914, -0.0692397823], rtol=1e-5) assert_allclose( grad[15:19], [-2.2818579721, -0.7857900494, -1.1561738739, 0.7205513543], rtol=1e-5) assert_allclose( grad[19:23], [0.1565786903, -2.9713197014, 0.4173437636, -0.0944053917], rtol=1e-5) assert_allclose( grad[-4:], [-0.2978489043, 1.6373689599, 0.6585058144, -0.2955021786], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -115, rtol=1e-5) assert_allclose( grad[1:5], [-36., -6., -7., -14.], rtol=1e-5) assert_allclose( grad[15:19], [-22., -10., -18., -9.], rtol=1e-5) assert_allclose( grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose( grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 35.0, rtol=1e-5) assert_allclose( grad[1:5], [14.1, 4.1, 3.1, 6.1], rtol=1e-5) assert_allclose( grad[15:19], [8.1, 5.1, 2.1, 6.1], rtol=1e-5) assert_allclose( grad[19:23], [14.1, 14.1, 4.1, 4.1], rtol=1e-5) assert_allclose( grad[-4:], [2.1, 2.1, 6.1, 6.1], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 0.6000000000000028, rtol=1e-5) assert_allclose( grad[1:5], [-0.5, 1.31, -0.28, 1.73], rtol=1e-4) assert_allclose( grad[15:19], [2.86, 2.27, -0.82, 3.69], rtol=1e-5) assert_allclose( grad[19:23], [-0.05, -0.4, 0.31, 1.4], rtol=1e-5) assert_allclose( grad[-4:], [-0.48, -0.9, 1.77, 3.6], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 234.84160000000003, rtol=1e-5) assert_allclose( grad[1:5], [78.28, 17.0564, 14.9512, 33.0668], rtol=1e-5) assert_allclose( grad[15:19], [49.89, 26.1354, 31.0472, 27.967], rtol=1e-5) assert_allclose( grad[19:23], [168.997, 37.31, 36.622072, 9.28216], rtol=1e-5) assert_allclose( grad[-4:], [101.688816, 30.9072, 86.899864, 27.822], rtol=1e-5)
def test_filter_event(self): from kamrecsys.data import EventWithScoreData # load movie_lens data = load_movielens_mini() # filter events filter_cond = np.arange(data.n_events) % 3 == 0 filtered_data = super(EventWithScoreData, data).filter_event(filter_cond) assert_array_equal(filtered_data.event[:, 0], [1, 5, 3, 4, 0, 0, 0, 2, 2, 0]) assert_array_equal(filtered_data.event[:, 1], [1, 3, 6, 5, 7, 6, 4, 0, 7, 2]) assert_array_equal(filtered_data.to_eid(0, filtered_data.event[:, 0]), data.to_eid(0, data.event[filter_cond, 0])) assert_array_equal(filtered_data.to_eid(1, filtered_data.event[:, 1]), data.to_eid(1, data.event[filter_cond, 1])) assert_array_equal(filtered_data.event_feature['timestamp'], [ 875636053, 877889130, 891351328, 879362287, 878543541, 875072484, 889751712, 883599478, 883599205, 878542960 ]) assert_array_equal(filtered_data.eid[0], [1, 5, 6, 7, 8, 10]) assert_array_equal(filtered_data.eid[1], [1, 2, 3, 4, 5, 7, 8, 9]) assert_equal(filtered_data.iid[0], { 1: 0, 5: 1, 6: 2, 7: 3, 8: 4, 10: 5 }) assert_equal(filtered_data.iid[1], { 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 7: 5, 8: 6, 9: 7 }) assert_equal( filtered_data.feature[0]['zip'], [u'85711', u'15213', u'98101', u'91344', u'05201', u'90703']) assert_equal(filtered_data.feature[1]['name'], [ u'Toy Story (1995)', u'GoldenEye (1995)', u'Four Rooms (1995)', u'Get Shorty (1995)', u'Copycat (1995)', u'Twelve Monkeys (1995)', u'Babe (1995)', u'Dead Man Walking (1995)' ]) # dummy event data data = EventData() data.set_event(np.tile(np.arange(5), (2, 2)).T) filtered_data = data.filter_event( [True, False, True, True, False, False, True, True, False, False]) assert_equal(filtered_data.n_events, 5) assert_array_equal(filtered_data.event, [[0, 0], [2, 2], [3, 3], [1, 1], [2, 2]])
def test_class(self): import numpy as np from kamrecsys.datasets import load_movielens_mini from kamrecsys.tm.plsa_multi import EventScorePredictor data = load_movielens_mini() rcmdr = EventScorePredictor(tol=1e-8, k=2, random_state=1234) self.assertDictEqual( vars(rcmdr), { 'k': 2, 'tol': 1e-8, 'maxiter': 100, 'alpha': 1.0, 'random_state': 1234, '_rng': None, 'iid': None, 'eid': None, 'n_objects': None, 'n_otypes': 0, 'n_score_levels_': 0, 'i_loss_': np.inf, 'f_loss_': np.inf, 'n_iter_': 0, 'pZ_': None, 'pYgZ_': None, 'pRgZ_': None, 'pXgZ_': None, 'n_events_': 0, 'n_users_': 0, 'n_items_': 0, '_q': None, 'score_levels_': None, 'use_expectation': True }) # import logging # logging.getLogger('kamrecsys').addHandler(logging.StreamHandler()) rcmdr.fit(data) self.assertAlmostEqual(rcmdr.i_loss_, 5.41836900049, delta=1e-5) self.assertAlmostEqual(rcmdr.f_loss_, 5.17361298499, delta=1e-5) assert_allclose(rcmdr.score_levels_, [1, 2, 3, 4, 5], rtol=1e-5) # known user and item self.assertAlmostEqual(rcmdr.predict((1, 7)), 3.64580117249, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((1, 9)), 3.6587422493, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 7)), 3.60707987724, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 9)), 3.62184516985, delta=1e-5) # known user and unknown item self.assertAlmostEqual(rcmdr.predict((1, 11)), 3.66032199689, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((5, 12)), 3.62387542269, delta=1e-5) # unknown user and known item self.assertAlmostEqual(rcmdr.predict((3, 7)), 3.60821491793, delta=1e-5) self.assertAlmostEqual(rcmdr.predict((11, 9)), 3.62304301551, delta=1e-5) # unknown user and item self.assertAlmostEqual(rcmdr.predict((3, 11)), 3.62507437787, delta=1e-5) x = np.array([[1, 7], [1, 9], [1, 11], [3, 7], [3, 9], [3, 11], [5, 7], [5, 9], [5, 11]]) assert_allclose(rcmdr.predict(x), [ 3.64580117249, 3.6587422493, 3.66032199689, 3.60821491793, 3.62304301551, 3.62507437787, 3.60707987724, 3.62184516985, 3.62387542269 ], rtol=1e-5) rcmdr.use_expectation = False assert_allclose(rcmdr.predict(x), [4., 5., 5., 4., 4., 4., 4., 4., 4.], rtol=1e-5)
def test_grad_loss(self): # setup data = load_movielens_mini() rec = PMF(C=0.1, k=2, random_state=1234, tol=1e-03) rec._rng = check_random_state(rec.random_state) ev = data.event sc = data.score n_objects = data.n_objects rec._init_coef(ev, sc, n_objects) # set array's view mu = rec._coef.view(rec._dt)['mu'] bu = rec._coef.view(rec._dt)['bu'] bi = rec._coef.view(rec._dt)['bi'] p = rec._coef.view(rec._dt)['p'] q = rec._coef.view(rec._dt)['q'] # initial parameters grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -0.0212968638573, rtol=1e-5) assert_allclose( grad[1:5], [0.0363059823, 0.0167339885, -0.0260526426, -0.0018141656], rtol=1e-5) assert_allclose( grad[15:19], [-0.0757162534, -0.02525473, -0.0393169069, 0.0252035637], rtol=1e-5) assert_allclose( grad[19:23], [0.0047896573, -0.0979586199, 0.012605792, -0.0028619178], rtol=1e-5) assert_allclose( grad[-4:], [-0.0108829851, 0.0536257719, 0.0211630636, -0.0097388071], rtol=1e-5) # all zero mu[0] = 0.0 bu[0][:] = 0.0 bi[0][:] = 0.0 p[0][:, :] = 0.0 q[0][:, :] = 0.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], -3.83333333333, rtol=1e-5) assert_allclose(grad[1:5], [-1.2, -0.2, -0.2333333333, -0.4666666667], rtol=1e-5) assert_allclose(grad[15:19], [-0.7333333333, -0.3333333333, -0.6, -0.3], rtol=1e-5) assert_allclose(grad[19:23], [0., 0., 0., 0.], rtol=1e-5) assert_allclose(grad[-4:], [0., 0., 0., 0.], rtol=1e-5) # all one mu[0] = 1.0 bu[0][:] = 1.0 bi[0][:] = 1.0 p[0][:, :] = 1.0 q[0][:, :] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 1.16666666667, rtol=1e-5) assert_allclose( grad[1:5], [0.4685185185, 0.1351851852, 0.1018518519, 0.2018518519], rtol=1e-5) assert_allclose( grad[15:19], [0.2685185185, 0.1685185185, 0.0685185185, 0.2018518519], rtol=1e-5) assert_allclose( grad[19:23], [0.4685185185, 0.4685185185, 0.1351851852, 0.1351851852], rtol=1e-5) assert_allclose( grad[-4:], [0.0685185185, 0.0685185185, 0.2018518519, 0.2018518519], rtol=1e-5) mu[0] = 1.0 bu[0][:] = np.arange(0.0, 0.8, 0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) + 1.0 p[0][:, 0] = 0.5 p[0][:, 1] = 1.0 q[0][:, 0] = 0.2 q[0][:, 1] = 1.0 grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 0.02, rtol=1e-5) assert_allclose( grad[1:5], [-0.0166666667, 0.0435185185, -0.0096296296, 0.0572222222], rtol=1e-4) assert_allclose(grad[15:19], [0.092962963, 0.0731481481, -0.03, 0.1201851852], rtol=1e-5) assert_allclose( grad[19:23], [-0.0024074074, -0.0148148148, 0.0095925926, 0.0451851852], rtol=1e-5) assert_allclose( grad[-4:], [-0.0162962963, -0.0314814815, 0.0587037037, 0.1185185185], rtol=1e-5) mu[0] = 2.0 bu[0][:] = np.arange(0.8, 0.0, -0.1) bi[0][:] = np.arange(0.0, 1.0, 0.1) * 1.5 + 1.0 p[0][:, 0] = np.arange(0.0, 0.8, 0.1) * 0.8 + 3 p[0][:, 1] = 1.0 q[0][:, 0] = np.arange(1.0, 0.0, -0.1) * 0.3 + 2 q[0][:, 1] = np.arange(0.0, 1.0, 0.1) grad = rec.grad_loss(rec._coef, ev, sc, n_objects) assert_allclose(grad[0], 7.82805333333, rtol=1e-5) assert_allclose( grad[1:5], [2.6081481481, 0.5675096296, 0.4974844444, 1.1014859259], rtol=1e-5) assert_allclose( grad[15:19], [1.6601851852, 0.868142963, 1.0316474074, 0.9287518519], rtol=1e-5) assert_allclose( grad[19:23], [5.6287888889, 1.2421851852, 1.2161727704, 0.3079238519], rtol=1e-5) assert_allclose( grad[-4:], [3.3865753481, 1.0290548148, 2.8936547259, 0.9260666667], rtol=1e-5)