功能介绍

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

参数说明

名称 中文名称 描述 类型 是否必须? 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
outputCol 输出结果列列名 输出结果列列名,必选 String
reservedCols 算法保留列名 算法保留列 String[] null
numFeatures 向量维度 生成向量长度 Integer 262144
categoricalCols 离散特征列名 可选,默认选择String类型和Boolean类型作为离散特征,如果没有则为空 String[]

脚本示例

脚本代码

  1. import numpy as np
  2. import pandas as pd
  3. data = np.array([
  4. [1.1, True, "2", "A"],
  5. [1.1, False, "2", "B"],
  6. [1.1, True, "1", "B"],
  7. [2.2, True, "1", "A"]
  8. ])
  9. df = pd.DataFrame({"double": data[:, 0], "bool": data[:, 1], "number": data[:, 2], "str": data[:, 3]})
  10. inOp = BatchOperator.fromDataframe(df, 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().2, True, "1", "A"]
  13. ])
  14. df = pd.DataFrame({"double": data[:, 0], "bool": data[:, 1], "number": data[:, 2], "str": data[:, 3]})
  15. inOp1 = BatchOperator.fromDataframe(df, schemaStr='double double, bool boolean, number int, str string')
  16. inOp2 = StreamOperator.fromDataframe(df, schemaStr='double double, bool boolean, number int, str string')
  17. hasher = FeatureHasherBatchOp().setSelectedCols(["double", "bool", "number", "str"]).setOutputCol("output").setNumFeatures(200)
  18. hasher.linkFrom(inOp1).print()
  19. hasher = FeatureHasherStreamOp().setSelectedCols(["double", "bool", "number", "str"]).setOutputCol("output").setNumFeatures(200)
  20. hasher.linkFrom(inOp2).print()
  21. StreamOperator.execute()

脚本运行结果

输出数据
  1. double bool number str output
  2. 0 1.1 True 2 A $200$13:2.0 38:1.1 45:1.0 195:1.0
  3. 1 1.1 False 2 B $200$13:2.0 30:1.0 38:1.1 76:1.0
  4. 2 1.1 True 1 B $200$13:1.0 38:1.1 76:1.0 195:1.0
  5. 3 2.2 True 1 A $200$13:1.0 38:2.2 45:1.0 195:1.0