如何为 mlpack 的 kmeans 构建 HamerlyKmeans 对象

How to constuct the HamerlyKmeans object for mlpack's kmeans

本文关键字:构建 HamerlyKmeans 对象 kmeans mlpack      更新时间:2023-10-16

我是 mlpack 的新手,使用的是 3.3.1。我想做一些 KMean 聚类。我可以很好地使用 naiveKMeans 类,但我想使用另一种方法,例如 HamelyKMeans 类。

从手册来看,我似乎需要构造我自己的该类的本地对象来传递它,而不是这行代码中的 NaiveKMeans:

KMeans<mlpack::metric::EuclideanDistance, kmeans::SampleInitialization,
kmeans::MaxVarianceNewCluster, kmeans::NaiveKMeans, arma::mat> km =KMeans(0);

但是当我尝试按如下方式构造它时,我收到一个编译器错误:

// for StackOverflow
#include <mlpack/prereqs.hpp>
#include <mlpack/core.hpp>
#include <mlpack/core/util/cli.hpp>
#include <mlpack/methods/kmeans/kmeans.hpp>
#include <mlpack/methods/kmeans/allow_empty_clusters.hpp>
#include <mlpack/methods/kmeans/kill_empty_clusters.hpp>
#include <mlpack/methods/kmeans/refined_start.hpp>
#include <mlpack/methods/kmeans/elkan_kmeans.hpp>
#include <mlpack/methods/kmeans/hamerly_kmeans.hpp>
#include <mlpack/methods/kmeans/pelleg_moore_kmeans.hpp>
#include <mlpack/methods/kmeans/dual_tree_kmeans.hpp>
using namespace mlpack;
using namespace mlpack::kmeans;
using namespace mlpack::metric;
using namespace mlpack::util;
int main(int argc, char **argv) {
arma::mat in_data(10,10);
for ( int i=0; i < 10; i++ ) {
for ( int j=0; j < 10; j++ ) {
in_data(j,i)=i+j;
}
}
kmeans::HamerlyKMeans< metric::EuclideanDistance, arma::mat> 
ek = kmeans::HamerlyKMeans(in_data,EuclideanDistance());
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                
// HamerlyKMeans to be used instead of NaiveKMeans below

KMeans<mlpack::metric::EuclideanDistance, kmeans::SampleInitialization,
kmeans::MaxVarianceNewCluster, kmeans::NaiveKMeans, arma::mat> km =KMeans(0);
arma::Row<size_t> assignments; // Cluster assignments.
arma::mat centroids; // Cluster centroids.
km.Cluster(in_data,5,assignments, centroids); // 5 clusters.   
}

编译器错误消息为:

错误:没有可行的构造函数或推导指南来推导"HamerlyKMeans"的模板参数 [build] kmeans::HamerlyKMeans<metric::EuclideanDistance,> ek = kmeans::HamerlyKMeans(in_data,EuclideanDistance(((; [构建] ^

[build]/usr/include/mlpack/methods/kmeans/hamerly_kmeans.hpp:26:3:注意:候选函数 [使用 MetricType = mlpack::metric::LMetric<2,true>,MatType = arma::Mat] 不可行:期望第二个参数的 l 值

[build] HamerlyKMeans(const MatType&dataset, MetricType&metric(; [构建] ^ [build]/usr/include/mlpack/methods/kmeans/hamerly_kmeans.hpp:19:7:注意:候选函数模板不可行:需要 1 个参数,但提供了 2 个参数 [构建]类哈默利克曼斯 [构建] ^

但是当我查看hamerly_kmeans.hpp时,我看到:

template<typename MetricType, typename MatType>
class HamerlyKMeans
{
public:
/**
* Construct the HamerlyKMeans object, which must store several sets of
* bounds.
*/
HamerlyKMeans(const MatType& dataset, MetricType& metric);

我倒是很不知所措。我根本不了解类的模板。想法?

我已经想通了。我将HamerlyKMeans类的构造更改为此

metric::EuclideanDistance euclid_distance;  
kmeans::HamerlyKMeans< metric::EuclideanDistance, arma::mat> ek = 
kmeans::HamerlyKMeans(in_data,euclid_distance);

我对此的最好理解是编译器告诉我,第二个参数告诉我 metric::EuclidDistance(( 不构造 l 值(我认为它是一个变量,但上述更改创建了变量euclid_distance这是一个 l 值。上面的更改编译和链接很好。