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
fea31cb7
Commit
fea31cb7
authored
Jan 11, 2022
by
hugopiq
Browse files
ok
parent
bc46e86c
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
CART/CARTTrained.h
View file @
fea31cb7
This diff is collapsed.
Click to expand it.
CART/Python/CART_Training.py
View file @
fea31cb7
...
...
@@ -4,45 +4,11 @@ from sklearn.model_selection import train_test_split
import
pandas
as
pd
from
matplotlib
import
pyplot
as
plt
from
sklearn
import
tree
from
sklearn.metrics
import
ConfusionMatrixDisplay
,
confusion_matrix
from
sklearn.metrics
import
ConfusionMatrixDisplay
,
confusion_matrix
,
accuracy_score
import
graphviz
from
CART
import
CartTree
def
trainCartCropRec
():
# Extract dataset
dataset
=
'../../DATASETS/croprec.csv'
crop_df
=
pd
.
read_csv
(
dataset
,
header
=
0
)
Y
=
crop_df
.
label
.
values
features
=
crop_df
.
columns
.
values
[:
-
1
]
crop_df
=
crop_df
.
drop
(
'label'
,
axis
=
1
)
X
=
crop_df
.
values
classes
=
np
.
unique
(
Y
)
print
(
features
,
classes
)
print
(
classes
.
shape
)
# TRAIN/TEST SPLIT
X_train
,
X_test
,
Y_train
,
Y_test
=
train_test_split
(
X
,
Y
,
test_size
=
0.33
,
random_state
=
42
)
X_test
,
X_val
,
Y_test
,
Y_val
=
train_test_split
(
X_test
,
Y_test
,
test_size
=
0.33
,
random_state
=
42
)
X_train
=
np
.
delete
(
X_train
,
[
-
1
,
-
2
],
axis
=
1
)
X_val
=
np
.
delete
(
X_val
,
[
-
1
,
-
2
],
axis
=
1
)
new_df
=
pd
.
DataFrame
(
X_test
)
files
=
new_df
.
iloc
[:,
-
2
:]
files
.
columns
=
[
"Classes"
,
"Paths"
]
save_file_as_csv
(
files
,
"CART"
)
X_test
=
np
.
delete
(
X_test
,
[
-
1
,
-
2
],
axis
=
1
)
# My CART_TREES TREE
print
(
"Training !"
)
myclassifier
=
CartTree
(
max_depth
=
12
)
myclassifier
.
fit
(
X_train
,
Y_train
)
print
(
myclassifier
)
def
trainCart
(
comparisonSKLearn
=
False
):
dataset
=
"build/Extraction/features.csv"
df
=
pd
.
read_csv
(
dataset
,
header
=
0
)
...
...
@@ -62,7 +28,6 @@ def trainCart(comparisonSKLearn=False):
files
=
new_df
.
iloc
[:,
-
2
:]
files
.
columns
=
[
"Classes"
,
"Paths"
]
save_file_as_csv
(
files
,
"CART"
)
print
(
"Splitting test, train"
,
X_train
.
shape
,
X_test
.
shape
)
# My CART_TREES TREE
print
(
"Training !"
)
...
...
@@ -70,38 +35,39 @@ def trainCart(comparisonSKLearn=False):
myclassifier
.
fit
(
X_train
,
Y_train
)
# Write in cpp header file CARTTrainedcpp.h
write_model
(
myclassifier
)
# Print confusion matrix
Y_pred
=
myclassifier
.
predict
(
X_test
)
print
(
confusion_matrix
(
Y_test
,
Y_pred
,
labels
=
classes
))
# print()
# Comparison with SKLearn:
if
comparisonSKLearn
:
# SKLEARN CART_TREES TREE
clf
=
tree
.
DecisionTreeClassifier
(
random_state
=
None
,
max_depth
=
12
)
clf
=
clf
.
fit
(
X_train
,
Y_train
)
dot_data
=
tree
.
export_graphviz
(
clf
,
out_file
=
None
,
feature_names
=
features
,
class_names
=
classes
,
filled
=
True
,
rounded
=
True
,
special_characters
=
True
)
graph
=
graphviz
.
Source
(
dot_data
)
graph
.
render
(
"rCrop recommandations"
)
#
dot_data = tree.export_graphviz(clf,
#
out_file=None,
#
feature_names=features,
#
class_names=classes,
#
filled=True,
#
rounded=True,
#
special_characters=True)
#
graph = graphviz.Source(dot_data)
#
graph.render("rCrop recommandations")
# PREDICTIONS
Y_pred
=
myclassifier
.
predict
(
X_test
)
print
(
"accuracy :"
,
accuracy_score
(
Y_test
,
Y_pred
))
print
(
confusion_matrix
(
Y_test
,
Y_pred
,
labels
=
classes
))
ConfusionMatrixDisplay
.
from_predictions
(
Y_test
,
Y_pred
,
display_labels
=
classes
)
plt
.
title
(
"My predictions"
)
plt
.
show
()
Y_pred
=
clf
.
predict
(
X_test
)
ConfusionMatrixDisplay
.
from_predictions
(
Y_test
,
Y_pred
,
display_labels
=
classes
)
plt
.
title
(
"SKLEARN"
)
plt
.
show
()
# Y_pred = clf.predict(X_test)
# print("accuracy :", accuracy_score(Y_test, Y_pred))
# ConfusionMatrixDisplay.from_predictions(
# Y_test, Y_pred, display_labels=classes)
# plt.title("SKLEARN")
# plt.show()
def
write_model
(
classifier
):
...
...
@@ -114,7 +80,6 @@ def write_model(classifier):
def
save_file_as_csv
(
data
,
name_folder
):
genres
=
data
[
"Classes"
].
tolist
()
paths
=
data
[
"Paths"
].
tolist
()
print
(
genres
)
...
...
@@ -125,5 +90,4 @@ def save_file_as_csv(data, name_folder):
if
__name__
==
"__main__"
:
# trainCartCropRec()
trainCart
(
False
)
trainCart
(
comparisonSKLearn
=
True
)
CART/main.cpp
View file @
fea31cb7
...
...
@@ -9,17 +9,16 @@ int main(int argc, char **argv)
if
(
argc
>
1
)
{
file_path
=
argv
[
1
];
auto
start_chrono
=
std
::
chrono
::
high_resolution_clock
::
now
();
std
::
map
<
FTYPE
,
DataVector
>
features
=
compute_features_for
(
file_path
);
std
::
string
result2
=
cartModel
(
features
);
auto
stop_chrono
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
duration
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
stop_chrono
-
start_chrono
);
std
::
cout
<<
"Predicted genre (version weight saved): "
<<
result2
<<
" with "
<<
duration
.
count
()
<<
" microseconds"
<<
std
::
endl
;
}
else
{
file_path
=
"/home/hugo/Documents/embedded-machine-learning/DATASETS/genres/disco/disco.00010.au"
;
}
std
::
map
<
FTYPE
,
DataVector
>
features
=
compute_features_for
(
file_path
);
std
::
string
result
=
cartModel
(
features
);
std
::
cout
<<
"Compute accuracy...
\n
"
;
real
accuracy
=
compute_accuracy_CART
(
"/home/hugo/Documents/embedded-machine-learning/CART/file_test.csv"
,
(
vFunctionCallCART
)
cartModel
,
matrix
);
std
::
cout
<<
"Accuracy: "
<<
accuracy
<<
"
\n
"
;
std
::
cout
<<
result
<<
std
::
endl
;
std
::
cout
<<
"Confusion matrix:
\n
"
;
print_2D_array
(
matrix
);
return
0
;
...
...
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