static protected Double selectValue(Map<String, ?> values, String key, Double defaultValue){ Number value = (Number)values.get(key); if(value == null){ return defaultValue; } return ValueUtil.asDouble(value); }
@Override public RegressionModel encodeModel(Schema schema){ List<? extends Number> coef = getCoef(); List<? extends Number> intercept = getIntercept(); return RegressionModelUtil.createRegression(schema.getFeatures(), ValueUtil.asDoubles(coef), ValueUtil.asDouble(Iterables.getOnlyElement(intercept)), null, schema); }
public Double getGamma(){ return ValueUtil.asDouble((Number)get("_gamma")); }
@Override public SupportVectorMachineModel encodeModel(Schema schema){ int[] shape = getSupportVectorsShape(); int numberOfVectors = shape[0]; int numberOfFeatures = shape[1]; List<Integer> support = getSupport(); List<? extends Number> supportVectors = getSupportVectors(); List<? extends Number> dualCoef = getDualCoef(); List<? extends Number> intercept = getIntercept(); SupportVectorMachineModel supportVectorMachineModel = LibSVMUtil.createRegression(new CMatrix<>(ValueUtil.asDoubles(supportVectors), numberOfVectors, numberOfFeatures), SupportVectorMachineUtil.formatIds(support), ValueUtil.asDouble(Iterables.getOnlyElement(intercept)), ValueUtil.asDoubles(dualCoef), schema) .setKernel(SupportVectorMachineUtil.createKernel(getKernel(), getDegree(), getGamma(), getCoef0())); return supportVectorMachineModel; }
public Double getGamma(){ return ValueUtil.asDouble((Number)get("_gamma")); }
@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> intercepts = getIntercept(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); if(numberOfClasses == 1){ ClassifierUtil.checkSize(2, categoricalLabel); return RegressionModelUtil.createBinaryLogisticClassification(features, ValueUtil.asDoubles(CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, 0)), ValueUtil.asDouble(intercepts.get(0)), RegressionModel.NormalizationMethod.LOGIT, hasProbabilityDistribution, schema); } else if(numberOfClasses >= 3){ ClassifierUtil.checkSize(numberOfClasses, categoricalLabel); Schema segmentSchema = new Schema(new ContinuousLabel(null, DataType.DOUBLE), Collections.emptyList()); List<RegressionModel> regressionModels = new ArrayList<>(); for(int i = 0, rows = categoricalLabel.size(); i < rows; i++){ RegressionModel regressionModel = RegressionModelUtil.createRegression(features, ValueUtil.asDoubles(CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, i)), ValueUtil.asDouble(intercepts.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(); } }
public Double getCoef0(){ return ValueUtil.asDouble((Number)get("coef0")); }
RegressionModel firstRegressionModel = RegressionModelUtil.createRegression(features, ValueUtil.asDoubles(CMatrixUtil.getRow(coef, 1, numberOfFeatures, 0)), ValueUtil.asDouble(intercepts.get(0)), null, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE)); RegressionTable regressionTable = RegressionModelUtil.createRegressionTable(features, ValueUtil.asDoubles(CMatrixUtil.getRow(coef, numberOfClasses, numberOfFeatures, i)), ValueUtil.asDouble(intercepts.get(i))) .setTargetCategory(categoricalLabel.getValue(i));
public Double getCoef0(){ return ValueUtil.asDouble((Number)get("coef0")); }
static private TargetValueStats encodeTargetValueStats(List<String> values, List<? extends Number> means, List<? extends Number> variances){ TargetValueStats targetValueStats = new TargetValueStats(); ClassDictUtil.checkSize(values, means, variances); for(int i = 0; i < values.size(); i++){ GaussianDistribution gaussianDistribution = new GaussianDistribution(ValueUtil.asDouble(means.get(i)), ValueUtil.asDouble(variances.get(i))); TargetValueStat targetValueStat = new TargetValueStat(values.get(i)) .setContinuousDistribution(gaussianDistribution); targetValueStats.addTargetValueStats(targetValueStat); } return targetValueStats; }
public double getThreshold(){ Number threshold; // SkLearn 0.19 if(containsKey("threshold_")){ threshold = (Number)getScalar("threshold_"); } else // SkLearn 0.20+ { threshold = (Number)getScalar("_threshold_"); } return ValueUtil.asDouble(threshold); }
static public Segmentation createSegmentation(Segmentation.MultipleModelMethod multipleModelMethod, List<? extends Model> models, List<? extends Number> weights){ if((weights != null) && (models.size() != weights.size())){ throw new IllegalArgumentException(); } List<Segment> segments = new ArrayList<>(); for(int i = 0; i < models.size(); i++){ Model model = models.get(i); Number weight = (weights != null ? weights.get(i) : null); Segment segment = new Segment() .setId(String.valueOf(i + 1)) .setPredicate(new True()) .setModel(model); if(weight != null && !ValueUtil.isOne(weight)){ segment.setWeight(ValueUtil.asDouble(weight)); } segments.add(segment); } return new Segmentation(multipleModelMethod, segments); }
@Override public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount){ if(splitValue.doubleValue() != 0d || (terminalClassCount == null || terminalClassCount.size() != levels.size())){ throw new IllegalArgumentException(); } node = NodeUtil.toComplexNode(node); List<ScoreDistribution> scoreDistributions = node.getScoreDistributions(); Double maxProbability = null; for(int i = 0; i < terminalClassCount.size(); i++){ String value = levels.getValue(i); Double probability = ValueUtil.asDouble(terminalClassCount.getValue(i)); if(maxProbability == null || (maxProbability).compareTo(probability) < 0){ node.setScore(value); maxProbability = probability; } ScoreDistribution scoreDisctibution = new ScoreDistribution(value, probability); scoreDistributions.add(scoreDisctibution); } return node; } };
Double bias = ValueUtil.asDouble((Number)interceptVector.get(column));
static public Kernel createKernel(String kernel, Integer degree, Double gamma, Double coef0){ switch(kernel){ case "linear": return new LinearKernel(); case "poly": return new PolynomialKernel() .setGamma(gamma) .setCoef0(coef0) .setDegree(ValueUtil.asDouble(degree)); case "rbf": return new RadialBasisKernel() .setGamma(gamma); case "sigmoid": return new SigmoidKernel() .setGamma(gamma) .setCoef0(coef0); default: throw new IllegalArgumentException(kernel); } } }
apply = PMMLUtil.createApply("/", apply, PMMLUtil.createConstant(Math.sqrt(ValueUtil.asDouble(explainedVarianceValue))));