Weighted sampler

From cas-bigdatalab/piflow · View interactive version

Weighted sampler is an AI agent skill from the GitHub repository cas-bigdatalab/piflow. 加权采样算子。读取JSONL记录,按数值型权重字段执行随机采样,支持有放回和无放回两种模式。当用户提到加权抽样、按权重抽样、优先级采样、重要性重采样等需求时使用此skill。即使用户没有明确说出“加权采样”,只要任务涉及按记录权重选择样本,就应该使用此skill。不负责按类别分层均衡、普通随机采样或文本内容过滤。

How to use this skill

Skill file

WeightedSampler - 加权采样算子

功能概述

根据记录的权重字段进行加权随机采样,权重越大的记录被选中的概率越高。支持有放回和无放回两种模式,适用于重要性采样、优先级抽样等场景。

触发条件

核心参数说明

必需参数

可选参数

输入文件格式

输入文件必须是JSONL,每行一个JSON对象。weight_field 对应的值必须能转换为数值;非数值、缺失、或小于等于 0 的记录会被跳过。

使用方法

python scripts/run_weighted_sampler.py --input data.jsonl --output sampled.jsonl --weight_field priority_score --sample_size 1000 --seed 42

输出示例

{"id": 12, "score": 101, "_sample_weight": 101.0, "_sample_normalized_weight": 0.22911217218528326}
{"id": 2, "score": 120, "_sample_weight": 120.0, "_sample_normalized_weight": 0.2722124818042969}

环境要求

注意事项

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.