public function test_scenario2()
 {
     $data = array(array('filename' => 'data/grades.csv', 'input_data' => array('Tutorial' => 99.47, 'Midterm' => 53.12, 'TakeHome' => 87.95999999999999), 'prediction' => 50));
     foreach ($data as $item) {
         print "I create a data source uploading a " . $item["filename"] . " file\n";
         $source = self::$api->create_source($item["filename"], $options = array('name' => 'local_test_source'));
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $source->code);
         $this->assertEquals(1, $source->object->status->code);
         print "check local source is ready\n";
         $resource = self::$api->_check_resource($source->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         print "create dataset with local source\n";
         $dataset = self::$api->create_dataset($source->resource);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $dataset->code);
         $this->assertEquals(BigMLRequest::QUEUED, $dataset->object->status->code);
         $model = self::$api->create_model($dataset->resource);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model->code);
         print "check model is ready\n";
         $resource = self::$api->_check_resource($model->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         $list_of_models = array(self::$api->get_model($model->resource));
         print "I create a local multi model\n";
         $local_multimodel = new MultiModel($list_of_models);
         print "I create a batch prediction\n";
         $batch_predict = $local_multimodel->batch_predict(array($item["input_data"]), null, true, false, Tree::LAST_PREDICTION, null, false, true);
         $this->assertEquals(current($batch_predict)->predictions[0]["prediction"][0], $item["prediction"]);
     }
 }
Example #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_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);
 }
 public function test_scenario2()
 {
     $data = array(array('filename' => 'data/iris.csv', 'params' => array("tags" => array("mytag"), 'missing_splits' => false), 'data_input' => array(array("petal width" => 0.5), array("petal length" => 6, "petal width" => 2)), 'tag' => 'mytag', 'predictions' => array("Iris-setosa", "Iris-virginica")));
     foreach ($data as $item) {
         print "\nSuccessfully creating a local batch prediction from a multi model\n";
         print "I create a data source uploading a " . $item["filename"] . " file\n";
         $source = self::$api->create_source($item["filename"], $options = array('name' => 'local_test_source', 'project' => self::$project->resource));
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $source->code);
         $this->assertEquals(1, $source->object->status->code);
         print "And I wait until the source is ready\n";
         $resource = self::$api->_check_resource($source->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         print "And I create dataset with local source\n";
         $dataset = self::$api->create_dataset($source->resource);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $dataset->code);
         $this->assertEquals(BigMLRequest::QUEUED, $dataset->object->status->code);
         print "And I wait until the dataset is ready " . $dataset->resource . " \n";
         $resource = self::$api->_check_resource($dataset->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         $list_of_models = array();
         print "And I create model with params " . json_encode($item["params"]) . "\n";
         $model_1 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_1->code);
         print "And I wait until the is ready\n";
         $resource = self::$api->_check_resource($model_1->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_1->resource));
         print "And I create model with params " . json_encode($item["params"]) . "\n";
         $model_2 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_2->code);
         print "And I wait until the is ready\n";
         $resource = self::$api->_check_resource($model_2->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_2->resource));
         print "And I create model with params " . json_encode($item["params"]) . "\n";
         $model_3 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_3->code);
         print "And I wait until the is ready\n";
         $resource = self::$api->_check_resource($model_3->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_3->resource));
         print "And I create a local multi model\n";
         $local_multimodel = new MultiModel($list_of_models);
         print "When I create a batch multimodel prediction for " . json_encode($item["data_input"]) . " \n";
         $predictions = $local_multimodel->batch_predict($item["data_input"], null, array('to_file' => false));
         $i = 0;
         print "Then the predictions are " . json_encode($item["predictions"]) . "\n";
         foreach ($predictions as $multivote) {
             foreach ($multivote->predictions as $prediction) {
                 $this->assertEquals($prediction["prediction"], $item["predictions"][$i]);
             }
             $i += 1;
         }
         $this->assertEquals($i, count($predictions));
     }
 }
Example #4
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;
 }
 public function test_scenario2()
 {
     $data = array(array('filename' => 'data/iris.csv', 'params' => array("tags" => array("mytag"), 'missing_splits' => false), 'data_input' => array(array("petal width" => 0.5), array("petal length" => 6, "petal width" => 2)), 'tag' => 'mytag', 'predictions' => array("Iris-setosa", "Iris-virginica")));
     foreach ($data as $item) {
         print "I create a data source uploading a " . $item["filename"] . " file\n";
         $source = self::$api->create_source($item["filename"], $options = array('name' => 'local_test_source'));
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $source->code);
         $this->assertEquals(1, $source->object->status->code);
         print "check local source is ready\n";
         $resource = self::$api->_check_resource($source->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         print "create dataset with local source\n";
         $dataset = self::$api->create_dataset($source->resource);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $dataset->code);
         $this->assertEquals(BigMLRequest::QUEUED, $dataset->object->status->code);
         print "check the dataset is ready " . $dataset->resource . " \n";
         $resource = self::$api->_check_resource($dataset->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         $list_of_models = array();
         print "create model_1\n";
         $model_1 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_1->code);
         print "check model_1 is ready\n";
         $resource = self::$api->_check_resource($model_1->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_1->resource));
         print "create model_2\n";
         $model_2 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_2->code);
         print "check model_2 is ready\n";
         $resource = self::$api->_check_resource($model_2->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_2->resource));
         print "create model_3\n";
         $model_3 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_3->code);
         print "check model_3 is ready\n";
         $resource = self::$api->_check_resource($model_3->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_3->resource));
         print "I create a local multi model\n";
         $local_multimodel = new MultiModel($list_of_models);
         print "I create a prediction\n";
         $predictions = $local_multimodel->batch_predict($item["data_input"], null, true, false, Tree::LAST_PREDICTION, null, false, false);
         $i = 0;
         foreach ($predictions as $multivote) {
             foreach ($multivote->predictions as $prediction) {
                 $this->assertEquals($prediction["prediction"], $item["predictions"][$i]);
             }
             $i += 1;
         }
         $this->assertEquals($i, count($predictions));
     }
 }
 public function test_scenario1()
 {
     $data = array(array('filename' => 'data/iris.csv', 'params' => array("tags" => array("mytag"), 'missing_splits' => false), 'data_input' => array(array("petal width" => 0.5), array("petal length" => 6, "petal width" => 2), array("petal length" => 4, "petal width" => 1.5)), 'tag' => 'mytag', 'path' => 'tmp/', 'predictions' => array("Iris-setosa", "Iris-virginica", "Iris-versicolor")));
     foreach ($data as $item) {
         print "I create a data source uploading a " . $item["filename"] . " file\n";
         $source = self::$api->create_source($item["filename"], $options = array('name' => 'local_test_source'));
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $source->code);
         $this->assertEquals(1, $source->object->status->code);
         print "check local source is ready\n";
         $resource = self::$api->_check_resource($source->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         print "create dataset with local source\n";
         $dataset = self::$api->create_dataset($source->resource);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $dataset->code);
         $this->assertEquals(BigMLRequest::QUEUED, $dataset->object->status->code);
         print "check the dataset is ready " . $dataset->resource . " \n";
         $resource = self::$api->_check_resource($dataset->resource, null, 20000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         print "create model_1\n";
         $model_1 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_1->code);
         $list_of_models = array();
         print "check model_1 is ready\n";
         $resource = self::$api->_check_resource($model_1->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_1->resource));
         print "create model_2\n";
         $model_2 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_2->code);
         print "check model_2 is ready\n";
         $resource = self::$api->_check_resource($model_2->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_2->resource));
         print "create model_3\n";
         $model_3 = self::$api->create_model($dataset->resource, $item["params"]);
         $this->assertEquals(BigMLRequest::HTTP_CREATED, $model_3->code);
         print "check model_3 is ready\n";
         $resource = self::$api->_check_resource($model_3->resource, null, 3000, 30);
         $this->assertEquals(BigMLRequest::FINISHED, $resource["status"]);
         array_push($list_of_models, self::$api->get_model($model_3->resource));
         print "I create a local multi model\n";
         $local_multimodel = new MultiModel($list_of_models);
         if (!is_dir($item["path"])) {
             mkdir($item["path"]);
         }
         print "I create a batch prediction\n";
         $batch_predict = $local_multimodel->batch_predict($item["data_input"], $item["path"]);
         print "I combine the votes in\n";
         $votes = $local_multimodel->batch_votes($item["path"]);
         print "test the plurarity combined prediction\n";
         $i = 0;
         foreach ($votes as $vote) {
             $this->assertEquals($item["predictions"][$i], $vote->combine());
             $i += 1;
         }
         print "test the confidence weighted prediction\n";
         $i = 0;
         foreach ($votes as $vote) {
             $this->assertEquals($item["predictions"][$i], $vote->combine(1));
             $i += 1;
         }
     }
 }