/// <summary> /// Reduces a number of predictions voting for classification and averaging /// predictions for regression. /// </summary> /// <param name="method"> {0|1|2|3} method Code associated to the voting method (plurality, /// confidence weighted or probability weighted or threshold). </param> /// <param name="withConfidence"> if withConfidence is true, the combined confidence /// (as a weighted of the prediction average of the confidences /// of votes for the combined prediction) will also be given. </param> /// <returns> {{"prediction": prediction, "confidence": combinedConfidence}} </returns> public virtual Dictionary <object, object> combine(int method = PLURALITY_CODE, bool withConfidence = false, bool?addConfidence = false, bool?addDistribution = false, bool?addCount = false, bool?addMedian = false, IDictionary options = null) { // there must be at least one prediction to be combined if (this.predictions.Length == 0) { throw new Exception("No predictions to be combined."); } string[] keys = WEIGHT_KEYS[COMBINER_MAP[method]]; // and all predictions should have the weight-related keys if (keys != null && keys.Length > 0) { checkKeys(this.predictions, keys); } // Regression ensemble if (this.is_regression()) { foreach (Dictionary <object, object> prediction in predictions) { if (!prediction.ContainsKey("confidence")) { prediction["confidence"] = 0.0; } } if (method == CONFIDENCE_CODE) { return(this.errorWeighted(withConfidence, addConfidence, addDistribution, addCount, addMedian)); } return(this.avg(withConfidence, addConfidence, addDistribution, addCount, addMedian)); } // Categorical ensemble MultiVote multiVote = null; if (method == THRESHOLD_CODE) { int? threshold = (int?)options["threshold"]; string category = (string)options["category"]; multiVote = singleOutCategory(threshold, category); } else if (method == PROBABILITY_CODE) { multiVote = new MultiVote(this.probabilityWeight()); } else { multiVote = this; } return(multiVote.combineCategorical(COMBINATION_WEIGHTS[COMBINER_MAP[method]], withConfidence)); }
/// <summary> /// Returns a distribution formed by grouping the distributions of each predicted node. /// </summary> protected internal Dictionary <string, object> getGroupedDistribution(MultiVote multiVoteInstance) { Dictionary <object, double> joinedDist = new Dictionary <object, double>(); string distributionUnit = "counts"; Dictionary <object, double> predictionDist; object distribution; foreach (Dictionary <object, object> prediction in multiVoteInstance.Predictions) { // Equivalent to: // JSONArray predictionDist = (JSONArray) prediction.get("distribution"); predictionDist = null; distribution = prediction["distribution"]; if (distribution is IDictionary) { predictionDist = (Dictionary <object, double>)distribution; } else if ((distribution == null) || (!(distribution is JsonArray))) { predictionDist = new Dictionary <object, double>(); } else { predictionDist = Utils.convertDistributionArrayToMap((JsonArray)distribution); } // Equivalent to: // joinedDist = Utils.mergeDistributions(joinedDist, Utils.convertDistributionArrayToMap(predictionDist)); joinedDist = Utils.mergeDistributions(joinedDist, predictionDist); if ("counts".Equals(distributionUnit) && joinedDist.Count > BINS_LIMIT) { distributionUnit = "bins"; } joinedDist = Utils.mergeBins(joinedDist, BINS_LIMIT); } Dictionary <string, object> distributionInfo = new Dictionary <string, object>(); distributionInfo["distribution"] = Utils.convertDistributionMapToSortedArray(joinedDist); distributionInfo["distributionUnit"] = distributionUnit; return(distributionInfo); }
/// <summary> /// Given a multi vote instance (alist of predictions), extends the list /// with another list of predictions and adds the order information. /// /// For instance, predictions_info could be: /// /// [{'prediction': 'Iris-virginica', 'confidence': 0.3}, /// {'prediction': 'Iris-versicolor', 'confidence': 0.8}] /// /// where the expected prediction keys are: prediction (compulsory), /// confidence, distribution and count. /// </summary> /// <param name="votes"> </param> public virtual void extend(MultiVote votes) { if (votes.predictions != null && votes.predictions.Length > 0) { int order = nextOrder(); List <Dictionary <object, object> > predictionsList = new List <Dictionary <object, object> >(predictions); foreach (Dictionary <object, object> prediction in votes.predictions) { prediction["order"] = (order + 1); predictionsList.Add(prediction); } predictions = (Dictionary <object, object>[])predictionsList.ToArray(); } }
public Dictionary <object, object> predict(Dictionary <string, dynamic> inputData, bool byName = true, Combiner combiner = Combiner.Plurality, int missing_strategy = 0, bool addDistribution = true) { mv = new MultiVote(); if (_models.Count > 1) { inputData = _models[0].prepareInputData(inputData); } for (i = 0; i < this._models.Count; i++) { _modelsPredictions[i] = this._models[i].predict(inputData, byName, missing_strategy); mv.append(_modelsPredictions[i].toDictionary(addDistribution)); } return(mv.combine((int)combiner, addDistribution: addDistribution)); }
public Dictionary <object, object> predict(Dictionary <string, dynamic> inputData, bool byName = true, Combiner combiner = Combiner.Plurality, int missing_strategy = 0, bool addDistribution = true) { mv = new MultiVote(); bool addConfidence = areBoostedTrees; if (_models.Count > 1) { inputData = _models[0].prepareInputData(inputData); byName = false; } mv.areBoostedTrees = areBoostedTrees; Dictionary <object, object> vote; for (i = 0; i < this._models.Count; i++) { if (areBoostedTrees) { _modelsBoostedPredictions[i] = this._models[i].predictBoosted(inputData, byName, missing_strategy); vote = _modelsBoostedPredictions[i].toDictionary(); vote["weight"] = this._models[i].Boosting["weight"]; if (this._models[i].Boosting.ContainsKey("objective_class")) { vote["class"] = this._models[i].Boosting["objective_class"]; } mv.append(vote); } else { _modelsPredictions[i] = this._models[i].predict(inputData, byName, missing_strategy); mv.append(_modelsPredictions[i].toDictionary(addDistribution)); } } return(mv.combine((int)combiner, addDistribution: addDistribution, addConfidence: addConfidence)); }
public TupleComparator(MultiVote outerInstance) { this.outerInstance = outerInstance; }