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

功能介绍

vector绝对值最大标准化是对vector数据按照数值最大绝对值进行标准化的组件, 将数据归一到-1和1之间。输入的向量维度可以不相同。
计算公式为 value / max( | value | )
该组件生成Vector绝对值最大标准化的模型

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCol 选中的列名 计算列对应的列名 String 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR]

代码示例

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. data = BatchOperator.fromDataframe(df, schemaStr="col string, vec string")
  14. trainOp = VectorMaxAbsScalerTrainBatchOp()\
  15. .setSelectedCol("vec")
  16. model = trainOp.linkFrom(data)
  17. batchPredictOp = VectorMaxAbsScalerPredictBatchOp()
  18. batchPredictOp.linkFrom(model, data).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.vector.VectorMaxAbsScalerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.vector.VectorMaxAbsScalerTrainBatchOp;
  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 VectorMaxAbsScalerTrainBatchOpTest {
  10. @Test
  11. public void testVectorMaxAbsScalerTrainBatchOp() 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. BatchOperator <?> data = new MemSourceBatchOp(df, "col string, vec string");
  22. BatchOperator <?> trainOp = new VectorMaxAbsScalerTrainBatchOp()
  23. .setSelectedCol("vec");
  24. BatchOperator <?> model = trainOp.linkFrom(data);
  25. BatchOperator <?> batchPredictOp = new VectorMaxAbsScalerPredictBatchOp();
  26. batchPredictOp.linkFrom(model, data).print();
  27. }
  28. }

运行结果

| col | vec | | —- | —- |

| a | 0.09910802775024777 1.0 |

| b | -0.024777006937561942 0.09 |

| c | 0.9930624380574826 0.01 |

| d | -0.9900891972249752 1.0 |

| a | 0.013875123885034686 0.01 |

| b | -0.02180376610505451 0.09 |

| c | 1.0 0.01 |