예제 #1
0
 function predict($input_data, $by_name = true, $method = MultiVote::PLURALITY_CODE, $with_confidence = false, $add_confidence = false, $add_distribution = false, $add_count = false, $add_median = false, $add_min = false, $add_max = false, $options = null, $missing_strategy = Tree::LAST_PREDICTION, $median = false)
 {
     /*
        Makes a prediction based on the prediction made by every model.
        The method parameter is a numeric key to the following combination
        methods in classifications/regressions:
           0 - majority vote (plurality)/ average: PLURALITY_CODE
           1 - confidence weighted majority vote / error weighted:
              CONFIDENCE_CODE
           2 - probability weighted majority vote / average:
              PROBABILITY_CODE
           3 - threshold filtered vote / doesn't apply:
              THRESHOLD_CODE
     */
     if (count($this->models_splits) > 1) {
         $votes = new MultiVote(array());
         $models = array();
         $api = $this->api;
         $order = 0;
         foreach ($this->models_splits as $model_split) {
             $models = array();
             foreach ($model_split as $model_id) {
                 array_push($models, $api::retrieve_resource($model_id, $api::ONLY_MODEL));
             }
             $multi_model = new MultiModel($models, $this->api);
             $votes_split = $multi_model->generate_votes($input_data, $by_name, $missing_strategy, $add_median || $median, $add_min, $add_max);
             if ($median) {
                 foreach ($votes_split->predictions as $prediction) {
                     $prediction['prediction'] = $prediction['median'];
                 }
             }
             $votes->extend($votes_split->predictions);
         }
         #return $votes->combine($method, $with_confidence, $options);
     } else {
         # When only one group of models is found you use the
         # corresponding multimodel to predict
         $votes_split = $this->multi_model->generate_votes($input_data, $by_name, $missing_strategy, $add_median || $median, $add_min, $add_max);
         $votes = new MultiVote($votes_split->predictions);
         if ($median) {
             $new_predictions = array();
             foreach ($votes->predictions as $prediction) {
                 $prediction->prediction = $prediction->median;
                 array_push($new_predictions, $prediction);
             }
             $votes->predictions = $new_predictions;
         }
     }
     return $votes->combine($method, $with_confidence, $add_confidence, $add_distribution, $add_count, $add_median, $add_min, $add_max, $options);
 }
예제 #2
0
 function _predict($input_data, $by_name = true, $method = MultiVote::PLURALITY_CODE, $with_confidence = false, $add_confidence = false, $add_distribution = false, $add_count = false, $add_median = false, $add_unused_fields = false, $add_min = false, $add_max = false, $options = null, $missing_strategy = Tree::LAST_PREDICTION, $median = false)
 {
     /*
        Makes a prediction based on the prediction made by every model.
       
        :param input_data: Test data to be used as input
     	 :param by_name: Boolean that is set to true if field_names (as
     	                 alternative to field ids) are used in the
     			 input_data dict
     	 :param method: numeric key code for the following combination
                       methods in classifications/regressions:
     
           0 - majority vote (plurality)/ average: PLURALITY_CODE
           1 - confidence weighted majority vote / error weighted:
              CONFIDENCE_CODE
           2 - probability weighted majority vote / average:
              PROBABILITY_CODE
           3 - threshold filtered vote / doesn't apply:
              THRESHOLD_CODE
     
     	 The following parameter causes the result to be returned as a list
         :param add_confidence: Adds confidence to the prediction
         :param add_distribution: Adds the predicted node's distribution to the prediction
     	  :param add_count: Adds the predicted nodes' instances to the prediction
     	  :param add_median: Adds the median of the predicted nodes' distribution
     	                     to the prediction
         :param add_min: Boolean, if true adds the minimum value in the
     	                          prediction's distribution (for regressions only)
         :param add_max: Boolean, if true adds the maximum value in the
                       prediction's distribution (for regressions only)
         :param add_unused_fields: Boolean, if true adds the information about
                                 the fields in the input_data that are not
                                 being used in the model as predictors.
         :param options: Options to be used in threshold filtered votes.
         :param missing_strategy: numeric key for the individual model's
                                prediction method. See the model predict
                                method.
         :param median: Uses the median of each individual model's predicted
                      node as individual prediction for the specified
                      combination method.				  
     */
     if (count($this->models_splits) > 1) {
         $votes = new MultiVote(array());
         $models = array();
         $api = $this->api;
         $order = 0;
         foreach ($this->models_splits as $model_split) {
             $models = array();
             foreach ($model_split as $model_id) {
                 array_push($models, $api::retrieve_resource($model_id, $api::ONLY_MODEL));
             }
             $multi_model = new MultiModel($models, $this->api);
             $votes_split = $multi_model->generate_votes($input_data, $by_name, $missing_strategy, $add_median || $median, $add_min, $add_max, $add_unused_fields);
             if ($median) {
                 foreach ($votes_split->predictions as $prediction) {
                     $prediction['prediction'] = $prediction['median'];
                 }
             }
             $votes->extend($votes_split->predictions);
         }
         #return $votes->combine($method, $with_confidence, $options);
     } else {
         # When only one group of models is found you use the
         # corresponding multimodel to predict
         $votes_split = $this->multi_model->generate_votes($input_data, $by_name, $missing_strategy, $add_median || $median, $add_min, $add_max, $add_unused_fields);
         $votes = new MultiVote($votes_split->predictions);
         if ($median) {
             $new_predictions = array();
             foreach ($votes->predictions as $prediction) {
                 $prediction->prediction = $prediction->median;
                 array_push($new_predictions, $prediction);
             }
             $votes->predictions = $new_predictions;
         }
     }
     $result = $votes->combine($method, $with_confidence, $add_confidence, $add_distribution, $add_count, $add_median, $add_min, $add_max, $options);
     if ($add_unused_fields) {
         $unused_fields = array_unique(array_keys($input_data));
         foreach ($votes->predictions as $index => $prediction) {
             $unused_fields = array_intersect($unused_fields, array_unique($prediction->unused_fields));
         }
         if (!is_array($result)) {
             $result = array("prediction" => $result);
         }
         $result['unused_fields'] = $unused_fields;
     }
     return $result;
 }