-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor NearestCentroid to be stateless
- Loading branch information
Showing
6 changed files
with
163 additions
and
53 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
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,30 @@ | ||
/* | ||
* This software is distributed under BSD 3-clause license (see LICENSE file). | ||
* | ||
* Authors: Yuhui Liu | ||
*/ | ||
#include <gtest/gtest.h> | ||
#include <shogun/classifier/NearestCentroid.h> | ||
#include <shogun/distance/EuclideanDistance.h> | ||
#include <shogun/labels/MulticlassLabels.h> | ||
|
||
using namespace shogun; | ||
TEST(NearestCentroid, fit_and_predict) | ||
{ | ||
SGMatrix<float64_t> X{{-10, -1}, {-2, -1}, {-3, -2}, | ||
{1, 1}, {2, 1}, {3, 2}}; | ||
SGVector<float64_t> y{1, 1, 1, 2, 2, 2}; | ||
|
||
auto train_data = std::make_shared<DenseFeatures<float64_t>>(X); | ||
auto train_labels = std::make_shared<MulticlassLabels>(y); | ||
auto distance = std::make_shared<EuclideanDistance>(); | ||
|
||
SGMatrix<float64_t> t{{3, 2}, {-10, -1}}; | ||
auto test_data = std::make_shared<DenseFeatures<float64_t>>(t); | ||
auto result_labels = std::make_shared<NearestCentroid>(distance) | ||
->fit(train_data, train_labels) | ||
->predict(test_data); | ||
auto result = result_labels->as<MulticlassLabels>()->get_labels(); | ||
EXPECT_EQ(result[0], 2); | ||
EXPECT_EQ(result[1], 1); | ||
} |