用户运营面临画像碎片化——知识图谱画像将标签覆盖率提40%,年化增收15万元
KG-Powered User Profiling — 知识图谱驱动的用户画像:产品知识增强推荐
Skill-KG-Powered-User-Profiling
建立品类知识图谱,清晰看到哪些是入口品/引流品/利润品,指导下一步选品扩张方向
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
1. 解决的问题
用户运营面临画像碎片化——知识图谱画像将标签覆盖率提40%,年化增收15万元
2. 核心算法逻辑
核心思想
传统协同过滤仅依赖 user-item 矩阵,缺乏对产品语义的理解,导致跨品类推荐能力弱。KG-Powered User Profiling 通过异构图融合将产品知识图谱(属性/认证/成分/适用年龄段)与用户行为图(购买/浏览/评价)结合,构建知识增强的用户偏好向量。
关键机制
1. 异构图结构
\mathcal{G} = \{(u, r_b, i) | u \in \mathcal{U}, i \in \mathcal{I}\} \cup \{(i, r_k, e) | i \in \mathcal{I}, e \in \mathcal{E}\}其中 $r_b$ 为行为关系(购买/浏览),$r_k$ 为知识关系(属于品类/含成分/具认证)。
2. 图路径偏好传播
用户购买产品 $i$,$i$ 具备属性 $e$(如"有机认证"),则用户对 $e$ 的偏好权重更新:
\text{pref}(u, e) = \sum_{i \in \mathcal{B}(u)} \text{score}(u, i) \cdot \mathbf{1}[(i, r_k, e) \in \mathcal{G}]3. 知识感知用户向量
\mathbf{u}_{\text{kg}} = \frac{1}{|\mathcal{E}_u|} \sum_{e \in \mathcal{E}_u} \text{pref}(u, e) \cdot \mathbf{e}_e其中 $\mathcal{E}_u$ 为用户通过购买历史激活的知识实体集合。
4. 冷启动处理
新用户无行为历史时,利用注册信息(宝宝月龄)直接在 KG 中检索"适用月龄"属性节点,得到初始偏好向量,解决冷启动问题。
技术要点
- 稀疏性处理:对低频行为做指数衰减权重(越近的行为权重越高)
- 多跳推理:支持 2 跳路径(用户→产品→品类→产品)发现间接关联
- 增量更新:新行为发生时仅更新相关实体权重,无需全图重算
3. 业务应用场景
业务背景:用户历史购买了有机奶粉,如何推荐有机辅食/有机婴儿护肤?
效果:跨越奶粉→辅食→婴儿护肤的品类边界,CTR 提升 18%,用户 LTV 增加。
业务背景:WF-D 选品阶段,发现用户购买了竞品 X 但未购买我方同类产品,通过 KG 分析竞品属性关联,找到用户未购买但知识关联强的品类。
4. 输入数据要求
未提取到输入条目;请核对原始 Skill 卡片与代码。
5. 输出结果
未提取到输出条目;请核对原始 Skill 卡片与代码。
6. 业务价值与 ROI
未自动抽取;请查看原始 Skill 卡片。
7. 代码模板
代码块数量:3
"""
KG-Powered User Profiling — 知识图谱驱动的用户画像
Python 标准库实现,无第三方依赖
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional
import math
import heapq
# ─────────────────────────────────────────────
# 数据结构
# ─────────────────────────────────────────────
@dataclass
class ProductKGNode:
"""产品知识图谱节点"""
product_id: str
name: str
category: str # 一级品类,如 "奶粉"
sub_category: str = "" # 二级品类,如 "有机奶粉"
attributes: dict[str, str] = field(default_factory=dict) # 如 {"成分": "DHA", "适用月龄": "0-6"}
certifications: list[str] = field(default_factory=list) # 如 ["EU有机认证", "FDA认证"]
tags: list[str] = field(default_factory=list) # 如 ["有机", "益生菌"]
price_tier: str = "mid" # low/mid/high
@dataclass
class UserAction:
"""用户行为记录"""
user_id: str
product_id: str
action_type: str # purchase/view/review
timestamp: float # Unix timestamp
score: float = 1.0 # 评分(review时有效)
# ─────────────────────────────────────────────
# 用户 KG 画像构建器
# ─────────────────────────────────────────────
class UserKGProfiler:
"""从行为历史推断用户知识偏好向量"""
# 行为类型基础权重
ACTION_WEIGHTS = {
"purchase": 3.0,
"review": 2.0,
"view": 0.5,
}
def __init__(
self,
kg_nodes: list[ProductKGNode],
decay_factor: float = 0.9,
max_age_days: float = 180.0,
):
"""
Args:
kg_nodes: 产品知识图谱节点列表
decay_factor: 时间衰减底数(越旧行为权重越低)
max_age_days: 最大有效行为天数
"""
self.decay_factor = decay_factor
self.max_age_days = max_age_days
self._kg: dict[str, ProductKGNode] = {n.product_id: n for n in kg_nodes}
def _time_weight(self, action_ts: float, now_ts: float) -> float:
"""时间衰减权重:指数衰减"""
age_days = (now_ts - action_ts) / 86400.0
if age_days > self.max_age_days:
return 0.0
return self.decay_factor ** (age_days / 30.0) # 按月衰减
def build_profile(
self,
actions: list[UserAction],
now_ts: Optional[float] = None,
) -> dict[str, float]:
"""
构建用户知识偏好向量。
Returns:
dict: {知识实体 -> 偏好权重},实体包括品类/认证/标签/属性值
"""
import time as _time
if now_ts is None:
now_ts = _time.time()
pref: dict[str, float] = {}
for action in actions:
node = self._kg.get(action.product_id)
if node is None:
continue
base_w = self.ACTION_WEIGHTS.get(action.action_type, 0.5)
time_w = self._time_weight(action.timestamp, now_ts)
w = base_w * time_w
if w < 1e-6:
continue
# 品类实体
for entity in [node.category, node.sub_category]:
if entity:
pref[f"cat:{entity}"] = pref.get(f"cat:{entity}", 0.0) + w
# 认证实体
for cert in node.certifications:
pref[f"cert:{cert}"] = pref.get(f"cert:{cert}", 0.0) + w * 1.5
# 标签实体
for tag in node.tags:
pref[f"tag:{tag}"] = pref.get(f"tag:{tag}", 0.0) + w
# 属性值实体
for attr_key, attr_val in node.attributes.items():
entity_key = f"attr:{attr_key}:{attr_val}"
pref[entity_key] = pref.get(entity_key, 0.0) + w * 0.8
# L1 归一化
total = sum(pref.values())
if total > 0:
pref = {k: v / total for k, v in pref.items()}
return pref
def cold_start_profile(self, baby_age_months: int) -> dict[str, float]:
"""
冷启动:根据宝宝月龄生成初始偏好向量
"""
if baby_age_months <= 6:
stage = "0-6m"
elif baby_age_months <= 12:
stage = "6-12m"
elif baby_age_months <= 24:
stage = "12-24m"
else:
stage = "24m+"
return {f"attr:适用月龄:{stage}": 1.0}
# ─────────────────────────────────────────────
# KG 增强推荐器
# ─────────────────────────────────────────────
class KGEnhancedRecommender:
"""知识路径增强的推荐分数计算"""
def __init__(
self,
kg_nodes: list[ProductKGNode],
profiler: UserKGProfiler,
alpha: float = 0.6, # KG 分数权重
beta: float = 0.4, # 流行度分数权重
):
self.profiler = profiler
self.alpha = alpha
self.beta = beta
self._kg: dict[str, ProductKGNode] = {n.product_id: n for n in kg_nodes}
# 简易流行度(按产品出现次数估算,实际场景可换为销量)
self._popularity: dict[str, float] = {pid: 1.0 for pid in self._kg}
def _kg_score(
self,
user_profile: dict[str, float],
node: ProductKGNode,
) -> float:
"""计算用户偏好向量与产品知识实体的匹配分数"""
score = 0.0
# 品类匹配
score += user_profile.get(f"cat:{node.category}", 0.0) * 1.0
score += user_profile.get(f"cat:{node.sub_category}", 0.0) * 1.2
# 认证匹配(权重更高)
for cert in node.certifications:
score += user_profile.get(f"cert:{cert}", 0.0) * 1.5
# 标签匹配
for tag in node.tags:
score += user_profile.get(f"tag:{tag}", 0.0) * 1.0
# 属性匹配
for attr_key, attr_val in node.attributes.items():
score += user_profile.get(f"attr:{attr_key}:{attr_val}", 0.0) * 0.8
return score
def recommend(
self,
user_id: str,
user_history: list[UserAction],
top_k: int = 10,
exclude_purchased: bool = True,
) -> list[tuple[str, float]]:
"""
生成 top-k 推荐列表。
Returns:
list of (product_id, score),按分数降序
"""
user_profile = self.profiler.build_profile(user_history)
purchased = set()
if exclude_purchased:
purchased = {a.product_id for a in user_history if a.action_type == "purchase"}
scored: list[tuple[float, str]] = []
for pid, node in self._kg.items():
if pid in purchased:
continue
kg_s = self._kg_score(user_profile, node)
pop_s = self._popularity.get(pid, 1.0)
# 归一化流行度到 [0,1]
max_pop = max(self._popularity.values()) if self._popularity else 1.0
pop_norm = pop_s / max_pop
final_score = self.alpha * kg_s + self.beta * pop_norm
scored.append((final_score, pid))
# 取 top_k
top = heapq.nlargest(top_k, scored, key=lambda x: x[0])
return [(pid, score) for score, pid in top]
# ─────────────────────────────────────────────
# 测试入口
# ─────────────────────────────────────────────
def _run_test() -> None:
"""5个产品 KG + 3个用户行为历史,验证知识增强推荐结果"""
import time
now = time.time()
one_week = 7 * 86400
# 构建产品 KG
kg = [
ProductKGNode(
product_id="P001", name="有机奶粉A",
category="奶粉", sub_category="有机奶粉",
attributes={"适用月龄": "0-6m", "成分": "DHA"},
certifications=["EU有机认证"], tags=["有机", "益生菌"],
),
ProductKGNode(
product_id="P002", name="有机米粉B",
category="辅食", sub_category="有机辅食",
attributes={"适用月龄": "6-12m", "成分": "铁锌"},
certifications=["EU有机认证"], tags=["有机", "无添加"],
),
ProductKGNode(
product_id="P003", name="婴儿护肤霜C",
category="护肤", sub_category="婴儿护肤",
attributes={"适用月龄": "0-6m", "成分": "金盏花"},
certifications=["EU有机认证"], tags=["有机", "敏感肌"],
),
ProductKGNode(
product_id="P004", name="普通奶粉D",
category="奶粉", sub_category="普通奶粉",
attributes={"适用月龄": "0-6m", "成分": "乳清蛋白"},
certifications=["FDA认证"], tags=["经济实惠"],
),
ProductKGNode(
product_id="P005", name="益生菌滴剂E",
category="营养补充", sub_category="益生菌",
attributes={"适用月龄": "0-12m", "成分": "乳双歧杆菌"},
certifications=["EU有机认证"], tags=["有机", "益生菌"],
),
]
profiler = UserKGProfiler(kg, decay_factor=0.9)
recommender = KGEnhancedRecommender(kg, profiler)
# 用户1:购买了有机奶粉A,偏好有机
u1_history = [
UserAction("u1", "P001", "purchase", now - one_week, 1.0),
UserAction("u1", "P001", "review", now - 3 * 86400, 5.0),
]
# 用户2:浏览了多个产品
u2_history = [
UserAction("u2", "P004", "purchase", now - 2 * one_week, 1.0),
UserAction("u2", "P002", "view", now - one_week, 1.0),
]
# 用户3:新用户冷启动(宝宝3个月)
cold_profile = profiler.cold_start_profile(baby_age_months=3)
# 验证推荐
recs_u1 = recommender.recommend("u1", u1_history, top_k=3)
recs_u2 = recommender.recommend("u2", u2_history, top_k=3)
print("=== KG-Powered User Profiling 测试 ===")
print(f"\n[用户1-有机偏好] 推荐结果(已排除购买过的P001):")
for pid, score in recs_u1:
node = next(n for n in kg if n.product_id == pid)
print(f" {pid} {node.name} | score={score:.4f} | certs={node.certifications}")
print(f"\n[用户2-普通偏好] 推荐结果:")
for pid, score in recs_u2:
node = next(n for n in kg if n.product_id == pid)
print(f" {pid} {node.name} | score={score:.4f}")
print(f"\n[用户3-冷启动 宝宝3个月] 偏好向量:{cold_profile}")
# 断言:用户1(有机偏好)推荐列表中,EU有机认证产品排前
u1_top1_pid = recs_u1[0][0]
u1_top1_node = next(n for n in kg if n.product_id == u1_top1_pid)
assert "EU有机认证" in u1_top1_node.certifications, \
f"期望推荐有机产品,实际推荐了 {u1_top1_node.name}"
print("\n✅ 断言通过:用户1首推为EU有机认证产品")
# 断言:冷启动偏好向量非空
assert len(cold_profile) > 0, "冷启动偏好向量不应为空"
print("✅ 断言通过:冷启动偏好向量构建成功")
print("\n全部测试通过 ✓")
if __name__ == "__main__":
_run_test()
print("[✓] KG Powered User Profiling 测试通过")8. 论文来源
论文链接用于核验方法来源,不自动证明本页商业结果。