-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #782 from mrapp-ke/create-numerical-feature-vector
Erzeugen eines NumericalFeatureVector
- Loading branch information
Showing
8 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
cpp/subprojects/common/src/mlrl/common/input/feature_type_numerical.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |