|
| 1 | +#include <stdio.h> |
| 2 | +#include <math.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +#define num_sample 5 |
| 6 | +#define num_class 2 |
| 7 | + |
| 8 | +double calculate_gini_index(int cls_label[], int num_samples, int num_classes) |
| 9 | +{ |
| 10 | + double gini = 1.0; |
| 11 | + |
| 12 | + int cls_count[num_classes]; |
| 13 | + for (int i = 0; i < num_classes; i++) |
| 14 | + { |
| 15 | + cls_count[i] = 0; |
| 16 | + } |
| 17 | + |
| 18 | + for (int i = 0; i < num_samples; i++) |
| 19 | + { |
| 20 | + cls_count[cls_label[i] - 1]++; |
| 21 | + } |
| 22 | + for (int i = 0; i < num_classes; i++) |
| 23 | + { |
| 24 | + double p = (double)cls_count[i] / num_samples; |
| 25 | + gini -= p * p; |
| 26 | + } |
| 27 | + |
| 28 | + return gini; |
| 29 | +} |
| 30 | + |
| 31 | +double calculate_information_gain(int *class_labels, int *feature_values, int num_samples, int threshold, int num_classes) |
| 32 | +{ |
| 33 | + int left_count = 0; |
| 34 | + int right_count = 0; |
| 35 | + int *left_class_counts = (int *)malloc(num_classes * sizeof(int)); |
| 36 | + int *right_class_counts = (int *)malloc(num_classes * sizeof(int)); |
| 37 | + |
| 38 | + for (int i = 0; i < num_classes; ++i) |
| 39 | + { |
| 40 | + left_class_counts[i] = 0; |
| 41 | + right_class_counts[i] = 0; |
| 42 | + } |
| 43 | + |
| 44 | + for (int i = 0; i < num_samples; i++) |
| 45 | + { |
| 46 | + if (feature_values[i] <= threshold) |
| 47 | + { |
| 48 | + left_count++; |
| 49 | + left_class_counts[class_labels[i] - 1]++; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + right_count++; |
| 54 | + right_class_counts[class_labels[i] - 1]++; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + double total_gini = calculate_gini_index(class_labels, num_samples, num_classes); |
| 59 | + double left_gini = calculate_gini_index(left_class_counts, left_count, num_classes); |
| 60 | + double right_gini = calculate_gini_index(right_class_counts, right_count, num_classes); |
| 61 | + |
| 62 | + double p_left = (double)left_count / num_samples; |
| 63 | + double p_right = (double)right_count / num_samples; |
| 64 | + |
| 65 | + // Calculate information gain using Gini index |
| 66 | + double information_gain = total_gini - (p_left * left_gini + p_right * right_gini); |
| 67 | + |
| 68 | + // Free allocated memory |
| 69 | + free(left_class_counts); |
| 70 | + free(right_class_counts); |
| 71 | + |
| 72 | + return information_gain; |
| 73 | +} |
| 74 | + |
| 75 | +int main() |
| 76 | +{ |
| 77 | + int data[4][3] = { |
| 78 | + {5, 10, 15}, |
| 79 | + {50, 55, 60}, |
| 80 | + {100, 150, 200}, |
| 81 | + {250, 300, 350}}; |
| 82 | + |
| 83 | + int class_labels[4] = {1, 1, 2, 2}; |
| 84 | + int num_samples = 4; |
| 85 | + int num_classes = 2; |
| 86 | + int num_features = 3; |
| 87 | + |
| 88 | + for (int i = 0; i < num_features; i++) |
| 89 | + { |
| 90 | + double gini_index = calculate_gini_index(class_labels, num_sample, num_class); |
| 91 | + printf("attribute %d gini index : %lf\n", i, gini_index); |
| 92 | + } |
| 93 | + |
| 94 | + // for (int i = 0; i < num_features; i++) |
| 95 | + // { |
| 96 | + // int *feature_values = (int *)malloc(num_samples * sizeof(int)); // Allocate memory for feature values |
| 97 | + // for (int j = 0; j < num_samples; j++) |
| 98 | + // { |
| 99 | + // feature_values[j] = data[j][i]; // Copy the values of the i-th column to the feature values array |
| 100 | + // } |
| 101 | + // for (int threshold = 0; threshold < num_samples; threshold++) // Iterate over thresholds for each feature |
| 102 | + // { |
| 103 | + // double info_gain = calculate_information_gain(class_labels, feature_values, num_samples, data[threshold][i], num_classes); |
| 104 | + |
| 105 | + // printf("Feature %d, Threshold %d: Information Gain = %lf\n", i, data[threshold][i], info_gain); |
| 106 | + // } |
| 107 | + // free(feature_values); // Free allocated memory |
| 108 | + // } |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
0 commit comments