|
Thank you for your reply.
I believe this algorithm is for C4.5 not really ID#. Because in this implementation we use information gain not just entropy. Am I right?
Do you have any source code for ID3 (just using Information entropy)
thank you for your help. my assignment due on Monday
|
|
|
|
|
I personally believe this version is ID3 without Gini Index or any missing attribution handler ( apart from getmostcommonvalue function )
This below code is partly extract from my web application with a little modification from Rossvelt 's one..
using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Collections;
namespace TravelSys.DAL.DSS
{
/// Class that represents an attribute used in the class of decision
public class Attribute
{
//string value;
ArrayList mValues;
string mName;
public object mLabel;
/// Initializes a new instance of a class Attribute
public Attribute(string name, string[] values)
{
mName = name;
mValues = new ArrayList(values);
mValues.Sort();
}
public Attribute(object Label)
{
mLabel = Label;
mName = string.Empty;
mValues = null;
}
/// Indicates the attribute name
public string AttributeName
{
get
{
return mName;
}
}
/// Returns an array with the attribute values
public string[] values
{
get
{
if (mValues != null)
return (string[])mValues.ToArray(typeof(string));
else
return null;
}
}
/// Indicates whether a value is allowed for this attribute
public bool isValidValue(string value)
{
return indexValue(value) >= 0;
}
/// Returns the index value
public int indexValue(string value)
{
if (mValues != null)
return mValues.BinarySearch(value);
else
return -1;
}
/// <returns>
public override string ToString()
{
if (mName != string.Empty)
{
return mName;
}
else
{
return mLabel.ToString();
}
}
}
/// Class that represent the decision tree built;
///
public class TreeNode
{
private ArrayList mChilds = null;
private Attribute mAttribute;
private int nData = 0;
private double _percentage = 100.0;
private TreeNode _parent = null;
int j = 1;
/// Initialize a new instance of TreeNode
public TreeNode(Attribute attribute)
{
try
{
if (attribute.values != null)
{
j++;
mChilds = new ArrayList(attribute.values.Length);
for (int i = 0; i < attribute.values.Length; i++)
mChilds.Add(null);
}
else
{
mChilds = new ArrayList(1);
mChilds.Add(null);
}
mAttribute = attribute;
}
catch (Exception)
{
}
}
/// Adds a TreeNode child in this branch name by indicating ValueName
public void AddTreeNode(TreeNode treeNode, string ValueName)
{
int index = mAttribute.indexValue(ValueName);
treeNode._parent = this;
mChilds[index] = treeNode;
}
/// Returns the total number of children node
public int totalChilds
{
get
{
return mChilds.Count;
}
}
/// Returns the child node of a node
public TreeNode getChild(int index)
{
return (TreeNode)mChilds[index];
}
public TreeNode Parent
{
get { return this._parent; }
}
/// Attribute that is connected to Node
public Attribute attribute
{
get
{
return mAttribute;
}
}
public int totalData
{
get { return nData; }
set { nData = value; }
}
public double percentage
{
get { return _percentage; }
set { _percentage = value; }
}
/// Returns the child of a node by the name of the branch leading to it
public TreeNode getChildByBranchName(string branchName)
{
int index = mAttribute.indexValue(branchName);
return (TreeNode)mChilds[index];
}
public static int MaxChildScoreIndex(TreeNode root)
{
double val = ((TreeNode)root.mChilds[0]).percentage;
int ind = 0;
for (int i = 0; i < root.mChilds.Count; i++)
{
if (val < ((TreeNode)root.mChilds[i]).percentage)
{
ind = i;
val = ((TreeNode)root.mChilds[i]).percentage;
}
}
return ind;
}
}
/// <summary>
/// Class that implements a decision tree using ID3 algorithm
/// </summary>
public class DecisionTreeID3
{
private DataTable mSamples;
private int mTotalPositives = 0;
private int mTotal = 0;
private string mTargetAttribute = "Outcome";
private double mEntropySet = 0.0;
/// Returns the total number of positive samples in a table of samples
private int countTotalPositives(DataTable samples)
{
int result = 0;
foreach (DataRow aRow in samples.Rows)
{
if ((string)aRow[mTargetAttribute.Trim()] == "Go")
result++;
}
return result;
}
// Calculate the entropy given the following formula
/// -p+log2p+ - p-log2p-
private double calcEntropy(int positives, int negatives)
{
int total = positives + negatives;
if (total == 0)
{
return 0;
}
double ratioPositive = (double)positives / total;
double ratioNegative = (double)negatives / total;
if (ratioPositive != 0)
ratioPositive = -(ratioPositive) * System.Math.Log(ratioPositive, 2);
if (ratioNegative != 0)
ratioNegative = -(ratioNegative) * System.Math.Log(ratioNegative, 2);
double result = ratioPositive + ratioNegative;
return result;
}
/// Returns the best attribute
private void getValuesToAttribute(DataTable samples, Attribute attribute, string value, out int positives, out int negatives)
{
positives = 0;
negatives = 0;
foreach (DataRow aRow in samples.Rows)
{
if (((string)aRow[attribute.AttributeName] == value))
if ((string)aRow[mTargetAttribute.Trim()] == "Go")
positives++;
else
negatives++;
}
}
/// Calculates the gain of an attribute
private double gain(DataTable samples, Attribute attribute)
{
string[] values = attribute.values;
double sum = 0.0;
for (int i = 0; i < values.Length; i++)
{
int positives, negatives;
positives = negatives = 0;
getValuesToAttribute(samples, attribute, values[i], out positives, out negatives);
double entropy = calcEntropy(positives, negatives);
sum += -(double)((positives + negatives) * entropy) / mTotal;
}
return mEntropySet + sum;
}
/// Returns the best attribute
private Attribute getBestAttribute(DataTable samples, Attribute[] attributes)
{
double maxGain = 0.0;
Attribute result = null;
try
{
foreach (Attribute attribute in attributes)
{
double aux = gain(samples, attribute);
if (aux > maxGain)
{
maxGain = aux;
result = attribute;
}
}
}
catch
{ }
return result;
}
/// Returns true if all examples are positive sampling
private bool allSamplesPositives(DataTable samples, string targetAttribute)
{
foreach (DataRow row in samples.Rows)
{
if ((string)row[targetAttribute.Trim()] == "NotGo")
return false;
}
return true;
}
/// Returns true if all are negative examples of sampling
private bool allSamplesNegatives(DataTable samples, string targetAttribute)
{
foreach (DataRow row in samples.Rows)
{
if ((string)row[targetAttribute.Trim()] == "Go")
return false;
}
return true;
}
/// Returns a list of all the distinct values from a table of random
private ArrayList getDistinctValues(DataTable samples, string targetAttribute)
{
ArrayList distinctValues = new ArrayList(samples.Rows.Count);
foreach (DataRow row in samples.Rows)
{
if (distinctValues.IndexOf(row[targetAttribute]) == -1)
distinctValues.Add(row[targetAttribute]);
}
return distinctValues;
}
/// Return the most common value within a sampling
private object getMostCommonValue(DataTable samples, string targetAttribute)
{
ArrayList distinctValues = getDistinctValues(samples, targetAttribute);
int[] count = new int[distinctValues.Count];
if (count.Length == 0) return distinctValues[0];
foreach (DataRow row in samples.Rows)
{
int index = distinctValues.IndexOf(row[targetAttribute]);
count[index]++;
}
int MaxIndex = 0;
int MaxCount = 0;
for (int i = 0; i < count.Length; i++)
{
if (count[i] > MaxCount)
{
MaxCount = count[i];
MaxIndex = i;
}
}
return distinctValues[MaxIndex];
}
/// Assemble a decision tree based on samples submitted
private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
{
if (allSamplesPositives(samples, targetAttribute) == true)
{
TreeNode tmpNode = new TreeNode(new Attribute(true));
tmpNode.totalData = samples.Rows.Count;
return tmpNode;
}
if (allSamplesNegatives(samples, targetAttribute) == true)
{
TreeNode tmpNode = new TreeNode(new Attribute(false));
tmpNode.totalData = samples.Rows.Count;
return tmpNode;
//return new TreeNode(new Attribute(false));
}
if (attributes.Length == 0)
return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute)));
mTotal = samples.Rows.Count;
mTargetAttribute = targetAttribute;
mTotalPositives = countTotalPositives(samples);
mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives);
Attribute bestAttribute = getBestAttribute(samples, attributes);
TreeNode root = new TreeNode(bestAttribute);
root.totalData = mTotal;
DataTable aSample = samples.Clone();
try
{
foreach (string value in bestAttribute.values)
{
// Selects all elements with the value of this attribute
aSample.Rows.Clear();
DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");
foreach (DataRow row in rows)
{
aSample.Rows.Add(row.ItemArray);
}
// Selects all elements with the value of this attribute
// Create a new list of attributes unless the attribute that is the current best attribute
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]);
}
// Create a new list of attributes unless the attribute that is the current best attribute
if (aSample.Rows.Count == 0)
{
try
{
TreeNode retNode = new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute)));
root.AddTreeNode(retNode, value);
}
catch
{
root.AddTreeNode(new TreeNode(new Attribute("")), value);
}
// return retNode;
}
else
{
DecisionTreeID3 dc3 = new DecisionTreeID3();
TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute)));
root.AddTreeNode(ChildNode, value);
if (root.totalData > 0)
{
ChildNode.percentage = (Convert.ToDouble(ChildNode.totalData) / Convert.ToDouble(root.totalData)) * 100;
}
}
}
}
catch (Exception)
{
}
return root;
}
/// Assemble a decision tree based on samples submitted
public TreeNode mountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
{
mSamples = samples;
return internalMountTree(mSamples, targetAttribute, attributes);
}
}
/// Class which exemplifies the use of ID3
public class ID3Sample
{
public static string _temptext = "";
public static double Treescore = 1;
public string temptext
{
get
{
return _temptext;
}
set
{
_temptext = value;
}
}
/// Print Node and Branch tree
public static void printNode(TreeNode root, int tabs, bool showValue)
{
//Check if it is root or leaf line, if root then try branch and sub-branch next
//Write Root line
if (tabs == 0)
{
_temptext = "";
if (showValue)
{
_temptext += "##" + root.attribute + "## [" + root.totalData + "] (" + root.percentage.ToString("#0.00") + "%)<br>";
}
else
{
_temptext += "##" + root.attribute + "##<br>";
}
}
// Write Leaf line
else
{
if (showValue)
{
_temptext += " --> " + root.attribute + " [" + root.totalData + "] (" + root.percentage.ToString("#0.00") + "%)<br>";
}
else
{
_temptext += " --> " + root.attribute + "<br>";
}
}
try
// Write Branch line
{
if (root.attribute.values != null)
{
tabs += 1;
for (int i = 0; i < root.attribute.values.Length; i++)
{
if (tabs == 1)
{
_temptext += "<br>" + "..++" + root.attribute.values[i];
}
else if (tabs == 2)
{
_temptext += "<br>" + "..........++" + root.attribute.values[i];
}
else if (tabs == 3)
{
_temptext += "<br>" + ".........................++" + root.attribute.values[i];
}
else if (tabs == 4)
{
_temptext += "<br>" + "............................................++" + root.attribute.values[i];
}
else
{
_temptext += "<br>" + "...............................................................++" + root.attribute.values[i];
}
TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
printNode(childNode, tabs, showValue);
}
}
else
{ _temptext += "<br>"; }
}
catch (Exception)
{
_temptext += "<br>";
}
}
|
|
|
|
|
Dear Roosevelts, thank you for your great contribution. I has implemented your decision tree code to my project and found that there was a problem if one of root attributes contains no data ( total == 0 )and tree result is a little bit different from WEKA's result as shown below .
private double calcEntropy(int positives, int negatives)
{
int total = positives + negatives;
double ratioPositive = (double)positives / total;
double ratioNegative = (double)negatives / total;
if (ratioPositive != 0)
ratioPositive = -(ratioPositive) * System.Math.Log(ratioPositive, 2);
if (ratioNegative != 0)
ratioNegative = -(ratioNegative) * System.Math.Log(ratioNegative, 2);
double result = ratioPositive + ratioNegative;
return result;
}
/// Returns the best attribute
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Below is my modification
// Calculate the entropy given the following formula
/// -p+log2p+ - p-log2p-
private double calcEntropy(int positives, int negatives)
{
int total = positives + negatives;
if (total == 0)
{
return 0;
}
double ratioPositive = (double)positives / total;
double ratioNegative = (double)negatives / total;
if (ratioPositive != 0)
ratioPositive = -(ratioPositive) * System.Math.Log(ratioPositive, 2);
if (ratioNegative != 0)
ratioNegative = -(ratioNegative) * System.Math.Log(ratioNegative, 2);
double result = ratioPositive + ratioNegative;
return result;
}
/// Returns the best attribute
/// Assemble a decision tree based on samples submitted
private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
{
if (allSamplesPositives(samples, targetAttribute) == true)
{
TreeNode tmpNode = new TreeNode(new Attribute(true));
tmpNode.totalData = samples.Rows.Count;
return tmpNode;
}
if (allSamplesNegatives(samples, targetAttribute) == true)
{
TreeNode tmpNode = new TreeNode(new Attribute(false));
tmpNode.totalData = samples.Rows.Count;
return tmpNode;
//return new TreeNode(new Attribute(false));
}
if (attributes.Length == 0)
return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute)));
mTotal = samples.Rows.Count;
mTargetAttribute = targetAttribute;
mTotalPositives = countTotalPositives(samples);
mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives);
Attribute bestAttribute = getBestAttribute(samples, attributes);
TreeNode root = new TreeNode(bestAttribute);
root.totalData = mTotal;
DataTable aSample = samples.Clone();
try
{
foreach (string value in bestAttribute.values)
{
// Selects all elements with the value of this attribute
aSample.Rows.Clear();
DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");
foreach (DataRow row in rows)
{
aSample.Rows.Add(row.ItemArray);
}
// Selects all elements with the value of this attribute
// Create a new list of attributes unless the attribute that is the current best attribute
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]);
}
// Create a new list of attributes unless the attribute that is the current best attribute
if (aSample.Rows.Count == 0)
{
try
{
TreeNode retNode = new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute)));
root.AddTreeNode(retNode, value);
}
catch
{
root.AddTreeNode(new TreeNode(new Attribute("")), value);
}
// return retNode;
}
else
{
DecisionTreeID3 dc3 = new DecisionTreeID3();
TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute)));
root.AddTreeNode(ChildNode, value);
if (root.totalData > 0)
{
ChildNode.percentage = (Convert.ToDouble(ChildNode.totalData) / Convert.ToDouble(root.totalData)) * 100;
}
}
}
}
catch (Exception)
{
}
return root;
}
/// Assemble a decision tree based on samples submitted
modified on Monday, September 27, 2010 5:05 AM
|
|
|
|
|
thanhks for code.
But when I use adventure work DW to predict buyer, compare with ssas get different result. Can u explain it?
le quyen
|
|
|
|
|
Hello, there is error in
if (aSample.Rows.Count == 0)
{
return (new TreeNode(new Attribute(getMostCommonValue(ASamples, targetAttribute))),value);
}
it should be
if (aSample.Rows.Count == 0)
{
root.AddTreeNode(new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute))),value);
}
because if the count is zero the program will resulted an error
--
Kevin Tanadi
--
|
|
|
|
|
can u give me a codes about b-trees in c++
|
|
|
|
|
hi! i need id3 decision tree code in c++ .input is from a text file.
i am confused in computing information content
plz help me.
|
|
|
|
|
Hi
Wwere you able to get a sample code in C++. would it also be possible that you can post it or email it
Thanks
|
|
|
|
|
Alslam Alikum ;
I am sorry for late in answer your email to me
this is the java code
package guiID3;<br />
<br />
<br />
import java.awt.Cursor;<br />
import java.awt.FlowLayout;<br />
import java.awt.GridBagConstraints;<br />
import java.awt.GridBagLayout;<br />
import java.awt.GridLayout;<br />
import java.awt.Insets;<br />
import java.awt.Point;<br />
import javax.swing.JEditorPane;<br />
import javax.swing.border.SoftBevelBorder;<br />
import javax.swing.border.BevelBorder;<br />
import javax.swing.JTextPane;<br />
import javax.swing.BoxLayout;<br />
import java.awt.event.ActionEvent;<br />
import java.awt.event.ActionListener;<br />
import java.awt.event.MouseAdapter;<br />
import java.awt.event.MouseEvent;<br />
import java.io.IOException;<br />
<br />
import javax.swing.BorderFactory;<br />
import javax.swing.ImageIcon;<br />
import javax.swing.JButton;<br />
import javax.swing.JDialog;<br />
import javax.swing.JFrame;<br />
import javax.swing.JLabel;<br />
import javax.swing.JPanel;<br />
import javax.swing.WindowConstants;<br />
<br />
<br />
public class AboutGUIId3 extends javax.swing.JDialog {<br />
<br />
<br />
private static final long serialVersionUID = -1893243061264575311L;<br />
<br />
{<br />
try {<br />
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");<br />
} catch(Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
private JLabel jLabel1;<br />
private JLabel jLabel2;<br />
private JPanel jPanel1;<br />
private JButton jButton2;<br />
private JLabel jLabel4;<br />
private JTextPane jTextPane3;<br />
private JTextPane jTextPane2;<br />
private JTextPane jTextPane1;<br />
private JLabel jLabel3;<br />
private JPanel jPanel2;<br />
private JLabel jLabel5;<br />
private JButton jButton1;<br />
private JFrame parent;<br />
<br />
<br />
<br />
<br />
public AboutGUIId3(JDialog frame, JFrame parent) {<br />
super(frame, "About GUI ID3", true);<br />
this.parent = parent;<br />
initGUI();<br />
}<br />
<br />
private void initGUI() {<br />
try {<br />
GridBagLayout thisLayout = new GridBagLayout();<br />
thisLayout.columnWeights = new double[] {0.1};<br />
thisLayout.columnWidths = new int[] {7};<br />
thisLayout.rowWeights = new double[] {0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};<br />
thisLayout.rowHeights = new int[] {25,10,20,8,180,11,52,2,7};<br />
this.getContentPane().setLayout(thisLayout);<br />
this.setModal(true);<br />
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);<br />
<br />
Point p = parent.getLocationOnScreen();<br />
this.setLocation(new Double(p.getX() + 30).intValue(), new Double(p.getY() + 50).intValue());<br />
<br />
this.getContentPane().setForeground(new java.awt.Color(255,255,255));<br />
this.getContentPane().setBackground(new java.awt.Color(255,255,255));<br />
{<br />
jLabel2 = new JLabel();<br />
this.getContentPane().add(<br />
jLabel2,<br />
new GridBagConstraints(<br />
0,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 11, 0, 0),<br />
0,<br />
0));<br />
jLabel2.setText("Version 1");<br />
}<br />
{<br />
jLabel1 = new JLabel();<br />
this.getContentPane().add(<br />
jLabel1,<br />
new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.SOUTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 11, 0, 0),<br />
0,<br />
0));<br />
GridLayout jLabel1Layout = new GridLayout(3, 1);<br />
jLabel1Layout.setRows(3);<br />
jLabel1Layout.setVgap(33);<br />
jLabel1.setLayout(jLabel1Layout);<br />
jLabel1.setText("GUI ID3");<br />
jLabel1.setFont(new java.awt.Font("Dialog",1,14));<br />
}<br />
{<br />
jPanel1 = new JPanel();<br />
FlowLayout jPanel1Layout = new FlowLayout();<br />
jPanel1Layout.setAlignment(FlowLayout.RIGHT);<br />
jPanel1Layout.setHgap(15);<br />
jPanel1Layout.setVgap(9);<br />
jPanel1.setLayout(jPanel1Layout);<br />
this.getContentPane().add(<br />
jPanel1,<br />
new GridBagConstraints(<br />
0,<br />
8,<br />
1,<br />
2,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(7, 0, 0, 0),<br />
0,<br />
0));<br />
{<br />
jButton1 = new JButton();<br />
jPanel1.add(jButton1);<br />
jButton1.setText("Ok");<br />
jButton1.setPreferredSize(new java.awt.Dimension(65, 23));<br />
jButton1.setLocation(new java.awt.Point(0, 7));<br />
jButton1.setBounds(246, 6, 56, 24);<br />
jButton1.setMaximumSize(new java.awt.Dimension(65, 23));<br />
jButton1.setMinimumSize(new java.awt.Dimension(65, 23));<br />
jButton1.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
setVisible(false);<br />
}<br />
});<br />
}<br />
}<br />
{<br />
jLabel5 = new JLabel();<br />
this.getContentPane().add(<br />
jLabel5,<br />
new GridBagConstraints(<br />
0,<br />
6,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTH,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel5.setIcon(new ImageIcon(getClass().getClassLoader().getResource("qoraan1.JPG")));<br />
jLabel5.setToolTipText("State University of Santa Catarina, campus Joinville.");<br />
}<br />
{<br />
jButton2 = new JButton();<br />
this.getContentPane().add(<br />
jButton2,<br />
new GridBagConstraints(<br />
0,<br />
6,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.PAGE_END,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jButton2.setText("<html><a href=\"http://www.joinville.udesc.br\">http://www.joinville.udesc.br</a></html>");<br />
jButton2.setBackground(new java.awt.Color(255,255,255));<br />
jButton2.setContentAreaFilled(false);<br />
jButton2.setBorderPainted(false);<br />
jButton2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));<br />
jButton2.setFont(new java.awt.Font("Arial",0,11));<br />
jButton2.setMaximumSize(new java.awt.Dimension(50, 9));<br />
jButton2.setMinimumSize(new java.awt.Dimension(180, 14));<br />
jButton2.setToolTipText("Website of the State University of Santa Catarina, campus Joinville. Available in Portuguese only.");<br />
jButton2.addMouseListener(new MouseAdapter() {<br />
public void mouseEntered(MouseEvent evt) {<br />
setCursor(Cursor<br />
.getPredefinedCursor(Cursor.HAND_CURSOR));<br />
}<br />
public void mouseExited(MouseEvent evt) {<br />
setCursor(Cursor<br />
.getPredefinedCursor(Cursor.DEFAULT_CURSOR));<br />
}<br />
});<br />
jButton2.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
String[] cmd = new String[4];<br />
cmd[0] = "cmd.exe";<br />
cmd[1] = "/C";<br />
cmd[2] = "start";<br />
cmd[3] = "http://www.joinville.udesc.br";<br />
try {<br />
Runtime.getRuntime().exec(cmd);<br />
} catch (IOException e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
});<br />
}<br />
{<br />
jPanel2 = new JPanel();<br />
GridBagLayout jPanel2Layout = new GridBagLayout();<br />
jPanel2Layout.columnWeights = new double[] {0.1};<br />
jPanel2Layout.columnWidths = new int[] {7};<br />
jPanel2Layout.rowWeights = new double[] {0.1,0.1,0.1,0.1};<br />
jPanel2Layout.rowHeights = new int[] {7,7,7,7};<br />
this.getContentPane().add(<br />
jPanel2,<br />
new GridBagConstraints(<br />
0,<br />
4,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(0, 11, 0, 11),<br />
0,<br />
0));<br />
<br />
jPanel2.setMinimumSize(new java.awt.Dimension(530, 10));<br />
jPanel2.setPreferredSize(new java.awt.Dimension(530, 10));<br />
jPanel2.setSize(540, 54);<br />
jPanel2.setBackground(new java.awt.Color(240,240,240));<br />
jPanel2.setLayout(jPanel2Layout);<br />
{<br />
jLabel4 = new JLabel();<br />
jPanel2.add(jLabel4, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));<br />
jLabel4.setText("Copyright Notice");<br />
jLabel4.setFont(new java.awt.Font("Tahoma",1,11));<br />
}<br />
{<br />
jTextPane1 = new JTextPane();<br />
jPanel2.add(jTextPane1, new GridBagConstraints(<br />
0,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(0, 11, 0, 11),<br />
0,<br />
0));<br />
jTextPane1.setText("This software is freely available for research and teaching. It is not meant for commercial purposes. If you publish any material involving the use of this software, please quote the original paper that describes the algorithm in detail, as suggested below:");<br />
jTextPane1.setOpaque(false);<br />
jTextPane1.setEditable(false);<br />
jTextPane1.setFocusable(false);<br />
jTextPane1.setLocation(new java.awt.Point(555, 0));<br />
}<br />
{<br />
jTextPane2 = new JTextPane();<br />
jPanel2.add(jTextPane2, new GridBagConstraints(<br />
0,<br />
2,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(0, 11, 0, 11),<br />
0,<br />
0));<br />
jTextPane2.setText("Parpinelli, R.S., Lopes, H.S., Freitas, A.A. \"Data mining with an ant colony optimization algorithm\". IEEE Transactions on Evolutionary Computation, special issue on Ant Colony Algorithms, v. 6, n. 4, p. 321-332, August, 2002.");<br />
jTextPane2.setOpaque(false);<br />
jTextPane2.setEditable(false);<br />
jTextPane2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));<br />
}<br />
{<br />
jTextPane3 = new JTextPane();<br />
jPanel2.add(jTextPane3, new GridBagConstraints(<br />
0,<br />
3,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(0, 11, 0, 11),<br />
0,<br />
0));<br />
jTextPane3.setText("We do not take any responsibility for the results that you obtain with this software, we do not offer any guarantees, nor do we help with the maintenance of this program.");<br />
jTextPane3.setOpaque(false);<br />
jTextPane3.setEditable(false);<br />
jTextPane3.setFocusable(false);<br />
}<br />
}<br />
{<br />
jLabel3 = new JLabel();<br />
this.getContentPane().add(<br />
jLabel3,<br />
new GridBagConstraints(<br />
0,<br />
2,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 11, 0, 0),<br />
0,<br />
0));<br />
jLabel3.setText("Developed by Fernando Meyer under the orientation of M.Sc. Rafael Stubs Parpinelli.");<br />
}<br />
this.setSize(580, 386);<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
<br />
private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) {<br />
parent.addMouseListener(new java.awt.event.MouseAdapter() {<br />
public void mousePressed(java.awt.event.MouseEvent e) {<br />
if(e.isPopupTrigger())<br />
menu.show(parent, e.getX(), e.getY());<br />
}<br />
public void mouseReleased(java.awt.event.MouseEvent e) {<br />
if(e.isPopupTrigger())<br />
menu.show(parent, e.getX(), e.getY());<br />
}<br />
});<br />
}<br />
<br />
}<br />
<br />
----------------class Attribute-----------
<br />
package guiID3;<br />
<br />
<br />
public class Attribute {<br />
private String attributeName;<br />
private String [] typesArray;<br />
private int [] intTypesArray;<br />
int x;<br />
public Attribute(String [] typesArray){<br />
this.typesArray = typesArray;<br />
initializeIntTypesArray();<br />
}<br />
public Attribute(String name)<br />
{<br />
attributeName = name;<br />
}<br />
<br />
<br />
public void setAttributeAsInt(int x){<br />
this.x=x;<br />
} <br />
public int getAttributeAsInt(){<br />
return x;<br />
}<br />
<br />
private void initializeIntTypesArray(){<br />
intTypesArray = new int[typesArray.length];<br />
for(int x=0; x < typesArray.length; x++){<br />
intTypesArray[x] = x;<br />
}<br />
}<br />
<br />
public void setAttributeName(String name){<br />
attributeName = name;<br />
}<br />
public String getAttributeName(){<br />
return attributeName;<br />
}<br />
public String[] getTypes(){<br />
return typesArray;<br />
}<br />
public int[] getIntTypesArray(){<br />
return intTypesArray;<br />
}<br />
public int indexOf(int type){<br />
for(int x=0; x < intTypesArray.length; x++)<br />
if(intTypesArray[x] == type)<br />
return x;<br />
return -1;<br />
}<br />
<br />
public int indexOf(String value){<br />
for(int x=0; x < typesArray.length; x++){<br />
if(value.compareTo(typesArray[x]) == 0){<br />
return x;<br />
}<br />
}<br />
return -1;<br />
}<br />
}<br />
<br />
<br />
<br />
----------------
|
|
|
|
|
class DataInstance
package guiID3;<br />
<br />
public class DataInstance implements Cloneable { <br />
private int [] valuesArray;<br />
<br />
<br />
public DataInstance(int [] valueArray){<br />
this.valuesArray = valueArray;<br />
}<br />
<br />
public int[] getValues(){<br />
return valuesArray;<br />
}<br />
public void setValues(int [] p){<br />
valuesArray = p;<br />
}<br />
<br />
<br />
public int getClassValue(){<br />
return valuesArray[valuesArray.length-1];<br />
}<br />
<br />
public Object clone() throws CloneNotSupportedException{<br />
DataInstance myClone = (DataInstance) super.clone();
return myClone;<br />
}<br />
}<br />
---------------------- class GUIID3JFrame
package guiID3;<br />
<br />
<br />
import java.awt.BorderLayout;<br />
import java.awt.Canvas;<br />
import java.awt.Cursor;<br />
import java.awt.Event;<br />
import java.awt.GridBagConstraints;<br />
import java.awt.GridBagLayout;<br />
import java.awt.Insets;<br />
import java.awt.event.ActionEvent;<br />
import java.awt.event.ActionListener;<br />
import java.awt.event.KeyEvent;<br />
import java.awt.event.MouseAdapter;<br />
import java.awt.event.MouseEvent;<br />
import java.io.File;<br />
import java.text.DecimalFormat;<br />
<br />
import javax.swing.BorderFactory;<br />
import javax.swing.BoxLayout;<br />
import javax.swing.JButton;<br />
import javax.swing.JCheckBox;<br />
import javax.swing.JDialog;<br />
import javax.swing.JFileChooser;<br />
import javax.swing.JLabel;<br />
import javax.swing.JMenu;<br />
import javax.swing.JMenuBar;<br />
import javax.swing.JMenuItem;<br />
import javax.swing.JPanel;<br />
import javax.swing.JProgressBar;<br />
import javax.swing.JScrollPane;<br />
import javax.swing.JSeparator;<br />
import javax.swing.JTabbedPane;<br />
import javax.swing.JTable;<br />
import javax.swing.JTextArea;<br />
import javax.swing.JTextField;<br />
import javax.swing.KeyStroke;<br />
import javax.swing.ListSelectionModel;<br />
import javax.swing.SwingConstants;<br />
import javax.swing.WindowConstants;<br />
import javax.swing.border.BevelBorder;<br />
import javax.swing.event.ListSelectionEvent;<br />
import javax.swing.event.ListSelectionListener;<br />
import javax.swing.table.DefaultTableModel;<br />
import javax.swing.table.TableModel;<br />
<br />
public class GUIID3JFrame extends javax.swing.JFrame {<br />
<br />
private static final long serialVersionUID = 1L;<br />
<br />
{<br />
try {<br />
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");<br />
} catch(Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
<br />
private JMenuBar jMenuBar1;<br />
private JMenu jMenu1;<br />
private JMenuItem jMenuItem1;<br />
private JLabel jLabelNumberOfInstances;<br />
private JTable jTable1;<br />
private JTable jTable2;<br />
private TableModel jTable1Model;<br />
private TableModel jTable2Model;<br />
private JLabel jLabel8;<br />
private JLabel jLabel4;<br />
private JLabel jLabel14;<br />
private JLabel jLabel12;<br />
private JLabel jLabel6;<br />
private JTextField jTextField5;<br />
private JTextField jTextField4;<br />
private JTextField jTextField3;<br />
private JTextField jTextField2;<br />
private JPanel jPanel9;<br />
private JTextField jTextField1;<br />
private JLabel jLabel13;<br />
private JPanel jPanel7;<br />
private JButton jButton1;<br />
private JLabel jLabelDistinct;<br />
private JLabel jLabel11;<br />
private JPanel jPanel4;<br />
private JLabel jLabel7;<br />
private JLabel jLabel10;<br />
private JLabel jLabel9;<br />
private JScrollPane jScrollPane3;<br />
private JPanel jPanel6;<br />
private JPanel jPanel5;<br />
private JPanel jPanel3;<br />
private JTabbedPane jTabbedPane1;<br />
private JScrollPane jScrollPane1;<br />
private JLabel jLabelNumberOfAttributes;<br />
private JLabel jLabel5;<br />
private JLabel jLabel3;<br />
private JLabel jLabel2;<br />
private JLabel jLabel1;<br />
private JPanel jPanel1;<br />
private JPanel jPanel2;<br />
private JMenuItem jMenuItem3;<br />
private JMenu jMenu2;<br />
private JMenuItem jMenuItem2;<br />
private JSeparator jSeparator1;<br />
private MyFileReader myFileReader;<br />
private Canvas canvas1;<br />
private JTextField jTextField6;<br />
private JLabel jLabel17;<br />
private JButton jButton2;<br />
private JProgressBar jProgressBar1;<br />
private JPanel jPanel8;<br />
private JCheckBox jCheckBox3;<br />
private JCheckBox jCheckBox2;<br />
private JLabel jLabel15;<br />
private JCheckBox jCheckBox1;<br />
private JTextArea jTextArea1;<br />
private JScrollPane jScrollPane2;<br />
private boolean isClassifying;<br />
private ID3 cv;<br />
private Attribute [] attributesArray;<br />
private DataInstance [] dataInstancesArray;<br />
<br />
<br />
public static void main(String[] args) {<br />
GUIID3JFrame inst = new GUIID3JFrame();<br />
inst.setVisible(true);<br />
<br />
}<br />
<br />
public GUIID3JFrame() {<br />
super();<br />
try{<br />
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");<br />
javax.swing.SwingUtilities.updateComponentTreeUI(this);<br />
}catch(Exception e){}<br />
initGUI();<br />
}<br />
<br />
private void initGUI() {<br />
try {<br />
{<br />
jMenuBar1 = new JMenuBar();<br />
setJMenuBar(jMenuBar1);<br />
jMenuBar1.setPreferredSize(new java.awt.Dimension(392, 26));<br />
{<br />
jMenu1 = new JMenu();<br />
jMenuBar1.add(jMenu1);<br />
jMenu1.setText("File");<br />
jMenu1.setDisplayedMnemonicIndex(0);<br />
jMenu1.setMnemonic(java.awt.event.KeyEvent.VK_F);<br />
{<br />
jMenuItem1 = new JMenuItem();<br />
jMenu1.add(jMenuItem1);<br />
jMenuItem1.setText("Open...");<br />
jMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));<br />
jMenuItem1.setDisplayedMnemonicIndex(0);<br />
jMenuItem1.setMnemonic(java.awt.event.KeyEvent.VK_O);<br />
jMenuItem1.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
final JFileChooser fc = new JFileChooser();<br />
fc.setCurrentDirectory(new File(System.getProperty("user.dir")));<br />
fc.addChoosableFileFilter(new ArffFilter());<br />
int returnVal = fc.showOpenDialog(GUIID3JFrame.this);<br />
if(returnVal == 0){<br />
myFileReader = new MyFileReader(fc.getSelectedFile());<br />
if(myFileReader.fileIsOk()){<br />
attributesArray = myFileReader.getAttributesArray();<br />
dataInstancesArray = myFileReader.getDataInstancesArray();<br />
setLabel2(myFileReader.getRelation());<br />
jLabelNumberOfAttributes.setText(String.valueOf(myFileReader.getAttributesNo()));<br />
jLabelNumberOfInstances.setText(String.valueOf(myFileReader.getInstancesNo()));<br />
setTableAtt1(myFileReader.getAttributesArray());<br />
jTabbedPane1.setEnabledAt(1,true);<br />
}<br />
}<br />
}<br />
});<br />
}<br />
{<br />
jSeparator1 = new JSeparator();<br />
jMenu1.add(jSeparator1);<br />
}<br />
{<br />
jMenuItem2 = new JMenuItem();<br />
jMenu1.add(jMenuItem2);<br />
jMenuItem2.setText("Exit");<br />
jMenuItem2.setDisplayedMnemonicIndex(1);<br />
jMenuItem2.setMnemonic(java.awt.event.KeyEvent.VK_X);<br />
jMenuItem2.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
System.exit(0);<br />
}<br />
});<br />
}<br />
}<br />
{<br />
jMenu2 = new JMenu();<br />
jMenuBar1.add(jMenu2);<br />
jMenu2.setText("Help");<br />
jMenu2.setDisplayedMnemonicIndex(0);<br />
jMenu2.setMnemonic(java.awt.event.KeyEvent.VK_H);<br />
{<br />
jMenuItem3 = new JMenuItem();<br />
jMenu2.add(jMenuItem3);<br />
jMenuItem3.setText("About GUI Ant-Miner");<br />
jMenuItem3.setDisplayedMnemonicIndex(0);<br />
jMenuItem3.setMnemonic(java.awt.event.KeyEvent.VK_A);<br />
jMenuItem3.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
JDialog dialog = new JDialog(GUIID3JFrame.this, true);<br />
AboutGUIId3 inst = new AboutGUIId3(dialog, GUIID3JFrame.this);<br />
inst.setVisible(true);<br />
}<br />
});<br />
}<br />
}<br />
}<br />
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);<br />
BorderLayout thisLayout = new BorderLayout();<br />
this.getContentPane().setLayout(thisLayout);<br />
this.setTitle("GUI ID3");<br />
{<br />
jTabbedPane1 = new JTabbedPane();<br />
this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);<br />
jTabbedPane1.setPreferredSize(new java.awt.Dimension(792, 542));<br />
{<br />
jPanel2 = new JPanel();<br />
GridBagLayout jPanel2Layout = new GridBagLayout();<br />
jPanel2Layout.columnWeights = new double[] {1.0,1.0};<br />
jPanel2Layout.columnWidths = new int[] {400,420};<br />
jPanel2Layout.rowWeights = new double[] {1.0};<br />
jPanel2Layout.rowHeights = new int[] {};<br />
jTabbedPane1.addTab("Preprocess", null, jPanel2, null);<br />
jPanel2.setPreferredSize(new java.awt.Dimension(787, 514));<br />
jPanel2.setLayout(jPanel2Layout);<br />
jPanel2.setMinimumSize(new java.awt.Dimension(400, 400));<br />
{<br />
jScrollPane1 = new JScrollPane();<br />
jPanel2.add(jScrollPane1, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
1.0,<br />
0.0,<br />
GridBagConstraints.NORTH,<br />
GridBagConstraints.HORIZONTAL,<br />
new Insets(72, 5, 5, 2),<br />
0,<br />
0));<br />
jScrollPane1.setBounds(7, 80, 385, 200);<br />
jScrollPane1.setBorder(BorderFactory.createTitledBorder("Attributes"));<br />
jScrollPane1.setSize(364, 200);<br />
jScrollPane1.setMinimumSize(new java.awt.Dimension(23, 240));<br />
jScrollPane1.setPreferredSize(new java.awt.Dimension(385, 250));<br />
<br />
}<br />
{<br />
jPanel1 = new JPanel();<br />
jPanel2.add(jPanel1, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
1.0,<br />
0.0,<br />
GridBagConstraints.NORTH,<br />
GridBagConstraints.HORIZONTAL,<br />
new Insets(5, 5, 5, 2),<br />
0,<br />
0));<br />
GridBagLayout jPanel1Layout = new GridBagLayout();<br />
jPanel1Layout.columnWeights = new double[] { 0.1, 0.1,<br />
0.1, 0.1, 0.1, 0.1 };<br />
jPanel1Layout.columnWidths = new int[] {30,66,120,63,100,20};<br />
jPanel1Layout.rowHeights = new int[] {18,18};<br />
jPanel1Layout.rowWeights = new double[] {0.1,0.1};<br />
jPanel1.setLayout(jPanel1Layout);<br />
<br />
jPanel1.setBounds(7, 8, 385, 72);<br />
jPanel1.setBorder(BorderFactory.createTitledBorder("Relation"));<br />
jPanel1.setSize(320, 59);<br />
{<br />
jLabel1 = new JLabel();<br />
jPanel1.add(jLabel1, new GridBagConstraints(<br />
1,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel1.setText("Relation: ");<br />
jLabel1<br />
.setHorizontalTextPosition(SwingConstants.RIGHT);<br />
jLabel1<br />
.setHorizontalAlignment(SwingConstants.RIGHT);<br />
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));<br />
}<br />
{<br />
jLabel2 = new JLabel();<br />
jPanel1.add(jLabel2, new GridBagConstraints(<br />
2,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));<br />
jLabel2.setName(" ");<br />
jLabel2.setText(" ");<br />
}<br />
{<br />
jLabel3 = new JLabel();<br />
jPanel1.add(jLabel3, new GridBagConstraints(<br />
1,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel3.setText("Instances: ");<br />
jLabel3.setLayout(null);<br />
jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));<br />
}<br />
{<br />
jLabelNumberOfInstances = new JLabel();<br />
jPanel1.add(<br />
jLabelNumberOfInstances,<br />
new GridBagConstraints(<br />
2,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabelNumberOfInstances.setFont(new java.awt.Font("Dialog", 0, 12));<br />
jLabelNumberOfInstances.setText(" ");<br />
}<br />
{<br />
jLabel5 = new JLabel();<br />
jPanel1.add(jLabel5, new GridBagConstraints(<br />
3,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel5.setText("Attributes: ");<br />
jLabel5.setBounds(0, 0, 41, 16);<br />
jLabel5.setFont(new java.awt.Font("Dialog", 0, 12));<br />
}<br />
{<br />
jLabelNumberOfAttributes = new JLabel();<br />
jPanel1.add(<br />
jLabelNumberOfAttributes,<br />
new GridBagConstraints(<br />
4,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabelNumberOfAttributes.setBounds(0, 0, 41, 16);<br />
jLabelNumberOfAttributes.setFont(new java.awt.Font("Dialog", 0, 12));<br />
jLabelNumberOfAttributes.setText(" ");<br />
}<br />
<br />
}<br />
{<br />
jPanel5 = new JPanel();<br />
BoxLayout jPanel5Layout = new BoxLayout(jPanel5, javax.swing.BoxLayout.Y_AXIS);<br />
jPanel2.add(jPanel5, new GridBagConstraints(<br />
1,<br />
0,<br />
1,<br />
1,<br />
1.0,<br />
0.0,<br />
GridBagConstraints.NORTH,<br />
GridBagConstraints.HORIZONTAL,<br />
new Insets(5, 2, 0, 5),<br />
0,<br />
0));<br />
jPanel5.setBounds(396, 8, 385, 250);<br />
jPanel5.setLayout(jPanel5Layout);<br />
jPanel5.setBorder(BorderFactory.createTitledBorder("Selected Attribute"));<br />
jPanel5.setMinimumSize(new java.awt.Dimension(300, 250));<br />
jPanel5.setSize(318, 250);<br />
{<br />
jPanel6 = new JPanel();<br />
GridBagLayout jPanel6Layout = new GridBagLayout();<br />
jPanel6Layout.columnWeights = new double[] {0.1,0.1,0.1,0.1,0.1,0.1};<br />
jPanel6Layout.columnWidths = new int[] {30,52,120,92,58,25};<br />
jPanel6Layout.rowWeights = new double[] {1.0,1.0,0.1};<br />
jPanel6Layout.rowHeights = new int[] {18,22,4};<br />
jPanel5.add(jPanel6);<br />
jPanel6.setLayout(jPanel6Layout);<br />
jPanel6.setBounds(6, 19, 369, 41);<br />
jPanel6.setPreferredSize(new java.awt.Dimension(376, 35));<br />
jPanel6.setMinimumSize(new java.awt.Dimension(10, 35));<br />
{<br />
jLabel8 = new JLabel();<br />
jPanel6.add(jLabel8, new GridBagConstraints(<br />
2,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel8.setFont(new java.awt.Font("Dialog",0,12));<br />
jLabel8.setText(" ");<br />
}<br />
{<br />
jLabel9 = new JLabel();<br />
jPanel6.add(jLabel9, new GridBagConstraints(<br />
1,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel9.setText("Missing: ");<br />
jLabel9.setFont(new java.awt.Font("Dialog",0,12));<br />
jLabel9.setHorizontalAlignment(SwingConstants.RIGHT);<br />
jLabel9.setHorizontalTextPosition(SwingConstants.RIGHT);<br />
}<br />
{<br />
jLabel10 = new JLabel();<br />
jPanel6.add(jLabel10, new GridBagConstraints(<br />
2,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel10.setFont(new java.awt.Font("Dialog",0,12));<br />
jLabel10.setText(" ");<br />
}<br />
{<br />
jLabel11 = new JLabel();<br />
jPanel6.add(jLabel11, new GridBagConstraints(<br />
3,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel11.setText("Distinct Values: ");<br />
jLabel11.setFont(new java.awt.Font("Dialog",0,12));<br />
}<br />
{<br />
jLabelDistinct = new JLabel();<br />
jPanel6.add(<br />
jLabelDistinct,<br />
new GridBagConstraints(<br />
4,<br />
1,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabelDistinct.setFont(new java.awt.Font("Dialog",0,12));<br />
jLabelDistinct.setText(" ");<br />
}<br />
{<br />
jLabel7 = new JLabel();<br />
jPanel6.add(jLabel7, new GridBagConstraints(<br />
1,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel7.setText("Name: ");<br />
jLabel7.setFont(new java.awt.Font(<br />
"Dialog",<br />
0,<br />
12));<br />
jLabel7.setBounds(0, 0, 45, 16);<br />
}<br />
}<br />
{<br />
jScrollPane3 = new JScrollPane();<br />
jPanel5.add(jScrollPane3);<br />
jScrollPane3.setBounds(5, 63, 374, 181);<br />
jScrollPane3.setPreferredSize(new java.awt.Dimension(368, 181));<br />
}<br />
}<br />
{<br />
jPanel4 = new JPanel();<br />
BorderLayout jPanel4Layout = new BorderLayout();<br />
jPanel4.setLayout(jPanel4Layout);<br />
jPanel2.add(jPanel4, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(263, 4, 5, 7), 0, 0));<br />
jPanel4.setBounds(398, 264, 381, 225);<br />
jPanel4.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));<br />
jPanel4.setMinimumSize(new java.awt.Dimension(10, 210));<br />
jPanel4.setSize(337, 200);<br />
jPanel4.setPreferredSize(new java.awt.Dimension(381, 210));<br />
}<br />
}<br />
{<br />
jPanel3 = new JPanel();<br />
jTabbedPane1.addTab("Classify", null, jPanel3, null);<br />
GridBagLayout jPanel3Layout = new GridBagLayout();<br />
jPanel3Layout.columnWeights = new double[] {0.0,1.0};<br />
jPanel3Layout.columnWidths = new int[] {150,500};<br />
jPanel3Layout.rowWeights = new double[] {1.0};<br />
jPanel3Layout.rowHeights = new int[] {7};<br />
jPanel3.setLayout(jPanel3Layout);<br />
{<br />
jButton1 = new JButton();<br />
jPanel3.add(jButton1, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(292, 10, 0, 0),<br />
0,<br />
0));<br />
jButton1.setText("Start");<br />
jButton1.setBounds(19, 234, 99, 24);<br />
jButton1.setToolTipText("Start classification. This operation may take a few minutes.");<br />
jButton1.setSize(75, 23);<br />
jButton1.setMinimumSize(new java.awt.Dimension(75, 23));<br />
jButton1.setMaximumSize(new java.awt.Dimension(75, 23));<br />
jButton1.setPreferredSize(new java.awt.Dimension(75, 23));<br />
jButton1.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
setIsClassifying(true);<br />
<br />
cv = new ID3(GUIID3JFrame.this);<br />
<br />
cv.setAttributesArray(attributesArray);<br />
cv.setDataInstancesArray(dataInstancesArray);<br />
cv.start();<br />
}<br />
});<br />
}<br />
<br />
<br />
<br />
{<br />
jScrollPane2 = new JScrollPane();<br />
jPanel3.add(jScrollPane2, new GridBagConstraints(<br />
1,<br />
0,<br />
1,<br />
1,<br />
1.0,<br />
1.0,<br />
GridBagConstraints.CENTER,<br />
GridBagConstraints.BOTH,<br />
new Insets(5, 2, 0, 3),<br />
0,<br />
0));<br />
jScrollPane2.setBounds(271, 8, 509, 501);<br />
jScrollPane2.setBorder(BorderFactory.createTitledBorder("Output"));<br />
{<br />
jTextArea1 = new JTextArea();<br />
jScrollPane2.setViewportView(jTextArea1);<br />
jTextArea1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));<br />
jTextArea1.setEditable(false);<br />
}<br />
}<br />
{<br />
jCheckBox1 = new JCheckBox();<br />
jPanel3.add(jCheckBox1, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(212, 10, 0, 0),<br />
0,<br />
0));<br />
jCheckBox1.setText("Clear output screen");<br />
jCheckBox1.setBounds(17, 201, 133, 30);<br />
jCheckBox1.setFont(new java.awt.Font("Dialog",0,12));<br />
jCheckBox1.setSelected(true);<br />
jCheckBox1.setToolTipText("Clear the output screen the next time a classification task is run.");<br />
}<br />
{<br />
jCheckBox2 = new JCheckBox();<br />
jPanel3.add(jCheckBox2, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(235, 10, 0, 0),<br />
0,<br />
0));<br />
jCheckBox2.setText("Print test set");<br />
jCheckBox2.setFont(new java.awt.Font("Dialog",0,12));<br />
}<br />
{<br />
jCheckBox3 = new JCheckBox();<br />
jPanel3.add(jCheckBox3, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(258, 10, 0, 0),<br />
0,<br />
0));<br />
jCheckBox3.setText("Print training set");<br />
jCheckBox3.setFont(new java.awt.Font("Dialog",0,12));<br />
}<br />
{<br />
jButton2 = new JButton();<br />
jPanel3.add(jButton2, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.NORTHWEST,<br />
GridBagConstraints.NONE,<br />
new Insets(292, 95, 0, 0),<br />
0,<br />
0));<br />
jButton2.setText("Cancel");<br />
jButton2.setToolTipText("Cancel task.");<br />
jButton2.setPreferredSize(new java.awt.Dimension(75, 23));<br />
jButton2.setMaximumSize(new java.awt.Dimension(75, 23));<br />
jButton2.setMinimumSize(new java.awt.Dimension(75, 23));<br />
jButton2.setEnabled(false);<br />
jButton2.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent evt) {<br />
cv.stop();<br />
}<br />
});<br />
jButton2.addMouseListener(new MouseAdapter() {<br />
public void mouseExited(MouseEvent evt) {<br />
if (isClassifying)<br />
jTabbedPane1<br />
.setCursor(Cursor<br />
.getPredefinedCursor(Cursor.WAIT_CURSOR));<br />
}<br />
public void mouseEntered(MouseEvent evt) {<br />
jTabbedPane1<br />
.setCursor(Cursor<br />
.getPredefinedCursor(Cursor.DEFAULT_CURSOR));<br />
}<br />
});<br />
}<br />
}<br />
}<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
<br />
pack();<br />
this.setSize(800, 600);<br />
setGraph();<br />
<br />
jTabbedPane1.setEnabledAt(1,false);<br />
jTabbedPane1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));<br />
{<br />
jPanel8 = new JPanel();<br />
GridBagLayout jPanel8Layout = new GridBagLayout();<br />
jPanel8Layout.columnWeights = new double[] {0.1,0.1};<br />
jPanel8Layout.columnWidths = new int[] {7,7};<br />
jPanel8Layout.rowWeights = new double[] {0.1};<br />
jPanel8Layout.rowHeights = new int[] {7};<br />
jPanel8.setLayout(jPanel8Layout);<br />
this.getContentPane().add(jPanel8, BorderLayout.SOUTH);<br />
jPanel8.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));<br />
{<br />
jProgressBar1 = new JProgressBar();<br />
jPanel8.add(jProgressBar1, new GridBagConstraints(<br />
1,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.EAST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jProgressBar1.setPreferredSize(new java.awt.Dimension(140, 18));<br />
}<br />
{<br />
jLabel17 = new JLabel();<br />
jPanel8.add(jLabel17, new GridBagConstraints(<br />
0,<br />
0,<br />
1,<br />
1,<br />
0.0,<br />
0.0,<br />
GridBagConstraints.WEST,<br />
GridBagConstraints.NONE,<br />
new Insets(0, 0, 0, 0),<br />
0,<br />
0));<br />
jLabel17.setText("Idle");<br />
}<br />
}<br />
<br />
}<br />
public void setLabel2(String label){<br />
jLabel2.setText(label);<br />
}<br />
<br />
public JLabel getJLabel2(){<br />
return jLabel2;<br />
}<br />
public boolean getJCheckBox1IsSelected(){<br />
return jCheckBox1.isSelected();<br />
}<br />
public boolean getJCheckBox2IsSelected(){<br />
return jCheckBox2.isSelected();<br />
}<br />
public boolean getJCheckBox3IsSelected(){<br />
return jCheckBox3.isSelected();<br />
}<br />
public String getjTextField2Value(){<br />
return jTextField2.getText();<br />
}<br />
public JTextField getJTextField1(){<br />
return jTextField1;<br />
}<br />
public JTextField getJTextField3(){<br />
return jTextField3;<br />
}<br />
public JTextField getJTextField4(){<br />
return jTextField4;<br />
}<br />
public JTextField getJTextField5(){<br />
return jTextField5;<br />
}<br />
public JTextField getJTextField6(){<br />
return jTextField6;<br />
}<br />
public JTextArea getJTextArea1(){<br />
return jTextArea1;<br />
}<br />
public JProgressBar getJProgressBar1(){<br />
return jProgressBar1;<br />
}<br />
public void setTableAtt1(Attribute [] attributesArray){<br />
String numberAndName[][] = new String[attributesArray.length][2];<br />
for(int n=0; n < attributesArray.length; n++){<br />
numberAndName[n][0] = new Integer(n + 1).toString();<br />
numberAndName[n][1] = attributesArray[n].getAttributeName();<br />
}<br />
<br />
jTable1Model = new MyTableModel(<br />
numberAndName,<br />
new String[] { "#", "Name" });<br />
jTable1 = new JTable();<br />
jScrollPane1.setViewportView(jTable1);<br />
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);<br />
jTable1.setModel(jTable1Model);<br />
<br />
ListSelectionModel rowSM = jTable1.getSelectionModel();<br />
rowSM.addListSelectionListener(new ListSelectionListener() {<br />
public void valueChanged(ListSelectionEvent e) {<br />
if (e.getValueIsAdjusting()) return;<br />
ListSelectionModel lsm = (ListSelectionModel)e.getSource();<br />
if (lsm.isSelectionEmpty()) {<br />
} else {<br />
int selectedRow = lsm.getMinSelectionIndex();<br />
setTableAtt2(selectedRow, myFileReader.generateStats(selectedRow));<br />
jLabel8.setText(myFileReader.getAttributesArray()[selectedRow].getAttributeName());<br />
<br />
DecimalFormat myFormatter = new DecimalFormat("###.#");<br />
jLabel10.setText(String.valueOf(myFileReader.calculateMissing(selectedRow))+ " ("+myFormatter.format(((float) myFileReader.calculateMissing(selectedRow)/myFileReader.getDataInstancesArray().length)*100)+"%)");<br />
<br />
jLabelDistinct.setText(String.valueOf(jTable2.getModel().getRowCount()));<br />
}<br />
}<br />
});<br />
}<br />
public void setTableAtt2(int attributeIndex, int [] typesOccurences){<br />
String labelAndCount[][] = new String[typesOccurences.length][2];<br />
for(int n=0; n < typesOccurences.length; n++){<br />
labelAndCount[n][0] = myFileReader.getAttributesArray()[attributeIndex].getTypes()[n];<br />
labelAndCount[n][1] = new Integer(typesOccurences[n]).toString();<br />
}<br />
jTable2Model = new MyTableModel(<br />
labelAndCount,<br />
new String[] { "Attribute Value", "Quantity of Instances" });<br />
jTable2 = new JTable();<br />
jScrollPane3.setViewportView(jTable2);<br />
jTable2.setModel(jTable2Model);<br />
}<br />
<br />
public void setGraph(){<br />
canvas1 = new AttGraph();<br />
jPanel4.add(canvas1, BorderLayout.CENTER);<br />
<br />
canvas1.setBounds(2, 2, 377, 221);<br />
}<br />
<br />
public void setInitialState(){<br />
jTabbedPane1.setEnabledAt(1,false);<br />
jTabbedPane1.setSelectedIndex(0);<br />
if(jTable1 != null){<br />
jTable1.removeAll();<br />
jTable1.setVisible(false);<br />
jTable1Model = null;<br />
}<br />
if(jTable2 != null){<br />
jTable2.removeAll();<br />
jTable2.setVisible(false);<br />
jTable2Model = null;<br />
}<br />
jLabel2.setText("");<br />
jLabelNumberOfInstances.setText("");<br />
jLabelNumberOfAttributes.setText("");<br />
jLabel8.setText("");<br />
jLabel10.setText("");<br />
jLabelDistinct.setText("");<br />
}<br />
<br />
public void setIsClassifying(boolean b){<br />
if(b){<br />
jLabel17.setText("Classifying " + jLabel2.getText() + "...");<br />
isClassifying = true;<br />
jMenuItem1.setEnabled(false);<br />
jButton1.setEnabled(false);<br />
jButton2.setEnabled(true);<br />
jTabbedPane1.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));<br />
}else{<br />
jLabel17.setText("Idle");<br />
isClassifying = false;<br />
jMenuItem1.setEnabled(true);<br />
jButton1.setEnabled(true);<br />
jButton2.setEnabled(false);<br />
jTabbedPane1.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));<br />
}<br />
}<br />
<br />
public MyFileReader getMyFileReader(){<br />
return myFileReader;<br />
}<br />
<br />
<br />
private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) {<br />
parent.addMouseListener(new java.awt.event.MouseAdapter() {<br />
public void mousePressed(java.awt.event.MouseEvent e) {<br />
if(e.isPopupTrigger())<br />
menu.show(parent, e.getX(), e.getY());<br />
}<br />
public void mouseReleased(java.awt.event.MouseEvent e) {<br />
if(e.isPopupTrigger())<br />
menu.show(parent, e.getX(), e.getY());<br />
}<br />
});<br />
}<br />
<br />
public JLabel getJLabel17() {<br />
return jLabel17;<br />
}<br />
<br />
class MyTableModel extends DefaultTableModel{<br />
<br />
private static final long serialVersionUID = 1L;<br />
public MyTableModel(Object[][] data, Object[] columnNames) {<br />
setDataVector(data, columnNames);<br />
}<br />
public boolean isCellEditable(int row, int column) {<br />
return false;<br />
}<br />
}<br />
<br />
|
|
|
|
|
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?
|
|
|
|
|