Java 类名:com.alibaba.alink.operator.batch.dataproc.StandardScalerTrainBatchOp
Python 类名:StandardScalerTrainBatchOp

功能介绍

标准化是对数据进行按正态化处理的组件
训练过程计算数据的均值和标准差,在预测组件中使用模型结果

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
withMean 是否使用均值 是否使用均值,默认使用 Boolean true
withStd 是否使用标准差 是否使用标准差,默认使用 Boolean true

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["a", 10.0, 100],
  6. ["b", -2.5, 9],
  7. ["c", 100.2, 1],
  8. ["d", -99.9, 100],
  9. ["a", 1.4, 1],
  10. ["b", -2.2, 9],
  11. ["c", 100.9, 1]
  12. ])
  13. colnames = ["col1", "col2", "col3"]
  14. selectedColNames = ["col2", "col3"]
  15. inOp = BatchOperator.fromDataframe(df, schemaStr='col1 string, col2 double, col3 long')
  16. # train
  17. trainOp = StandardScalerTrainBatchOp()\
  18. .setSelectedCols(selectedColNames)
  19. trainOp.linkFrom(inOp)
  20. # batch predict
  21. predictOp = StandardScalerPredictBatchOp()
  22. predictOp.linkFrom(trainOp, inOp).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.dataproc.StandardScalerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.StandardScalerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class StandardScalerTrainBatchOpTest {
  10. @Test
  11. public void testStandardScalerTrainBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of("a", 10.0, 100),
  14. Row.of("b", -2.5, 9),
  15. Row.of("c", 100.2, 1),
  16. Row.of("d", -99.9, 100),
  17. Row.of("a", 1.4, 1),
  18. Row.of("b", -2.2, 9),
  19. Row.of("c", 100.9, 1)
  20. );
  21. String[] selectedColNames = new String[] {"col2", "col3"};
  22. BatchOperator <?> inOp = new MemSourceBatchOp(df, "col1 string, col2 double, col3 int");
  23. BatchOperator <?> trainOp = new StandardScalerTrainBatchOp()
  24. .setSelectedCols(selectedColNames);
  25. trainOp.linkFrom(inOp);
  26. BatchOperator <?> predictOp = new StandardScalerPredictBatchOp();
  27. predictOp.linkFrom(trainOp, inOp).print();
  28. }
  29. }

运行结果

| col1 | col2 | col3 | | —- | —- | —- |

| a | -0.0784 | 1.4596 |

| b | -0.2592 | -0.4814 |

| c | 1.2270 | -0.6521 |

| d | -1.6687 | 1.4596 |

| a | -0.2028 | -0.6521 |

| b | -0.2549 | -0.4814 |

| c | 1.2371 | -0.6521 |