向量近似最近邻预测 (VectorApproxNearestNeighborPredictBatchOp)

Java 类名:com.alibaba.alink.operator.batch.similarity.VectorApproxNearestNeighborPredictBatchOp

Python 类名:VectorApproxNearestNeighborPredictBatchOp

功能介绍

该功能由训练和预测组成,该组件为预测功能

该功能由预测时候的topN和radius参数控制, 如果填写了topN,则输出最近邻,如果填写了radius,则输出radius范围内的邻居。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
selectedCol 选中的列名 计算列对应的列名 String
modelFilePath 模型的文件路径 模型的文件路径 String null
outputCol 输出结果列 输出结果列列名,可选,默认null String null
radius radius值 radius值 Double null
reservedCols 算法保留列名 算法保留列 String[] null
topN TopN的值 TopN的值 Integer x >= 1 null
numThreads 组件多线程线程个数 组件多线程线程个数 Integer 1

代码示例

Python 代码

from pyalink.alink import *

import pandas as pd

useLocalEnv(1)

df = pd.DataFrame([
    [0, "0 0 0"],
    [1, "1 1 1"],
    [2, "2 2 2"]
])

inOp = BatchOperator.fromDataframe(df, schemaStr='id int, vec string')
train = VectorApproxNearestNeighborTrainBatchOp().setIdCol("id").setSelectedCol("vec").linkFrom(inOp)
predict = VectorApproxNearestNeighborPredictBatchOp().setSelectedCol("vec").setTopN(3).linkFrom(train, inOp)
predict.print()

Java 代码

import org.apache.flink.types.Row;

import com.alibaba.alink.operator.batch.BatchOperator;
import com.alibaba.alink.operator.batch.similarity.VectorApproxNearestNeighborPredictBatchOp;
import com.alibaba.alink.operator.batch.similarity.VectorApproxNearestNeighborTrainBatchOp;
import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class VectorApproxNearestNeighborPredictBatchOpTest {
	@Test
	public void testVectorApproxNearestNeighborPredictBatchOp() throws Exception {
		List <Row> df = Arrays.asList(
			Row.of(0, "0 0 0"),
			Row.of(1, "1 1 1"),
			Row.of(2, "2 2 2")
		);
		BatchOperator <?> inOp = new MemSourceBatchOp(df, "id int, vec string");
		BatchOperator <?> train = new VectorApproxNearestNeighborTrainBatchOp().setIdCol("id").setSelectedCol("vec")
			.linkFrom(inOp);
		BatchOperator <?> predict = new VectorApproxNearestNeighborPredictBatchOp().setSelectedCol("vec").setTopN(3)
			.linkFrom(train, inOp);
		predict.print();
	}
}

运行结果

id vec
0 {“ID”:“[0,1,2]”,“METRIC”:“[0.0,1.7320508075688772,3.4641016151377544]”}
1 {“ID”:“[1,2,0]”,“METRIC”:“[0.0,1.7320508075688772,1.7320508075688772]”}
2 {“ID”:“[2,1,0]”,“METRIC”:“[0.0,1.7320508075688772,3.4641016151377544]”}