Begin Test Folding

This commit is contained in:
2023-10-06 17:08:54 +02:00
parent b9e0028e9d
commit 17e079edd5
10 changed files with 250 additions and 55 deletions

View File

@@ -34,7 +34,7 @@ namespace bayesnet {
void Graph::kruskal_algorithm()
{
// sort the edges ordered on decreasing weight
sort(G.begin(), G.end(), [](const auto& left, const auto& right) {return left.first > right.first;});
stable_sort(G.begin(), G.end(), [](const auto& left, const auto& right) {return left.first > right.first;});
for (int i = 0; i < G.size(); i++) {
int uSt, vEd;
uSt = find_set(G[i].second.first);

View File

@@ -9,6 +9,7 @@ add_executable(b_main main.cc Folding.cc Experiment.cc Datasets.cc Dataset.cc Mo
add_executable(b_manage manage.cc Results.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc)
add_executable(b_list list.cc Datasets.cc Dataset.cc)
add_executable(b_best best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc ExcelFile.cc)
add_executable(testx testx.cpp Datasets.cc Dataset.cc Folding.cc)
target_link_libraries(b_main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(b_manage "${TORCH_LIBRARIES}" libxlsxwriter.so ArffFiles mdlp stdc++fs)
@@ -17,4 +18,5 @@ else()
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp)
target_link_libraries(b_best Boost::boost "${XLSXWRITER_LIB}")
endif()
target_link_libraries(b_list ArffFiles mdlp "${TORCH_LIBRARIES}")
target_link_libraries(b_list ArffFiles mdlp "${TORCH_LIBRARIES}")
target_link_libraries(testx ArffFiles mdlp "${TORCH_LIBRARIES}")

View File

@@ -47,6 +47,7 @@ namespace platform {
{
stratified_indices = vector<vector<int>>(k);
int fold_size = n / k;
cout << "Fold SIZE: " << fold_size << endl;
// Compute class counts and indices
auto class_indices = map<int, vector<int>>();
vector<int> class_counts(*max_element(y.begin(), y.end()) + 1, 0);
@@ -64,16 +65,20 @@ namespace platform {
if (num_samples_to_take == 0)
continue;
auto remainder_samples_to_take = class_counts[label] % k;
cout << "Remainder samples to take: " << remainder_samples_to_take << endl;
for (auto fold = 0; fold < k; ++fold) {
auto it = next(class_indices[label].begin(), num_samples_to_take);
move(class_indices[label].begin(), it, back_inserter(stratified_indices[fold])); // ##
class_indices[label].erase(class_indices[label].begin(), it);
}
auto chosen = vector<bool>(k, false);
while (remainder_samples_to_take > 0) {
int fold = (rand() % static_cast<int>(k));
if (stratified_indices[fold].size() == fold_size + 1) {
if (chosen.at(fold)) {
continue;
}
chosen[k] = true;
cout << "One goes to fold " << fold << " that had " << stratified_indices[fold].size() << " elements before" << endl;
auto it = next(class_indices[label].begin(), 1);
stratified_indices[fold].push_back(*class_indices[label].begin());
class_indices[label].erase(class_indices[label].begin(), it);

65
src/Platform/testx.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Folding.h"
#include "map"
#include "Datasets.h"
#include <map>
#include <iostream>
#include <sstream>
using namespace std;
using namespace platform;
string counts(vector<int> y, vector<int> indices)
{
auto result = map<int, int>();
stringstream oss;
for (auto i = 0; i < indices.size(); ++i) {
result[y[indices[i]]]++;
}
string final_result = "";
for (auto i = 0; i < result.size(); ++i)
oss << i << " -> " << setprecision(2) << fixed
<< (double)result[i] * 100 / indices.size() << "% (" << result[i] << ") //";
oss << endl;
return oss.str();
}
int main()
{
map<string, string> balance = {
{"iris", "33,33% (50) / 33,33% (50) / 33,33% (50)"},
{"diabetes", "34,90% (268) / 65,10% (500)"},
{"ecoli", "42,56% (143) / 22,92% (77) / 0,60% (2) / 0,60% (2) / 10,42% (35) / 5,95% (20) / 1,49% (5) / 15,48% (52)"},
{"glass", "32,71% (70) / 7,94% (17) / 4,21% (9) / 35,51% (76) / 13,55% (29) / 6,07% (13)"}
};
for (const auto& file_name : { "iris", "glass", "ecoli", "diabetes" }) {
auto dt = Datasets(true, "Arff");
auto [X, y] = dt.getVectors(file_name);
//auto fold = KFold(5, 150);
auto fold = StratifiedKFold(5, y, -1);
cout << "***********************************************************************************************" << endl;
cout << "Dataset: " << file_name << endl;
cout << "Nº Samples: " << dt.getNSamples(file_name) << endl;
cout << "Class states: " << dt.getNClasses(file_name) << endl;
cout << "Balance: " << balance.at(file_name) << endl;
for (int i = 0; i < 5; ++i) {
cout << "Fold: " << i << endl;
auto [train, test] = fold.getFold(i);
cout << "Train: ";
cout << "(" << train.size() << "): ";
// for (auto j = 0; j < static_cast<int>(train.size()); j++)
// cout << train[j] << ", ";
cout << endl;
cout << "Train Statistics : " << counts(y, train);
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Test: ";
cout << "(" << test.size() << "): ";
// for (auto j = 0; j < static_cast<int>(test.size()); j++)
// cout << test[j] << ", ";
cout << endl;
cout << "Test Statistics: " << counts(y, test);
cout << "==============================================================================" << endl;
}
cout << "***********************************************************************************************" << endl;
}
}