推荐系统:ImportError: cannot import name ‘evaluate‘ from ‘surprise

根据官方文档,evaluate()方法在1.0.5版中已弃用(功能上已由model_selection.cross_validate()取代),并在1.1.0版中删除

分析:

需要改两个地方

第一个是导入库:将from surprise import evaluate 换成
from surprise.model_selection import cross_validate即可。

第二个是应用:data.split(n_folds=5)和evaluate(svd, data, measures=['RMSE', 'MAE'])两处代码不能用,要换成cross_validate函数。

具体如下:

原始代码:

from surprise import Reader, Dataset, SVD,evaluate
reader = Reader()
ratings = pd.read_csv('data/ratings_small.csv')
ratings.head()

data = Dataset.load_from_df(ratings[['userId', 'movieId', 'rating']], reader)
data.split(n_folds=5)
svd = SVD()
evaluate(svd, data, measures=['RMSE', 'MAE'])

更改后的代码:

from surprise import Reader, Dataset, SVD
from surprise.model_selection import cross_validate
reader = Reader()
ratings = pd.read_csv('data/ratings_small.csv')
ratings.head()

data = Dataset.load_from_df(ratings[['userId', 'movieId', 'rating']], reader)
# data.split(n_folds=5)
svd = SVD()
# evaluate(svd, data, measures=['RMSE', 'MAE'])
cross_validate(algo=svd,data=data,measures=['RMSE', 'MAE'],cv=5,verbose=True)

参考资料:

运行出错 ImportError:cannot import name 'evaluate' from 'surprise' - 灰信网(软件开发博客聚合)

 AttributeError: 'DatasetAutoFolds' object has no attribute 'split' - Stack Overflow

你可能感兴趣的:(#,Python各种问题报错,深度学习,python,推荐算法)