Пример #1
0
def test_basic_methods_MatrixPreferenceDataModel():
    #Empty Dataset
    model = MatrixPreferenceDataModel({})
    assert_equals(model.dataset, {})
    assert_array_equal(np.array([]), model.user_ids())
    assert_array_equal(np.array([]), model.item_ids())
    assert_equals(True, model.has_preference_values())
    assert_equals(0, model.users_count())
    assert_equals(0, model.items_count())
    assert_equals(-np.inf, model.maximum_preference_value())
    assert_equals(np.inf, model.minimum_preference_value())

    assert("MatrixPreferenceDataModel (0 by 0)" in model.__str__())

    #Simple DataSet
    model = MatrixPreferenceDataModel(movies)
    assert_equals(model.dataset, movies)
    assert_array_equal(np.array(['Leopoldo Pires', 'Lorena Abreu', 'Luciana Nunes',
      'Marcel Caraciolo', 'Maria Gabriela', 'Penny Frewman', 'Sheldom', 'Steve Gates']),
      model.user_ids())
    assert_array_equal(np.array(['Just My Luck', 'Lady in the Water', 'Snakes on a Plane',
               'Superman Returns', 'The Night Listener', 'You, Me and Dupree']), model.item_ids())
    assert_equals(True, model.has_preference_values())
    assert_equals(8, model.users_count())
    assert_equals(6, model.items_count())
    assert_equals(5.0, model.maximum_preference_value())
    assert_equals(1.0, model.minimum_preference_value())
    assert_equals([('Just My Luck', 3.0), ('Lady in the Water', 2.5),
             ('Snakes on a Plane', 3.5), ('Superman Returns', 3.5),
             ('The Night Listener', 3.0), ('You, Me and Dupree', 2.5)], model['Marcel Caraciolo'])
    elements = [pref  for pref in model]
    assert_array_equal([('Lady in the Water', 2.5), ('Snakes on a Plane', 3.0), \
         ('Superman Returns', 3.5), ('The Night Listener', 4.0)], elements[0][1])

    assert("MatrixPreferenceDataModel (8 by 6)" in model.__str__())

    #SampleSongs DataSet
    songs = load_sample_songs()
    model = MatrixPreferenceDataModel(songs.data)
    assert_equals(model.dataset, songs.data)
    assert_array_equal(np.array([1, 2, 3, 4, 5, 6, 7, 8]),
      model.user_ids())
    assert_array_equal(np.array([1, 2, 3, 4, 5, 6, 7, 8]), model.item_ids())
    assert_equals(True, model.has_preference_values())
    assert_equals(8, model.users_count())
    assert_equals(8, model.items_count())
    assert_equals(5.0, model.maximum_preference_value())
    assert_equals(1.0, model.minimum_preference_value())
    assert_equals([(1, 2.5), (2, 3.5), (3, 5.0), (4, 2.0), (5, 4.5), (6, 1.5), (7, 2.0)], model[1])
    elements = [pref  for pref in model]
    assert_array_equal([(1, 2.5), (2, 3.5), (3, 5.0), (4, 2.0), (5, 4.5), (6, 1.5), (7, 2.0)],
            elements[0][1])
    assert("MatrixPreferenceDataModel (8 by 8)" in model.__str__())
Пример #2
0
def test__iter__ItemSimilarity():
    #MATRIXMODEL
    model = MatrixPreferenceDataModel(movies)
    similarity = ItemSimilarity(model, cosine_distances, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, pearson_correlation)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)
    similarity = ItemSimilarity(model, sorensen_coefficient, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, jaccard_coefficient)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())
Пример #3
0
def test_get_similarities__ItemSimilarity():
    #MatrixModel
    model = MatrixPreferenceDataModel(movies)

    similarity = ItemSimilarity(model, cosine_distances, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Pearson Without limits
    similarity = ItemSimilarity(model, pearson_correlation)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, euclidean_distances)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)

    similarity = ItemSimilarity(model, jaccard_coefficient, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Sorensen Without limits
    similarity = ItemSimilarity(model, sorensen_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())
Пример #4
0
def test__iter__ItemSimilarity():
    #MATRIXMODEL
    model = MatrixPreferenceDataModel(movies)
    similarity = ItemSimilarity(model, cosine_distances, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, pearson_correlation)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)
    similarity = ItemSimilarity(model, sorensen_coefficient, 3)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 3)

    similarity = ItemSimilarity(model, jaccard_coefficient)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), 0)

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    item_ids = []
    prefs = []
    for item_id, preferences in similarity:
        item_ids.append(item_id)
        prefs.append(preferences)
    assert_equals(len(item_ids), model.items_count())

    for pref in prefs:
        assert_equals(len(pref), model.items_count())
Пример #5
0
def test_get_similarities__ItemSimilarity():
    #MatrixModel
    model = MatrixPreferenceDataModel(movies)

    similarity = ItemSimilarity(model, cosine_distances, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Pearson Without limits
    similarity = ItemSimilarity(model, pearson_correlation)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, euclidean_distances)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, manhattan_distances, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    #MatrixBooleanPrefDataModel
    model = MatrixBooleanPrefDataModel(movies)

    similarity = ItemSimilarity(model, jaccard_coefficient, 3)

    sim = similarity.get_similarities('Snakes on a Plane')

    assert_equals(len(sim), model.items_count())

    #Sorensen Without limits
    similarity = ItemSimilarity(model, sorensen_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, loglikehood_coefficient, 0)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())

    similarity = ItemSimilarity(model, sorensen_coefficient, 20)

    sim = similarity.get_similarities('Lady in the Water')

    assert_equals(len(sim), model.items_count())