python surprise

surprise 库


目录

参考文档

Surprise’ documentation
GitHub — Surprise

安装

With pip (you’ll need numpy, and a C compiler. Windows users might prefer using conda):

$ pip install numpy
$ pip install scikit-surprise

With conda:

$ conda install -c conda-forge scikit-surprise

For the latest version, you can also clone the repo and build the source (you’ll first need Cython and numpy):

$ pip install numpy cython
$ git clone https://github.com/NicolasHug/surprise.git
$ cd surprise
$ python setup.py install

预测算法

  1. 所有算法都由AlgoBase基类生成,基类里实现了一些关键的方法,(e.g. predict, fit and test).
  2. Every algorithm is part of the global Surprise namespace, so you only need to import their names from the Surprise package
    任何算法都是在surprise库的全局命名空间中,可以直接调用。

prediction_algorithms

算法类名说明解释
random_pred.NormalPredictorAlgorithm predicting a random rating based on the distribution of the training set, which is assumed to be normal.根据训练集的分布特征随机给出一个预测值
baseline_only.BaselineOnlyAlgorithm predicting the baseline estimate for given user and item.给定用户和Item,给出基于baseline的估计值
knns.KNNBasicA basic collaborative filtering algorithm.最基础的协作过滤
knns.KNNWithMeansA basic collaborative filtering algorithm, taking into account the mean ratings of each user.将每个用户评分的均值考虑在内的协作过滤实现
knns.KNNBaselineA basic collaborative filtering algorithm taking into account a baseline rating.考虑基线评级的协作过滤
matrix_factorization.SVDThe famous SVD algorithm, as popularized by Simon Funk during the Netflix Prize.SVD实现
matrix_factorization.SVDppThe SVD++ algorithm, an extension of SVD taking into account implicit ratings.SVD++,即LFM+SVD
matrix_factorization.NMFA collaborative filtering algorithm based on Non-negative Matrix Factorization.基于矩阵分解的协作过滤
slope_one.SlopeOneA simple yet accurate collaborative filtering algorithm.一个简单但精确的协作过滤算法
co_clustering.CoClusteringA collaborative filtering algorithm based on co-clustering.基于协同聚类的协同过滤算法

The algorithm base class 算法基类

The surprise.prediction_algorithms.algo_base module defines the base class AlgoBase from which every single prediction algorithm has to inherit.

  1. fit(trainset)

    Train an algorithm on a given training set.
    This method is called by every derived class as the first basic step for training an algorithm. It basically just initializes some internal structures and set the self.trainset attribute.
    Parameters: trainset (Trainset) – A training set, as returned by the folds method.
    Returns: self

    在一个给定的数据集上训练一个算法

Baselines estimates configuration 基线法估计配置

中用到最小化平方误差函数的算法(包括Baseline 方法 和相似度计算)都需要配置参数,不同的参数会导致算法有不同的性能,且不同的 baseline 用于不同的算法,参数配置不一样。
使用默认的baseline参数已经可以取得一定的性能的。
需要注意的是,一些相似度度量用到Baseline,无论实际的预测算法是否用到baseline,都需要配置相关参数
具体的参数配置,可以参考这篇论文
Factor in the Neighbors: Scalable and Accurate Collaborative Filtering

使用GridSearchCV调整算法参数

cross_validate()函数针对给定的一组参数报告交叉验证过程的准确性度量。 如果你想知道哪个参数组合可以产生最好的结果,那么GridSearchCV类就可以解决问题。 给定一个参数的字典,这个类彻底地尝试所有参数组合,并报告任何准确性度量(在不同分割上的平均值)的最佳参数。 它受到scikit-learn的GridSearchCV的启发。

Similarity measure configuration 相似度度量配置

Many algorithms use a similarity measure to estimate a rating. The way they can be configured is done in a similar fashion as for baseline ratings: you just need to pass a sim_options argument at the creation of an algorithm. This argument is a dictionary with the following (all optional) keys:

通过指定sim_options这个字典变量来配置相似度指标
1. name: 指定相似度指标的名字,similarities module 中给出了MSD
2. user_based: 指定是使用计算用户之间的相似度还是Item之间的相似度,这个地方的选择对预测算法的性能有巨大影响,默认值:True
3. min_support:当相似度不为0时,最小公共用户数或公共项目数
4. shrinkage: 收缩参数,仅用于 pearson_baseline 相似度

Trainset class

It is used by the fit() method of every prediction algorithm. You should not try to built such an object on your own but rather use the Dataset.folds() method or the DatasetAutoFolds.build_full_trainset() method.

训练集不应该由个人创建,可以通过Dataset.folds()或者DatasetAutoFolds.build_full_trainset()方法创建。

源码解读

reader 类

def __init__(self, name=None, line_format='user item rating',
            sep=None,rating_scale=(1, 5), skip_lines=0):

建立阅读器的格式,自动将评分定在rating_scale区间
self.offset : self.offset = -lower_bound + 1 if lower_bound <= 0 else 0

def parse_line(self, line)
    '''Parse a line.

        Ratings are translated so that they are all strictly positive.

        Args:
            line(str): The line to parse

        Returns:
            tuple: User id, item id, rating and timestamp. The timestamp is set  to ``None`` if it does no exist.
    '''

解析一行,返回需要的格式数据

dataset 类

def build_full_trainset(self):
    """Do not split the dataset into folds and just return a trainset as
    is, built from the whole dataset.

    User can then query for predictions, as shown in the :ref:`User Guide
    <train_on_whole_trainset>`.

    Returns:
        The :class:`Trainset <surprise.Trainset>`.
    """

将所有数据用于生成训练集

 def construct_trainset(self, raw_trainset):

建立 `raw_id` 到 `inner_id`的映射
得到 ur 字典 --- `用户-评分`字典
     ir 字典 --- `Item-评分`字典
     n_users --- 用户数
     n_items --- Item数
     n_ratings --- 评分记录数

建立训练集

trainset 类

KNNBaseline 类

  1. KNNBaseline
    KNNBaseline.png
    考虑基础评分的协同过滤
    最好用 pearson_baseline 相似度
    关于Baseline方法的作用,可以参考这篇文章
    推荐系统的协同过滤算法实现和浅析 pdf

KNNBasic 类

KNNBasic.png

def fit(self, trainset):
    """
    计算相似度
    计算公式的由来可以参考相关书籍
    """
def estimate(self, u, i):
    """
    估计用户u对物品i的打分
    找出在给物品i打过分的k个近邻用户
    根据相应的预测评分计算公式计算预测评分
    返回值: est, details({'actual_k': actual_k})
    """

example

库中,是预测所有用户没有评论过的物品的评分
不适合做Top-N推荐

split 数据分割

数据分割部分的分割操作都是针对所有数据集的操作,适合评分预测的应用,不适合Top-N推荐的应用(有待验证)

ShuffleSplit 类

shuffleSplit
将所有的训练数据随机地分成k份。

train_test_split
将数据分割为训练集和测试集割为训练集和测试集

<think>好的,我需要帮助用户解决如何在Python中安装Surprise库的问题。首先,我应该回顾用户提供的引用内容,看看有没有相关的安装指南。引用[2]提到用户遇到安装失败的问题,并提供了解决办法,比如可能需要安装依赖库或者手动下载资源包。引用[3]和[4]也提到了Surprise是scikit系列的一部分,可能需要特定的安装步骤。 首先,我应该检查常见的安装方法,比如使用pip安装。通常,安装Python库最直接的方式是运行`pip install scikit-surprise`,因为Surprise的正式名称是scikit-surprise。但用户可能因为网络问题或者缺少依赖导致失败,这时候需要建议他们使用镜像源,比如清华的镜像,或者安装必要的依赖项,比如C++编译工具链,因为某些库需要编译。 另外,引用[2]提到可能需要手动下载资源包,比如从PyPI下载whl文件然后安装。这时候应该指导用户如何找到对应版本的whl文件,尤其是注意Python版本和系统架构(32位或64位)。例如,用户需要访问PyPI的Surprise页面,找到合适的whl文件,然后用pip安装。 还需要考虑用户的环境,比如是否使用了虚拟环境,或者权限问题导致安装失败,这时候可能需要建议用户以管理员权限运行命令,或者使用`--user`选项。 另外,安装完成后,应该建议用户进行简单的测试,比如导入库并检查版本,以确保安装成功。 最后,用户可能遇到的问题可能包括缺少Microsoft Visual C++ Build Tools,这时候需要提醒他们安装相应的编译工具,或者使用预编译的whl文件来避免编译步骤。 总结一下,安装步骤应该包括: 1. 使用pip常规安装,加上镜像源加速。 2. 如果失败,尝试安装依赖项,如numpy和scipy。 3. 如果仍然失败,手动下载whl文件安装。 4. 处理可能的编译工具缺失问题。 5. 验证安装是否成功。 同时,需要将这些步骤清晰、分点列出,确保用户能够逐步跟随操作,避免技术术语过多,但保持准确。此外,要引用用户提供的参考资料,比如引用[2]提到的解决办法,引用[3]的安装指南,以及引用[4]关于Surprise的简介。</think>### 如何在Python中安装Surprise库? Surprise库是Python中用于构建推荐系统的重要工具,但安装过程中可能因环境或依赖问题导致失败。以下是详细的安装步骤及常见问题解决方法: --- #### **方法一:使用pip常规安装** 1. **直接安装** 在终端或命令提示符中运行以下命令: ```bash pip install scikit-surprise ``` *注意:Surprise库的正式名称为`scikit-surprise`,而非`surprise`[^3][^4]。* 2. **使用国内镜像加速** 若因网络问题下载缓慢或失败,可添加清华镜像源: ```bash pip install scikit-surprise -i https://pypi.tuna.tsinghua.edu.cn/simple ``` --- #### **方法二:解决依赖问题** Surprise依赖`numpy`和`scipy`,若未提前安装可能导致失败。建议先安装依赖: ```bash pip install numpy scipy ``` 再重新执行`pip install scikit-surprise`。 --- #### **方法三:手动安装(推荐用于失败场景)** 1. **下载预编译的whl文件** 访问PyPI页面([scikit-surprise下载链接](https://pypi.org/project/scikit-surprise/#files)),根据Python版本和系统选择对应的whl文件(例如`scikit_surprise-1.1.3-cp39-cp39-win_amd64.whl`表示Python 3.9的64位版本)[^2]。 2. **本地安装whl文件** 在whl文件所在目录运行: ```bash pip install 文件名.whl ``` --- #### **方法四:解决编译工具缺失问题** 若安装时提示`Microsoft Visual C++ 14.0 is required`,需安装以下工具之一: - **Windows用户**:下载并安装 [Microsoft C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/)。 - **替代方案**:使用预编译的whl文件(方法三),避免本地编译。 --- #### **验证安装** 安装完成后,运行以下Python代码测试: ```python import surprise print(surprise.__version__) # 预期输出:1.1.3 ``` --- ### **常见问题总结** | 问题类型 | 解决方法 | |-------------------|--------------------------------------------------------------------------| | 网络超时 | 使用镜像源(如清华源)加速下载 | | 依赖缺失 | 提前安装`numpy`和`scipy` | | 缺少编译工具 | 安装Microsoft C++ Build Tools或使用预编译whl文件 | | 权限不足 | 在命令中添加`--user`选项或以管理员身份运行终端 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值