From 7821d46651281e3bce24569f22166196374a013a Mon Sep 17 00:00:00 2001 From: Michael Rapp Date: Mon, 25 Sep 2023 14:54:30 +0200 Subject: [PATCH 1/3] Change visibility of the member "numValues_" of the class NominalFeatureVector to protected. --- .../include/mlrl/common/input/feature_vector_nominal.hpp | 6 ++++-- .../common/src/mlrl/common/input/feature_vector_nominal.cpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp index 753922acd6..296b3a01c3 100644 --- a/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp +++ b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp @@ -18,10 +18,12 @@ class NominalFeatureVector : public AbstractFeatureVector { uint32* indptr_; - const uint32 numValues_; - const int32 majorityValue_; + protected: + + const uint32 numValues_; + public: /** diff --git a/cpp/subprojects/common/src/mlrl/common/input/feature_vector_nominal.cpp b/cpp/subprojects/common/src/mlrl/common/input/feature_vector_nominal.cpp index f4fead5202..458b5a3c3a 100644 --- a/cpp/subprojects/common/src/mlrl/common/input/feature_vector_nominal.cpp +++ b/cpp/subprojects/common/src/mlrl/common/input/feature_vector_nominal.cpp @@ -2,7 +2,7 @@ NominalFeatureVector::NominalFeatureVector(uint32 numValues, uint32 numElements, int32 majorityValue) : values_(new int32[numValues]), indices_(new uint32[numElements]), indptr_(new uint32[numValues + 1]), - numValues_(numValues), majorityValue_(majorityValue) { + majorityValue_(majorityValue), numValues_(numValues) { indptr_[numValues] = numElements; } From 85315e63cded7b336399f245a67d9573f126a721 Mon Sep 17 00:00:00 2001 From: Michael Rapp Date: Mon, 25 Sep 2023 14:54:41 +0200 Subject: [PATCH 2/3] Add class OrdinalFeatureVector. --- .../common/input/feature_vector_ordinal.hpp | 61 +++++++++++++++++++ cpp/subprojects/common/meson.build | 1 + .../common/input/feature_vector_ordinal.cpp | 24 ++++++++ 3 files changed, 86 insertions(+) create mode 100644 cpp/subprojects/common/include/mlrl/common/input/feature_vector_ordinal.hpp create mode 100644 cpp/subprojects/common/src/mlrl/common/input/feature_vector_ordinal.cpp diff --git a/cpp/subprojects/common/include/mlrl/common/input/feature_vector_ordinal.hpp b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_ordinal.hpp new file mode 100644 index 0000000000..ae56c0516c --- /dev/null +++ b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_ordinal.hpp @@ -0,0 +1,61 @@ +/* + * @author Michael Rapp (michael.rapp.ml@gmail.com) + */ +#pragma once + +#include "mlrl/common/input/feature_vector_nominal.hpp" + +/** + * A feature vector that stores the indices of the examples that are associated with each value, except for the majority + * value, i.e., the most frequent value, of an ordinal feature. + */ +class OrdinalFeatureVector : public NominalFeatureVector { + private: + + uint32* order_; + + public: + + /** + * @param numValues The number of distinct values of the ordinal feature, excluding the majority value + * @param numElements The number of elements in the vector, i.e., the number of examples not associated with + * the majority value + * @param majorityValue The majority value, i.e., the most frequent value, of the ordinal feature + */ + OrdinalFeatureVector(uint32 numValues, uint32 numElements, int32 majorityValue); + + ~OrdinalFeatureVector() override; + + /** + * Returns an `index_iterator` to the beginning of the ordered indices of the values of the ordinal feature. + * + * @param index The index of the value + * @return An `index_iterator` to the beginning + */ + index_iterator order_begin(uint32 index); + + /** + * Returns an `index_iterator` to the end of the ordered indices of the values of the ordinal feature. + * + * @param index The index of the value + * @return An `index_iterator` to the end + */ + index_iterator order_end(uint32 index); + + /** + * Returns an `index_const_iterator` to the beginning of the ordered indices of the values of the ordinal + * feature. + * + * @param index The index of the value + * @return An `index_const_iterator` to the beginning + */ + index_const_iterator order_cbegin(uint32 index) const; + + /** + * Returns an `index_const_iterator` to the end of the ordered indices of the values of the ordinal feature. + * + * @param index The index of the value + * @return An `index_const_iterator` to the end + */ + index_const_iterator order_cend(uint32 index) const; +}; diff --git a/cpp/subprojects/common/meson.build b/cpp/subprojects/common/meson.build index 88d0ea250d..a69821a49e 100644 --- a/cpp/subprojects/common/meson.build +++ b/cpp/subprojects/common/meson.build @@ -45,6 +45,7 @@ source_files = [ 'src/mlrl/common/input/feature_vector_equal.cpp', 'src/mlrl/common/input/feature_vector_nominal.cpp', 'src/mlrl/common/input/feature_vector_numerical.cpp', + 'src/mlrl/common/input/feature_vector_ordinal.cpp', 'src/mlrl/common/input/label_matrix_c_contiguous.cpp', 'src/mlrl/common/input/label_matrix_csc.cpp', 'src/mlrl/common/input/label_matrix_csr.cpp', diff --git a/cpp/subprojects/common/src/mlrl/common/input/feature_vector_ordinal.cpp b/cpp/subprojects/common/src/mlrl/common/input/feature_vector_ordinal.cpp new file mode 100644 index 0000000000..ba3bb98c65 --- /dev/null +++ b/cpp/subprojects/common/src/mlrl/common/input/feature_vector_ordinal.cpp @@ -0,0 +1,24 @@ +#include "mlrl/common/input/feature_vector_ordinal.hpp" + +OrdinalFeatureVector::OrdinalFeatureVector(uint32 numValues, uint32 numElements, int32 majorityValue) + : NominalFeatureVector(numValues, numElements, majorityValue), order_(new uint32[numValues]) {} + +OrdinalFeatureVector::~OrdinalFeatureVector() { + delete[] order_; +} + +OrdinalFeatureVector::index_iterator OrdinalFeatureVector::order_begin(uint32 index) { + return order_; +} + +OrdinalFeatureVector::index_iterator OrdinalFeatureVector::order_end(uint32 index) { + return &order_[numValues_]; +} + +OrdinalFeatureVector::index_const_iterator OrdinalFeatureVector::order_cbegin(uint32 index) const { + return order_; +} + +OrdinalFeatureVector::index_const_iterator OrdinalFeatureVector::order_cend(uint32 index) const { + return &order_[numValues_]; +} From 02f6493672bc2a1c6b1d992d20298019168d627b Mon Sep 17 00:00:00 2001 From: Michael Rapp Date: Mon, 25 Sep 2023 15:08:09 +0200 Subject: [PATCH 3/3] Add missing comment. --- .../include/mlrl/common/input/feature_vector_nominal.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp index 296b3a01c3..ea80b74466 100644 --- a/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp +++ b/cpp/subprojects/common/include/mlrl/common/input/feature_vector_nominal.hpp @@ -22,6 +22,9 @@ class NominalFeatureVector : public AbstractFeatureVector { protected: + /** + * The number of distinct values of the nominal feature, excluding the majority value. + */ const uint32 numValues_; public: