Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Binary classifier target.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_analytics\local\target;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Binary classifier target.
  31   *
  32   * @package   core_analytics
  33   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  abstract class binary extends discrete {
  37  
  38      /**
  39       * is_linear
  40       *
  41       * @return bool
  42       */
  43      public function is_linear() {
  44          return false;
  45      }
  46  
  47      /**
  48       * Returns the target discrete values.
  49       *
  50       * Only useful for targets using discrete values, must be overwriten if it is the case.
  51       *
  52       * @return array
  53       */
  54      public static final function get_classes() {
  55          return array(0, 1);
  56      }
  57  
  58      /**
  59       * Returns the predicted classes that will be ignored.
  60       *
  61       * @return array
  62       */
  63      public function ignored_predicted_classes() {
  64          // Zero-value class is usually ignored in binary classifiers.
  65          return array(0);
  66      }
  67  
  68      /**
  69       * Is the calculated value a positive outcome of this target?
  70       *
  71       * @param string $value
  72       * @param string $ignoredsubtype
  73       * @return int
  74       */
  75      public function get_calculation_outcome($value, $ignoredsubtype = false) {
  76  
  77          if (!self::is_a_class($value)) {
  78              throw new \moodle_exception('errorpredictionformat', 'analytics');
  79          }
  80  
  81          if (in_array($value, $this->ignored_predicted_classes(), false)) {
  82              // Just in case, if it is ignored the prediction should not even be recorded but if it would, it is ignored now,
  83              // which should mean that is it nothing serious.
  84              return self::OUTCOME_VERY_POSITIVE;
  85          }
  86  
  87          // By default binaries are danger when prediction = 1.
  88          if ($value) {
  89              return self::OUTCOME_VERY_NEGATIVE;
  90          }
  91          return self::OUTCOME_VERY_POSITIVE;
  92      }
  93  
  94      /**
  95       * classes_description
  96       *
  97       * @return string[]
  98       */
  99      protected static function classes_description() {
 100          return array(
 101              get_string('yes'),
 102              get_string('no')
 103          );
 104      }
 105  
 106  }