mirror of
https://github.com/rmontanana/mdlp.git
synced 2025-08-18 00:45:57 +00:00
Reformat some test files
This commit is contained in:
@@ -7,43 +7,35 @@ using namespace std;
|
|||||||
|
|
||||||
ArffFiles::ArffFiles() = default;
|
ArffFiles::ArffFiles() = default;
|
||||||
|
|
||||||
vector<string> ArffFiles::getLines() const
|
vector<string> ArffFiles::getLines() const {
|
||||||
{
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long int ArffFiles::getSize() const
|
unsigned long int ArffFiles::getSize() const {
|
||||||
{
|
|
||||||
return lines.size();
|
return lines.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<string, string>> ArffFiles::getAttributes() const
|
vector<pair<string, string>> ArffFiles::getAttributes() const {
|
||||||
{
|
|
||||||
return attributes;
|
return attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ArffFiles::getClassName() const
|
string ArffFiles::getClassName() const {
|
||||||
{
|
|
||||||
return className;
|
return className;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ArffFiles::getClassType() const
|
string ArffFiles::getClassType() const {
|
||||||
{
|
|
||||||
return classType;
|
return classType;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<vector<float>>& ArffFiles::getX()
|
vector<vector<float>> &ArffFiles::getX() {
|
||||||
{
|
|
||||||
return X;
|
return X;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<int>& ArffFiles::getY()
|
vector<int> &ArffFiles::getY() {
|
||||||
{
|
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArffFiles::load(const string& fileName, bool classLast)
|
void ArffFiles::load(const string &fileName, bool classLast) {
|
||||||
{
|
|
||||||
ifstream file(fileName);
|
ifstream file(fileName);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
throw invalid_argument("Unable to open file");
|
throw invalid_argument("Unable to open file");
|
||||||
@@ -87,8 +79,7 @@ void ArffFiles::load(const string& fileName, bool classLast)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArffFiles::generateDataset(bool classLast)
|
void ArffFiles::generateDataset(bool classLast) {
|
||||||
{
|
|
||||||
X = vector<vector<float>>(attributes.size(), vector<float>(lines.size()));
|
X = vector<vector<float>>(attributes.size(), vector<float>(lines.size()));
|
||||||
auto yy = vector<string>(lines.size(), "");
|
auto yy = vector<string>(lines.size(), "");
|
||||||
int labelIndex = classLast ? static_cast<int>(attributes.size()) : 0;
|
int labelIndex = classLast ? static_cast<int>(attributes.size()) : 0;
|
||||||
@@ -108,16 +99,14 @@ void ArffFiles::generateDataset(bool classLast)
|
|||||||
y = factorize(yy);
|
y = factorize(yy);
|
||||||
}
|
}
|
||||||
|
|
||||||
string ArffFiles::trim(const string& source)
|
string ArffFiles::trim(const string &source) {
|
||||||
{
|
|
||||||
string s(source);
|
string s(source);
|
||||||
s.erase(0, s.find_first_not_of(" \n\r\t"));
|
s.erase(0, s.find_first_not_of(" \n\r\t"));
|
||||||
s.erase(s.find_last_not_of(" \n\r\t") + 1);
|
s.erase(s.find_last_not_of(" \n\r\t") + 1);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<int> ArffFiles::factorize(const vector<string>& labels_t)
|
vector<int> ArffFiles::factorize(const vector<string> &labels_t) {
|
||||||
{
|
|
||||||
vector<int> yy;
|
vector<int> yy;
|
||||||
yy.reserve(labels_t.size());
|
yy.reserve(labels_t.size());
|
||||||
map<string, int> labelMap;
|
map<string, int> labelMap;
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "../Metrics.h"
|
#include "../Metrics.h"
|
||||||
|
|
||||||
|
|
||||||
namespace mdlp {
|
namespace mdlp {
|
||||||
class TestMetrics : public Metrics, public testing::Test {
|
class TestMetrics : public Metrics, public testing::Test {
|
||||||
public:
|
public:
|
||||||
@@ -11,22 +10,19 @@ namespace mdlp {
|
|||||||
|
|
||||||
TestMetrics() : Metrics(y_, indices_) {};
|
TestMetrics() : Metrics(y_, indices_) {};
|
||||||
|
|
||||||
void SetUp() override
|
void SetUp() override {
|
||||||
{
|
|
||||||
setData(y_, indices_);
|
setData(y_, indices_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(TestMetrics, NumClasses)
|
TEST_F(TestMetrics, NumClasses) {
|
||||||
{
|
|
||||||
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
||||||
EXPECT_EQ(1, computeNumClasses(4, 8));
|
EXPECT_EQ(1, computeNumClasses(4, 8));
|
||||||
EXPECT_EQ(2, computeNumClasses(0, 10));
|
EXPECT_EQ(2, computeNumClasses(0, 10));
|
||||||
EXPECT_EQ(2, computeNumClasses(8, 10));
|
EXPECT_EQ(2, computeNumClasses(8, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TestMetrics, Entropy)
|
TEST_F(TestMetrics, Entropy) {
|
||||||
{
|
|
||||||
EXPECT_EQ(1, entropy(0, 10));
|
EXPECT_EQ(1, entropy(0, 10));
|
||||||
EXPECT_EQ(0, entropy(0, 5));
|
EXPECT_EQ(0, entropy(0, 5));
|
||||||
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
||||||
@@ -34,8 +30,7 @@ namespace mdlp {
|
|||||||
ASSERT_NEAR(0.468996f, entropy(0, 10), precision);
|
ASSERT_NEAR(0.468996f, entropy(0, 10), precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TestMetrics, InformationGain)
|
TEST_F(TestMetrics, InformationGain) {
|
||||||
{
|
|
||||||
ASSERT_NEAR(1, informationGain(0, 5, 10), precision);
|
ASSERT_NEAR(1, informationGain(0, 5, 10), precision);
|
||||||
ASSERT_NEAR(1, informationGain(0, 5, 10), precision); // For cache
|
ASSERT_NEAR(1, informationGain(0, 5, 10), precision); // For cache
|
||||||
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1};
|
||||||
|
Reference in New Issue
Block a user