0

我是 mlpack 的新手并使用 3.3.1。我想做一些 KMeans 聚类。我可以很好地使用 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, arma::mat> ek = kmeans::HamerlyKMeans(in_data,EuclideanDistance()) 的模板参数;[构建] ^

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

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

但是当我查看 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);

我已经很变态了。我完全不理解类的模板。想法?

4

1 回答 1

0

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

  metric::EuclideanDistance euclid_distance;  

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

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

于 2020-06-09T14:17:56.217 回答