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
mlpack_kmeans.Cluster(data, k, clusters, centroids); |
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
mlpack::kmeans::KMeans<> mlpack_kmeans(max_iter); |
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
//cluster the data | |
arma::Row<size_t> clusters; | |
arma::mat centroids; |
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
// create data | |
int i = 0; | |
for(; i < samples / 2; ++i) | |
{ | |
data.col(i) = arma::vec({1, 1}) + 0.25*arma::randn<arma::vec>(dim); | |
} | |
for(; i < samples; ++i) | |
{ | |
data.col(i) = arma::vec({2, 3}) + 0.25*arma::randn<arma::vec>(dim); | |
} |
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
arma::mat data(dim, samples, arma::fill::zeros); |
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
int k = 2; //amount of clusters | |
int dim = 2;//dimensionality of program | |
int samples = 50; | |
int max_iter = 10;//maximum number of iterations |
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
#include <bits/stdc++.h>// include basic c++ liraries | |
#include <mlpack/methods/kmeans/kmeans.hpp>// mlpack library for k-means | |
#include <armadillo>//ml pack is dependent on armadillo | |
Using namespace std; |
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
SquaredLoss<> loss; //initializing square loss object | |
Data<RealVector> prediction = model(data.inputs()); //prediction is calculated based on data inputs | |
cout << "squared loss: " << loss(data.labels(), prediction) << endl; // In end we ouptut the loss |
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
// show model parameters | |
cout << "intercept: " << model.offset() << endl; | |
cout << "matrix: " << model.matrix() << endl; |
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
//training the model | |
trainer.train(model, data);// train function ro training the model. |