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

功能介绍

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

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
predictionCol 预测结果列名 预测结果列名 String
modelFilePath 模型的文件路径 模型的文件路径 String null
reservedCols 算法保留列名 算法保留列 String[] null
vectorCol 向量列名 向量列对应的列名,默认值是null String 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] null
numThreads 组件多线程线程个数 组件多线程线程个数 Integer 1

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. [0.0,0.0,0.0],
  6. [0.1,0.2,0.1],
  7. [0.2,0.2,0.8],
  8. [9.0,9.5,9.7],
  9. [9.1,9.1,9.6],
  10. [9.2,9.3,9.9]
  11. ])
  12. # batch source
  13. inOp = BatchOperator.fromDataframe(df, schemaStr='x1 double, x2 double, x3 double')
  14. trainOp = PcaTrainBatchOp()\
  15. .setK(2)\
  16. .setSelectedCols(["x1","x2","x3"])
  17. predictOp = PcaPredictBatchOp()\
  18. .setPredictionCol("pred")
  19. # batch train
  20. inOp.link(trainOp)
  21. # batch predict
  22. predictOp.linkFrom(trainOp,inOp)
  23. predictOp.print()
  24. # stream predict
  25. inStreamOp = StreamOperator.fromDataframe(df, schemaStr='x1 double, x2 double, x3 double')
  26. predictStreamOp = PcaPredictStreamOp(trainOp)\
  27. .setPredictionCol("pred")
  28. predictStreamOp.linkFrom(inStreamOp)
  29. predictStreamOp.print()
  30. 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.PcaPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.feature.PcaTrainBatchOp;
  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.PcaPredictStreamOp;
  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 PcaPredictBatchOpTest {
  13. @Test
  14. public void testPcaPredictBatchOp() throws Exception {
  15. List <Row> df = Arrays.asList(
  16. Row.of(0.0, 0.0, 0.0),
  17. Row.of(0.1, 0.2, 0.1),
  18. Row.of(0.2, 0.2, 0.8),
  19. Row.of(9.0, 9.5, 9.7),
  20. Row.of(9.1, 9.1, 9.6),
  21. Row.of(9.2, 9.3, 9.9)
  22. );
  23. BatchOperator <?> inOp = new MemSourceBatchOp(df, "x1 double, x2 double, x3 double");
  24. BatchOperator <?> trainOp = new PcaTrainBatchOp()
  25. .setK(2)
  26. .setSelectedCols("x1", "x2", "x3");
  27. BatchOperator <?> predictOp = new PcaPredictBatchOp()
  28. .setPredictionCol("pred");
  29. inOp.link(trainOp);
  30. predictOp.linkFrom(trainOp, inOp);
  31. predictOp.print();
  32. StreamOperator <?> inStreamOp = new MemSourceStreamOp(df, "x1 double, x2 double, x3 double");
  33. StreamOperator <?> predictStreamOp = new PcaPredictStreamOp(trainOp)
  34. .setPredictionCol("pred");
  35. predictStreamOp.linkFrom(inStreamOp);
  36. predictStreamOp.print();
  37. StreamOperator.execute();
  38. }
  39. }

运行结果

| x1 | x2 | x3 | pred | | —- | —- | —- | —- |

| 9.0 | 9.5 | 9.7 | 3.2280384305400736,1.1516225426477789E-4 |

| 0.2 | 0.2 | 0.8 | 0.13565076707329407,0.09003329494282108 |

| 9.2 | 9.3 | 9.9 | 3.250783163664603,0.0456526246528135 |

| 9.1 | 9.1 | 9.6 | 3.182618319978973,0.027469531992220464 |

| 0.1 | 0.2 | 0.1 | 0.045855205015063565,-0.012182917696915518 |

| 0.0 | 0.0 | 0.0 | 0.0,0.0 |