-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomForest.c
81 lines (74 loc) · 2.4 KB
/
RandomForest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "RandomForest.h"
RandomForest *RandomForest_create(int numberOfTrees, Dataset *data, int maxDepth, float baggingProportion, float ferature_Bagging, float prunningThreshold, int GiniOrEntropy)
{
if (numberOfTrees <= 0 || !data || maxDepth <= 0 || baggingProportion <= 0 || prunningThreshold <= 0)
abort();
RandomForest *randomForest = (RandomForest *)calloc(1, sizeof(RandomForest));
randomForest->treeCount = numberOfTrees;
randomForest->classCount = data->classCount;
randomForest->trees = (DecisionTreeNode **)calloc(numberOfTrees, sizeof(DecisionTreeNode *));
#pragma omp parallel for num_threads(2)
for (int i = 0; i < numberOfTrees; i++)
{
bool *ferature_Bagg = Dataset_bagging_features(data, ferature_Bagging);
Subproblem *sp = Dataset_bagging(data, baggingProportion);
randomForest->trees[i] = DecisionTree_create(sp, 0, maxDepth, prunningThreshold, GiniOrEntropy, ferature_Bagg);
Subproblem_destroy(sp);
free(ferature_Bagg);
}
return randomForest;
}
int RandomForest_predict(RandomForest *rf, Instance *instance)
{
if (!rf || !instance)
{
// printf("Erreur de prédiction\n");
return -1;
}
int *votes = (int *)calloc(rf->classCount, sizeof(int));
for (int i = 0; i < rf->treeCount; i++)
{
int prediction = DecisionTree_predict(rf->trees[i], instance);
votes[prediction]++;
}
int majorityVote = 0;
int maxVotes = votes[0];
for (int i = 1; i < rf->classCount; i++)
{
if (votes[i] > maxVotes)
{
maxVotes = votes[i];
majorityVote = i;
}
}
free(votes);
return majorityVote;
}
float RandomForest_evaluate(RandomForest *rf, Dataset *data)
{
int correctPredictions = 0;
for (int i = 0; i < data->instanceCount; i++)
{
if (RandomForest_predict(rf, &data->instances[i]) == data->instances[i].classID)
correctPredictions++;
}
return (float)correctPredictions / (float)data->instanceCount;
}
int RandomForest_nodeCount(RandomForest *rf)
{
int totalNodes = 0;
for (int i = 0; i < rf->treeCount; i++)
{
totalNodes += Decision_nodeCount(rf->trees[i]);
}
return totalNodes;
}
void RandomForest_destroy(RandomForest *rf)
{
for (int i = 0; i < rf->treeCount; i++)
{
DecisionTree_destroy(rf->trees[i]);
}
free(rf->trees);
free(rf);
}