Skip to content

Commit

Permalink
remove redundant doc string in subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYuHui committed Jun 27, 2020
1 parent 11f8db6 commit 09d0111
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 70 deletions.
60 changes: 23 additions & 37 deletions src/shogun/labels/BinaryLabelEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@
#include <unordered_set>
namespace shogun
{

/** @brief Implements a reversible mapping from
* any form of labels to binary labels (+1, -1).
*
*/
class BinaryLabelEncoder : public LabelEncoder
{
public:
BinaryLabelEncoder() = default;

~BinaryLabelEncoder() = default;

/** Fit label encoder
*
* @param Target values.
* @return SGVector which contains unique labels.
*/
SGVector<float64_t> fit(const std::shared_ptr<Labels>& labs) override
{
const auto result_vector = labs->as<DenseLabels>()->get_labels();
check_is_valid(result_vector);
if (!can_convert_float_to_int(result_vector))
{
std::set<float64_t> s(
result_vector.begin(), result_vector.end());
io::warn(
"({}, {}) have been converted to (-1, 1).",
result_vector[0], result_vector[1]);
"({}, {}) have been converted to (-1, 1).", *s.begin(),
*s.end());
}
auto result_labels = fit_impl(result_vector);
return result_labels;
}
/** Transform labels to normalized encoding.
*
* @param Target values to be transformed.
* @return Labels transformed to be normalized.
*/

std::shared_ptr<Labels>
transform(const std::shared_ptr<Labels>& labs) override
{
Expand All @@ -57,34 +53,29 @@ namespace shogun
std::transform(
transformed_vec.begin(), transformed_vec.end(),
transformed_vec.begin(), [](float64_t e) {
if (std::abs(e - 0.0) <=
std::numeric_limits<float64_t>::epsilon())
return -1.0;
else
return e;
return Math::fequals(
e, 0.0,
std::numeric_limits<float64_t>::epsilon())
? -1.0
: e;
});
return std::make_shared<BinaryLabels>(transformed_vec);
}
/** Transform labels back to original encoding.
*
* @param normailzed encoding labels
* @return original encoding labels
*/

std::shared_ptr<Labels>
inverse_transform(const std::shared_ptr<Labels>& labs) override
{
auto normalized_labels = labs->as<BinaryLabels>();
normalized_labels->ensure_valid();
auto normalized_vector = normalized_labels->get_labels();
check_is_valid(normalized_vector);
std::transform(
normalized_vector.begin(), normalized_vector.end(),
normalized_vector.begin(), [](float64_t e) {
if (std::abs(e + 1.0) <=
std::numeric_limits<float64_t>::epsilon())
return 0.0;
else
return e;
return Math::fequals(
e, -1.0,
std::numeric_limits<float64_t>::epsilon())
? 0.0
: e;
});
auto origin_vec = inverse_transform_impl(normalized_vector);
SGVector<int32_t> result_vev(origin_vec.vlen);
Expand All @@ -93,11 +84,7 @@ namespace shogun
[](auto&& e) { return static_cast<int32_t>(e); });
return std::make_shared<BinaryLabels>(result_vev);
}
/** Fit label encoder and return encoded labels.
*
* @param Target values.
* @return Labels transformed to be normalized.
*/

std::shared_ptr<Labels>
fit_transform(const std::shared_ptr<Labels>& labs) override
{
Expand All @@ -118,11 +105,10 @@ namespace shogun
std::unordered_set<float64_t>(vec.begin(), vec.end());
require(
unique_set.size() == 2,
"Binary labels should contain only two elements, can not interpret ({}) as binary labels",
"Binary labels should contain only two elements, can not "
"interpret ({}) as binary labels",
fmt::join(unique_set, ", "));
}


};
} // namespace shogun

Expand Down
17 changes: 10 additions & 7 deletions src/shogun/labels/LabelEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include <shogun/lib/SGVector.h>
namespace shogun
{

/** @brief Implements a reversible mapping from any
* form of labels to one of Shogun's target label spaces
* (binary, multi-class, etc).
*
*/
class LabelEncoder : public SGObject
{
public:
Expand Down Expand Up @@ -76,11 +80,10 @@ namespace shogun
std::transform(
result_vector.begin(), result_vector.end(), converted.begin(),
[& unique_labels = unique_labels,
&normalized_to_origin =
normalized_to_origin](const auto& old_label) {
&inverse_mapping = inverse_mapping](const auto& old_label) {
auto new_label = std::distance(
unique_labels.begin(), unique_labels.find(old_label));
normalized_to_origin[new_label] = old_label;
inverse_mapping[new_label] = old_label;
return new_label;
});
return converted;
Expand All @@ -93,8 +96,8 @@ namespace shogun
std::transform(
result_vector.begin(), result_vector.end(),
original_vector.begin(),
[& normalized_to_origin = normalized_to_origin](const auto& e) {
return normalized_to_origin[e];
[& inverse_mapping = inverse_mapping](const auto& e) {
return inverse_mapping[e];
});
return original_vector;
}
Expand All @@ -114,7 +117,7 @@ namespace shogun
}

std::set<float64_t> unique_labels;
std::unordered_map<float64_t, float64_t> normalized_to_origin;
std::unordered_map<float64_t, float64_t> inverse_mapping;
};
} // namespace shogun

Expand Down
38 changes: 12 additions & 26 deletions src/shogun/labels/MulticlassLabelsEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,47 @@

namespace shogun
{

/** @brief Implements a reversible mapping from
* any form of labels to multi-class labels.
*
*/
class MulticlassLabelsEncoder : public LabelEncoder
{
public:
MulticlassLabelsEncoder() = default;

~MulticlassLabelsEncoder() = default;

/** Fit label encoder
*
* @param Target values.
* @return SGVector which contains unique labels.
*/
SGVector<float64_t> fit(const std::shared_ptr<Labels>& labs) override
{
const auto result_vector = labs->as<DenseLabels>()->get_labels();
if (!can_convert_float_to_int(result_vector))
{
std::set<float64_t> s(result_vector.begin(), result_vector.end());
{
std::set<float64_t> s(
result_vector.begin(), result_vector.end());
io::warn(
"({}) have been converted to (0...{})",
fmt::join(s, ", "),
result_vector.vlen - 1);
"({}) have been converted to (0...{})", fmt::join(s, ", "),
result_vector.vlen - 1);
}
return fit_impl(result_vector);
}
/** Transform labels to normalized encoding.
*
* @param Target values to be transformed.
* @return Labels transformed to be normalized.
*/

std::shared_ptr<Labels>
transform(const std::shared_ptr<Labels>& labs) override
{
const auto result_vector = labs->as<DenseLabels>()->get_labels();
return std::make_shared<MulticlassLabels>(
transform_impl(result_vector));
}
/** Transform labels back to original encoding.
*
* @param normailzed encoding labels
* @return original encoding labels
*/

std::shared_ptr<Labels>
inverse_transform(const std::shared_ptr<Labels>& labs) override
{
auto normalized_vector = labs->as<DenseLabels>()->get_labels();
return std::make_shared<MulticlassLabels>(
inverse_transform_impl(normalized_vector));
}
/** Fit label encoder and return encoded labels.
*
* @param Target values.
* @return Labels transformed to be normalized.
*/

std::shared_ptr<Labels>
fit_transform(const std::shared_ptr<Labels>& labs) override
{
Expand Down

0 comments on commit 09d0111

Please sign in to comment.