我正在尝试为分类数据集找到近似最近的邻居。为此,我正在使用MinHashLSH
Spark 中存在的模型。
我的数据集有分类数据。所以我使用StringIndexer
后跟OneHotEncoderEstimator
后跟VectorAssembler
将分类值转换为连续值。
现在我想从我的数据集中找到给定键的最近邻居,这个键应该是向量形式。我无法找到将分类键转换为连续向量的方法。
List<Row> dataA = Arrays.asList(RowFactory.create(0, "apple"),
RowFactory.create(1, "banana"),
RowFactory.create(2, "coconut"));
StructType schema = new StructType(
new StructField[] { new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
new StructField("fruits", DataTypes.StringType, false, Metadata.empty()) });
Dataset<Row> dfA = spark.createDataFrame(dataA, schema);
StringIndexer stringIndexer = new StringIndexer().setInputCol("fruits").setOutputCol("fruitIndex").setHandleInvalid("keep");
OneHotEncoderEstimator encoder = new OneHotEncoderEstimator().setInputCols(new String[]{"fruitIndex"}).setOutputCols(new String[]{"fruitVec"});
String[] featuredCols = new String[] {"fruitIndex","fruitVec"};
VectorAssembler assembler = new VectorAssembler().setInputCols(featuredCols).setOutputCol("features");
Pipeline sovPipeline = new Pipeline().setStages(new PipelineStage[]{stringIndexer, encoder, assembler});
// Feature Transformation
PipelineModel plModel = sovPipeline.fit(dfA);
Dataset<Row> dfT = plModel.transform(dfA);
MinHashLSH mh = new MinHashLSH().setNumHashTables(5).setInputCol("features").setOutputCol("hashes");
MinHashLSHModel model = mh.fit(dfT);
// model.approxNearestNeighbors(dfT, key, 2).show();
如何从分类键key
为方法创建(数字连续向量) ?approxNearestNeighbors