离散值特征分析是一种用于处理和分析离散特征(即取值为有限个不同类别的特征)的技术。该分析方法包括统计离散特征的分布情况,计算每个离散值的Gini指数和熵(Entropy),以及评估特征的重要性指标,如Gini增益(Gini Gain)、信息增益(Information Gain)和信息增益比(Information Gain Ratio),以便选择对模型性能影响最大的特征。
配置组件
方式一:可视化方式
在Designer工作流页面添加离散值特征分析组件,并在界面右侧配置相关参数:
参数 | 描述 |
特征列 | 用来表现训练样本数据特征的列。 |
标签列 | 标签字段。 |
稀疏矩阵 | 当输入表数据为稀疏格式时,需要设置KV格式的特征。 |
方式二:PAI命令方式
使用PAI命令配置离散值特征分析组件参数。您可以使用SQL脚本组件进行PAI命令调用,详情请参见场景4:在SQL脚本组件中执行PAI命令。
PAI
-name enum_feature_selection
-project algo_public
-DinputTableName=enumfeautreselection_input
-DlabelColName=label
-DfeatureColNames=col0,col1
-DenableSparse=false
-DoutputCntTableName=enumfeautreselection_output_cntTable
-DoutputValueTableName=enumfeautreselection_output_valuetable
-DoutputEnumValueTableName=enumfeautreselection_output_enumvaluetable;
参数名称 | 是否必选 | 默认值 | 描述 |
inputTableName | 是 | 无 | 输入表的名称。 |
inputTablePartitions | 否 | 默认选择全表 | 输入表中,参与训练的分区。系统支持以下格式:
说明 指定多个分区时,分区之间使用英文逗号(,)分隔。 |
featureColNames | 否 | 无 | 输入表中,用于训练的特征列名。 |
labelColName | 否 | 无 | 输入表中,标签列的名称。 |
enableSparse | 否 | false | 输入数据是否为稀疏格式,取值范围为{true,false}。 |
kvFeatureColNames | 否 | 默认选择全表 | KV格式的特征。 |
kvDelimiter | 否 | 英文冒号(:) | 当输入表数据为稀疏格式时,key和value之间的分隔符。 |
itemDelimiter | 否 | 英文逗号(,) | 当输入表数据为稀疏格式时,KV对之间的分隔符。 |
outputCntTableName | 否 | 不涉及 | 输出离散特征的枚举值分布数表。 |
outputValueTableName | 否 | 不涉及 | 输出离散特征的gini、entropy表。 |
outputEnumValueTableName | 否 | 不涉及 | 输出离散特征枚举值gini、entropy表。 |
lifecycle | 否 | 无 | 表的生命周期。 |
coreNum | 否 | 系统自动分配 | 计算的核心数,取值范围为正整数。 |
memSizePerCore | 否 | 系统自动分配 | 每个核心的内存,取值范围为1 MB~65536 MB。 |
使用示例
使用如下SQL语句,生成输入数据:
drop table if exists enum_feature_selection_test_input;
create table enum_feature_selection_test_input
as
select
*
from
(
select
'00' as col_string,
1 as col_bigint,
0.0 as col_double
union all
select
cast(null as string) as col_string,
0 as col_bigint,
0.0 as col_double
union all
select
'01' as col_string,
0 as col_bigint,
1.0 as col_double
union all
select
'01' as col_string,
1 as col_bigint,
cast(null as double) as col_double
union all
select
'01' as col_string,
1 as col_bigint,
1.0 as col_double
union all
select
'00' as col_string,
0 as col_bigint,
0.0 as col_double
) tmp;
输入数据如下所示:
+------------+------------+------------+
| col_string | col_bigint | col_double |
+------------+------------+------------+
| 01 | 1 | 1.0 |
| 01 | 0 | 1.0 |
| 01 | 1 | NULL |
| NULL | 0 | 0.0 |
| 00 | 1 | 0.0 |
| 00 | 0 | 0.0 |
+------------+------------+------------+
PAI命令方式
运行命令
drop table if exists enum_feature_selection_test_input_enum_value_output; drop table if exists enum_feature_selection_test_input_cnt_output; drop table if exists enum_feature_selection_test_input_value_output; PAI -name enum_feature_selection -project algo_public -DitemDelimiter=":" -Dlifecycle="28" -DoutputValueTableName="enum_feature_selection_test_input_value_output" -DkvDelimiter="," -DlabelColName="col_bigint" -DfeatureColNames="col_double,col_string" -DoutputEnumValueTableName="enum_feature_selection_test_input_enum_value_output" -DenableSparse="false" -DinputTableName="enum_feature_selection_test_input" -DoutputCntTableName="enum_feature_selection_test_input_cnt_output";
运行结果
enum_feature_selection_test_input_cnt_output
+------------+------------+------------+------------+ | colname | colvalue | labelvalue | cnt | +------------+------------+------------+------------+ | col_double | NULL | 1 | 1 | | col_double | 0 | 0 | 2 | | col_double | 0 | 1 | 1 | | col_double | 1 | 0 | 1 | | col_double | 1 | 1 | 1 | | col_string | NULL | 0 | 1 | | col_string | 00 | 0 | 1 | | col_string | 00 | 1 | 1 | | col_string | 01 | 0 | 1 | | col_string | 01 | 1 | 2 | +------------+------------+------------+------------+
enum_feature_selection_test_input_value_output
+------------+------------+------------+------------+------------+---------------+ | colname | gini | entropy | infogain | ginigain | infogainratio | +------------+------------+------------+------------+------------+---------------+ | col_double | 0.3888888888888889 | 0.792481250360578 | 0.20751874963942196 | 0.1111111111111111 | 0.14221913160264427 | | col_string | 0.38888888888888884 | 0.792481250360578 | 0.20751874963942196 | 0.11111111111111116 | 0.14221913160264427 | +------------+------------+------------+------------+------------+---------------+
enum_feature_selection_test_input_enum_value_output
+------------+------------+------------+------------+ | colname | colvalue | gini | entropy | +------------+------------+------------+------------+ | col_double | NULL | 0.0 | 0.0 | | col_double | 0 | 0.22222222222222224 | 0.4591479170272448 | | col_double | 1 | 0.16666666666666666 | 0.3333333333333333 | | col_string | NULL | 0.0 | 0.0 | | col_string | 00 | 0.16666666666666666 | 0.3333333333333333 | | col_string | 01 | 0.2222222222222222 | 0.4591479170272448 | +------------+------------+------------+------------+