Java Code Examples for org.jpmml.converter.Schema#getFeatures()
The following examples show how to use
org.jpmml.converter.Schema#getFeatures() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
static private void checkSchema(Schema schema){ Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); if(label == null){ return; } for(Feature feature : features){ if(Objects.equals(label.getName(), feature.getName())){ throw new IllegalArgumentException("Label column '" + label.getName() + "' is contained in the list of feature columns"); } } }
Example 2
Source File: GeneralizedLinearRegressionModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 5 votes |
@Override public GeneralRegressionModel encodeModel(Schema schema){ GeneralizedLinearRegressionModel model = getTransformer(); Object targetCategory = null; MiningFunction miningFunction = getMiningFunction(); switch(miningFunction){ case CLASSIFICATION: CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); SchemaUtil.checkSize(2, categoricalLabel); targetCategory = categoricalLabel.getValue(1); break; default: break; } List<Feature> features = new ArrayList<>(schema.getFeatures()); List<Double> featureCoefficients = new ArrayList<>(VectorUtil.toList(model.coefficients())); RegressionTableUtil.simplify(this, targetCategory, features, featureCoefficients); GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, miningFunction, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null) .setDistribution(parseFamily(model.getFamily())) .setLinkFunction(parseLinkFunction(model.getLink())) .setLinkParameter(parseLinkParameter(model.getLink())); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, model.intercept(), targetCategory); return generalRegressionModel; }
Example 3
Source File: MVRConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
@Override public GeneralRegressionModel encodeModel(Schema schema){ RGenericVector mvr = getObject(); RDoubleVector coefficients = mvr.getDoubleElement("coefficients"); RDoubleVector xMeans = mvr.getDoubleElement("Xmeans"); RDoubleVector yMeans = mvr.getDoubleElement("Ymeans"); RNumberVector<?> ncomp = mvr.getNumericElement("ncomp"); RStringVector rowNames = coefficients.dimnames(0); RStringVector columnNames = coefficients.dimnames(1); RStringVector compNames = coefficients.dimnames(2); int rows = rowNames.size(); int columns = columnNames.size(); int components = compNames.size(); List<? extends Feature> features = schema.getFeatures(); List<Double> featureCoefficients = FortranMatrixUtil.getColumn(coefficients.getValues(), rows, (columns * components), 0 + (ValueUtil.asInt(ncomp.asScalar()) - 1)); Double intercept = yMeans.getValue(0); for(int j = 0; j < rowNames.size(); j++){ intercept -= (featureCoefficients.get(j) * xMeans.getValue(j)); } GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null) .setLinkFunction(GeneralRegressionModel.LinkFunction.IDENTITY); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, null); return generalRegressionModel; }
Example 4
Source File: RuleSetClassifier.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
@Override public RuleSetModel encodeModel(Schema schema){ String defaultScore = getDefaultScore(); List<Object[]> rules = getRules(); Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); RuleSelectionMethod ruleSelectionMethod = new RuleSelectionMethod(RuleSelectionMethod.Criterion.FIRST_HIT); RuleSet ruleSet = new RuleSet() .addRuleSelectionMethods(ruleSelectionMethod); if(defaultScore != null){ ruleSet .setDefaultConfidence(1d) .setDefaultScore(defaultScore); } Scope scope = new DataFrameScope(FieldName.create("X"), features); for(Object[] rule : rules){ String predicate = TupleUtil.extractElement(rule, 0, String.class); String score = TupleUtil.extractElement(rule, 1, String.class); Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope); SimpleRule simpleRule = new SimpleRule(score, pmmlPredicate); ruleSet.addRules(simpleRule); } RuleSetModel ruleSetModel = new RuleSetModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(label), ruleSet); return ruleSetModel; }
Example 5
Source File: SelectFirstUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static private MiningModel encodeModel(MiningFunction miningFunction, List<Object[]> steps, Schema schema){ if(steps.size() < 1){ throw new IllegalArgumentException(); } Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); Segmentation segmentation = new Segmentation(Segmentation.MultipleModelMethod.SELECT_FIRST, null); Scope scope = new DataFrameScope(FieldName.create("X"), features); for(Object[] step : steps){ String name = TupleUtil.extractElement(step, 0, String.class); Estimator estimator = TupleUtil.extractElement(step, 1, Estimator.class); String predicate = TupleUtil.extractElement(step, 2, String.class); if(!(miningFunction).equals(estimator.getMiningFunction())){ throw new IllegalArgumentException(); } Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope); Model model = estimator.encodeModel(schema); Segment segment = new Segment(pmmlPredicate, model) .setId(name); segmentation.addSegments(segment); } MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(label)) .setSegmentation(segmentation); return miningModel; }
Example 6
Source File: LinearModelUtil.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
static public <C extends ModelConverter<?> & HasRegressionTableOptions> Model createRegression(C converter, Vector coefficients, double intercept, Schema schema){ ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel(); String representation = (String)converter.getOption(HasRegressionTableOptions.OPTION_REPRESENTATION, null); List<Feature> features = new ArrayList<>(schema.getFeatures()); List<Double> featureCoefficients = new ArrayList<>(VectorUtil.toList(coefficients)); RegressionTableUtil.simplify(converter, null, features, featureCoefficients); if(representation != null && (GeneralRegressionModel.class.getSimpleName()).equalsIgnoreCase(representation)){ GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.REGRESSION, MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel), null, null, null); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, null); return generalRegressionModel; } return RegressionModelUtil.createRegression(features, featureCoefficients, intercept, NormalizationMethod.NONE, schema); }
Example 7
Source File: EarthConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public GeneralRegressionModel encodeModel(Schema schema){ RGenericVector earth = getObject(); RDoubleVector coefficients = earth.getDoubleElement("coefficients"); Double intercept = coefficients.getValue(0); List<? extends Feature> features = schema.getFeatures(); SchemaUtil.checkSize(coefficients.size() - 1, features); List<Double> featureCoefficients = (coefficients.getValues()).subList(1, features.size() + 1); GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null) .setLinkFunction(GeneralRegressionModel.LinkFunction.IDENTITY); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, null); return generalRegressionModel; }
Example 8
Source File: LMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public Model encodeModel(Schema schema){ RGenericVector lm = getObject(); RDoubleVector coefficients = lm.getDoubleElement("coefficients"); Double intercept = coefficients.getElement(getInterceptName(), false); List<? extends Feature> features = schema.getFeatures(); SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features); List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients); return RegressionModelUtil.createRegression(features, featureCoefficients, intercept, null, schema); }
Example 9
Source File: GLMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public Model encodeModel(Schema schema){ RGenericVector glm = getObject(); RDoubleVector coefficients = glm.getDoubleElement("coefficients"); RGenericVector family = glm.getGenericElement("family"); Double intercept = coefficients.getElement(getInterceptName(), false); RStringVector familyFamily = family.getStringElement("family"); RStringVector familyLink = family.getStringElement("link"); Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features); List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients); MiningFunction miningFunction = getMiningFunction(familyFamily.asScalar()); Object targetCategory = null; switch(miningFunction){ case CLASSIFICATION: { CategoricalLabel categoricalLabel = (CategoricalLabel)label; SchemaUtil.checkSize(2, categoricalLabel); targetCategory = categoricalLabel.getValue(1); } break; default: break; } GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, miningFunction, ModelUtil.createMiningSchema(label), null, null, null) .setDistribution(parseFamily(familyFamily.asScalar())) .setLinkFunction(parseLinkFunction(familyLink.asScalar())) .setLinkParameter(parseLinkParameter(familyLink.asScalar())); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory); switch(miningFunction){ case CLASSIFICATION: generalRegressionModel.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, (CategoricalLabel)label)); break; default: break; } return generalRegressionModel; }
Example 10
Source File: NNConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public Model encodeModel(Schema schema){ RGenericVector nn = getObject(); RExp actFct = nn.getElement("act.fct"); RBooleanVector linearOutput = nn.getBooleanElement("linear.output"); RGenericVector weights = nn.getGenericElement("weights"); RStringVector actFctType = actFct.getStringAttribute("type"); // Select the first repetition weights = (RGenericVector)weights.getValue(0); NeuralNetwork.ActivationFunction activationFunction = NeuralNetwork.ActivationFunction.LOGISTIC; switch(actFctType.asScalar()){ case "logistic": activationFunction = NeuralNetwork.ActivationFunction.LOGISTIC; break; case "tanh": activationFunction = NeuralNetwork.ActivationFunction.TANH; break; default: throw new IllegalArgumentException(); } ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); NeuralInputs neuralInputs = NeuralNetworkUtil.createNeuralInputs(features, DataType.DOUBLE); List<NeuralLayer> neuralLayers = new ArrayList<>(); List<? extends NeuralEntity> entities = neuralInputs.getNeuralInputs(); for(int i = 0; i < weights.size(); i++){ boolean hidden = (i < (weights.size() - 1)); NeuralLayer neuralLayer = new NeuralLayer(); if(hidden || (linearOutput != null && !linearOutput.asScalar())){ neuralLayer.setActivationFunction(activationFunction); } RDoubleVector layerWeights = (RDoubleVector)weights.getValue(i); RIntegerVector layerDim = layerWeights.dim(); int layerRows = layerDim.getValue(0); int layerColumns = layerDim.getValue(1); for(int j = 0; j < layerColumns; j++){ List<Double> neuronWeights = FortranMatrixUtil.getColumn(layerWeights.getValues(), layerRows, layerColumns, j); String id; if(hidden){ id = "hidden/" + String.valueOf(i) + "/" + String.valueOf(j); } else { id = "output/" + String.valueOf(j); } Neuron neuron = NeuralNetworkUtil.createNeuron(entities, neuronWeights.subList(1, neuronWeights.size()), neuronWeights.get(0)) .setId(id); neuralLayer.addNeurons(neuron); } neuralLayers.add(neuralLayer); entities = neuralLayer.getNeurons(); } NeuralNetwork neuralNetwork = new NeuralNetwork(MiningFunction.REGRESSION, NeuralNetwork.ActivationFunction.IDENTITY, ModelUtil.createMiningSchema(continuousLabel), neuralInputs, neuralLayers) .setNeuralOutputs(NeuralNetworkUtil.createRegressionNeuralOutputs(entities, continuousLabel)); return neuralNetwork; }
Example 11
Source File: LinearDiscriminantAnalysis.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
private Model encodeMultinomialModel(Schema schema){ String sklearnVersion = getSkLearnVersion(); int[] shape = getCoefShape(); int numberOfClasses = shape[0]; int numberOfFeatures = shape[1]; List<? extends Number> coef = getCoef(); List<? extends Number> intercept = getIntercept(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); // See https://github.com/scikit-learn/scikit-learn/issues/6848 boolean corrected = (sklearnVersion != null && SkLearnUtil.compareVersion(sklearnVersion, "0.21") >= 0); if(!corrected){ return super.encodeModel(schema); } // End if if(numberOfClasses >= 3){ SchemaUtil.checkSize(numberOfClasses, categoricalLabel); Schema segmentSchema = (schema.toAnonymousRegressorSchema(DataType.DOUBLE)).toEmptySchema(); List<RegressionModel> regressionModels = new ArrayList<>(); for(int i = 0, rows = categoricalLabel.size(); i < rows; i++){ RegressionModel regressionModel = RegressionModelUtil.createRegression(features, CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, i), intercept.get(i), RegressionModel.NormalizationMethod.NONE, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction(" + categoricalLabel.getValue(i) + ")"), OpType.CONTINUOUS, DataType.DOUBLE)); regressionModels.add(regressionModel); } return MiningModelUtil.createClassification(regressionModels, RegressionModel.NormalizationMethod.SOFTMAX, true, schema); } else { throw new IllegalArgumentException(); } }
Example 12
Source File: MultilayerPerceptronUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
static public NeuralNetwork encodeNeuralNetwork(MiningFunction miningFunction, String activation, List<? extends HasArray> coefs, List<? extends HasArray> intercepts, Schema schema){ NeuralNetwork.ActivationFunction activationFunction = parseActivationFunction(activation); ClassDictUtil.checkSize(coefs, intercepts); Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); NeuralInputs neuralInputs = NeuralNetworkUtil.createNeuralInputs(features, DataType.DOUBLE); List<? extends NeuralEntity> entities = neuralInputs.getNeuralInputs(); List<NeuralLayer> neuralLayers = new ArrayList<>(); for(int layer = 0; layer < coefs.size(); layer++){ HasArray coef = coefs.get(layer); HasArray intercept = intercepts.get(layer); int[] shape = coef.getArrayShape(); int rows = shape[0]; int columns = shape[1]; NeuralLayer neuralLayer = new NeuralLayer(); List<?> coefMatrix = coef.getArrayContent(); List<?> interceptVector = intercept.getArrayContent(); for(int column = 0; column < columns; column++){ List<? extends Number> weights = (List)CMatrixUtil.getColumn(coefMatrix, rows, columns, column); Number bias = (Number)interceptVector.get(column); Neuron neuron = NeuralNetworkUtil.createNeuron(entities, weights, bias) .setId(String.valueOf(layer + 1) + "/" + String.valueOf(column + 1)); neuralLayer.addNeurons(neuron); } neuralLayers.add(neuralLayer); entities = neuralLayer.getNeurons(); if(layer == (coefs.size() - 1)){ neuralLayer.setActivationFunction(NeuralNetwork.ActivationFunction.IDENTITY); switch(miningFunction){ case REGRESSION: break; case CLASSIFICATION: CategoricalLabel categoricalLabel = (CategoricalLabel)label; // Binary classification if(categoricalLabel.size() == 2){ List<NeuralLayer> transformationNeuralLayers = NeuralNetworkUtil.createBinaryLogisticTransformation(Iterables.getOnlyElement(entities)); neuralLayers.addAll(transformationNeuralLayers); neuralLayer = Iterables.getLast(transformationNeuralLayers); entities = neuralLayer.getNeurons(); } else // Multi-class classification if(categoricalLabel.size() > 2){ neuralLayer.setNormalizationMethod(NeuralNetwork.NormalizationMethod.SOFTMAX); } else { throw new IllegalArgumentException(); } break; default: break; } } } NeuralOutputs neuralOutputs = null; switch(miningFunction){ case REGRESSION: neuralOutputs = NeuralNetworkUtil.createRegressionNeuralOutputs(entities, (ContinuousLabel)label); break; case CLASSIFICATION: neuralOutputs = NeuralNetworkUtil.createClassificationNeuralOutputs(entities, (CategoricalLabel)label); break; default: break; } NeuralNetwork neuralNetwork = new NeuralNetwork(miningFunction, activationFunction, ModelUtil.createMiningSchema(label), neuralInputs, neuralLayers) .setNeuralOutputs(neuralOutputs); return neuralNetwork; }
Example 13
Source File: StackingUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
static public <E extends Estimator> MiningModel encodeStacking(List<? extends E> estimators, List<String> stackMethods, PredictFunction predictFunction, E finalEstimator, boolean passthrough, Schema schema){ ClassDictUtil.checkSize(estimators, stackMethods); Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); SkLearnEncoder encoder = (SkLearnEncoder)getEncoder(features); List<Feature> stackFeatures = new ArrayList<>(); List<Model> models = new ArrayList<>(); for(int i = 0; i < estimators.size(); i++){ E estimator = estimators.get(i); String stackMethod = stackMethods.get(i); Model model = estimator.encodeModel(schema); List<Feature> predictFeatures = predictFunction.apply(i, model, stackMethod, encoder); if(predictFeatures != null && predictFeatures.size() > 0){ stackFeatures.addAll(predictFeatures); } models.add(model); } if(passthrough){ stackFeatures.addAll(features); } { Schema stackSchema = new Schema(label, stackFeatures); Model finalModel = finalEstimator.encodeModel(stackSchema); models.add(finalModel); } return MiningModelUtil.createModelChain(models); }
Example 14
Source File: LinearModelUtil.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
static public <C extends ModelConverter<?> & HasRegressionTableOptions> Model createBinaryLogisticClassification(C converter, Vector coefficients, double intercept, Schema schema){ CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); String representation = (String)converter.getOption(HasRegressionTableOptions.OPTION_REPRESENTATION, null); List<Feature> features = new ArrayList<>(schema.getFeatures()); List<Double> featureCoefficients = new ArrayList<>(VectorUtil.toList(coefficients)); RegressionTableUtil.simplify(converter, null, features, featureCoefficients); if(representation != null && (GeneralRegressionModel.class.getSimpleName()).equalsIgnoreCase(representation)){ Object targetCategory = categoricalLabel.getValue(1); GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), null, null, null) .setLinkFunction(GeneralRegressionModel.LinkFunction.LOGIT); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory); return generalRegressionModel; } return RegressionModelUtil.createBinaryLogisticClassification(features, featureCoefficients, intercept, RegressionModel.NormalizationMethod.LOGIT, true, schema); }
Example 15
Source File: LinearModelUtil.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 3 votes |
static public <C extends ModelConverter<?> & HasRegressionTableOptions> Model createSoftmaxClassification(C converter, Matrix coefficients, Vector intercepts, Schema schema){ CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); MatrixUtil.checkRows(categoricalLabel.size(), coefficients); List<RegressionTable> regressionTables = new ArrayList<>(); for(int i = 0; i < categoricalLabel.size(); i++){ Object targetCategory = categoricalLabel.getValue(i); List<Feature> features = new ArrayList<>(schema.getFeatures()); List<Double> featureCoefficients = new ArrayList<>(MatrixUtil.getRow(coefficients, i)); RegressionTableUtil.simplify(converter, targetCategory, features, featureCoefficients); double intercept = intercepts.apply(i); RegressionTable regressionTable = RegressionModelUtil.createRegressionTable(features, featureCoefficients, intercept) .setTargetCategory(targetCategory); regressionTables.add(regressionTable); } RegressionModel regressionModel = new RegressionModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), regressionTables) .setNormalizationMethod(RegressionModel.NormalizationMethod.SOFTMAX); return regressionModel; }
Example 16
Source File: LRMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 3 votes |
@Override public Model encodeModel(Schema schema){ RGenericVector lrm = getObject(); RDoubleVector coefficients = lrm.getDoubleElement("coefficients"); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); SchemaUtil.checkSize(2, categoricalLabel); Object targetCategory = categoricalLabel.getValue(1); Double intercept = coefficients.getElement(getInterceptName(), false); List<? extends Feature> features = schema.getFeatures(); SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features); List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients); GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), null, null, null) .setLinkFunction(GeneralRegressionModel.LinkFunction.LOGIT) .setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel)); GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory); return generalRegressionModel; }
Example 17
Source File: BaseEstimator.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 2 votes |
@Override public Model encodeModel(Schema schema){ MojoModel mojoModel = getMojoModel(); Converter<?> converter; try { ConverterFactory converterFactory = ConverterFactory.newConverterFactory(); converter = converterFactory.newConverter(mojoModel); } catch(Exception e){ throw new IllegalArgumentException(e); } Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); H2OEncoder encoder = new H2OEncoder(); Schema h2oSchema = converter.encodeSchema(encoder); Label h2oLabel = h2oSchema.getLabel(); List<? extends Feature> h2oFeatures = h2oSchema.getFeatures(); List<Feature> sortedFeatures = new ArrayList<>(); for(Feature h2oFeature : h2oFeatures){ FieldName name = h2oFeature.getName(); Feature feature; if(features instanceof FeatureList){ FeatureList namedFeatures = (FeatureList)features; feature = namedFeatures.getFeature(name.getValue()); } else { int index = Integer.parseInt((name.getValue()).substring(1)) - 1; feature = features.get(index); } sortedFeatures.add(feature); } Schema mojoModelSchema = converter.toMojoModelSchema(new Schema(label, sortedFeatures)); return converter.encodeModel(mojoModelSchema); }
Example 18
Source File: LinearClassifier.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 2 votes |
@Override public Model encodeModel(Schema schema){ int[] shape = getCoefShape(); int numberOfClasses = shape[0]; int numberOfFeatures = shape[1]; boolean hasProbabilityDistribution = hasProbabilityDistribution(); List<? extends Number> coef = getCoef(); List<? extends Number> intercept = getIntercept(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); if(numberOfClasses == 1){ SchemaUtil.checkSize(2, categoricalLabel); return RegressionModelUtil.createBinaryLogisticClassification(features, CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, 0), intercept.get(0), RegressionModel.NormalizationMethod.LOGIT, hasProbabilityDistribution, schema); } else if(numberOfClasses >= 3){ SchemaUtil.checkSize(numberOfClasses, categoricalLabel); Schema segmentSchema = (schema.toAnonymousRegressorSchema(DataType.DOUBLE)).toEmptySchema(); List<RegressionModel> regressionModels = new ArrayList<>(); for(int i = 0, rows = categoricalLabel.size(); i < rows; i++){ RegressionModel regressionModel = RegressionModelUtil.createRegression(features, CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, i), intercept.get(i), RegressionModel.NormalizationMethod.LOGIT, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction(" + categoricalLabel.getValue(i) + ")"), OpType.CONTINUOUS, DataType.DOUBLE)); regressionModels.add(regressionModel); } return MiningModelUtil.createClassification(regressionModels, RegressionModel.NormalizationMethod.SIMPLEMAX, hasProbabilityDistribution, schema); } else { throw new IllegalArgumentException(); } }