python文本分类评价指标 top1如何计算_推荐系统排序(Ranking)评价指标

本文介绍了Python中计算文本分类评价指标的方法,包括准确率、召回率的计算代码,并详细阐述了Mean Average Precision (MAP)、Normalized Discounted Cumulative Gain (NDCG)的概念及计算过程,旨在帮助理解推荐系统和信息检索的评估标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、准确率(Precision)和召回率(Recall)

(令R(u)是根据用户在训练集上的行为给用户作出的推荐列表,而T(u)是用户在测试集上的行为列表。)

对用户u推荐N个物品(记为R(u)),令用户u在测试集上喜欢的物品集合为T(u),然后可以通过准确率/召回率评测推荐算法的精度:

准确率描述最终的推荐列表中有多少比例是发生过的用户—物品评分记录;

召回率描述有多少比例的用户—物品评分记录包含在最终的推荐列表中。

准确率和召回率计算方法的Python代码如下:

defRecall(train,test,N):

hit=0

all=0for user intrain.keys():

Tu=test[user]

rank=GetRecommendation(user,N)for item,pui inrank:if item inTu:

hit+=1all+=len(Tu)return hit/(all*1.0)defPrecision(train,test,N):

hit=0

all=0for user intrain.keys():

Tu=test[user]

rank=GetRecommendation(user,N)for item,pui inrank:if item inTu:

hit+=1all+=Nreturn hit/(all*1.0)

View Code

下面的Python代码同时计算出了一个推荐算法的准确率和召回率:

defPrecisionRecall(test, N):

hit=0

n_recall=0

n_precision=0for user, items intest.items():

rank=Recommend(user, N)

hit+= len(rank &items)

n_recall+=len(items)

n_precision+=Nreturn [hit / (1.0 * n_recall), hit / (1.0 * n_precision)]

View Code

有的时候,为了全面评测TopN推荐的准确率和召回率,一般会选取不同的推荐列表长度N,计算出一组准确率/召回率,然后画出准确率/召回率曲线(precision/recall curve)。

二、Mean average precision(MAP)&#x

### Python 熵权 TOPSIS 实现 #### 代码实现 下面展示了一个完整的基于 Numpy Pandas 的熵权 TOPSIS 方法的 Python 实现: ```python import numpy as np import pandas as pd def entropy_weight(data): """计算熵权""" p = data / data.sum(axis=0) e = -(p * np.log(p)).sum() / np.log(len(data)) w = (1 - e).sum() return w def topsis(df, weights=None, is_benefit=True): """ :param df: 输入的数据框,每一列代表一个指标 :param weights: 权重列表,默认为 None 表示使用熵权法自动计算权重 :param is_benefit: 列表表示各指标是否为效益型(True)还是成本型(False),默认全为 True """ # 数据标准化 norm_df = (df - df.min()) / (df.max() - df.min()) if not isinstance(weights, list): # 如果未提供权重,则通过熵权法求解 weights = entropy_weight(norm_df) # 加权后的矩阵 weighted_matrix = norm_df.mul(weights, axis=1) # 极大值极小值理想解 positive_ideal_solution = weighted_matrix.max().values negative_ideal_solution = weighted_matrix.min().values # 调整正负理想解方向 for i in range(len(is_benefit)): if not is_benefit[i]: temp = positive_ideal_solution[i] positive_ideal_solution[i] = negative_ideal_solution[i] negative_ideal_solution[i] = temp # 计算距离并归一化得分 distance_to_positive = ((weighted_matrix.values - positive_ideal_solution)**2).sum(axis=1)**0.5 distance_to_negative = ((weighted_matrix.values - negative_ideal_solution)**2).sum(axis=1)**0.5 score = distance_to_negative / (distance_to_positive + distance_to_negative) result = pd.DataFrame({ 'Score': score, 'Ranking': (-score).rank(method='min') }) return result.sort_values('Ranking'), weights if __name__ == '__main__': # 示例数据集 data = { "Cost": [897, 634, 567], "Profit": [12, 15, 10], "Time": [10, 12, 8] } df = pd.DataFrame(data=data) evaluation_result, calculated_weights = topsis( df=df, is_benefit=[False, True, False]) # Cost and Time are cost-type indicators; Profit is benefit type. print(evaluation_result) ``` 此段程序实现了熵权 TOPSIS 法的核心逻辑,并能够处理不同类型的评价指标[^2][^4]。 #### 性能评估标准 对于 Topsis 结果的质量评估主要依赖以下几个方面: - **稳定性**: 当输入数据发生微小变化时,最终排名应保持相对稳定。这可以通过多次运行算法并对结果的一致性进行检验来验证。 - **合理性**: 排名应当符合常识判断或领域专家的意见。例如,在企业绩效评估场景下,高盈利能力的企业应该获得较高的综合评分。 - **敏感度分析**: 测试不同的参数设置(如权重分配方式)对总体结论的影响程度。如果改变某些假设条件后得到截然相反的结果,则说明该模型可能存在问题。 - **可解释性**: 模型输出不仅限于简单的分数排序,还应对每个因素的重要性给予清晰阐述,便于使用者理解决策依据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值