from surprise.prediction_algorithms import KNNBasic
from model_selection.validation import cross_validate
def surprise_code():
reader = Reader(line_format="user item rating", sep=',', skip_lines=1)
data = Dataset.load_from_file('./ml-latest-small/ratings.csv', reader)
# print(len(data.raw_ratings))
algo = KNNBasic()
perf = cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=2, verbose=0)
print(perf)
def fit_and_score(algo, trainset, testset, measures,
return_train_measures=False):
start_fit = time.time()
algo.fit(trainset)
fit_time = time.time() - start_fit
start_test = time.time()
predictions = algo.test(testset)
test_time = time.time() - start_test
if return_train_measures:
pass
test_measures = dict()
train_measures = dict()
for m in measures:
f = getattr(accuracy, m.lower())
f(predictions, verbose=0) =
if return_train_measures:
pass
return test_measures, train_measures, fit_time, test_time
这里我们看一下 cross_validation() 函数的作用:
def cross_validate(algo, data, measures=['rmse', 'mae'], cv=None,
return_train_measures=False, n_jobs=1,
pre_dispatch='2*n_jobs', verbose=False):
measures = [m.lower() for m in measures]
cv = get_cv(cv)
delayed_list = (delayed(fit_and_score)(algo, trainset, testset, measures, return_train_measures)
for (trainset, testset) in cv.split(data))
out = Parallel(n_jobs=n_jobs, pre_dispatch=pre_dispatch)(delayed_list)
(test_measures_dicts,
train_measures_dicts,
fit_times,
test_times) = zip(*out)
test_measures = dict()
train_measures = dict()
ret = dict()
for m in measures:
# transformer tuple of dicts to dict of lists.
test_measures[m] = np.asarray([d[m] for d in test_measures_dicts])
ret['test_'+m] = test_measures[m]
if return_train_measures:
pass
ret['fit_time'] = fit_times
ret['test_time'] = test_times
if verbose:
pass
return ret
由于微信平台算法改版,公号内容将不再以时间排序展示,如果大家想第一时间看到我们的推送,强烈建议星标我们和给我们多点点【在看】。星标具体步骤为:
(1)点击页面最上方"AINLP",进入公众号主页。
(2)点击右上角的小点点,在弹出页面点击“设为星标”,就可以啦。
感谢支持,比心。
推荐阅读
征稿启示| 200元稿费+5000DBC(价值20个小时GPU算力)
完结撒花!李宏毅老师深度学习与人类语言处理课程视频及课件(附下载)
模型压缩实践系列之——bert-of-theseus,一个非常亲民的bert压缩方法
文本自动摘要任务的“不完全”心得总结番外篇——submodular函数优化
斯坦福大学NLP组Python深度学习自然语言处理工具Stanza试用
关于AINLP
AINLP 是一个有趣有AI的自然语言处理社区,专注于 AI、NLP、机器学习、深度学习、推荐算法等相关技术的分享,主题包括文本摘要、智能问答、聊天机器人、机器翻译、自动生成、知识图谱、预训练模型、推荐系统、计算广告、招聘信息、求职经验分享等,欢迎关注!加技术交流群请添加AINLPer(id:ainlper),备注工作/研究方向+加群目的。
阅读至此了,分享、点赞、在看三选一吧🙏