Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neue Klasse NominalFeatureVector #777

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/subprojects/common/include/mlrl/common/data/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cmath>
#include <limits>

typedef int int32;
typedef long int int64;
typedef unsigned char uint8;
typedef unsigned int uint32;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* @author Michael Rapp ([email protected])
*/
#pragma once

#include "mlrl/common/input/feature_vector_common.hpp"

/**
* A feature vector that stores the indices of the examples that are associated with each value, except for the minority
* value, i.e., the least frequent value, of a nominal feature.
*/
class NominalFeatureVector final : public AbstractFeatureVector {
private:

int32* values_;

uint32* indices_;

uint32* indptr_;

const uint32 numValues_;

const int32 minorityValue_;

public:

/**
* @param numValues The number of distinct values of the nominal feature, excluding the minority value
* @param numExamples The total number of examples
* @param minorityValue The minority value, i.e., the least frequent value, of the nominal feature
*/
NominalFeatureVector(uint32 numValues, uint32 numExamples, int32 minorityValue);

~NominalFeatureVector() override;

/**
* An iterator that provides access to the values of the nominal feature and allows to modify them.
*/
typedef int32* value_iterator;

/**
* An iterator that provides read-only access to the values of the nominal feature.
*/
typedef const int32* value_const_iterator;

/**
* An iterator that provides access to the indices of the examples that are associated with each value of the
* nominal feature and allows to modify them.
*/
typedef uint32* index_iterator;

/**
* An iterator that provides read-only access to the indices of the examples that are associated with each value
* of the nominal feature.
*/
typedef const uint32* index_const_iterator;

/**
* An iterator that provides access to the indices that specify the first element in the array of example
* indices that corresponds to each value of the nominal feature.
*/
typedef uint32* indptr_iterator;

/**
* Returns a `value_iterator` to the beginning of the values of the nominal feature.
*
* @return A `value_iterator` to the beginning
*/
value_iterator values_begin();

/**
* Returns a `value_iterator` to the end of the values of the nominal feature.
*
* @return A `value_iterator` to the end
*/
value_iterator values_end();

/**
* Returns a `value_const_iterator` to the beginning of the values of the nominal feature.
*
* @return A `value_const_iterator` to the beginning
*/
value_const_iterator values_cbegin() const;

/**
* Returns a `value_const_iterator` to the end of the value of the nominal feature.
*
* @return A `value_const_iterator` to the end
*/
value_const_iterator values_cend() const;

/**
* Returns an `index_iterator` to the beginning of the indices of the examples that are associated with a
* specific value of the nominal feature.
*
* @param index The index of the value
* @return An `index_iterator` to the beginning
*/
index_iterator indices_begin(uint32 index);

/**
* Returns an `index_iterator` to the end of the indices of the examples that are associated with a specific
* value of the nominal feature.
*
* @param index The index of the value
* @return An `index_iterator` to the end
*/
index_iterator indices_end(uint32 index);

/**
* Returns an `index_const_iterator` to the beginning of the indices of the examples that are associated with a
* specific value of the nominal feature.
*
* @param index The index of the value
* @return An `index_const_iterator` to the beginning
*/
index_const_iterator indices_cbegin(uint32 index) const;

/**
* Returns an `index_const_iterator` to the end of the indices of the examples that are associated with a
* specific value of the nominal feature.
*
* @param index The index of the value
* @return An `index_const_iterator` to the end
*/
index_const_iterator indices_cend(uint32 index) const;

/**
* Returns an `indptr_iterator` to the beginning of the indices that specify the first element in the array of
* example indices that corresponds to each value of the nominal feature.
*
* @return An `indptr_iterator` to the beginning
*/
indptr_iterator indptr_begin();

/**
* Returns an `indptr_iterator` to the end of the indices that specify the first element in the array of example
* indices that corresponds to each value of the nominal feature.
*
* @return An `indptr_iterator` to the end
*/
indptr_iterator indptr_end();

/**
* Returns the minority value, i.e., the least frequent value, of the nominal feature.
*
* @return The minority value
*/
int32 getMinorityValue() const;

uint32 getNumElements() const override;
};
1 change: 1 addition & 0 deletions cpp/subprojects/common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ source_files = [
'src/mlrl/common/input/feature_vector.cpp',
'src/mlrl/common/input/feature_vector_common.cpp',
'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/label_matrix_c_contiguous.cpp',
'src/mlrl/common/input/label_matrix_csc.cpp',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "mlrl/common/input/feature_vector_nominal.hpp"

NominalFeatureVector::NominalFeatureVector(uint32 numValues, uint32 numExamples, int32 minorityValue)
: values_(new int32[numValues]), indices_(new uint32[numExamples]), indptr_(new uint32[numValues + 1]),
numValues_(numValues), minorityValue_(minorityValue) {
indptr_[numValues] = numExamples;
}

NominalFeatureVector::~NominalFeatureVector() {
delete[] values_;
delete[] indices_;
delete[] indptr_;
}

NominalFeatureVector::value_iterator NominalFeatureVector::values_begin() {
return values_;
}

NominalFeatureVector::value_iterator NominalFeatureVector::values_end() {
return &values_[numValues_];
}

NominalFeatureVector::value_const_iterator NominalFeatureVector::values_cbegin() const {
return values_;
}

NominalFeatureVector::value_const_iterator NominalFeatureVector::values_cend() const {
return &values_[numValues_];
}

NominalFeatureVector::index_iterator NominalFeatureVector::indices_begin(uint32 index) {
return &indices_[indptr_[index]];
}

NominalFeatureVector::index_iterator NominalFeatureVector::indices_end(uint32 index) {
return &indices_[indptr_[index + 1]];
}

NominalFeatureVector::index_const_iterator NominalFeatureVector::indices_cbegin(uint32 index) const {
return &indices_[indptr_[index]];
}

NominalFeatureVector::index_const_iterator NominalFeatureVector::indices_cend(uint32 index) const {
return &indices_[indptr_[index + 1]];
}

NominalFeatureVector::indptr_iterator NominalFeatureVector::indptr_begin() {
return indptr_;
}

NominalFeatureVector::indptr_iterator NominalFeatureVector::indptr_end() {
return &indptr_[numValues_];
}

int32 NominalFeatureVector::getMinorityValue() const {
return minorityValue_;
}

uint32 NominalFeatureVector::getNumElements() const {
return numValues_;
}
Loading