Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Masina de copt paine - pareri

Cum reactivez Google Maps?

Conectare tableta X220la Tv

Femeile tinere nu mai vor sa munc...
 La mulți ani @un_dac!

La multi ani de Sfantul Gheorghe&...

Job - Facultate sau certificare

Deadpool & Wolverine (2023)
 sistem hibrid eoliana + panouri +...

Outlook e muta pe Android

Constructie Mun. Iasi. Casa P+1.

Cum mai rezolvati cu chiriasii ra...
 Tastatura si mouse cu baterie int...

AC Gree duce la palpait de becuri

Sfat / recomandare construire aco...

Cablu analog vs digital
 

concepte si design OOP

- - - - -
  • Please log in to reply
198 replies to this topic

#199
miti20

miti20

    New Member

  • Grup: Junior Members
  • Posts: 1
  • Înscris: 13.03.2016
Salut. Am o problema si as avea mare nevoie de ajutor.
Vreau sa fac un sitem de recomandare bazat pe arbori de decizie.
Mi-am construit arborele de decizie cu algoritmul id3(am folosit clasele: id3.class.php si DS_Tree.class.php )
Datele mele pe baza carora am construit arborle sunt intr-o baza de date si am construi arborele ca si in fisierul (data.php)
Eu as vrea ca pe baza datelor unui utilizator sa ii recomand un anumit exercitiu cum as putea sa fac acest lucru?
Cum as putea sa creez reguli de decizie si cum as putea pe urma sa ma folosesc de ele si de arborele meu pt a recomanda.
id3.class.php
   <?php
if(!defined('NODE_NAME')) define('NODE_NAME', 'name');
if(!defined('NODE_CLASS_NAME')) define('NODE_CLASS_NAME', 'category');
class id3
{
    private $AttrList = array(); //list of attribute
    private $Values = array();
    private $Class = array(); //classification result
    private $Instance = array(); //
    public $tree = array();   // tree

private $log = array(); // debug log output
    private $dbg_c = 0;
    private $class_name = NODE_CLASS_NAME;
    public function init($AttrList, $Values, $Class, $Instance, $tree) {
    $this->AttrList = $AttrList;
    $this->Values = $Values;
    $this->Class = $Class;
    $this->Instance = $Instance;
    $this->tree = $tree;
    }
    public function run() {
    while(!$this->check_completed()) {
    $attr = $this->get_minI_attr();
   $this->log[] = $attr;
    $attr_key = array_search($attr, $this->AttrList);
    unset($this->AttrList[$attr_key]);  //distruge variabila specificata
    $this->tree->update_node_attr('name', $attr);
    $this->make_tree($attr);
  
   if (empty($this->AttrList)) {
    // echo '<pre class="brush: php">';
  $recordTree=$this->tree;
    return $recordTree;
  // var_export($this->log);
// echo '</pre>';
    exit;
    throw new Exception("AttrList is empty!");
    }
    }
  }
    /**
* Check whether the algorithm can be terminated
* @return bool
*/
    private function check_completed() {
    $leaf_nodes = $this->tree->get_leafs();
    $end_flag = true;
    foreach ($leaf_nodes as $n) {
    if(!in_array($n[NODE_NAME], $this->Class)) {
    $end_flag = false;
    break;
    }
    }
    return $end_flag;
    }
    /**
* Calculation example subset of the set of entropy
* @param $subset
* @return float|int
*/
    private function calculate_entropy($subset) {
    $ret = 0;
    $e = array(); //number of instances
  
    $e_all = count($subset);
    foreach($subset as $s) { //calculate the number of instances of each property
    if (isset($e[$s[$this->class_name]])) {
    $e[$s[$this->class_name]]++;
    }
    }
    foreach($e as $en) { //calculate the entropy
    $t = $en / $e_all;
    if ($t == 0) continue; //log2(0) patch
    $ret-= $t * log($t, 2);
  
   //if ($this->dbg_c == 3)   
    //$this->log[] = var_export($t * log($t, 2), true);
    }
    return $ret;
    }
    /**
* In accordance with rule subsets of examples taken
* @param $attr
* @param $value
* @return array
*/
    private function get_subset($attr, $value) {
    $ret = array();
    foreach($this->Instance as $k => $i) {
    if (isset($i[$attr]) && $i[$attr] == $value) {
    $ret[$k] = $i;
    }
    }
    return $ret;
    }
    /**
* Subset of the set of test cases have the same category

* @param $subset
* @return bool|string
*/
    private function has_same_class($subset) {
    $the_class = '';
    foreach($subset as $s) {
    if ($the_class != '' && $the_class != $s[$this->class_name]) {
    return false;
    } else if ($the_class == '') {
    $the_class = $s[$this->class_name];
    }
    }
    return $the_class;
    }
    /**
* Calculate the average amount of information to find out the current state of minimal property name
* @return mixed
*/
    private function get_minI_attr() {
    $i_avg = array();
    foreach ($this->AttrList as $attr) {
    $i_avg[$attr] = 0; // property of entropy
    foreach($this->Values[$attr] as $v) { //calculate a value of a property of entropy
    $subset = $this->get_subset($attr, $v);
    $this->dbg_c++;
    $subset_I = $this->calculate_entropy($subset);
    $this->log[] = var_export($attr, true);
    $this->log[] = var_export($v, true);
    $this->log[] = var_export($subset_I, true);
    $i_avg[$attr] += count($subset) / count($this->Instance) * $subset_I;
    }
    }
    asort($i_avg);
    return key($i_avg);
    }
    /**
* Generate sub-division level decision tree based on the specified attribute
* @param $attr
*/
    private function make_tree($attr) {
    foreach($this->Values[$attr] as $v) {
    $subset = $this->get_subset($attr, $v);
    if($the_class = $this->has_same_class($subset)) {
    $node =array(
    'name' => $the_class,
    'arc' => $v
    );
    $this->tree->insert_node($node);
    $this->Instance = array_diff_key($this->Instance, $subset); //update instance set left unsorted
    } else {
    $node =array(
    'name' => 'start',
    'arc' => $v
    );
    $unresolved = $this->tree->insert_node($node);
    }
    }
    if (isset($unresolved)) {
    $this->tree->goto_index($unresolved);
  
    }
    }
}
?>
Ds_Tree.class.php
<?php
if (!defined('NODE_PARENT')) define('NODE_PARENT', 'parent');
if (!defined('NODE_CHILDREN')) define('NODE_CHILDREN', 'children');
if (!defined('NODE_ARC')) define('NODE_ARC', 'arc');
class DS_Tree
{
    private  $nodes = array(); //array of nodes
    private $current_node_ptr; //current node pointer (index)
    /**
* Tree is empty
* @return bool
*/
    public function is_empty() {
    return empty($this->nodes);
    }
    /**
* returns the number of nodes
* @return int
*/
    public function get_size() {
    return count($this->nodes);
    }
    /**
* insert child node in the current node and returns inserted node id
* @param $node
* @return int
*/
    public function insert_node($node) {
    $node_id = array_push($this->nodes, $node) - 1;
    $new_node = &$this->nodes[$node_id];
    if ($node_id != 0) { //insert non-root node
    $pnode = &$this->nodes[$this->current_node_ptr];
    if (empty($pnode[NODE_CHILDREN]))
    $pnode[NODE_CHILDREN] = array();
    array_push($pnode[NODE_CHILDREN], $node_id);
    $new_node[NODE_PARENT] = $this->current_node_ptr;
    } else {
    $new_node[NODE_PARENT] = NULL;
    }
    return $node_id;
    }
    /**
* update the current property value of the node
* @param $key
* @param $value
*/
    public function update_node_attr($key, $value) {
    $cur_node = &$this->nodes[$this->current_node_ptr];
    $cur_node[$key] = $value;
    }
    /**
* get the current value of the property
* @param $key
* @return mixed
*/
    public function get_node_attr($key) {
    return $this->nodes[$key];
    }
    /**
* current node as the root node of the tree
*/
    public function goto_root() {
    if ($this->get_size() > 0) {
    reset($this->nodes);
    $this->current_node_ptr = key($this->nodes);
    return true;
    } else {
    return false;
    }
    }
    /**
* skip the specified index node
* @param $index
* @return bool
*/
    public function goto_index($index) {
    if(isset($this->nodes[$index])) {
    $this->current_node_ptr = $index;
    return true;
    } else {
    return false;
    }
    }
    /**
* Retrieve the current node index
* @return mixed
*/
    public function get_index() {
    return $this->current_node_ptr;
    }
    /**
* take sub-nodes
* @return int
*/
    public function count_children() {
    return count($this->nodes[$this->current_node_ptr][NODE_CHILDREN]);
    }
    /**
* take a leaf node
* @return array
*/
    public function get_leafs() {
    $ret = array();
    foreach($this->nodes as $k => $n) {
    if (empty($n[NODE_CHILDREN]))
    $ret[$k] = $n;
    }
    return $ret;
    }
public function draw_tree() {
   return var_export($this->nodes, true);
    }
    /**
* retuns the layer below the current node child node
* @return array
*/
    public function get_children() {
    $ret = array();
    $children_id = $this->nodes[$this->current_node_ptr][NODE_CHILDREN];
    foreach($children_id as $k) {
    $ret[$k] = $this->nodes[$k];
    }
    return $ret;
    }
}
data.php
<?php
error_reporting(E_ALL);
include_once(dirname(__FILE__) . '/id3.class.php');
include_once(dirname(__FILE__) . '/DS_Tree.class.php');
   $servername = "localhost";
$username = "root";
$password = "";
$dbname = "fitnessdb";
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
?>
<?php

$sql="SELECT Sex,Deficulty,MuscleGroup,Exercises FROM sondaj ";
$result = $conn->query($sql);
  $data_array_instances=array();
  while( $row = mysqli_fetch_array($result)){

array_push($data_array_instances,$row);
  }

  mysqli_free_result($result);
  

   $sqlSex="SELECT DISTINCT Sex FROM sondaj ";
   $resultS = $conn->query($sqlSex);
   $data_array_valuesS=array();
   while( $rowS = mysqli_fetch_array($resultS)){

$data_array_valuesS[]=$rowS['Sex'];
  }

  mysqli_free_result($resultS);
  

   $sqlDificulty="SELECT DISTINCT Dificulty FROM sondaj ";
   $resultG = $conn->query($sqlDificulty);
   $data_array_valuesG=array();
   while( $rowG = mysqli_fetch_array($resultG)){

$data_array_valuesG[]=$rowG['Dificulty'];
  }

  mysqli_free_result($resultG);
  

   $sqlExercises="SELECT DISTINCT Exercises FROM sondaj ";
   $resultEx = $conn->query($sqlExercises);
   $data_array_valuesEx=array();
   while( $rowEx = mysqli_fetch_array($resultEx)){

$data_array_valuesEx[]=$rowEx['Exercises'];
  }

  mysqli_free_result($resultEx);

  
   $sqlMuscleGroup="SELECT DISTINCT MuscleGroup FROM sondaj ";
   $resultM = $conn->query($sqlMuscleGroup);
   $data_array_valuesM=array();
   while( $rowM = mysqli_fetch_array($resultM)){

$data_array_valuesM[]=$rowM['MuscleGroup'];
  }

  mysqli_free_result($resultM);
  
   $data_array_values=array();
   $data_array_values=array(
    Sex" => array($data_array_valuesS),
"Dificulty" => array($data_array_valuesG),
"MuscleGroup" => array($data_array_valuesM)
    );

  
   $data_array_AttrList=array('Sex','Dificulty','MuscleGroup');
   $data_class=array( "Exercises" =>array($data_array_valuesEx));
  
   $tree= new DS_Tree();
   $node=array('name' => 'start');
  
   $tree->insert_node($node);
   $tree->goto_root();
  
   $mytree = new id3();
   $mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree);
  
  $mytree->run();

/*$arraytree=array();
$arraytree=$tree->draw_tree();*/

echo '<pre class="brush: php">';
   print_r($mytree);
   //print_r($mytree->tree);

   echo '</pre>';

Anunturi

Second Opinion Second Opinion

Folosind serviciul second opinion ne puteți trimite RMN-uri, CT -uri, angiografii, fișiere .pdf, documente medicale.

Astfel vă vom putea da o opinie neurochirurgicală, fără ca aceasta să poată înlocui un consult de specialitate. Răspunsurile vor fi date prin e-mail în cel mai scurt timp posibil (de obicei în mai putin de 24 de ore, dar nu mai mult de 48 de ore). Second opinion – Neurohope este un serviciu gratuit.

www.neurohope.ro

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate