Java 类名:com.alibaba.alink.operator.stream.feature.PcaPredictStreamOp
Python 类名:PcaPredictStreamOp

功能介绍

主成分分析,是考察多个变量间相关性一种多元统计方法,研究如何通过少数几个主成分来揭示多个变量间的内部结构,即从原始变量中导出少数几个主成分,使它们尽可能多地保留原始变量的信息,且彼此间互不相关,作为新的综合指标。详细介绍请见维基百科链接wiki
主成分分析功能包含主成分分析训练和主成分分析预测(批和流)。

参数说明

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

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

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

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

| modelStreamFilePath | 模型流的文件路径 | 模型流的文件路径 | String | | | null |

| modelStreamScanInterval | 扫描模型路径的时间间隔 | 描模型路径的时间间隔,单位秒 | Integer | | | 10 |

| modelStreamStartTime | 模型流的起始时间 | 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) | String | | | null |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["a", 1, 1, 2.0, True],
  6. ["c", 1, 2, -3.0, True],
  7. ["a", 2, 2, 2.0, False],
  8. ["c", 0, 0, 0.0, False]
  9. ])
  10. batchSource = BatchOperator.fromDataframe(df, schemaStr='f_string string, f_long long, f_int int, f_double double, f_boolean boolean')
  11. streamSource = StreamOperator.fromDataframe(df, schemaStr='f_string string, f_long long, f_int int, f_double double, f_boolean boolean')
  12. trainOp = QuantileDiscretizerTrainBatchOp()\
  13. .setSelectedCols(['f_double'])\
  14. .setNumBuckets(8)\
  15. .linkFrom(batchSource)
  16. predictBatchOp = QuantileDiscretizerPredictBatchOp()\
  17. .setSelectedCols(['f_double'])
  18. predictBatchOp.linkFrom(trainOp,batchSource).print()
  19. predictStreamOp = QuantileDiscretizerPredictStreamOp(trainOp)\
  20. .setSelectedCols(['f_double'])
  21. predictStreamOp.linkFrom(streamSource).print()
  22. 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.QuantileDiscretizerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.QuantileDiscretizerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import com.alibaba.alink.operator.stream.StreamOperator;
  7. import com.alibaba.alink.operator.stream.feature.QuantileDiscretizerPredictStreamOp;
  8. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  9. import org.junit.Test;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class PcaPredictStreamOpTest {
  13. @Test
  14. public void testPcaPredictStreamOp() throws Exception {
  15. List <Row> df = Arrays.asList(
  16. Row.of("a", 1, 1, 2.0, true),
  17. Row.of("c", 1, 2, -3.0, true),
  18. Row.of("a", 2, 2, 2.0, false),
  19. Row.of("c", 0, 0, 0.0, false)
  20. );
  21. BatchOperator <?> batchSource = new MemSourceBatchOp(df,
  22. "f_string string, f_long int, f_int int, f_double double, f_boolean boolean");
  23. StreamOperator <?> streamSource = new MemSourceStreamOp(df,
  24. "f_string string, f_long int, f_int int, f_double double, f_boolean boolean");
  25. BatchOperator <?> trainOp = new QuantileDiscretizerTrainBatchOp()
  26. .setSelectedCols("f_double")
  27. .setNumBuckets(8)
  28. .linkFrom(batchSource);
  29. BatchOperator <?> predictBatchOp = new QuantileDiscretizerPredictBatchOp()
  30. .setSelectedCols("f_double");
  31. predictBatchOp.linkFrom(trainOp, batchSource).print();
  32. StreamOperator <?> predictStreamOp = new QuantileDiscretizerPredictStreamOp(trainOp)
  33. .setSelectedCols("f_double");
  34. predictStreamOp.linkFrom(streamSource).print();
  35. StreamOperator.execute();
  36. }
  37. }

运行结果

批预测结果

| f_string | f_long | f_int | f_double | f_boolean | | —- | —- | —- | —- | —- |

| a | 1 | 1 | 2 | true |

| c | 1 | 2 | 0 | true |

| a | 2 | 2 | 2 | false |

| c | 0 | 0 | 1 | false |

流预测结果

| f_string | f_long | f_int | f_double | f_boolean | | —- | —- | —- | —- | —- |

| a | 2 | 2 | 2 | false |

| c | 1 | 2 | 0 | true |

| c | 0 | 0 | 1 | false |

| a | 1 | 1 | 2 | true |