preview

Notes On The And Public Class Entropy

Decent Essays

S sunny = { D1,D2,D8,D9,D11 } Gain ( S sunny, Humidity) = 0.970 - (3/5)0.0 - (2/5) 0.0 = 0.970 Gain (S sunny, Temperature ) = 0.970 - (2/5) 0.0 - (2/5) 1.0 - (1/5) 0.0 = 0.570 Gain ( S sunny, wind ) = 0.970 - (2/5) 1.0 - (3/5) 0.190 Java. tree, Code import java.util.ArrayList; public class Entropy { public static double calculateEntropy(ArrayList data) { double entropy = 0; if(data.size() == 0) { // nothing to do return 0; } for(int i = 0; i < Hw1.setSize("PlayTennis"); i++) { int count = 0; for(int j = 0; j < data.size(); j++) { Record record = data.get(j); if(record.getAttributes().get(4).getValue() == i) { count++; } } double probability = count / (double)data.size(); if(count > 0) { entropy += -probability * (Math.log(probability) / Math.log(2)); } } return entropy; } public static double calculateGain(double rootEntropy, ArrayList subEntropies, ArrayList setSizes, int data) { double gain = rootEntropy; for(int i = 0; i < subEntropies.size(); i++) { gain += -((setSizes.get(i) / (double)data) * subEntropies.get(i)); } return gain; } } Gain calculation please refer to the above formula import java.io.*; import java.util.*; public class Tree { public Node buildTree(ArrayList records, Node root, LearningSet learningSet) { int bestAttribute = -1; double bestGain = 0; root.setEntropy(Entropy.calculateEntropy(root.getData())); if(root.getEntropy() == 0) {

Get Access