Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Estelle ARRICAU
Embedded Machine Learning
Commits
2fe51128
Commit
2fe51128
authored
Jan 12, 2022
by
estellearrc
Browse files
poop
parent
d087fc1a
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
RF/Python/RF_training.py
View file @
2fe51128
...
...
@@ -205,6 +205,8 @@ def train_RF(saveAlgo=False, path=""):
X_val
,
labelInd_val
,
cv
=
5
)
scores_random_best_f1
=
cross_val_score
(
model_best
,
X_val
,
labelInd_val
,
cv
=
5
,
scoring
=
"f1_micro"
)
predicted_probas
=
model_best
.
predict_proba
(
X_test
)
print
(
predicted_probas
[
0
,
:])
# print("----------------Random Forest---------------------")
# print("Cross validation accuracy moyenne best:", scores_random_best.mean())
# print("Cross validation accuracy moyenne:", scores_random.mean())
...
...
@@ -220,22 +222,24 @@ def train_RF(saveAlgo=False, path=""):
# print(prediction_random_best)
# print(model_best.apply(X_train))
# print(model_best.decision_path(X_train))
for
e
in
model_best
.
estimators_
:
# print(e.tree_)
# print(e.classes_)
# print(e.n_classes_)
# print(e.n_outputs_)
report
=
export_text
(
e
)
# print(report)
#
for e in model_best.estimators_:
# print(e.tree_)
# print(e.classes_)
# print(e.n_classes_)
# print(e.n_outputs_)
#
report = export_text(e)
# print(report)
# help(sklearn.tree._tree.Tree)
file
=
path
+
'RF/RFTrained.h'
open
(
file
,
'w'
).
close
()
with
open
(
file
,
'a'
)
as
f
:
f
.
write
(
"#ifndef RFTRAINED_H
\n
#define RFTRAINED_H
\n
#include <string>
\n
#include <numeric>
\n
#include <fstream>
\n
#include <map>
\n
#include
\"
../Helpers/globals.h
\"\n
#include <typeinfo>
\n
"
)
f
.
write
(
"#ifndef RFTRAINED_H
\n
#define RFTRAINED_H
\n
#include <string>
\n
#include <numeric>
\n
#include <fstream>
\n
#include <map>
\n
#include
\"
../Helpers/globals.h
\"\n
#include <typeinfo>
\n
#include <vector>
\n
"
)
f
.
write
(
"const int N_ESTIMATORS = "
+
str
(
model_best
.
n_estimators
)
+
";
\n
const int MAX_DEPTH = "
+
str
(
model_best
.
max_depth
)
+
";
\n
"
)
# f.write("int RFModel(int num_estimator, std::map < FTYPE, DataVector > &features){\n") #hard voting
# soft voting
f
.
write
(
"
int
RFModel(int num_estimator, std::map < FTYPE, DataVector > &features){
\n
"
)
"
void
RFModel(int num_estimator, std::map < FTYPE, DataVector > &features
, std::vector<double> &class_predicted_probas
){
\n
"
)
f
.
write
(
"DataVector featureVector = features[FTYPE::BINAVG];
\n
featureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());
\n
"
)
f
.
close
()
...
...
@@ -243,13 +247,14 @@ def train_RF(saveAlgo=False, path=""):
# Write in cpp header file RFTrainedcpp.h
with
open
(
file
,
'a'
)
as
f
:
text
=
""
text
=
str_tree
(
text
,
e
,
model_best
.
n_estimators
,
0
,
1
)
text
=
str_tree
(
text
,
e
,
model_best
.
max_depth
,
0
,
1
)
f
.
write
(
"
\n
if(num_estimator == "
+
str
(
count_estimator
)
+
"){
\n
"
+
text
+
"}
\n
"
)
f
.
close
()
count_estimator
+=
1
with
open
(
file
,
'a'
)
as
f
:
f
.
write
(
"return 0;}
\n
#endif"
)
# f.write("return 0;}\n#endif")
f
.
write
(
"}
\n
#endif"
)
f
.
close
()
print
(
"Save algo in "
+
path
+
"/RF/RFTrained.h file!"
)
return
model_best
,
history_best
...
...
@@ -281,9 +286,13 @@ def str_tree(text, decision_tree, max_depth, node, depth):
value
=
None
if
tree_
.
n_outputs
==
1
:
value
=
tree_
.
value
[
node
][
0
]
# print(value)
# print(str(value)[1:-2].replace(".", ".,"))
else
:
value
=
tree_
.
value
[
node
].
T
[
0
]
# print(value)
class_name
=
np
.
argmax
(
value
)
# print(class_name)
if
(
tree_
.
n_classes
[
0
]
!=
1
and
tree_
.
n_outputs
==
1
):
...
...
@@ -300,13 +309,19 @@ def str_tree(text, decision_tree, max_depth, node, depth):
"){
\n
"
+
str
(
left
)
+
"}
\n
"
secondeLayerELSE
=
"else{
\n
"
+
str
(
right
)
+
"}
\n
"
text
+=
secondeLayerIF
+
secondeLayerELSE
print
(
text
)
#
print(text)
else
:
# leaf
text
+=
"return "
+
str
(
int
(
class_name
))
+
";
\n
"
# text += "return " + str(int(class_name))+";\n" # hard voting
# soft voting
text
+=
"class_predicted_probas = {"
+
\
str
(
value
)[
1
:
-
2
].
replace
(
"."
,
".,"
)
+
"};
\n
"
else
:
subtree_depth
=
compute_depth
(
tree_
,
node
)
if
subtree_depth
==
1
:
text
+=
"return "
+
str
(
int
(
class_name
))
+
";
\n
"
# text += "return " + str(int(class_name))+";\n" # hard voting
# soft voting
text
+=
"class_predicted_probas = {"
+
\
str
(
value
)[
1
:
-
2
].
replace
(
"."
,
".,"
)
+
"};
\n
"
# print(text)
return
text
...
...
RF/RFTrained.h
View file @
2fe51128
This diff is collapsed.
Click to expand it.
RF/apply_RF.h
View file @
2fe51128
...
...
@@ -5,40 +5,84 @@
#include "../Helpers/evaluate_model.h"
#include <string>
#include <iostream>
#include <vector>
// #include <vector>
#include <cmath>
std
::
string
majorityVoting
(
std
::
vector
<
int
>
&
results
)
std
::
string
majorityVoting
(
std
::
vector
<
std
::
vector
<
double
>
>
&
results
)
{
std
::
vector
<
float
>
probas
(
CLASS_N
,
0
.
0
);
for
(
int
&
r
:
results
)
// soft voting (sum of probas) vs hard voting (majority vote)
std
::
vector
<
double
>
probas
(
CLASS_N
,
0
.
0
);
for
(
int
j
=
0
;
j
<
CLASS_N
;
j
++
)
{
probas
[
r
]
+=
1
.
0
/
N_ESTIMATORS
;
for
(
int
i
=
0
;
i
<
N_ESTIMATORS
;
i
++
)
probas
[
j
]
+=
results
[
i
][
j
]
/
CLASS_N
;
}
float
max
=
*
max_element
(
probas
.
begin
(),
probas
.
end
());
double
max
=
*
max_element
(
probas
.
begin
(),
probas
.
end
());
// std::cout<<"max ="<<max<<std::endl;
std
::
string
class_name
=
""
;
std
::
cout
<<
"------------------------------"
<<
std
::
endl
;
//
std::cout<<"------------------------------"<<std::endl;
double
vote
=
0
.
0
;
for
(
int
k
=
0
;
k
<
CLASS_N
;
k
++
)
{
std
::
cout
<<
probas
[
k
]
<<
std
::
endl
;
//
std::cout<<probas[k]<<std::endl;
if
(
probas
[
k
]
==
max
)
class_name
=
genres
[
k
];
{
// std::cout<<k<<std::endl;
class_name
=
genres
[
k
];
}
}
return
class_name
;
}
// std::string majorityVoting(std::vector<int> &results)
// {
// // soft voting (sum of probas) vs hard voting (majority vote)
// std::vector<double> probas(CLASS_N, 0.0);
// for (auto &r : results)
// {
// probas[r] += 1.0 / N_ESTIMATORS;
// }
// double max = *max_element(probas.begin(), probas.end());
// // std::cout<<"max ="<<max<<std::endl;
// std::string class_name = "";
// std::cout<<"------------------------------"<<std::endl;
// double vote = 0.0;
// for (int k = 0; k < CLASS_N; k++)
// {
// vote += probas[k] / double(CLASS_N); //soft voting
// std::cout<<vote<<std::endl;
// // std::cout<<probas[k]<<std::endl;
// if (probas[k] == max) //hard voting
// class_name = genres[k];
// }
// vote = round(vote*100.0);
// // std::cout<<vote<<std::endl;
// int class_k = int(vote);
// class_name = genres[class_k];
// return class_name;
// }
std
::
string
RFAlgo
(
std
::
map
<
FTYPE
,
DataVector
>
&
features
)
{
std
::
vector
<
int
>
results
;
// std::vector<int> results;
std
::
vector
<
std
::
vector
<
double
>>
results
;
std
::
vector
<
double
>
res
(
CLASS_N
,
0
.
0
);
for
(
int
k
=
0
;
k
<
N_ESTIMATORS
;
k
++
)
{
int
res
=
RFModel
(
k
,
features
);
// int res = RFModel(k, features, results);
RFModel
(
k
,
features
,
res
);
double
sum
=
std
::
accumulate
(
res
.
begin
(),
res
.
end
(),
0
.);
std
::
transform
(
res
.
begin
(),
res
.
end
(),
res
.
begin
(),
[
sum
](
double
c
){
return
c
/
sum
;
});
results
.
push_back
(
res
);
// std::cout << res << " ";
// for(double &p : res)
// std::cout << p << " ";
// std::cout <<" "<<std::endl;
}
std
::
string
result
=
majorityVoting
(
results
);
// std::cout <<result<<std::endl;
return
result
;
}
...
...
RF/main.cpp
View file @
2fe51128
...
...
@@ -13,7 +13,7 @@ int main(int argc, char **argv)
std
::
cout
<<
"Compute accuracy...
\n
"
;
real
accuracy
=
compute_accuracy_CART
(
"../../RF/file_test.csv"
,
(
vFunctionCallCART
)
RFAlgo
,
matrix
);
std
::
cout
<<
"Accuracy: "
<<
accuracy
<<
"
\n
"
;
std
::
cout
<<
"Confusion matrix:
\n
"
;
print_2D_array
(
matrix
);
//
std::cout << "Confusion matrix: \n";
//
print_2D_array(matrix);
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment