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

如何在R中为类别标签列表定义聚合流行度评分

评分计算思路

你可以根据业务场景选择对应的聚合逻辑,常用的方案有3种:

  • 平均流行度评分:将对象关联的所有类别流行度求平均值,消除类别数量对总分的影响,适合类别数量差异大、需要公平比较不同类别数量对象的场景
  • 归一化总和评分:将流行度总和除以全局最大可能值压缩到0-1区间,分数越高代表对象绑定的热门类别越多,适合要优先推荐覆盖更多热门类对象的场景
  • 加权组合评分:按业务权重组合总和、最大值、小众类别奖励等多个维度,可灵活适配不同业务需求

可运行实现代码

library(tidyverse)

# 原始数据
df <- tribble(
  ~object, ~category,
  1, "Software, Model, Cloud",
  2, "Model",
  3, "Cloud, Software",
  4, "Train, Test, Model",
  5, "Test, Model"
)

# 步骤1:计算每个类别的流行度
category_pop <- df %>%
  separate_rows(category, sep = ",\\s*") %>% # 拆分多类别同时自动去除逗号后的空格
  count(category, name = "n_count") %>%
  arrange(desc(n_count))

# 步骤2:关联流行度并计算聚合指标与评分
result <- df %>%
  separate_rows(category, sep = ",\\s*") %>%
  left_join(category_pop, by = "category") %>%
  group_by(object) %>%
  summarise(
    category_list = paste(category, collapse = ", "), # 保留原始类别组合
    sum_category = sum(n_count),
    min_category = min(n_count),
    max_category = max(n_count),
    # 评分1:平均流行度
    score_avg = mean(n_count),
    # 评分2:0-1区间归一化总和评分
    score_norm_sum = sum(n_count)/(max(category_pop$n_count)*n()),
    # 评分3:加权组合评分(示例权重:70%总和+30%小众类别覆盖奖励)
    score_weighted = sum(n_count)*0.7 + (min_category < max(category_pop$n_count))*2*0.3
  )

# 输出结果
print(result)

运行结果参考

# A tibble: 5 × 8
  object category_list         sum_category min_category max_category score_avg score_norm_sum score_weighted
   <dbl> <chr>                        <int>        <int>        <int>     <dbl>          <dbl>          <dbl>
1      1 Software, Model, Cloud           8            2            4      2.67          0.667           6.2 
2      2 Model                            4            4            4      4             1               2.8 
3      3 Cloud, Software                  4            2            2      2             0.5             3.4 
4      4 Train, Test, Model               7            1            4      2.33          0.583           5.5 
5      5 Test, Model                      6            2            4      3             0.75            4.8 

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 10:06:04