Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BaseDecisionTree.java package assignment2; public class BaseDecisionTree { private int featureIndex; // feature index on which private double threshold; // threshold is applied public static int

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

BaseDecisionTree.java

package assignment2;

public class BaseDecisionTree { private int featureIndex; // feature index on which private double threshold; // threshold is applied

public static int dim; // input feature vector dimension public static int nClasses; // number of classes

private int[] classFreq; // number of time instance of this class has reached this node private int total; // number of trained samples having reached this node

private BaseDecisionTree smallerBranch; // if feature value = threshold private BaseDecisionTree parent; // a reference to the parent node

/** * constructor for a leaf */ public BaseDecisionTree() {

this.featureIndex = -1; this.threshold = 0.0;

classFreq = new int[nClasses]; }

/** * constructor for a node */ public BaseDecisionTree(int featureIndex, double threshold) {

this.featureIndex = featureIndex; this.threshold = threshold;

classFreq = new int[nClasses]; }

/** * get the class associated with this decision tree */ public int getDecision(double[] vector) { // write your code here. return 0; } /** * return the class having highest probability */ public int getMaxClass() { // write your code here. return 0; }

/** *returns the number of samples that hit this node */ public int getTotal() { return total; }

/** *return the highest probability */ public double getMaxProb() { // write your code here. return 0; } /** * manually set the probability of class c (in %) * to use instead of updateProbCount */ public void setProb(int c, int percentage) {

classFreq[c] = percentage; total = 100; }

/** In this method you should calculate the probability for different input features *(gender, age and siblings) and use them to build your tree. This function takes an *input file input.txt. The file is provided in the zip folder. *For each feature (column) calculate the probability of survivedon survived(last column). *For example for feature 1 (male/female) we have female with 27% survived, count how many samples of female that has survived/total number of females. */ public int[] CalcProb(String inputFeatureWLabel) { /* * write your code here. */ }

/* * adds a new branch on the left */ public void setSmallerBranch(BaseDecisionTree ds) {

smallerBranch = ds; ds.parent = this; }

/* * adds a new branch on the right */ public void setGreaterBranch(BaseDecisionTree ds) { /* * write your code here. */ }

/* * returns the parent of this node */ public BaseDecisionTree getParent() {

return parent; }

/* * returns the left child of this node */ public BaseDecisionTree getSmallerBranch() {

return smallerBranch; }

/* * returns the right child of this node */ public BaseDecisionTree getGreaterBranch() {

return greaterBranch; }

/* * checks if it is a leaf */ public boolean isExternal() {

return featureIndex == -1; } /** * 1.Calculate the accuracy of your results. * After building your tree and testing with sample data in the tester class. *You should try it with real test data (output.txt) and compare your results with actual test results (last column in the file). *The accuracy will be your right decisions divided by the number of samples in output data. */ public int[] CalcAccuracy(String inputFeatureWLabel) {

// write your code here.

}

/** * In this function you should * be able to print all the nodes of decision tree in a pre-Order Traversal. */ public void print() {

// write your code here. } /** * toString */ public String toString() {

StringBuilder out = new StringBuilder("(" + featureIndex + " of " + dim + " ," + threshold + " , [" + classFreq[0]); for (int i = 1; i

return out.toString(); }

}

---------------------------------------------------

BaseDecisionTreeTester.java

package assignment2; import java.util.*;

public class BaseDecisionTreeTester {

/* * Build a decision tree * feature 0 is gender (0: female 1:male) * feature 1 is age * feature 2 is sibling count * class 0 is not survived * class 1 is survived * read the train file */ public static void main( String [] args ) { BaseDecisionTree.nClasses = 2; BaseDecisionTree.dim = 3; /* * is man? */ BaseDecisionTree s1 = new BaseDecisionTree(0, 0.99); BaseDecisionTree s2 = new BaseDecisionTree(); // leaf s2.setProb(1, 85); // survived at 36% probability

/* * is age 2.5 */ BaseDecisionTree s5 = new BaseDecisionTree(2,2.9); // leaf BaseDecisionTree s6 = new BaseDecisionTree(); // leaf s6.setProb(0, 15); // not survived at 2% BaseDecisionTree s7 = new BaseDecisionTree(); // leaf s7.setProb(1, 25); // survived at 20%

s1.setSmallerBranch(s2); s1.setGreaterBranch(s3); s3.setSmallerBranch(s4); s3.setGreaterBranch(s5); s5.setSmallerBranch(s6); s5.setGreaterBranch(s7); /* * this part is just for test your code. we simulated the tree and you can see * the right answers if you want o check them with yours. "1" means survived and "0" * means not survived. */ double[] test1 = {1,2,4}; System.out.println("Man, 2 years old with 4 siblings = " + s1.getDecision(test1));//answer is:0 double[] test2 = {1,10,5}; System.out.println("Man 10 years old with 5 siblings = " + s1.getDecision(test2));//answer is:1 double[] test3 = {0,2,0}; System.out.println("Woman 2 years old = " + s1.getDecision(test3));//answer is 1 double[] test4 = {1,20,0}; System.out.println("Woman 40 years old = " + s1.getDecision(test4));//answer is:1 double[] test5 = {1,34,1}; System.out.println("Man, 10 years old with 1 siblings = " + s1.getDecision(test5));//answer is:0 /* Please comment out this part after you calculate the probabilities from * input data. These inputs are based on output data and the answer for them is * in front of each one. you can test your decision tree with these.*/

/* double[] test1 = {0,39,1}; System.out.println("Man, 2 years old with 4 siblings = " + s1.getDecision(test1));//answer is 1 double[] test2 = {1,24,2}; System.out.println("Man 10 years old with 5 siblings = " + s1.getDecision(test2));//answer is 0 double[] test3 = {0,2,4}; System.out.println("Woman 2 years old = " + s1.getDecision(test3));//answer is 0 double[] test4 = {0, 40,4}; System.out.println("Woman 40 years old = " + s1.getDecision(test4));//answer is 0 double[] test5 = {1, 10, 1}; System.out.println("Man, 10 years old with 1 siblings = " + s1.getDecision(test5));//answer is 0 */ } } ----------------------------

In this assignment, we will explore decision trees. A binary decision tree is a full binary tree to identify the class to which observed data belongs. The following example illustrates this concept. Gender Female Age 2.9 Survived 27% not survived?% not survived?% Survived ?% This example shows a decision tree to predict whether a given person survived at Titanic or not by using their gender, age and number of siblings they had. They have been obtained based on a large number of samples (for which age, gender and siblings were known). We try to classify the titanic passengers based on these features to the status of survived/ not survived. We do so by applying the rules of the decision tree on a large number of observations. Also, it is possible to count how many samples of each class reach a given leaf. For example, for the leaf corresponding to the decision [ Female, Age

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

What are the sources of AirAsia's cost advantage?

Answered: 1 week ago

Question

=+1. Describe the value chain of the media industry!

Answered: 1 week ago

Question

=+3. Draw the submodels of an integrated business model!

Answered: 1 week ago