You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Surprise库SVD计算MAE和RMSE时出现Singleton array报错如何解决

报错原因

你错误导入了sklearn.model_selection下的train_test_split方法来拆分Surprise库的Dataset对象。sklearn的拆分方法只能识别数组、DataFrame等常规数据结构,无法处理Surprise自定义的Dataset类型,就会把它识别为单元素对象数组,抛出上述报错。

修复方案
  1. 修改导入语句,替换为Surprise库自带的拆分方法:
# 删掉原有sklearn的train_test_split导入(如果存在的话)
# from sklearn.model_selection import train_test_split
# 新增Surprise自带拆分方法的导入
from surprise.model_selection import train_test_split

如果你的代码同时需要用到sklearn的train_test_split,可以给Surprise的拆分方法设置别名避免冲突:

from surprise.model_selection import train_test_split as surprise_tts
# 拆分时调用别名方法即可
trainSet, testSet = surprise_tts(data, test_size=.25, random_state=0)
  1. 额外优化提示:Surprise的accuracy模块本身就内置了MAE、RMSE计算逻辑,不需要额外封装函数,可直接调用:
from surprise import accuracy

# 训练、预测逻辑和之前保持一致
print("RMSE: ", accuracy.rmse(predictions, verbose=False))
print("MAE: ", accuracy.mae(predictions, verbose=False))

修改后重新运行代码即可正常计算指标。

内容的提问来源于stack exchange,提问作者hubbaka ghoyati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 08:54:07