Java 类名:com.alibaba.alink.operator.stream.onlinelearning.FtrlModelFilterStreamOp
Python 类名:FtrlModelFilterStreamOp

功能介绍

该组件是对ftrl 实时训练出来的模型进行实时过滤,将指标不好的模型丢弃掉,仅保留达到用户要求的模型。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| labelCol | 标签列名 | 输入表中的标签列名 | String | ✓ | | |

| accuracyThreshold | 模型过滤的Accuracy阈值 | 模型过滤的Accuracy阈值 | Double | | | 0.5 |

| aucThreshold | 模型过滤的Auc阈值 | 模型过滤的Auc阈值 | Double | | | 0.5 |

| positiveLabelValueString | 正样本 | 正样本对应的字符串格式。 | String | | | null |

| vectorCol | 向量列名 | 向量列对应的列名,默认值是null | String | | 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] | null |

代码示例

以下代码仅用于示意,可能需要修改部分代码或者配置环境后才能正常运行!

Python 代码

  1. trainData0 = RandomTableSourceBatchOp() \
  2. .setNumCols(5) \
  3. .setNumRows(100) \
  4. .setOutputCols(["f0", "f1", "f2", "f3", "label"]) \
  5. .setOutputColConfs("label:weight_set(1.0,1.0,2.0,5.0)")
  6. model = LogisticRegressionTrainBatchOp() \
  7. .setFeatureCols(["f0", "f1", "f2", "f3"]) \
  8. .setLabelCol("label") \
  9. .setMaxIter(10).linkFrom(trainData0)
  10. trainData1 = RandomTableSourceStreamOp() \
  11. .setNumCols(5) \
  12. .setMaxRows(10000) \
  13. .setOutputCols(["f0", "f1", "f2", "f3", "label"]) \
  14. .setOutputColConfs("label:weight_set(1.0,1.0,2.0,5.0)") \
  15. .setTimePerSample(0.1)
  16. models = FtrlTrainStreamOp(model, None) \
  17. .setFeatureCols(["f0", "f1", "f2", "f3"]) \
  18. .setLabelCol("label") \
  19. .setTimeInterval(10) \
  20. .setAlpha(0.1) \
  21. .setBeta(0.1) \
  22. .setL1(0.1) \
  23. .setL2(0.1)\
  24. .setVectorSize(4)\
  25. .setWithIntercept(True) \
  26. .linkFrom(trainData1)
  27. FtrlModelFilterStreamOp().setAucThreshold(0.5).setAccuracyThreshold(0.5) \
  28. .setPositiveLabelValueString("1.0") \
  29. .setLabelCol("label").linkFrom(models, trainData1).print()
  30. StreamOperator.execute()

Java 代码

  1. package com.alibaba.alink.operator.stream.ml.onlinelearning;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.classification.LogisticRegressionTrainBatchOp;
  4. import com.alibaba.alink.operator.batch.source.RandomTableSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.onlinelearning.FtrlModelFilterStreamOp;
  7. import com.alibaba.alink.operator.stream.onlinelearning.FtrlPredictStreamOp;
  8. import com.alibaba.alink.operator.stream.onlinelearning.FtrlTrainStreamOp;
  9. import com.alibaba.alink.operator.stream.source.RandomTableSourceStreamOp;
  10. import org.junit.Test;
  11. public class FtrlTrainTestTest {
  12. @Test
  13. public void FtrlClassification() throws Exception {
  14. StreamOperator.setParallelism(2);
  15. BatchOperator trainData0 = new RandomTableSourceBatchOp()
  16. .setNumCols(5)
  17. .setNumRows(100L)
  18. .setOutputCols(new String[]{"f0", "f1", "f2", "f3", "label"})
  19. .setOutputColConfs("label:weight_set(1.0,1.0,2.0,5.0)");
  20. BatchOperator model = new LogisticRegressionTrainBatchOp()
  21. .setFeatureCols(new String[]{"f0", "f1", "f2", "f3"})
  22. .setLabelCol("label")
  23. .setMaxIter(10).linkFrom(trainData0);
  24. StreamOperator trainData1 = new RandomTableSourceStreamOp()
  25. .setNumCols(5)
  26. .setMaxRows(1000L)
  27. .setOutputCols(new String[]{"f0", "f1", "f2", "f3", "label"})
  28. .setOutputColConfs("label:weight_set(1.0,1.0,2.0,5.0)")
  29. .setTimePerSample(0.1);
  30. StreamOperator smodel = new FtrlTrainStreamOp(model)
  31. .setFeatureCols(new String[]{"f0", "f1", "f2", "f3"})
  32. .setLabelCol("label")
  33. .setTimeInterval(10)
  34. .setAlpha(0.1)
  35. .setBeta(0.1)
  36. .setL1(0.1)
  37. .setL2(0.1)
  38. .setVectorSize(4)
  39. .setWithIntercept(true)
  40. .linkFrom(trainData1);
  41. new FtrlModelFilterStreamOp().setAucThreshold(0.5).setAccuracyThreshold(0.5)
  42. .setPositiveLabelValueString("1.0")
  43. .setLabelCol("label").linkFrom(smodel, trainData1).print();
  44. StreamOperator.execute();
  45. }
  46. }

输出结果

| alinkmodelstreamtimestamp | alinkmodelstreamcount | model_id | model_info | label_value | | —- | —- | —- | —- | —- |

| 2021-06-10 19:39:41.169 | 4 | 1048576 | {“featureColNames”:[“f0”,”f1”,”f2”,”f3”],”featureColTypes”:null,”coefVector”:{“data”:[0.8344505544432526,0.12691581782275618,0.218543815526658,0.22769775985648064,0.05203808913915911]},”coefVectors”:null,”convergenceInfo”:null} | null |

| 2021-06-10 19:39:41.169 | 4 | 0 | {“hasInterceptItem”:”true”,”modelName”:””Logistic Regression””,”labelCol”:null,”linearModelType”:””LR””,”vectorSize”:”4”} | null |

| 2021-06-10 19:39:41.169 | 4 | 2251799812636673 | null | 1.0000 |

| 2021-06-10 19:39:41.169 | 4 | 2251799812636672 | null | 2.0000 |

| 2021-06-10 19:40:11.319 | 4 | 0 | {“hasInterceptItem”:”true”,”modelName”:””Logistic Regression””,”labelCol”:null,”linearModelType”:””LR””,”vectorSize”:”4”} | null |

| 2021-06-10 19:40:11.319 | 4 | 1048576 | {“featureColNames”:[“f0”,”f1”,”f2”,”f3”],”featureColTypes”:null,”coefVector”:{“data”:[0.9795436401762672,0.2366945713649036,0.3644473752499545,0.2654714469214479,0.23195535286616062]},”coefVectors”:null,”convergenceInfo”:null} | null |

| 2021-06-10 19:40:11.319 | 4 | 2251799812636672 | null | 2.0000 |

| 2021-06-10 19:40:11.319 | 4 | 2251799812636673 | null | 1.0000 |