It is currently Thu Mar 11, 2010 10:33 am


All times are UTC [ DST ]


Ask on Codemiles community and get answers Free and Fast :

Java is a high level programming language .Here ask your java questions ,and get answers free. Read java articles.

Our guest share with us your code snippets , your programming problems , your open source projects ,read articles and post yours .



Registration to Codemiles.com is FAST and FREE. Join Now!





Post new topic Reply to topic  [ 2 posts ] 
  Print view Previous topic | Next topic 
Author Message
 Post subject: java code for decision tree algorithm
PostPosted: Tue Oct 06, 2009 8:52 am 
Offline
Newbie
User avatar

Joined: Fri Oct 02, 2009 2:33 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
java code for decision tree algorithm ....
i need it urgently for my mini project...



TOP
 Profile Send private message  
 
| More
 Post subject: Re: java code for decision tree algorithm
PostPosted: Tue Oct 06, 2009 12:10 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 1495
Location: Earth
Has thanked: 8 time
Have thanks: 13 time
Hey is found this code :

There are many types of java decision tree algorithm like jaDTi ,
http://www.run.montefiore.ulg.ac.be/~fr ... are/jaDTi/
And this is helpful too :
http://www.scribd.com/doc/2062526/Decis ... -Algorithm

or id3 like this code :
Code:
import java.io.*;

import java.util.*;

public class ID3

{

int numAttributes;

String []attributeNames;

Vector []domains;

/* The class to represent a data point consisting of numAttributes values

of attributes */

class DataPoint {

public int []attributes;

public DataPoint(int numattributes) {

attributes = new int[numattributes];

}

};



/* The class to represent a node in the decomposition tree.

*/

class TreeNode {

public double entropy;

public Vector data;

public int decompositionAttribute;

public int decompositionValue;

public TreeNode []children;

public TreeNode parent;

public TreeNode() {

data = new Vector();

}

};

TreeNode root = new TreeNode();

public int getSymbolValue(int attribute, String symbol) {

int index = domains[attribute].indexOf(symbol);

if (index < 0) {

domains[attribute].addElement(symbol);

return domains[attribute].size() -1;

}

return index;

}



public int []getAllValues(Vector data, int attribute) {

Vector values = new Vector();

int num = data.size();

for (int i=0; i< num; i++) {

DataPoint point = (DataPoint)data.elementAt(i);

String symbol =

(String)domains[attribute].elementAt(point.attributes[attribute] );

int index = values.indexOf(symbol);

if (index < 0) {

values.addElement(symbol);

}

}

int []array = new int[values.size()];

for (int i=0; i< array.length; i++) {

String symbol = (String)values.elementAt(i);

array = domains[attribute].indexOf(symbol);

}

values = null;

return array;

}





public Vector getSubset(Vector data, int attribute, int value) {

Vector subset = new Vector();

int num = data.size();

for (int i=0; i< num; i++) {

DataPoint point = (DataPoint)data.elementAt(i);

if (point.attributes[attribute] == value) subset.addElement(point);

}

return subset;

}





public double calculateEntropy(Vector data) {

int numdata = data.size();

if (numdata == 0) return 0;

int attribute = numAttributes-1;

int numvalues = domains[attribute].size();

double sum = 0;

for (int i=0; i< numvalues; i++) {

int count=0;

for (int j=0; j< numdata; j++) {

DataPoint point = (DataPoint)data.elementAt(j);

if (point.attributes[attribute] == i) count++;

}

double probability = 1.*count/numdata;

if (count > 0) sum += -probability*Math.log(probability);

}

return sum;

}

public boolean alreadyUsedToDecompose(TreeNode node, int attribute) {

if (node.children != null) {

if (node.decompositionAttribute == attribute )

return true;

}

if (node.parent == null) return false;

return alreadyUsedToDecompose(node.parent, attribute);

}

public void decomposeNode(TreeNode node) {

double bestEntropy=0;

boolean selected=false;

int selectedAttribute=0;

int numdata = node.data.size();

int numinputattributes = numAttributes-1;

node.entropy = calculateEntropy(node.data);

if (node.entropy == 0) return;



for (int i=0; i< numinputattributes; i++) {

int numvalues = domains.size();

if ( alreadyUsedToDecompose(node, i) ) continue;

double averageentropy = 0;

for (int j=0; j< numvalues; j++) {

Vector subset = getSubset(node.data, i, j);

if (subset.size() == 0) continue;

double subentropy = calculateEntropy(subset);

averageentropy += subentropy *

subset.size();

}

averageentropy = averageentropy / numdata; //

Taking the weighted average

if (selected == false) {

selected = true;

bestEntropy = averageentropy;

selectedAttribute = i;

} else {

if (averageentropy < bestEntropy) {

selected = true;

bestEntropy = averageentropy;

selectedAttribute = i;

}

}

}

if (selected == false) return;

int numvalues = domains[selectedAttribute].size();

node.decompositionAttribute = selectedAttribute;

node.children = new TreeNode [numvalues];

for (int j=0; j< numvalues; j++) {

node.children[j] = new TreeNode();

node.children[j].parent = node;

node.children[j].data = getSubset(node.data,

selectedAttribute, j);

node.children[j].decompositionValue = j;

}



for (int j=0; j< numvalues; j++) {

decomposeNode(node.children[j]);

}



node.data = null;

}





public int readData(String filename) throws Exception {

FileInputStream in = null;

try {

File inputFile = new File(filename);

in = new FileInputStream(inputFile);

} catch ( Exception e) {

System.err.println( "Unable to open data file: " + filename + "\n" + e);

return 0;

}

BufferedReader bin = new BufferedReader(new InputStreamReader(in) );

String input;

while(true) {

input = bin.readLine();

if (input == null) {

System.err.println( "No data found in the data file: " + filename +

"\n");

return 0;

}

if (input.startsWith("//")) continue;

if (input.equals("")) continue;

break;

}



StringTokenizer tokenizer = new StringTokenizer(input);

numAttributes = tokenizer.countTokens();

if (numAttributes <= 1) {

System.err.println( "Read line: " + input);

System.err.println( "Could not obtain the names of attributes in the

line");

System.err.println( "Expecting at least one input attribute and one

output attribute");

return 0;

}

domains = new Vector[numAttributes];

for (int i=0; i < numAttributes; i++) domains = new Vector();

attributeNames = new String[numAttributes];

for (int i=0; i < numAttributes; i++) {

attributeNames = tokenizer.nextToken();

}



while(true) {

input = bin.readLine();

if (input == null) break;

if (input.startsWith("//")) continue;

if (input.equals("")) continue;

tokenizer = new StringTokenizer(input);

int numtokens = tokenizer.countTokens();

if (numtokens != numAttributes) {

System.err.println( "Read " + root.data.size() + " data");

System.err.println( "Last line read: " + input);

System.err.println( "Expecting " + numAttributes + " attributes");

return 0;

}

DataPoint point = new DataPoint(numAttributes);

for (int i=0; i < numAttributes; i++) {

point.attributes = getSymbolValue(i, tokenizer.nextToken()

);

}

root.data.addElement(point);

}

bin.close();

return 1;

}



public void printTree(TreeNode node, String tab) {

int outputattr = numAttributes-1;

if (node.children == null) {

int []values = getAllValues(node.data, outputattr );

if (values.length == 1) {

System.out.println(tab + "\t" + attributeNames[outputattr] + " = \"" +

domains[outputattr].elementAt(values[0]) + "\";");

return;

}

System.out.print(tab + "\t" + attributeNames[outputattr] + " = {");

for (int i=0; i < values.length; i++) {

System.out.print("\"" + domains[outputattr].elementAt(values) + "\"

");

if ( i != values.length-1 ) System.out.print( " , " );

}

System.out.println( " };");

return;

}

int numvalues = node.children.length;

for (int i=0; i < numvalues; i++) {

System.out.println(tab + "if( " +

attributeNames[node.decompositionAttribute] + " == \"" +

domains[node.decompositionAttribute].elementAt(i)

+ "\") {" );

printTree(node.children, tab + "\t");

if (i != numvalues-1) System.out.print(tab + "} else ");

else System.out.println(tab + "}");

}



}



public void createDecisionTree() {

decomposeNode(root);

printTree(root, "");

}



/* main function */

public static void main(String[] args) throws Exception {







ID3 me = new ID3();

int status = me.readData("c:\\in.txt");

if (status <= 0) return;

me.createDecisionTree();

}



}


_________________
Currenlty programming with : java , html , php , and javascript .


TOP
 Profile Send private message  
 
| More
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 


 Similar topics
 Topic title   Forum   Author   Replies 
 How to create a file in java script using FireFox  Scripting Language  danyrouss  0
 need a java program from system to mobile  Java  sudhar  0
 Need help getting started on a small java game?  Java  xmortumx  1
 here how to increase java heap size  Java  msi_333  6
 Java Chess  Finished Projects  msi_333  7

All times are UTC [ DST ]


Who is online

Users browsing this forum: Ask Jeeves [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  









Home
General Talks
Finished Projects
Code Library
Games
Tutorials
Programming help
Java
C/C++
C-sharp
Web Development
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
SEO
Google SEO
Others
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2009
mileX v1.0 designed by codemiles team