Beispiel #1
0
    def test_load_sparse(self):
        bunch = load_lastfm(cache=True, as_sparse=True)
        X = bunch.ratings
        assert sparse.issparse(X)

        # Assert on artists
        artists = bunch.artists
        assert isinstance(artists, np.ndarray)

        # Show that the next time we load this after caching, it's the same ref
        assert load_lastfm(cache=True, as_sparse=True) is bunch
Beispiel #2
0
    def test_load_bunch(self):
        u, i, r = unpack_bunch(load_lastfm(cache=True))

        # Show the users have NOT been encoded
        for array in (u, i):
            unq = np.sort(np.unique(array))
            with pytest.raises(AssertionError):
                assert_array_equal(unq, np.arange(unq.shape[0]))
Beispiel #3
0
from reclab.datasets import load_lastfm
from reclab.model_selection import train_test_split
from reclab.model_deployment import RecommenderDeployment
from reclab.collab import AlternatingLeastSquares

from scipy import sparse
from sklearn.preprocessing import LabelEncoder
from sklearn.externals import joblib
from numpy.testing import assert_array_equal

import pytest
import warnings
import os

# Load data and split into train/test
lastfm = load_lastfm(cache=True, as_sparse=True)
train, test = train_test_split(lastfm.ratings, random_state=42)


class TestRecommenderDeployment(object):
    def test_simple_deployment(self):
        als = AlternatingLeastSquares(factors=10, use_cg=False, iterations=3)
        als.fit(train)
        recs1 = als.recommend_for_user(0, test)

        deployment = RecommenderDeployment(estimator=als)
        recs2 = deployment.recommend_for_user(0, test[0, :].toarray()[0])
        assert_array_equal(recs1, recs2)

    def test_encoded_deployment(self):
        users = ['adam', 'betty', 'betty', 'frank', 'frank']