Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: No module named crab.metrics.classes #112

Open
bala17 opened this issue Sep 16, 2017 · 0 comments
Open

ImportError: No module named crab.metrics.classes #112

bala17 opened this issue Sep 16, 2017 · 0 comments

Comments

@bala17
Copy link

bala17 commented Sep 16, 2017

ImportError: No module named crab.metrics.classes

installed crab . but unable import.

from pprint import pprint
import csv
from scikits.crab.models import MatrixPreferenceDataModel, MatrixBooleanPrefDataModel
from scikits.crab.metrics import pearson_correlation, euclidean_distances, jaccard_coefficient, cosine_distances, manhattan_distances, spearman_coefficient
from scikits.crab.similarities import ItemSimilarity, UserSimilarity
from scikits.crab.recommenders.knn import ItemBasedRecommender, UserBasedRecommender
from scikits.crab.recommenders.knn.neighborhood_strategies import NearestNeighborsStrategy
from scikits.crab.recommenders.knn.item_strategies import ItemsNeighborhoodStrategy
from scikits.crab.recommenders.svd.classes import MatrixFactorBasedRecommender
from scikits.crab.metrics.classes import CfEvaluator

"""
import random

fieldnames = ['user_id', 'item_id', 'star_rating']
with open('dataset-recsys.csv', "w") as myfile: # writing data to new csv file
writer = csv.DictWriter(myfile, delimiter = ',', fieldnames = fieldnames)
writer.writeheader()

for x in range(1, 21):
    items = random.sample(list(range(1, 41)), 20)
    random.randint(1,5)
    for item in items:        
        writer.writerow({'user_id': x, 'item_id': item, 'star_rating': random.randint(1, 5)})

"""

dataset = {}
with open('sample_movielens_data.txt') as myfile:
reader = csv.DictReader(myfile, delimiter=',')
i = 0
for line in reader:
i += 1
if (i == 1):
continue

    if (int(line['user_id']) not in dataset):
        dataset[int(line['user_id'])] = {}
        
    dataset[int(line['user_id'])][int(line['item_id'])] = float(line['star_rating'])

model = MatrixPreferenceDataModel(dataset)

User-based Similarity

similarity = UserSimilarity(model, cosine_distances)
neighborhood = NearestNeighborsStrategy()
recsys = UserBasedRecommender(model, similarity, neighborhood)

Item-based Similarity

similarity = ItemSimilarity(model, cosine_distances)
nhood_strategy = ItemsNeighborhoodStrategy()
recsys = ItemBasedRecommender(model, similarity, nhood_strategy, with_preference=False)

recsys = MatrixFactorBasedRecommender(model=model, items_selection_strategy=nhood_strategy, n_features=10, n_interations=1)

evaluator = CfEvaluator()

rmse = evaluator.evaluate(recsys, 'rmse', permutation=False)
mae = evaluator.evaluate(recsys, 'mae', permutation=False)
nmae = evaluator.evaluate(recsys, 'nmae', permutation=False)
precision = evaluator.evaluate(recsys, 'precision', permutation=False)
recall = evaluator.evaluate(recsys, 'recall', permutation=False)
f1score = evaluator.evaluate(recsys, 'f1score', permutation=False)

all_scores = evaluator.evaluate(recsys, permutation=False)
#all_scores = evaluator.evaluate(boolean_recsys, permutation=False)

result = evaluator.evaluate(recsys, None, permutation=False, at=10, sampling_ratings=0.7)

Cross Validation

result = evaluator.evaluate_on_split(recsys, 'rmse', permutation=False, at=10, cv=5, sampling_ratings=0.7)

pprint (result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant