Find the Best Root Note to be used in building a Decision Tree in each of the fo
ID: 3603846 • Letter: F
Question
Find the Best Root Note to be used in building a Decision Tree in each of the following. For each one, show the calculation of Information Gain.
1. Which of the following attributes should be used as root note?
Film CountryOforigin BigStar Genre Success/Failure 1 1 scifi USA USA USA Europe Europe Europe yes no yes no yes yes yes no yes yes Success medy Failure comedySuccess comedy Success Failure romance Failure comedy Failure Failure comedy Success comedy Success co 7 Australia Brazil scifi Europe 5 10 USAExplanation / Answer
#include
#include
#include "io.h"
#include "stats.h"
int main(int argc, char* argv[])
{
int size, i;
char order;
// greet and get the number of stocks
print_greeting();
printf("How many stocks prices would you like to analyze? ");
scanf("%d", &size);
// read the data
float array[size];
read_array(array, size);
// get the stats
float mean = get_average(array, size);
float variance = get_variance(array, size);
float min = get_min(array, size);
float max = get_max(array, size);
float median = get_median(array, size);
// show the results
print_results(array, size, median, min, max, mean, variance);
return 0;
}
Stats.c
#include
#include "stats.h"
// sorts the values of an array according to order
void sort (float output[], const int size, char order)
{
int i, j;
float temp;
if (order == 'a' || order == 'A')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] > output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if (order == 'd' || order == 'D')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] < output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else
return;
}
// calculates the mean of the elements of an array
float get_average(const float array[], int size)
{
int i;
float sum = 0.0;
for (i = 0; i < size; i++)
sum += array[i];
sum /= size;
return sum;
}
// calculates the variance of the emelemts of an array
// this function calls the get_average to get the mean
float get_variance(const float array[], int size)
{
int i;
float sum = 0.0;
float mean = get_average(array, size);
for (i = 0; i < size; i++)
sum += array[i] * array[i];
sum = sum/size - mean*mean;
return sum;
}
// gets the median of an array after it sorts it
float get_median(const float array[], int size)
{
int i;
float temp_array[size]; // temp array tp be manipulated
float median;
// copy oroginal array to the temp array
for (i = 0; i < size; i++)
temp_array[i] = array[i];
sort(temp_array, size, 'a');
if (size % 2 == 0)
median = (temp_array[size/2] + temp_array[size/2-1])/2.0;
else
median = temp_array[size/2];
return median;
}
// finds the maximum value of the elements of an array
float get_max(const float array[], int size)
{
int i;
float max = array[0];
for (i = 0; i < size; i++)
if (array[i] >= max)
max = array[i];
return max;
}
// finds the minimum value of the elements of an array
float get_min(const float array[], int size)
{
int i;
float min = array[0];
for (i = 0; i < size; i++)
if (array[i] <= min)
min = array[i];
return min;
}
stats.h
#ifndef STATS_H
#define STATS_H
void sort (float output[], int size, char order);
float get_average(const float array[], int size);
float get_variance(const float array[], int size);
float get_median(const float array[], int size);
float get_max(const float array[], int size);
float get_min(const float array[], int size);
#endif
io.c
#include
#include "io.h"
// prompt the user for input and read the values into an array
void read_array(float array[], int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf ("Please enter stock #%d: ", i+1);
scanf("%f", &array[i]);
}
}
// say hi to the user
void print_greeting(void)
{
printf("This program asks for user input and gives variance, average, median, min, and max of stock prices. ");
}
// display array values
void print_array(const float array[], int size)
{
int i = 0;
for (i = 0; i < size; i++)
printf("%.2f ", array[i]);
printf(" ");
}
// print the stat results including input data
void print_results(const float array[], int size, float median, float min, float max, float mean, float variance)
{
printf(" Here are the Calculations ");
print_array(array, size);
printf("median: $%.2f ", median);
printf("min: $%.2f ", min);
printf("max: $%.2f ", max);
printf("variance: $%.2f ", variance);
printf("mean: $%.2f ", mean);
}
io.h
#ifndef IO_H
#define IO_H
void read_array(float array[], int size);
void print_greeting(void);
void print_array(const float array[], int size);
void print_results(const float array[], int size, float median, float min, float max, float mean, float variance);
#endif
utils.c
#include
#include
#include
#include "utils.h"
int get_num_tokens(const char str[], char delim)
{
int i = 0,count = 1;
while (str[i] != '')
{
if (str[i++] == delim)
count++;
}
return count;
}
void get_tokens_array(const char str[],float array[], float size, char delim)
{
char *input = const_cast(str);
char *d = &delim;
char *token = strtok(input, d);
int i = 0;
while (token != NULL)
{
printf(" token # %d is %s ", i+1, token);
array[i++] = atof(token);
token = strtok(NULL, d);
}
size = i;
}
Utils.h
#ifndef UTILS_H
#define UTILS_H
int get num tokens(const char str[], char delim);
void get tokens array(const char str[], float array[], float size, char delim);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.