Jump to content

Decision Tree Implementation (with code here)


kikaypixels

Recommended Posts

HI im trying to implement a decision tree using the inductive decision tree algorithm I already started with this code.. and I wonder where I could get references in implementing decision tree

thank you very much!

 

 

Here is my code i dont know how to continue :(

 

 

<?php include("sqlconfig.php"); ?>

<?php

mysql_select_db("bbFiles",$kcCon);

#Getting the total entropy

$prefList = mysql_query("SELECT preference from a1backtracking");

$countTotalrows = mysql_num_rows($prefList);

$prefcountYes = 0;

$prefcountNo = 0;

while($row = mysql_fetch_array($prefList))

{

if($row['preference']=="y")

{

if($prefcountYes>0)

$prefYes[$prefcountYes]=$row['preference'];

else

$prefYes = array($row['preference']);

$prefcountYes++;

}

elseif($row['preference']=="n")

{

if($prefcountNo>0)

$prefNo[$prefcountNo]= $row['preference'];

else

$prefNo = array($row['preference']);

$prefcountNo++;

}

else

{

echo "Category not recognized!";

echo "<br/>";

}

}

#computing for the main entropy

$entropy =( -$prefcountYes/$countTotalrows*(log10($prefcountYes/$countTotalrows)/log10(2))) + (-$prefcountNo/$countTotalrows*( log10($prefcountNo/$countTotalrows)/log10(2)));

# end compute

#function getting all YES small entropies

 

$catList= mysql_query("SELECT * from a1backtracking WHERE preference='y'");

$countA = 0;

$countB = 0;

$countC = 0;

$countD = 0;

while($row = mysql_fetch_array($catList))

{

if($row['category']=="cosmetics")

{

if($countA>0)

$catCosY[$countA]=$row['category'];

else

$catCosY = array($row['category']);

$countA++;

}

elseif($row['category']=="spa")

{

if($countB>0)

$catSpaY[$countB]= $row['category'];

else

$catSpaY = array($row['category']);

$countB++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

#texture

while($row = mysql_fetch_array($catList))

{

if($row['texture']=="rough")

{

if($countA>0)

$txtRouY[$countA]=$row['texture'];

else

$txtRouY = array($row['texture']);

$countA++;

}

elseif($row['texture']=="creamy")

{

if($countB>0)

$txtRouY[$countB]= $row['texture'];

else

$txtRouY = array($row['texture']);

$countB++;

}

elseif($row['texture']=="powdery")

{

if($countC>0)

$txtCreY[$countB]= $row['texture'];

else

$txtCreY = array($row['texture']);

$countC++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

#whitening

while($row = mysql_fetch_array($catList))

{

if($row['whitening']=="y")

{

if($countA>0)

$yeswhiteY[$countA]=$row['whitening'];

else

$yeswhiteY = array($row['whitening']);

$countA++;

}

elseif($row['whitening']=="n")

{

if($countB>0)

$nowhiteN[$countB]= $row['whitening'];

else

$nowhiteN = array($row['whitening']);

$countB++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

 

#bodypart

#texture

while($row = mysql_fetch_array($catList))

{

if($row['bodypart']=="face")

{

if($countA>0)

$bodFaceY[$countA]=$row['bodypart'];

else

$bodFaceY = array($row['bodypart']);

$countA++;

}

elseif($row['bodypart']=="skin")

{

if($countB>0)

$bodSkinY[$countB]= $row['bodypart'];

else

$bodSkinY = array($row['bodypart']);

$countB++;

}

elseif($row['bodypart']=="feet")

{

if($countC>0)

$txtCreY[$countB]= $row['bodypart'];

else

$txtCreY = array($row['bodypart']);

$countC++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

 

#entropy for preference N

$catListN= mysql_query("SELECT * from a1backtracking WHERE preference='n'");

$countA = 0;

$countB = 0;

$countC = 0;

$countD = 0;

while($row = mysql_fetch_array($catListN))

{

if($row['category']=="cosmetics")

{

if($countA>0)

$catCosY[$countA]=$row['category'];

else

$catCosY = array($row['category']);

$countA++;

}

elseif($row['category']=="spa")

{

if($countB>0)

$catSpaY[$countB]= $row['category'];

else

$catSpaY = array($row['category']);

$countB++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

#texture

while($row = mysql_fetch_array($catListN))

{

if($row['texture']=="rough")

{

if($countA>0)

$txtRouY[$countA]=$row['texture'];

else

$txtRouY = array($row['texture']);

$countA++;

}

elseif($row['texture']=="creamy")

{

if($countB>0)

$txtRouY[$countB]= $row['texture'];

else

$txtRouY = array($row['texture']);

$countB++;

}

elseif($row['texture']=="powdery")

{

if($countC>0)

$txtCreY[$countB]= $row['texture'];

else

$txtCreY = array($row['texture']);

$countC++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

#whitening

while($row = mysql_fetch_array($catListN))

{

if($row['whitening']=="y")

{

if($countA>0)

$yeswhiteY[$countA]=$row['whitening'];

else

$yeswhiteY = array($row['whitening']);

$countA++;

}

elseif($row['whitening']=="n")

{

if($countB>0)

$nowhiteN[$countB]= $row['whitening'];

else

$nowhiteN = array($row['whitening']);

$countB++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

 

#bodypart

#texture

while($row = mysql_fetch_array($catListN))

{

if($row['bodypart']=="face")

{

if($countA>0)

$bodFaceY[$countA]=$row['bodypart'];

else

$bodFaceY = array($row['bodypart']);

$countA++;

}

elseif($row['bodypart']=="skin")

{

if($countB>0)

$bodSkinY[$countB]= $row['bodypart'];

else

$bodSkinY = array($row['bodypart']);

$countB++;

}

elseif($row['bodypart']=="feet")

{

if($countC>0)

$txtCreY[$countB]= $row['bodypart'];

else

$txtCreY = array($row['bodypart']);

$countC++;

}else

{

echo "Category not recognized!";

echo "<br/>";

}

}

 

 

 

 

 

# Tree making

#IG levels

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

# THIS IS TESTING AREA TO TEST AND DISPLAY RESULTS

foreach($catSpaY as $value)

{

 

echo "We highly recommend: ".$value. "<br/>";

}

foreach($catCosY as $value)

{

 

echo "We highly recommend: ".$value. "<br/>";

}

foreach($prefYes as $value)

{

echo "Preference yes are: ". $value. "<br/>";

}

foreach($prefNo as $value)

{

echo "Preference no are: ". $value. "<br/>";

}

echo "Entropy is equal to: <b/>".$entropy."<br/>";

echo $entropy2."<br/>";

echo $entropy1."<br/>";

 

 

?>

<?php include("sqlclose.php"); ?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.