Skip to content

Instantly share code, notes, and snippets.

@geektoni
Created May 16, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geektoni/487fa1c3eac5fbd31c70c9dc54d67fb1 to your computer and use it in GitHub Desktop.
Save geektoni/487fa1c3eac5fbd31c70c9dc54d67fb1 to your computer and use it in GitHub Desktop.
#include <shogun/base/init.h>
#include <shogun/base/some.h>
#include <shogun/distance/Distance.h>
#include <shogun/features/Features.h>
#include <shogun/io/File.h>
#include <shogun/io/SerializableAsciiFile.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/observers/ParameterObserver.h>
#include <shogun/machine/Machine.h>
#include <shogun/mathematics/Math.h>
#include <shogun/util/factory.h>
using namespace shogun;
int main(int, char*[])
{
init_shogun_with_defaults();
CMath::init_random(1);
auto f_feats_train = wrap(csv_file("../../data/classifier_binary_2d_linear_features_train.dat"));
auto features_train = wrap(features(f_feats_train));
auto d = wrap(distance("EuclideanDistance"));
d->put("lhs", features_train);
d->put("rhs", features_train);
auto kmeans = wrap(machine("KMeans"));
kmeans->put("k", 2);
kmeans->put("distance", d);
auto logger = wrap(parameter_observer("ParameterObserverLogger"));
kmeans->subscribe(logger);
kmeans->train();
auto c = wrap(kmeans->get<SGMatrix<float64_t>>("cluster_centers"));
SG_SPRINT(c.to_string().c_str())
exit_shogun();
return 0;
}
// Output
root@404b07532e89:~/shogun/build/examples/meta/cpp/observers# ./a.out
[20130564581] Received a matrix called "mus" which contains: [
[ -2.09397, 0.45498],
[ -1.12079, 2.15617]
]
[20130564599] Received a matrix called "mus" which contains: [
[ -1.94008, 2.07997],
[ -1.85142, 1.96843]
]
[
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment