本文整理匯總了Python中recommender.Recommender.set_strategy方法的典型用法代碼示例。如果您正苦於以下問題:Python Recommender.set_strategy方法的具體用法?Python Recommender.set_strategy怎麽用?Python Recommender.set_strategy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類recommender.Recommender
的用法示例。
在下文中一共展示了Recommender.set_strategy方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RecommenderTests
# 需要導入模塊: from recommender import Recommender [as 別名]
# 或者: from recommender.Recommender import set_strategy [as 別名]
class RecommenderTests(unittest2.TestCase):
@classmethod
def setUpClass(self):
cfg = Config()
cfg.popcon_index = "test_data/.sample_pxi"
cfg.popcon_dir = "test_data/popcon_dir"
cfg.clusters_dir = "test_data/clusters_dir"
self.rec = Recommender(cfg)
def test_set_strategy(self):
self.rec.set_strategy("cb")
self.assertIsInstance(self.rec.strategy,ContentBasedStrategy)
self.assertEqual(self.rec.strategy.content,"full")
self.rec.set_strategy("cbt")
self.assertIsInstance(self.rec.strategy,ContentBasedStrategy)
self.assertEqual(self.rec.strategy.content,"tag")
self.rec.set_strategy("cbd")
self.assertIsInstance(self.rec.strategy,ContentBasedStrategy)
self.assertEqual(self.rec.strategy.content,"desc")
self.rec.set_strategy("col")
self.assertIsInstance(self.rec.strategy,CollaborativeStrategy)
def test_get_recommendation(self):
user = User({"inkscape": 1, "gimp": 1, "eog":1})
result = self.rec.get_recommendation(user)
self.assertIsInstance(result, RecommendationResult)
self.assertGreater(len(result.item_score),0)
示例2: ContentBasedSuite
# 需要導入模塊: from recommender import Recommender [as 別名]
# 或者: from recommender.Recommender import set_strategy [as 別名]
class ContentBasedSuite(expsuite.PyExperimentSuite):
def reset(self, params, rep):
if params['name'].startswith("content"):
cfg = Config()
# if the index was not built yet
# app_axi = AppAptXapianIndex(cfg.axi,"results/arnaldo/AppAxi")
cfg.axi = "data/AppAxi"
cfg.index_mode = "old"
cfg.weight = params['weight']
self.rec = Recommender(cfg)
self.rec.set_strategy(params['strategy'])
self.repo_size = self.rec.items_repository.get_doccount()
self.user = LocalSystem()
self.user.app_pkg_profile(self.rec.items_repository)
self.user.no_auto_pkg_profile()
self.sample_size = int(
len(self.user.pkg_profile) * params['sample'])
# iteration should be set to 10 in config file
# self.profile_size = range(10,101,10)
def iterate(self, params, rep, n):
if params['name'].startswith("content"):
item_score = dict.fromkeys(self.user.pkg_profile, 1)
# Prepare partition
sample = {}
for i in range(self.sample_size):
key = random.choice(item_score.keys())
sample[key] = item_score.pop(key)
# Get full recommendation
user = User(item_score)
recommendation = self.rec.get_recommendation(user, self.repo_size)
# Write recall log
recall_file = "results/content/recall/%s-%s-%.2f-%d" % \
(params['strategy'], params[
'weight'], params['sample'], n)
output = open(recall_file, 'w')
output.write("# weight=%s\n" % params['weight'])
output.write("# strategy=%s\n" % params['strategy'])
output.write("# sample=%f\n" % params['sample'])
output.write("\n%d %d %d\n" %
(self.repo_size, len(item_score), self.sample_size))
notfound = []
ranks = []
for pkg in sample.keys():
if pkg in recommendation.ranking:
ranks.append(recommendation.ranking.index(pkg))
else:
notfound.append(pkg)
for r in sorted(ranks):
output.write(str(r) + "\n")
if notfound:
output.write("Out of recommendation:\n")
for pkg in notfound:
output.write(pkg + "\n")
output.close()
# Plot metrics summary
accuracy = []
precision = []
recall = []
f1 = []
g = Gnuplot.Gnuplot()
g('set style data lines')
g.xlabel('Recommendation size')
for size in range(1, len(recommendation.ranking) + 1, 100):
predicted = RecommendationResult(
dict.fromkeys(recommendation.ranking[:size], 1))
real = RecommendationResult(sample)
evaluation = Evaluation(predicted, real, self.repo_size)
accuracy.append([size, evaluation.run(Accuracy())])
precision.append([size, evaluation.run(Precision())])
recall.append([size, evaluation.run(Recall())])
f1.append([size, evaluation.run(F1())])
g.plot(Gnuplot.Data(accuracy, title="Accuracy"),
Gnuplot.Data(precision, title="Precision"),
Gnuplot.Data(recall, title="Recall"),
Gnuplot.Data(f1, title="F1"))
g.hardcopy(recall_file + "-plot.ps", enhanced=1, color=1)
# Iteration log
result = {'iteration': n,
'weight': params['weight'],
'strategy': params['strategy'],
'accuracy': accuracy[20],
'precision': precision[20],
'recall:': recall[20],
'f1': f1[20]}
return result
示例3: __init__
# 需要導入模塊: from recommender import Recommender [as 別名]
# 或者: from recommender.Recommender import set_strategy [as 別名]
class Survey:
def __init__(self):
logging.info("Setting up survey...")
self.cfg = Config()
self.rec = Recommender(self.cfg)
self.submissions_dir = "/var/www/AppRecommender/src/web/submissions/"
if not os.path.exists(self.submissions_dir):
os.makedirs(self.submissions_dir)
self.strategies = ["cbh", "cbh_eset",
"knn", "knn_eset", "knn_plus",
"knnco"]
def POST(self):
web_input = web.input(pkgs_file={})
if 'user_id' in web_input:
user_id = web_input['user_id'].encode('utf8')
user_dir = os.path.join(self.submissions_dir, user_id)
logging.info("New recommendation for user %s" % user_id)
uploaded_file = os.path.join(user_dir, "uploaded_file")
with open(uploaded_file) as uploaded:
if uploaded.readline().startswith('POPULARITY-CONTEST'):
user = PopconSystem(uploaded_file, user_id)
else:
user = PkgsListSystem(uploaded_file, user_id)
user.maximal_pkg_profile()
if len(user.pkg_profile) < 10:
error_msg = "Could not extract profile from uploaded file. It must have at least 10 applications." # noqa
logging.critical(error_msg)
return render.error([error_msg], "/survey/", "START")
else:
# Check the remaining strategies and select a new one
old_strategies = [dirs for root, dirs, files in
os.walk(os.path.join(self.submissions_dir,
user_id))]
if old_strategies:
strategies = [
s for s in self.strategies if s not in old_strategies[0]]
logging.info("Already used strategies %s" % old_strategies[0])
else:
strategies = self.strategies
if not strategies:
return render.thanks(user_id)
selected_strategy = random.choice(strategies)
logging.info("Selected \'%s\' from %s" %
(selected_strategy, strategies))
self.set_rec_strategy(selected_strategy)
prediction = self.rec.get_recommendation(user, 10).get_prediction()
logging.info("Prediction for user %s" % user_id)
logging.info(str(prediction))
self.save_prediction(user_id, selected_strategy, prediction)
# Load packages details
recommendation = [result[0] for result in prediction]
pkgs_details = []
for pkg_name in recommendation:
logging.info("Getting details of package %s" % pkg_name)
pkg = DebianPackage(pkg_name)
pkg.load_details()
pkgs_details.append(pkg)
if pkgs_details:
logging.info("Rendering survey slide...")
return render.survey(pkgs_details, user_id, selected_strategy,
len(strategies))
else:
return render.error(
["No recommendation produced for the uploaded file."],
"/survey/", "START")
def set_rec_strategy(self, selected_strategy):
k = 10
n = 20
if selected_strategy == "cbh":
pass
if selected_strategy == "cbh_eset":
pass
if selected_strategy == "knn":
pass
if selected_strategy == "knn_eset":
pass
if selected_strategy == "knn_plus":
pass
if selected_strategy == "knnco":
pass
self.rec.set_strategy(selected_strategy, k, n)
return selected_strategy
def save_prediction(self, user_id, strategy, prediction):
strategy_dir = os.path.join(self.submissions_dir, user_id, strategy)
if not os.path.exists(strategy_dir):
os.makedirs(strategy_dir)
ranking = 0
prediction_file = open(os.path.join(strategy_dir, "prediction"), "w")
try:
writer = csv.writer(prediction_file)
fieldnames = ('ranking', 'rating', 'package', 'evaluation')
writer.writerow(fieldnames)
for pkg, rating in prediction:
#.........這裏部分代碼省略.........