-
-
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 class (#5053)
* Add NonParametricMachine class (#5055) * add nonparametric machine * fix notebooks * Refactor NearestCentroid class
- Loading branch information
Showing
5 changed files
with
43 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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{0, 0, 0, 1, 1, 1}; | ||
|
||
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}, {-100, 100}}; | ||
auto test_data = std::make_shared<DenseFeatures<float64_t>>(t); | ||
auto clf = std::make_shared<NearestCentroid>(distance); | ||
clf->train(train_data, train_labels); | ||
auto result_labels = clf->apply(test_data); | ||
auto result = result_labels->as<MulticlassLabels>()->get_labels(); | ||
EXPECT_EQ(result[0], 1); | ||
EXPECT_EQ(result[1], 0); | ||
EXPECT_EQ(result[2], 0); | ||
} |