# 规则算子 [算子目录与公共示例环境](index.md) 各段代码独立使用公共示例变量。可选算法的可用状态和完整构造参数见本机节点属性。 ## 业务规则 `define_rule` 用已有输入特征编写一条可评估、可组合的业务规则。 - 输入 `in` → `samples`。接收 分类样本 - 输出 候选规则集 expression 返回逐行命中结果。规则只能引用当前样本的候选字段。输出可接规则集策略。 底层 API `hscredit.core.rules.Rule` | 配置项 | 初始值 | 作用 | | --- | --- | --- | | `name` | `'业务规则'` | 规则名称 | | `description` | `''` | 规则说明 | | `expression` | `''` | 规则表达式 | **调用示例** ```python business_rule = flow.define_rule(samples, name='年轻客户', expression='age_year < 25') ``` ## 规则集策略 `combine_rules` 把同一份样本上的候选规则组合成可预测的策略。 - 输入 `group` 候选规则组(候选规则集,可多接) - 输出 分类模型或规则制品 将多个规则来源依次接到同一候选规则组端口,logic='or' 表示任一命中,'and' 表示同时命中。各组须共享同一个样本划分。 底层 API `hscredit.core.models.combine_rules`、`hscredit.core.models.RulesClassifier` | 配置项 | 初始值 | 作用 | | --- | --- | --- | | `logic` | `'or'` | 组合逻辑 可选 or、and。 | | `name` | `'组合策略'` | 规则集名称 | | `description` | `''` | 规则集说明 | **调用示例** ```python strategy = flow.combine_rules(business_rule, logic='or', name='年龄策略') ``` ## 自定义规则 `custom_rules` 连接数据挖掘的候选规则,将策略输出接到分类与规则评估。 - 输入 `rules` 候选规则(候选规则集) - 输出 `out` 规则策略(分类模型或规则制品) 在右栏保留入口签名 def run(rules):,按端口返回约定的 Python 对象。 | 配置项 | 初始值 | 作用 | | --- | --- | --- | | `code` | 见节点编辑器 | 使用本类别预定义的 run 签名,完整示例见下方。 | **调用示例** ```python code = """def run(rules): from hscredit.web import flow_operators as flow return flow.combine_rules(rules, logic="or") """ result = custom.run_operator('custom_rules', code, rules=business_rule) ```