Java 类名:com.alibaba.alink.operator.batch.feature.CrossFeatureTrainBatchOp
Python 类名:CrossFeatureTrainBatchOp

功能介绍

将选定的特征列组合成单个向量类型的特征。

使用方式

该组件是训练组件,需要配合预测组件 CrossFeaturePredictBatch/StreamOp 使用。
为了训练模型,需要指定参与组合的特征列名(selectedCols)。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["1.0", "1.0", 1.0, 1],
  6. ["1.0", "1.0", 0.0, 1],
  7. ["1.0", "0.0", 1.0, 1],
  8. ["1.0", "0.0", 1.0, 1],
  9. ["2.0", "3.0", None, 0],
  10. ["2.0", "3.0", 1.0, 0],
  11. ["0.0", "1.0", 2.0, 0],
  12. ["0.0", "1.0", 1.0, 0]])
  13. data = BatchOperator.fromDataframe(df, schemaStr="f0 string, f1 string, f2 double, label bigint")
  14. train = CrossFeatureTrainBatchOp().setSelectedCols(['f0','f1','f2']).linkFrom(data)
  15. CrossFeaturePredictBatchOp().setOutputCol("cross").linkFrom(train, data).collectToDataframe()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.feature.CrossFeaturePredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.CrossFeatureTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class CrossFeatureTrainBatchOpTest {
  10. @Test
  11. public void testCrossFeatureTrainBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("1.0", "1.0", 1.0, 1),
  14. Row.of("1.0", "1.0", 0.0, 1),
  15. Row.of("1.0", "0.0", 1.0, 1),
  16. Row.of("1.0", "0.0", 1.0, 1),
  17. Row.of("2.0", "3.0", null, 0),
  18. Row.of("2.0", "3.0", 1.0, 0),
  19. Row.of("0.0", "1.0", 2.0, 0)
  20. );
  21. BatchOperator <?> data = new MemSourceBatchOp(df, "f0 string, f1 string, f2 double, label int");
  22. BatchOperator <?> train = new CrossFeatureTrainBatchOp().setSelectedCols("f0", "f1", "f2").linkFrom(data);
  23. new CrossFeaturePredictBatchOp().setOutputCol("cross").linkFrom(train, data).print();
  24. }
  25. }

运行结果

| f0 | f1 | f2 | label | cross | | —- | —- | —- | —- | —- |

| 1.0 | 1.0 | 1.0000 | 1 | $36$0:1.0 |

| 1.0 | 1.0 | 0.0000 | 1 | $36$9:1.0 |

| 1.0 | 0.0 | 1.0000 | 1 | $36$6:1.0 |

| 1.0 | 0.0 | 1.0000 | 1 | $36$6:1.0 |

| 2.0 | 3.0 | null | 0 | $36$22:1.0 |

| 2.0 | 3.0 | 1.0000 | 0 | $36$4:1.0 |

| 0.0 | 1.0 | 2.0000 | 0 | $36$29:1.0 |

| 0.0 | 1.0 | 1.0000 | 0 | $36$2:1.0 |