|
class ID3
package guiID3;<br />
import java.text.DecimalFormat;<br />
import java.util.ArrayList;<br />
import java.util.Date;<br />
import java.util.LinkedList;<br />
import java.util.List;<br />
import java.util.ListIterator;<br />
import java.util.Random;<br />
import javax.swing.JOptionPane;<br />
<br />
<br />
public class ID3 implements Runnable {<br />
private Attribute [] attributesArray;<br />
private DataInstance [] dataInstancesArray;<br />
private GUIID3JFrame caller;<br />
private boolean interrupted;<br />
private int numClasses;<br />
private Thread cvThread;<br />
private int[] count;<br />
private int[][][] freqTij;<br />
private double [][] infoTij;<br />
<br />
<br />
public ID3(GUIID3JFrame caller){<br />
this.caller = caller;<br />
interrupted = false;<br />
}<br />
<br />
public void start() {<br />
cvThread = new Thread(this);<br />
try {<br />
<br />
cvThread.start();<br />
caller.getJProgressBar1().setIndeterminate(true);<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
public void stop(){<br />
cvThread = null;<br />
interrupted = true;<br />
}<br />
<br />
<br />
<br />
public void run() {<br />
String targetAttribute="";<br />
int arraysSize = attributesArray.length - 1;<br />
int numClasses = attributesArray[arraysSize].getTypes().length; <br />
ArrayList usedAttributes=new ArrayList();<br />
TreeNode root = mountTree(dataInstancesArray, "result", attributesArray,usedAttributes);<br />
printNode(root, " ", caller);<br />
stop();<br />
<br />
}<br />
public static void printNode(TreeNode root,String tabs, GUIID3JFrame caller)<br />
{<br />
<br />
caller.getJTextArea1().append(tabs + "|" + root.getMAttribute().getAttributeName() + "|"+"\n");<br />
<br />
if (root.getMAttribute().getIntTypesArray() != null)<br />
{<br />
for (int i = 0; i < root.getMAttribute().getIntTypesArray().length; i++)<br />
{<br />
caller.getJTextArea1().append(tabs + "\t" + "<" + root.getMAttribute().getTypes()[i] + ">"+"\n");<br />
<br />
TreeNode childNode = root.getChildByBranchName(root.getMAttribute().getTypes()[i]);<br />
printNode(childNode,"\t" + tabs,caller);<br />
}<br />
}<br />
}<br />
<br />
private boolean checkSameClass(DataInstance[] dataInstancesArray, int targetClass,Attribute [] attributesArray)<br />
{<br />
boolean isEqual = true;<br />
for(int n=0; n < dataInstancesArray.length && isEqual; n++){<br />
if( dataInstancesArray[n].getClassValue()== targetClass)<br />
isEqual = true;<br />
else<br />
isEqual = false;<br />
}<br />
return isEqual;<br />
}<br />
<br />
<br />
private double calcEntropy(int [] count,int total)<br />
{<br />
double result=0.0;<br />
double [] ratio=new double[count.length];<br />
for(int i=0;i<=count.length-1;i++){<br />
ratio[i]=(double)count[i]/total;<br />
if (ratio[i]!=0.0){<br />
ratio[i]=-(double)(ratio[i])* log2(ratio[i]);<br />
result=result+ratio[i];<br />
}<br />
}<br />
return result;<br />
<br />
}<br />
<br />
private int[] getValuesToAttribute(DataInstance [] dataInstancesArray, Attribute att, int value, Attribute[] attributes)<br />
{<br />
int[] count = new int[attributes[attributes.length-1].getIntTypesArray().length];<br />
<br />
for(DataInstance d:dataInstancesArray)<br />
{<br />
if(d.getValues()[att.getAttributeAsInt()]== value){ <br />
for(int i=0;i<=count.length-1;i++){<br />
if(d.getClassValue()==i){<br />
count[i]++;<br />
}<br />
}<br />
} <br />
}<br />
return count;<br />
}<br />
<br />
<br />
private double gain(DataInstance [] dataInstancesArray, Attribute att, Attribute[] attributes,double mEntropySet)<br />
{ <br />
int [] values = att.getIntTypesArray();<br />
<br />
double sum = 0.0;<br />
<br />
for (int i = 0; i < values.length; i++)<br />
{<br />
<br />
int [] count=getValuesToAttribute( dataInstancesArray, att, values[i],attributes);<br />
int s=0;<br />
for(int j=0;j<=count.length-1;j++)<br />
s=s+count[j];<br />
double entropy = calcEntropy(count,s); <br />
sum += -(double)(s)/dataInstancesArray.length * entropy;<br />
}<br />
return mEntropySet + sum;<br />
}<br />
<br />
<br />
private Attribute getBestAttribute( DataInstance [] dataInstancesArray, Attribute[] attributes,double mEntropySet)<br />
{<br />
double maxGain = 0.0;<br />
Attribute result = null;<br />
<br />
for( int i=0;i<attributes.length-1;i++)<br />
{<br />
<br />
double aux = gain(dataInstancesArray, attributes[i] ,attributes, mEntropySet);<br />
if (aux > maxGain)<br />
{<br />
maxGain = aux;<br />
result = attributes[i];<br />
}<br />
}<br />
<br />
return result;<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
private int [] countFrqClass(DataInstance [] dataInstancesArray,Attribute[] attributes){<br />
int arraysSize = attributes.length - 1;<br />
numClasses = attributes[arraysSize].getTypes().length;<br />
<br />
int[] count = new int[numClasses];<br />
<br />
for(DataInstance sample :dataInstancesArray)<br />
{<br />
int index = sample.getClassValue();<br />
count[index]++;<br />
}<br />
return count;<br />
}<br />
<br />
<br />
<br />
private String getMostCommonValue(DataInstance [] dataInstancesArray,Attribute[] attributes)<br />
{<br />
<br />
int []count=countFrqClass(dataInstancesArray,attributes);<br />
int MaxIndex = 0;<br />
int MaxCount = 0;<br />
<br />
for(int i = 0; i < count.length; i++)<br />
{<br />
if (count[i] > MaxCount)<br />
{<br />
MaxCount = count[i];<br />
MaxIndex = i;<br />
}<br />
}<br />
<br />
return attributes[attributes.length - 1].getTypes()[MaxIndex] ;<br />
}<br />
<br />
private TreeNode internalMountTree(DataInstance [] dataInstancesArray, Attribute[] attributes,String targetAttribute, ArrayList usedAttributes)<br />
{<br />
for(int i=0;i<attributes[attributes.length-1].getIntTypesArray().length;i++){<br />
if (checkSameClass(dataInstancesArray,i ,attributes) == true){<br />
return new TreeNode(new Attribute(attributes[attributes.length-1].getTypes()[i]));<br />
}<br />
}<br />
<br />
if (attributes.length == 0)<br />
return new TreeNode(new Attribute(getMostCommonValue(dataInstancesArray, attributes))); <br />
int mTotal = dataInstancesArray.length;<br />
int[] count = countFrqClass(dataInstancesArray, attributes);<br />
double mEntropySet = calcEntropy(count,mTotal);<br />
Attribute bestAttribute = getBestAttribute( dataInstancesArray,attributes,mEntropySet); <br />
usedAttributes.add(bestAttribute.getAttributeName());<br />
<br />
TreeNode root = new TreeNode(bestAttribute);<br />
ArrayList listOfInstances=new ArrayList();<br />
<br />
for(int value:bestAttribute.getIntTypesArray()){<br />
<br />
listOfInstances.clear();<br />
for(int i=0;i<dataInstancesArray.length;i++){<br />
if(dataInstancesArray[i].getValues()[bestAttribute.getAttributeAsInt()]==value){<br />
listOfInstances.add((DataInstance)dataInstancesArray[i]); <br />
}<br />
}<br />
<br />
DataInstance [] trainingSetClone = new DataInstance[listOfInstances.size()];<br />
<br />
int y=0;<br />
for(ListIterator i2=listOfInstances.listIterator(); i2.hasNext();){<br />
trainingSetClone[y]=(DataInstance)i2.next();<br />
y++;<br />
}<br />
<br />
ArrayList aAttributes = new ArrayList(attributes.length - 1);<br />
for(int i = 0; i < attributes.length-1; i++)<br />
{<br />
if (usedAttributes.contains(attributes[i].getAttributeName())!=true)<br />
aAttributes.add(attributes[i]);<br />
<br />
}<br />
if (trainingSetClone.length== 0)<br />
{<br />
return new TreeNode(new Attribute(getMostCommonValue(trainingSetClone,attributes )));<br />
}<br />
else<br />
{ <br />
Attribute[ ] attributeSet=(Attribute[ ])aAttributes.toArray(new Attribute[aAttributes.size()]);<br />
TreeNode ChildNode = mountTree(trainingSetClone,targetAttribute,attributeSet,usedAttributes);<br />
root.AddTreeNode(ChildNode, value);<br />
}<br />
}<br />
return root;<br />
}<br />
<br />
<br />
<br />
public void setAttributesArray(Attribute[] attributesArray) {<br />
this.attributesArray = attributesArray;<br />
}<br />
<br />
public void setDataInstancesArray(DataInstance[] dataInstancesArray) {<br />
this.dataInstancesArray = dataInstancesArray;<br />
}<br />
<br />
public static double log2(double d) {<br />
return Math.log(d)/Math.log(2.0);<br />
}<br />
<br />
public TreeNode mountTree(DataInstance[] dataInstancesArray, String targetAttribute,Attribute [] attributesArrayused, ArrayList usedAttributes)<br />
{<br />
this.dataInstancesArray = dataInstancesArray;<br />
return internalMountTree(dataInstancesArray,attributesArray ,targetAttribute,usedAttributes );<br />
}<br />
<br />
}<br />
<br />
-----class MyFileReader-----
package guiID3;<br />
import java.io.BufferedReader;<br />
import java.io.File;<br />
import java.io.FileReader;<br />
import java.io.IOException;<br />
import java.io.StreamTokenizer;<br />
import java.util.ArrayList;<br />
import java.util.List;<br />
import java.util.ListIterator;<br />
import java.io.*;<br />
<br />
import javax.swing.JOptionPane;<br />
<br />
public class MyFileReader {<br />
private String relation;<br />
<br />
private DataInstance [] dataInstancesArray;<br />
private Attribute [] attributesArray;<br />
<br />
private boolean fileIsOk;<br />
<br />
public MyFileReader(File file){<br />
String lastWord = "";<br />
boolean gotNext, error=false;<br />
<br />
List attributesNameList = new ArrayList();<br />
List attributesList = new ArrayList();<br />
List attributeValuesList = new ArrayList(); <br />
List dataInstancesList = new ArrayList();<br />
<br />
try{<br />
<br />
BufferedReader in = new BufferedReader(new FileReader(file));<br />
StreamTokenizer st = new StreamTokenizer(in);<br />
<br />
st.commentChar('%');<br />
st.ordinaryChars(45,62);<br />
st.wordChars(95,95);<br />
st.wordChars(45,63);<br />
st.wordChars(38,39);<br />
st.whitespaceChars(',',',');<br />
st.nextToken();<br />
<br />
while(st.ttype != StreamTokenizer.TT_EOF){<br />
gotNext = false;<br />
if(st.ttype == 64){
st.nextToken();<br />
<br />
if(st.sval.trim().compareToIgnoreCase("RELATION") == 0){<br />
st.nextToken();<br />
relation = st.sval;<br />
} else if(st.sval.trim().compareToIgnoreCase("ATTRIBUTE") == 0){<br />
st.nextToken();<br />
<br />
attributesNameList.add(st.sval);<br />
attributeValuesList.clear();<br />
<br />
int linenum = st.lineno();<br />
st.nextToken();<br />
while(linenum == st.lineno()){<br />
if(st.ttype == StreamTokenizer.TT_WORD){<br />
attributeValuesList.add(st.sval);<br />
lastWord = st.sval;<br />
}<br />
st.nextToken();<br />
if(linenum != st.lineno() && lastWord.trim().compareToIgnoreCase("REAL") == 0){<br />
JOptionPane.showMessageDialog(null, "<html><font face=Dialog size=3>This version of GUI Ant-Miner can only handle nominal attributes.</font></html>", "Error", JOptionPane.ERROR_MESSAGE);<br />
fileIsOk = false;<br />
throw new IOException("File error");<br />
}<br />
gotNext = true;<br />
}<br />
<br />
<br />
<br />
attributesList.add(attributeValuesList.toArray(new String[attributeValuesList.size()]));<br />
<br />
<br />
<br />
} else if(st.sval.trim().compareToIgnoreCase("DATA") == 0){<br />
st.nextToken();<br />
while(st.ttype != StreamTokenizer.TT_EOF){<br />
List dataList = new ArrayList();<br />
int linenum = st.lineno();<br />
while(linenum == st.lineno()){<br />
if(st.ttype == StreamTokenizer.TT_WORD)<br />
dataList.add(st.sval);<br />
st.nextToken();<br />
gotNext = true;<br />
}<br />
dataInstancesList.add(dataList);<br />
}<br />
}<br />
}
if(!gotNext)
st.nextToken();<br />
}<br />
<br />
<br />
} catch (IOException e) {<br />
error = true;<br />
}<br />
<br />
if(!error){<br />
fileIsOk = true;<br />
<br />
int x, y;<br />
x=0;<br />
attributesArray = new Attribute[attributesList.size()];<br />
<br />
for(ListIterator i=attributesList.listIterator(); i.hasNext();){<br />
Attribute attribute = new Attribute((String[])i.next());<br />
attribute.setAttributeName((String)attributesNameList.get(x));<br />
attribute.setAttributeAsInt(x);<br />
attributesArray[x] = attribute;<br />
x++;<br />
}<br />
<br />
x=0;<br />
<br />
dataInstancesArray = new DataInstance[dataInstancesList.size()];<br />
for(ListIterator i=dataInstancesList.listIterator(); i.hasNext();){<br />
ArrayList tempList = (ArrayList)i.next();<br />
int dataArray[] = new int[attributesList.size()];<br />
y=0;<br />
for(ListIterator i2=tempList.listIterator(); i2.hasNext();){<br />
dataArray[y] = attributesArray[y].indexOf((String)i2.next());<br />
y++;<br />
}<br />
DataInstance dataInstance = new DataInstance(dataArray);<br />
dataInstancesArray[x++] = dataInstance;<br />
}<br />
<br />
<br />
<br />
}<br />
<br />
<br />
}<br />
<br />
public boolean fileIsOk(){<br />
return fileIsOk;<br />
}<br />
public int getAttributesNo(){<br />
return attributesArray.length;<br />
}<br />
public int getInstancesNo(){<br />
return dataInstancesArray.length;<br />
}<br />
public Attribute[] getAttributesArray(){<br />
return attributesArray;<br />
}<br />
public DataInstance[] getDataInstancesArray(){<br />
return dataInstancesArray;<br />
}<br />
public String getRelation(){<br />
return relation;<br />
}<br />
<br />
<br />
public int calculateMissing(int attributePos){<br />
int count=0;<br />
for(int x=0; x < dataInstancesArray.length; x++){<br />
if(dataInstancesArray[x].getValues()[attributePos] == -1)<br />
count++;<br />
}<br />
return count;<br />
}<br />
<br />
<br />
public int [] generateStats(int attributePos){<br />
int count;<br />
int [] typesArray = (attributesArray[attributePos]).getIntTypesArray();<br />
int [] returnArray = new int[typesArray.length];<br />
for(int type=0; type < typesArray.length; type++){<br />
count = 0;<br />
for(int x2=0; x2 < dataInstancesArray.length; x2++){<br />
if(dataInstancesArray[x2].getValues()[attributePos] == type)<br />
count++;<br />
}<br />
returnArray[type] = count;<br />
}<br />
return returnArray;<br />
}<br />
<br />
<br />
}<br />
|
|
|
|
|
class TreeNode
<br />
package guiID3;<br />
<br />
import java.util.ArrayList;<br />
<br />
public class TreeNode {<br />
<br />
private ArrayList mChilds = new ArrayList();<br />
private Attribute mAttribute;<br />
<br />
public TreeNode(Attribute attribute){<br />
if (attribute.getIntTypesArray() != null)<br />
{<br />
mChilds = new ArrayList(attribute.getIntTypesArray().length);<br />
for (int i = 0; i < attribute.getIntTypesArray().length; i++){<br />
mChilds.add(null);<br />
<br />
}<br />
}<br />
else<br />
{<br />
mChilds = new ArrayList(1);<br />
mChilds.add(null);<br />
}<br />
mAttribute = attribute;<br />
}<br />
<br />
public void AddTreeNode(TreeNode treeNode, int ValueName)<br />
{<br />
int index = mAttribute.indexOf(ValueName);<br />
mChilds.add(index,treeNode);<br />
}<br />
<br />
<br />
<br />
public TreeNode getChild(int index)<br />
{<br />
return (TreeNode)mChilds.get(index);<br />
}<br />
<br />
public Attribute getMAttribute()<br />
{<br />
return mAttribute;<br />
}<br />
<br />
public TreeNode getChildByBranchName(String branchName)<br />
{<br />
int index = mAttribute.indexOf(branchName);<br />
return (TreeNode)mChilds.get(index);<br />
}<br />
<br />
public int getmChilds() <br />
{<br />
return mChilds.size();<br />
}<br />
<br />
<br />
<br />
<br />
}<br />
<br />
best regards
|
|
|
|
|
This is an English language site, not Portuguese. Worthless.
|
|
|
|
|
alslam alikum;
good work; but when i want convert the code to java to make comparsion with other algorithms
i found that you just deal with two class problem you have make one method instead of two methods
{allSamplesPostive;;allSamplenegative}
cal this method checkSameClass the input to this method" Samples and targetClass";
and try to read from file it make your program good;
i hope if i missing something you declare it;
best regards;
|
|
|
|
|
I have changed your code into VB language, cause my task is in VB. I think there's no big difference. This is my training data.
'===================================================================================
'Exam Result Sample
'===================================================================================
col = result.Columns.Add("NFails")
col.DataType = GetType(String)
col = result.Columns.Add("NMarg")
col.DataType = GetType(String)
col = result.Columns.Add("Att")
col.DataType = GetType(String)
col = result.Columns.Add("Ext")
col.DataType = GetType(String)
col = result.Columns.Add("Ant")
col.DataType = GetType(String)
col = result.Columns.Add("Result")
col.DataType = GetType(String)
result.Rows.Add(New Object() {"0", "0", "good", "no", "P", "P"})
result.Rows.Add(New Object() {"0", "0", "poor", "yes", "F", "P"})
result.Rows.Add(New Object() {"0", "0", "good", "yes", "F", "P"})
result.Rows.Add(New Object() {"3", "0", "good", "no", "F", "F"})
result.Rows.Add(New Object() {"3", "1", "poor", "no", "F", "F"})
result.Rows.Add(New Object() {"3", "0", "good", "no", "P", "F"})
result.Rows.Add(New Object() {"3", "2", "good", "yes", "P", "R"})
result.Rows.Add(New Object() {"2", "1", "poor", "no", "F", "F"})
result.Rows.Add(New Object() {"2", "2", "good", "yes", "P", "R"})
result.Rows.Add(New Object() {"1", "0", "poor", "yes", "P", "R"})
result.Rows.Add(New Object() {"1", "1", "good", "yes", "F", "R"})
result.Rows.Add(New Object() {"1", "1", "good", "no", "F", "R"})
result.Rows.Add(New Object() {"1", "0", "poor", "no", "F", "F"})
result.Rows.Add(New Object() {"3", "2", "good", "no", "P", "F"})
result.Rows.Add(New Object() {"2", "2", "good", "no", "F", "R"})
result.Rows.Add(New Object() {"2", "1", "good", "yes", "P", "R"})
result.Rows.Add(New Object() {"2", "0", "poor", "no", "F", "F"})
'===================================================================================
in function InternalMountTree, there are some lines :
If aSample.Rows.Count = 0 Then
Return New TreeNode(New Attribute(getMostCommonValue(samples, targetAttr)))
Else
Dim dc3 As DecisionTreeID3 = New DecisionTreeID3()
Dim ChildNode As TreeNode = dc3.mountTree(aSample, targetAttr, _
DirectCast(aAttributes.ToArray(GetType(Attribute)), Attribute()))
root.addTreeNode(ChildNode, value)
End If
there's no need to return the treenode, just add a new treenode so it doesn't break the recursive way. The codes become like this :
If aSample.Rows.Count = 0 Then
Dim ChildNode As New TreeNode(New Attribute(getMostCommonValue(samples, targetAttr)))
root.addTreeNode(ChildNode, value)
'Return New TreeNode(New Attribute(getMostCommonValue(samples, targetAttr)))
Else
Dim dc3 As DecisionTreeID3 = New DecisionTreeID3()
Dim ChildNode As TreeNode = dc3.mountTree(aSample, targetAttr, _
DirectCast(aAttributes.ToArray(GetType(Attribute)), Attribute()))
root.addTreeNode(ChildNode, value)
End If
Please correct me if i'm wrong because i've also changed some procedures and functions to match with my task. And you were so great about making this algorithm become so simple... Thank you so much!
Best Regards,
dee
|
|
|
|
|
I'm interested in DataMining,i'm research and implement it with c#, and if u too,u can help me a association rule, thank a lot,
happy and lucky to u! and any body like it
Huynh Ly Thanh Nhan
|
|
|
|
|
if u not use function "getDistinctValues" , will have error in the second call function "mountTree".
---
and
ArrayList aAttributes = new ArrayList(attributes.Length - 1);
for(int i = 0; i < attributes.Length; i++)
{
if (attributes[i].AttributeName != bestAttribute.AttributeName)
aAttributes.Add(attributes[i]);
}
------
it have a problem if data example is a little,
----> can edit :
Attribute[] aAttributes = new Attribute [attributes.Length-1];
int j = 0;
for (int i = 0; i <attributes.length; i++)<br="" mode="hold"> {
if (attributes[i].AttributeName != bestAttribute.AttributeName)
{
Attribute t=new Attribute ( attributes[i].AttributeName,(string[])getDistinctValues(childSample, attributes[i].AttributeName).ToArray(typeof(string)));
aAttributes[j] = t;
j++;
}
}
--> ok?
Huynh Ly Thanh Nhan
modified on Wednesday, December 12, 2007 3:55:54 AM
|
|
|
|
|
alslam alikum;
find duplicate item --- it is true but why i have use function "getDistinctValues"
all i need save attributes that have been used
so i used another arrayList called it usedAttributes; which hold the attributes used during the loops
so frist
ArrayList usedAttributes=new ArrayList();
then
usedAttributes.add(bestAttribute.getAttributeName());
finally
ArrayList aAttributes = new ArrayList(attributes.length - 1);
for(int i = 0; i < attributes.length-1; i++)
{
if (usedAttributes.contains(attributes[i].getAttributeName())!=true)
aAttributes.add(attributes[i]);
}
}
|
|
|
|
|
Hi coders
Had to spend more than 2 hours for the translation from Portugese to English via translate.google.com
Hope the code now makes some sense for us English Speaking people
Get the translated Code from http://kirankonathala.com/AI/ID3DecisionTree.zip
Watch this space cos' Im gonna make a Java version of this Algorithm - our evaluator demands JAVA
I need to work out an extension of this progam wherein once we build a decision tree,it should parse Test Data to predict the results.
Inducing a Decision Tree - Supplying training data and expecting an output for Test Data (makes sense,right?)
Q)How do we parse the test data?
A)Once we form an optimal Decision tree from the Training Data,we need to parse the Test Data by froming predicates (p and q and .....).
Will be posting the Test Data part very soon!!
Kiran K
|
|
|
|
|
|
hi,can u sent me a translated code to me? i can`s down load that form the link http://kirankonathala.com/AI/ID3DecisionTree.zip.
my email is hiwinson@gmail.com, thanks very much.
|
|
|
|
|
Please share your extended program Kiran.
|
|
|
|
|
please i want english version
my mail m_wesemy@YAHOO.COM
|
|
|
|
|
please send me the translated version with comments to jibran.javed@gmail.com
i tried to access the link but not succeed
best wishes
thankful
javed
|
|
|
|
|
Cannot access the page either. Someone upload the code somewhere else?
|
|
|
|
|
This is a trick to get you to go to his spam site!
DO NOT FOLLOW LINK!
|
|
|
|
|
Hi all!!
The comments are in Portugese,will post the translated comments if time permits
amostragem - Sampling!!
http://translate.google.com
Kiran K
I need to modify the code to suit my problem - will post here if successful!
http://kirankonathala.com
|
|
|
|
|
SPAM! DO NOT FOLLOW LINK!
HAS NOTHING TO DO WITH C# OR ID3 ALOGRITHM!
|
|
|
|
|
Hey,add Console.Redline() in main() cos' it disappears before I even look at it
|
|
|
|
|
using System;
using System.Collections;
using System.Data;
//AUTHOR: Roosevelt dos Santos Júnior
namespace ExemploID3
{
///
/// Classe que representa um atributo utilizado na
......
......
classe de decisão
///
class ID3Sample
{
public static void printNode(TreeNode root, string tabs)
{
Console.WriteLine(tabs + '|' + root.attribute + '|');
if (root.attribute.values != null)
{
for (int i = 0; i < root.attribute.values.Length; i++)
{
Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
printNode(childNode, "\t" + tabs);
}
}
}
static DataTable getDataTable()
{
DataTable result = new DataTable("samples");
DataColumn column = result.Columns.Add("TF");
column.DataType = typeof(string);
column = result.Columns.Add("IDF");
column.DataType = typeof(string);
column = result.Columns.Add("POS");
column.DataType = typeof(string);
column = result.Columns.Add("result");
column.DataType = typeof(bool);
result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
result.Rows.Add(new object[] {"TF9", "IDF0", "A", false});
// result.Rows.Add(new object[] {"TF9", "IDF0", "A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "N N", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "N N N", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "N A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "N N", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "N N", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "N N A", false});
result.Rows.Add(new object[] {"TF9", "IDF0", "N N", true});
// result.Rows.Add(new object[] {"TF0", "IDF0", "N N N", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
result.Rows.Add(new object[] {"TF0", "IDF0", "A A", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "N N N", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "N N", false});
// result.Rows.Add(new object[] {"TF0", "IDF0", "A", false});
return result;
Console.ReadLine();
}
///
/// The main entry point for the application.
///
///
[STAThread]
static void Main(string[] args)
{
Attribute TF = new Attribute("TF", new string[] {"TF0", "TF9"});
Attribute IDF = new Attribute("IDF", new string[] {"IDF0"});
Attribute POS = new Attribute("POS", new string[] {"A", "N N", "N A", "N N N", "A A", "N N A"});
Attribute[] attributes = new Attribute[] {TF, IDF, POS};
DataTable samples = getDataTable();
DecisionTreeID3 id3 = new DecisionTreeID3();
TreeNode root = id3.mountTree(samples, "result", attributes);
printNode(root, "");
}
}
}
|
|
|
|
|
when two attributes are same gain then we chose one and there are have some case positives = negatives = 0 --> gain = -1.#IND000000000 -> our decision tree error
|
|
|
|
|
English please
I can't read comments !
Tx by P.P.
|
|
|
|
|
Hi,
Can't you read comments in source code?
|
|
|
|
|
No, they are in Portuguese. As much as some may not like it English is the international language for programmers so writing comments in anything but English ON AN ENGLISH LANGUAGE SITE is pretty silly.
In fact most technical words are in English. Just ask anyone in Brazil to tell you the name of the software/hardware that protects your network from the bad people on the Inter-web and they will tell you, "Um firewall." But if you ask them if they have a good "uma parede de fogo" or "um incêndio parede" they will look at you strangely.
|
|
|
|
|
I can't read comment you have Eng ver?
if you have it please give me one.
|
|
|
|
|