예제 #1
0
 public function __construct($model, $api = null)
 {
     if (check_model_structure($model)) {
         $this->resource_id = $model->resource;
     } else {
         if ($api == null) {
             $api = new BigML(null, null, null, $storage);
         }
         if (is_string($model)) {
             if (!$api::_checkModelId($model)) {
                 error_log("Wrong model id");
                 return null;
             }
             $model = $api::retrieve_resource($model, $api::ONLY_MODEL);
         }
     }
     if (property_exists($model, "object") && $model->object instanceof STDClass) {
         $model = $model->object;
     }
     if (property_exists($model, "model") && $model->model instanceof STDClass) {
         if ($model->status->code == BigMLRequest::FINISHED) {
             if (property_exists($model->model, "model_fields")) {
                 foreach ($model->model->model_fields as $key => $value) {
                     if (!property_exists($model->model->fields, $key)) {
                         throw new Exception("Some fields are missing to generate a local model " . $key . "  Please, provide a model with the complete list of fields.");
                     }
                     if (property_exists($model->model->fields->{$key}, "summary")) {
                         $model->model->model_fields->{$key}->summary = $model->model->fields->{$key}->summary;
                     }
                     $model->model->model_fields->{$key}->name = $model->model->fields->{$key}->name;
                 }
             }
             parent::__construct($model->model->model_fields, extract_objective($model->objective_fields));
             $this->description = $model->description;
             $this->field_importance = property_exists($model->model, "importance") ? $model->model->importance : null;
             if ($this->field_importance != null) {
                 $fields_importance = array();
                 foreach ($this->field_importance as $field => $value) {
                     if (property_exists($model->model->model_fields, $value[0])) {
                         array_push($fields_importance, $value);
                     }
                 }
                 $this->field_importance = $fields_importance;
             }
             if (property_exists($model, "locale" && $model->locale != null)) {
                 $this->locale = $model->locale;
             }
         } else {
             throw new Exception("The model isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the BaseModel instance. Could not  find the 'model' key in the resource:\n\n " . print_r($model));
     }
 }
예제 #2
0
 public function __construct($logistic_regression, $api = null, $storage = "storage")
 {
     $this->input_fields = array();
     $this->term_forms = array();
     $this->tag_clouds = array();
     $this->term_analysis = array();
     $this->items = array();
     $this->item_analysis = array();
     $this->categories = array();
     $this->data_field_types = array();
     $this->numeric_fields = array();
     $old_coefficients = false;
     if ($api == null) {
         $api = new BigML(null, null, null, $storage);
     }
     if (is_string($logistic_regression)) {
         if (file_exists($logistic_regression)) {
             $logistic_regression = json_decode(file_get_contents($logistic_regression));
         } else {
             if (!$api::_checkModelId($logistic_regression)) {
                 error_log("Wrong logistic regression id");
                 return null;
             } else {
                 $logistic_regression = $api::retrieve_resource($logistic_regression, $api::ONLY_MODEL);
             }
         }
     }
     if ($logistic_regression == null || !property_exists($logistic_regression, 'resource')) {
         error_log("Cannot create the Model instance. Could not find the 'logistic_regression' key in the resource");
         throw new Exception('Cannot create the logistic regression instance. Could not find the logistic regression key in the resource');
     }
     if (property_exists($logistic_regression, "object") && property_exists($logistic_regression->object, "status") && $logistic_regression->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The logistic_regression isn't finished yet");
     }
     if (property_exists($logistic_regression, "object") && $logistic_regression->object instanceof STDClass) {
         $logistic_regression = $logistic_regression->object;
     }
     if (property_exists($logistic_regression, "logistic_regression") && $logistic_regression->logistic_regression instanceof STDClass) {
         if ($logistic_regression->status->code == BigMLRequest::FINISHED) {
             $this->input_fields = property_exists($logistic_regression, "input_fields") ? $logistic_regression->input_fields : array();
             $this->dataset_field_types = property_exists($logistic_regression, "dataset_field_types") ? $logistic_regression->dataset_field_types : array();
             $objective_field = $logistic_regression->objective_fields;
             $logistic_regression_info = $logistic_regression->logistic_regression;
             $fields = property_exists($logistic_regression_info, "fields") ? $logistic_regression_info->fields : array();
             if (is_null($this->input_fields) or empty($this->input_fields)) {
                 $this->input_fields = array();
                 $fields_sorted_by_column_number = array();
                 foreach ($fields as $field_id => $field) {
                     $a[$field_id] = $field->column_number;
                 }
                 asort($fields_sorted_by_column_number);
                 foreach ($fields_sorted_by_column_number as $key => $value) {
                     array_push($this->input_fields, $key);
                 }
             }
             $this->coefficients = array();
             if (property_exists($logistic_regression_info, "coefficients")) {
                 $j = 0;
                 foreach ($logistic_regression_info->coefficients as $key => $coefficient) {
                     $this->coefficients[$coefficient[0]] = $coefficient[1];
                     if ($j == 0 and !is_array($coefficient[1])) {
                         $old_coefficients = true;
                     }
                     $j += 1;
                 }
             }
             $this->bias = property_exists($logistic_regression_info, "bias") ? $logistic_regression_info->bias : 0;
             $this->c = property_exists($logistic_regression_info, "c") ? $logistic_regression_info->c : null;
             $this->eps = property_exists($logistic_regression_info, "eps") ? $logistic_regression_info->eps : null;
             $this->lr_normalize = property_exists($logistic_regression_info, "normalize") ? $logistic_regression_info->normalize : null;
             $this->balance_fields = property_exists($logistic_regression_info, "balance_fields") ? $logistic_regression_info->balance_fields : null;
             $this->regularization = property_exists($logistic_regression_info, "regularization") ? $logistic_regression_info->regularization : null;
             $this->field_codings = property_exists($logistic_regression_info, "field_codings") ? $logistic_regression_info->field_codings : array();
             $this->missing_numerics = property_exists($logistic_regression_info, "missing_numerics") ? $logistic_regression_info->missing_numerics : false;
             $objective_id = extract_objective($objective_field);
             foreach ($fields as $field_id => $field) {
                 if ($field->optype == 'text') {
                     $this->term_forms[$field_id] = $field->summary->term_forms;
                     $this->tag_clouds[$field_id] = array();
                     # TODO revisar
                     foreach ($field->summary->tag_cloud as $tag => $value) {
                         array_push($this->tag_clouds[$field_id], $value[0]);
                     }
                     $this->term_analysis[$field_id] = $field->term_analysis;
                 } else {
                     if ($field->optype == 'items') {
                         $this->items[$field_id] = array();
                         foreach ($field->summary->items as $item => $value) {
                             array_push($this->items[$field_id], $value[0]);
                         }
                         $this->item_analysis[$field_id] = $field->item_analysis;
                     } else {
                         if ($field->optype == 'categorical') {
                             $this->categories[$field_id] = array();
                             foreach ($field->summary->categories as $key => $value) {
                                 array_push($this->categories[$field_id], $value[0]);
                             }
                         }
                     }
                 }
                 if ($this->missing_numerics && $field->optype == "numeric") {
                     $this->numeric_fields[$field_id] = true;
                 }
             }
             parent::__construct($fields, $objective_id);
             $this->field_codings = property_exists($logistic_regression_info, "field_codings") ? $logistic_regression_info->field_codings : array();
             $this->format_field_codings();
             foreach ($this->field_codings as $field_id => $field_coding) {
                 if (array_key_exists($field_id, $fields) && array_key_exists($field_id, $this->inverted_fields)) {
                     $this->field_codings[$this->inverted_fields[$field_id]] = $this->field_codings[$field_id];
                     unset($this->field_codings[$field_id]);
                 }
             }
             if ($old_coefficients) {
                 $this->map_coefficients();
             }
         } else {
             throw new Exception("The logistic regression isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the Model instance. Could not find the 'logistic regression' key in the resource:\n\n" . $logistic_regression);
     }
 }