Commit f734efd2 authored by Florian GAURIER's avatar Florian GAURIER
Browse files

Merge remote-tracking branch 'origin/master'

parents 9d7009cc 15d0415e
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix, classification_report
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
class RFgenerator :
def __init__(self, train_path="../cmake-build-debug/Extraction/train_features.csv", test_path="../cmake-build-debug/Extraction/test_features.csv"):
self.train_data = self.loadData(train_path)
self.test_data = self.loadData(test_path)
rd = RandomForestClassifier(max_leaf_nodes=None, max_depth=None, n_estimators=100, criterion='gini')
self.RandomForestClassifier = Pipeline([("scaler", StandardScaler()), ("rd", rd)])
self.RandomForestClassifier.fit(self.train_data["data"], self.train_data["labels"])
self.ypred = self.RandomForestClassifier.predict(self.test_data["data"])
self.cm = confusion_matrix(self.test_data["labels"], self.ypred)
self.cr = classification_report(self.test_data["labels"], self.ypred)
ConfusionMatrixDisplay(self.cm)
self.strScore()
def loadData(self, path):
"""Loads data"""
# Loading
file = pd.read_csv(path, sep=',')
data = dict()
data["data"] = np.array(file.values[:, :-2], dtype=float)
data["labels"] = file.values[:, -2].reshape((data["data"].shape[0], 1))
data["files"] = file.values[:, -1].reshape((data["data"].shape[0], 1))
return data
def strScore(self):
print("Confusion Matrix : \n", self.cm)
print("classification report : \n", self.cr)
def strParams(self):
print("Data mean : " + str(self.RandomForestClassifier["scaler"].mean_))
print("Data std : " + str(self.RandomForestClassifier["scaler"].scale_))
def code_generate(self):
with open("./params.h", "w") as svmdoth:
svmdoth.write("#ifndef EML_PARAMS_H\n")
svmdoth.write("#define EML_PARAMS_H\n")
svmdoth.write("\n")
svmdoth.write(self.np2cpp(self.RandomForestClassifier["rd"].classes_, "classes", "std::string") + ";\n")
svmdoth.write(self.np2cpp(self.RandomForestClassifier["scaler"].mean_, "mean") + ";\n")
svmdoth.write(self.np2cpp(self.RandomForestClassifier["scaler"].scale_, "sigma") + ";\n")
svmdoth.write(self.np2cpp(self.RandomForestClassifier["rd"].coef_, "rd_coefs") + ";\n")
svmdoth.write(self.np2cpp(self.RandomForestClassifier["rd"].intercept_, "rd_intercept") + ";\n")
svmdoth.write("\n")
svmdoth.write("#endif //EML_PARAMS_H")
def np2cppRec(self, x):
"""Recursive function to convert np array in C++. This recusrive function deals with data of the np array. The header is generated by np2cpp function"""
ret = "{"
for i in range(x.shape[0]):
if type(x[i]) == np.ndarray:
if i == x.shape[0] - 1:
ret += self.np2cppRec(x[i])[:-1]
else:
ret += self.np2cppRec(x[i])
else:
ret += str(x[i]) + ", "
ret = ret[:-2] + "}, "
return ret + "\n"
def np2cpp(self, x, name, type="real"):
"""Generates cpp code associated to a numpy array, no matter its dimensions. It generates the header of the c++ vector. np2cppRec deals with inside data."""
ret = "std::"
ret += "vector" + (x.ndim-1) * "<std::vector" + "<" + type + ">" + (x.ndim-1) * ">" + " " + name
ret += self.np2cppRec(x)[:-3]
return ret
if __name__ == "__main__":
RF = RFgenerator()
RF.strParams()
RF.code_generate()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment