Java 类名:com.alibaba.alink.operator.stream.dataproc.vector.VectorNormalizeStreamOp
Python 类名:VectorNormalizeStreamOp

功能介绍

对 Vector 进行正则化操作。
指定参数范数的阶,例如p = 2, 对于向量,计算向量的平方和再开二次方记为norm,最终计算结果为

参数说明

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

| selectedCol | 选中的列名 | 计算列对应的列名 | String | ✓ | 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] | |

| outputCol | 输出结果列 | 输出结果列列名,可选,默认null | String | | | null |

| p | 范数的阶 | 范数的阶,默认2 | Double | | | 2.0 |

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

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

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["1:3,2:4,4:7", 1],
  6. ["0:3,5:5", 3],
  7. ["2:4,4:5", 4]
  8. ])
  9. data = StreamOperator.fromDataframe(df, schemaStr="vec string, id bigint")
  10. VectorNormalizeStreamOp().setSelectedCol("vec").setOutputCol("vec_norm").linkFrom(data).print()
  11. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.stream.StreamOperator;
  3. import com.alibaba.alink.operator.stream.dataproc.vector.VectorNormalizeStreamOp;
  4. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class VectorNormalizeStreamOpTest {
  9. @Test
  10. public void testVectorNormalizeStreamOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("1:3,2:4,4:7", 1),
  13. Row.of("0:3,5:5", 3),
  14. Row.of("2:4,4:5", 4)
  15. );
  16. StreamOperator <?> data = new MemSourceStreamOp(df, "vec string, id int");
  17. new VectorNormalizeStreamOp().setSelectedCol("vec").setOutputCol("vec_norm").linkFrom(data).print();
  18. StreamOperator.execute();
  19. }
  20. }

运行结果

| vec | id | vec_norm | | —- | —- | —- |

| 1:3,2:4,4:7 | 1 | 1:0.34874291623145787 2:0.46499055497527714 4:0.813733471206735 |

| 0:3,5:5 | 3 | 0:0.5144957554275265 5:0.8574929257125441 |

| 2:4,4:5 | 4 | 2:0.6246950475544243 4:0.7808688094430304 |