Skip to content

Commit

Permalink
Merge pull request #782 from mrapp-ke/create-numerical-feature-vector
Browse files Browse the repository at this point in the history
Erzeugen eines NumericalFeatureVector
  • Loading branch information
michael-rapp authored Sep 25, 2023
2 parents 17c2854 + 5ccb978 commit 0a56958
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cpp/subprojects/common/include/mlrl/common/input/feature_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*/
#pragma once

#include "mlrl/common/data/view_csc.hpp"
#include "mlrl/common/data/view_fortran_contiguous.hpp"
#include "mlrl/common/input/feature_vector.hpp"

/**
* Defines an interface for all classes that represent the type of a feature.
*/
Expand All @@ -16,12 +20,38 @@ class IFeatureType {
*
* @return True, if the feature is ordinal, false otherwise
*/
// TODO Remove
virtual bool isOrdinal() const = 0;

/**
* Returns whether the feature is nominal or not.
*
* @return True, if the feature is nominal, false otherwise
*/
// TODO Remove
virtual bool isNominal() const = 0;

/**
* Creates and returns a feature vector that stores the feature values taken from a given Fortran-contiguous
* matrix for a certain feature.
*
* @param featureIndex The index of the feature
* @param featureMatrix A reference to an object of type `FortranContiguousConstView` that provides column-wise
* access to the feature values
* @return An unique pointer to an object of type `IFeatureVector` that has been created
*/
virtual std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const = 0;

/**
* Creates and returns a feature vector that stores the feature values taken from a given CSC matrix for a
* certain feature.
*
* @param featureIndex The index of the feature
* @param featureMatrix A reference to an object of type `CscConstView` that provides column-wise access to the
* feature values
* @return An unique pointer to an object of type `IFeatureVector` that has been created
*/
virtual std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ class NominalFeatureType final : public IFeatureType {
bool isOrdinal() const override;

bool isNominal() const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ class NumericalFeatureType final : public IFeatureType {
bool isOrdinal() const override;

bool isNominal() const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ class OrdinalFeatureType final : public IFeatureType {
bool isOrdinal() const override;

bool isNominal() const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const override;

std::unique_ptr<IFeatureVector> createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ bool NominalFeatureType::isOrdinal() const {
bool NominalFeatureType::isNominal() const {
return true;
}

std::unique_ptr<IFeatureVector> NominalFeatureType::createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const {
// TODO Implement
return nullptr;
}

std::unique_ptr<IFeatureVector> NominalFeatureType::createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const {
// TODO Implement
return nullptr;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,90 @@
#include "mlrl/common/input/feature_type_numerical.hpp"

#include "mlrl/common/input/feature_vector_equal.hpp"
#include "mlrl/common/input/feature_vector_numerical.hpp"
#include "mlrl/common/iterator/index_iterator.hpp"

#include <algorithm>

template<typename IndexIterator, typename ValueIterator>
static inline std::unique_ptr<NumericalFeatureVector> createNumericalFeatureVector(IndexIterator indexIterator,
ValueIterator valueIterator,
uint32 numElements) {
std::unique_ptr<NumericalFeatureVector> featureVectorPtr = std::make_unique<NumericalFeatureVector>(numElements, 0);
NumericalFeatureVector::iterator vectorIterator = featureVectorPtr->begin();
uint32 n = 0;

for (uint32 i = 0; i < numElements; i++) {
uint32 index = indexIterator[i];
float32 value = valueIterator[i];

if (std::isnan(value)) {
featureVectorPtr->setMissing(index, true);
} else {
IndexedValue<float32>& entry = vectorIterator[n];
entry.index = index;
entry.value = value;
n++;
}
}

featureVectorPtr->setNumElements(n, true);
return featureVectorPtr;
}

static inline std::unique_ptr<NumericalFeatureVector> createNumericalFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) {
FortranContiguousConstView<const float32>::value_const_iterator valueIterator =
featureMatrix.values_cbegin(featureIndex);
uint32 numElements = featureMatrix.getNumRows();
return createNumericalFeatureVector(valueIterator, IndexIterator(), numElements);
}

static inline std::unique_ptr<NumericalFeatureVector> createNumericalFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) {
CscConstView<const float32>::index_const_iterator indexIterator = featureMatrix.indices_cbegin(featureIndex);
CscConstView<const float32>::index_const_iterator indicesEnd = featureMatrix.indices_cend(featureIndex);
CscConstView<const float32>::value_const_iterator valueIterator = featureMatrix.values_cbegin(featureIndex);
uint32 numElements = indicesEnd - indexIterator;
return createNumericalFeatureVector(indexIterator, valueIterator, numElements);
}

template<typename FeatureMatrix>
static inline std::unique_ptr<IFeatureVector> createFeatureVectorInternally(uint32 featureIndex,
const FeatureMatrix& featureMatrix) {
std::unique_ptr<NumericalFeatureVector> featureVectorPtr =
createNumericalFeatureVector(featureIndex, featureMatrix);

// Sort the feature values...
std::sort(featureVectorPtr->begin(), featureVectorPtr->end(), IndexedValue<float32>::CompareValue());

// Check if all feature values are equal...
NumericalFeatureVector::const_iterator iterator = featureVectorPtr->cbegin();
uint32 numElements = featureVectorPtr->getNumElements();
float32 minValue = iterator[0].value;
float32 maxValue = iterator[numElements - 1].value;

if (isEqual(minValue, maxValue)) {
return std::make_unique<EqualFeatureVector>();
}

return featureVectorPtr;
}

bool NumericalFeatureType::isOrdinal() const {
return false;
}

bool NumericalFeatureType::isNominal() const {
return false;
}

std::unique_ptr<IFeatureVector> NumericalFeatureType::createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const {
return createFeatureVectorInternally(featureIndex, featureMatrix);
}

std::unique_ptr<IFeatureVector> NumericalFeatureType::createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const {
return createFeatureVectorInternally(featureIndex, featureMatrix);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ bool OrdinalFeatureType::isOrdinal() const {
bool OrdinalFeatureType::isNominal() const {
return false;
}

std::unique_ptr<IFeatureVector> OrdinalFeatureType::createFeatureVector(
uint32 featureIndex, const FortranContiguousConstView<const float32>& featureMatrix) const {
// TODO Implement
return nullptr;
}

std::unique_ptr<IFeatureVector> OrdinalFeatureType::createFeatureVector(
uint32 featureIndex, const CscConstView<const float32>& featureMatrix) const {
// TODO Implement
return nullptr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "mlrl/common/input/feature_vector_common.hpp"

AbstractFeatureVector::missing_index_const_iterator AbstractFeatureVector::missing_indices_cbegin() const {
return missingIndices_.indices_cbegin();
}

AbstractFeatureVector::missing_index_const_iterator AbstractFeatureVector::missing_indices_cend() const {
return missingIndices_.indices_cend();
}

void AbstractFeatureVector::setMissing(uint32 index, bool missing) {
missingIndices_.set(index, missing);
}

bool AbstractFeatureVector::isMissing(uint32 index) const {
return missingIndices_[index];
}

0 comments on commit 0a56958

Please sign in to comment.