org.jpmml.sparkml.FeatureConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmml-sparkml Show documentation
Show all versions of pmml-sparkml Show documentation
JPMML Apache Spark ML to PMML converter
The newest version!
/*
* Copyright (c) 2016 Villu Ruusmann
*
* This file is part of JPMML-SparkML
*
* JPMML-SparkML is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-SparkML is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-SparkML. If not, see .
*/
package org.jpmml.sparkml;
import java.util.List;
import org.apache.spark.ml.Transformer;
import org.apache.spark.ml.param.shared.HasInputCol;
import org.apache.spark.ml.param.shared.HasInputCols;
import org.apache.spark.ml.param.shared.HasOutputCol;
import org.apache.spark.ml.param.shared.HasOutputCols;
import org.jpmml.converter.Feature;
import org.jpmml.converter.SchemaUtil;
abstract
public class FeatureConverter extends TransformerConverter {
public FeatureConverter(T transformer){
super(transformer);
}
public List encodeFeatures(SparkMLEncoder encoder){
throw new UnsupportedOperationException();
}
public void registerFeatures(SparkMLEncoder encoder){
Transformer transformer = getTransformer();
InOutMode outputMode = getOutputMode();
if(outputMode == InOutMode.SINGLE){
HasOutputCol hasOutputCol = (HasOutputCol)transformer;
String outputCol = hasOutputCol.getOutputCol();
List features = encodeFeatures(encoder);
encoder.putFeatures(outputCol, features);
} else
if(outputMode == InOutMode.MULTIPLE){
HasOutputCols hasOutputCols = (HasOutputCols)transformer;
String[] outputCols = hasOutputCols.getOutputCols();
List features = encodeFeatures(encoder);
SchemaUtil.checkSize(outputCols.length, features);
for(int i = 0; i < outputCols.length; i++){
String outputCol = outputCols[i];
Feature feature = features.get(i);
encoder.putOnlyFeature(outputCol, feature);
}
}
}
public T getTransformer(){
return getObject();
}
protected InOutMode getInputMode(){
throw new IllegalArgumentException();
}
protected InOutMode getOutputMode(){
T transformer = getTransformer();
return getOutputMode(transformer);
}
static
public enum InOutMode {
SINGLE(){
@Override
public String[] getInputCols(T transformer){
return new String[]{transformer.getInputCol()};
}
@Override
public String[] getOutputCols(T transformer){
if(transformer instanceof HasOutputCol){
HasOutputCol hasOutputCol = (HasOutputCol)transformer;
return new String[]{hasOutputCol.getOutputCol()};
}
throw new IllegalArgumentException();
}
},
MULTIPLE(){
@Override
public String[] getInputCols(T transformer){
return transformer.getInputCols();
}
@Override
public String[] getOutputCols(T transformer){
if(transformer instanceof HasOutputCols){
HasOutputCols hasOutputCols = (HasOutputCols)transformer;
return hasOutputCols.getOutputCols();
}
throw new IllegalArgumentException();
}
},
;
abstract
public String[] getInputCols(T transformer);
abstract
public String[] getOutputCols(T transformer);
}
static
public String formatName(T transformer){
return transformer.getOutputCol();
}
static
public String formatName(T transformer, int index, int length){
if(length > 1){
return transformer.getOutputCol() + ("[" + index + "]");
}
return transformer.getOutputCol();
}
static
protected InOutMode getInputMode(T transformer){
if(transformer.isSet(transformer.inputCol())){
return InOutMode.SINGLE;
} else
if(transformer.isSet(transformer.inputCols())){
return InOutMode.MULTIPLE;
}
throw new IllegalArgumentException();
}
static
protected InOutMode getOutputMode(T transformer){
if(transformer instanceof HasOutputCol){
return InOutMode.SINGLE;
} else
if(transformer instanceof HasOutputCols){
return InOutMode.MULTIPLE;
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy