Java 类名:com.alibaba.alink.pipeline.feature.FeatureHasher
Python 类名:FeatureHasher

功能介绍

将多个特征组合成一个特征向量。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
outputCol 输出结果列列名 输出结果列列名,必选 String
selectedCols 选择的列名 计算列对应的列名列表 String[]
categoricalCols 离散特征列名 离散特征列名 String[]
numFeatures 向量维度 生成向量长度 Integer 262144
reservedCols 算法保留列名 算法保留列 String[] null
numThreads 组件多线程线程个数 组件多线程线程个数 Integer 1

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df1 = pd.DataFrame([
  5. [1.1, True, "2", "A"],
  6. [1.1, False, "2", "B"],
  7. [1.1, True, "1", "B"],
  8. [2.2, True, "1", "A"]
  9. ])
  10. inOp = BatchOperator.fromDataframe(df1, schemaStr='double double, bool boolean, number int, str string')
  11. binarizer = FeatureHasher().setSelectedCols(["double", "bool", "number", "str"]).setOutputCol("output").setNumFeatures(200)
  12. binarizer.transform(inOp).print()
  13. df2 = pd.DataFrame([
  14. [1.1, True, "2", "A"],
  15. [1.1, False, "2", "B"],
  16. [1.1, True, "1", "B"],
  17. [2.2, True, "1", "A"]
  18. ])
  19. inOp1 = BatchOperator.fromDataframe(df2, schemaStr='double double, bool boolean, number int, str string')
  20. inOp2 = StreamOperator.fromDataframe(df2, schemaStr='double double, bool boolean, number int, str string')
  21. hasher = FeatureHasherBatchOp().setSelectedCols(["double", "bool", "number", "str"]).setOutputCol("output").setNumFeatures(200)
  22. hasher.linkFrom(inOp1).print()
  23. hasher = FeatureHasherStreamOp().setSelectedCols(["double", "bool", "number", "str"]).setOutputCol("output").setNumFeatures(200)
  24. hasher.linkFrom(inOp2).print()
  25. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.feature.FeatureHasherBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.feature.FeatureHasherStreamOp;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import com.alibaba.alink.pipeline.feature.FeatureHasher;
  9. import org.junit.Test;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class FeatureHasherTest {
  13. @Test
  14. public void testFeatureHasher() throws Exception {
  15. List <Row> df1 = Arrays.asList(
  16. Row.of(1.1, true, 2, "A"),
  17. Row.of(1.1, false, 2, "B"),
  18. Row.of(1.1, true, 1, "B"),
  19. Row.of(2.2, true, 1, "A")
  20. );
  21. BatchOperator <?> inOp = new MemSourceBatchOp(df1, "double double, bool boolean, number int, str string");
  22. FeatureHasher binarizer = new FeatureHasher().setSelectedCols("double", "bool", "number", "str").setOutputCol(
  23. "output").setNumFeatures(200);
  24. binarizer.transform(inOp).print();
  25. List <Row> df2 = Arrays.asList(
  26. Row.of(1.1, true, 2, "A"),
  27. Row.of(1.1, false, 2, "B"),
  28. Row.of(1.1, true, 1, "B"),
  29. Row.of(2.2, true, 1, "A")
  30. );
  31. BatchOperator <?> inOp1 = new MemSourceBatchOp(df2, "double double, bool boolean, number int, str string");
  32. StreamOperator <?> inOp2 = new MemSourceStreamOp(df2, "double double, bool boolean, number int, str string");
  33. BatchOperator <?> hasher1 = new FeatureHasherBatchOp().setSelectedCols("double", "bool", "number", "str")
  34. .setOutputCol("output").setNumFeatures(200);
  35. hasher1.linkFrom(inOp1).print();
  36. StreamOperator <?> hasher2 = new FeatureHasherStreamOp().setSelectedCols("double", "bool", "number", "str")
  37. .setOutputCol("output").setNumFeatures(200);
  38. hasher2.linkFrom(inOp2).print();
  39. StreamOperator.execute();
  40. }
  41. }

运行结果

| double | bool | number | str | output | | —- | —- | —- | —- | —- |

| 1.1000 | true | 2 | A | $200$13:2.0 38:1.1 45:1.0 195:1.0 |

| 1.1000 | false | 2 | B | $200$13:2.0 30:1.0 38:1.1 76:1.0 |

| 1.1000 | true | 1 | B | $200$13:1.0 38:1.1 76:1.0 195:1.0 |

| 2.2000 | true | 1 | A | $200$13:1.0 38:2.2 45:1.0 195:1.0 |

| double | bool | number | str | output | | —- | —- | —- | —- | —- |

| 1.1000 | true | 2 | A | $200$13:2.0 38:1.1 45:1.0 195:1.0 |

| 1.1000 | false | 2 | B | $200$13:2.0 30:1.0 38:1.1 76:1.0 |

| 1.1000 | true | 1 | B | $200$13:1.0 38:1.1 76:1.0 195:1.0 |

| 2.2000 | true | 1 | A | $200$13:1.0 38:2.2 45:1.0 195:1.0 |

| double | bool | number | str | output | | —- | —- | —- | —- | —- |

| 2.2000 | true | 1 | A | $200$13:1.0 38:2.2 45:1.0 195:1.0 |

| 1.1000 | true | 1 | B | $200$13:1.0 38:1.1 76:1.0 195:1.0 |

| 1.1000 | true | 2 | A | $200$13:2.0 38:1.1 45:1.0 195:1.0 |

| 1.1000 | false | 2 | B | $200$13:2.0 30:1.0 38:1.1 76:1.0 |