Java 类名:com.alibaba.alink.pipeline.dataproc.StandardScaler
Python 类名:StandardScaler

功能介绍

标准化是对数据进行按正态化处理的组件
训练过程计算数据的均值和标准差,生成标准化模型

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCols 选择的列名 计算列对应的列名列表 String[]
modelFilePath 模型的文件路径 模型的文件路径 String null
outputCols 输出结果列列名数组 输出结果列列名数组,可选,默认null String[] null
overwriteSink 是否覆写已有数据 是否覆写已有数据 Boolean false
withMean 是否使用均值 是否使用均值,默认使用 Boolean true
withStd 是否使用标准差 是否使用标准差,默认使用 Boolean true
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", 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. sinOp = StreamOperator.fromDataframe(df, schemaStr='col1 string, col2 double, col3 long')
  17. model = StandardScaler()\
  18. .setSelectedCols(selectedColNames)\
  19. .fit(inOp)
  20. model.transform(inOp).print()
  21. model.transform(sinOp).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.source.MemSourceBatchOp;
  4. import com.alibaba.alink.operator.stream.StreamOperator;
  5. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  6. import com.alibaba.alink.pipeline.dataproc.StandardScaler;
  7. import com.alibaba.alink.pipeline.dataproc.StandardScalerModel;
  8. import org.junit.Test;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class StandardScalerTest {
  12. @Test
  13. public void testStandardScaler() throws Exception {
  14. List <Row> df = Arrays.asList(
  15. Row.of("a", 10.0, 100),
  16. Row.of("b", -2.5, 9),
  17. Row.of("c", 100.2, 1),
  18. Row.of("d", -99.9, 100),
  19. Row.of("a", 1.4, 1),
  20. Row.of("b", -2.2, 9),
  21. Row.of("c", 100.9, 1)
  22. );
  23. String[] selectedColNames = new String[] {"col2", "col3"};
  24. BatchOperator <?> inOp = new MemSourceBatchOp(df, "col1 string, col2 double, col3 int");
  25. StreamOperator <?> sinOp = new MemSourceStreamOp(df, "col1 string, col2 double, col3 int");
  26. StandardScalerModel model = new StandardScaler()
  27. .setSelectedCols(selectedColNames)
  28. .fit(inOp);
  29. model.transform(inOp).print();
  30. model.transform(sinOp).print();
  31. StreamOperator.execute();
  32. }
  33. }

运行结果

| 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 |

| col1 | col2 | col3 |

|

——

| c | 1.2371 | -0.6521 |

| b | -0.2592 | -0.4814 |

| c | 1.2270 | -0.6521 |

| b | -0.2549 | -0.4814 |

| a | -0.0784 | 1.4596 |

| a | -0.2028 | -0.6521 |

| d | -1.6687 | 1.4596 |