def metric(self):
        totalTimer = Timer()

        with totalTimer:
            data_feat = RealFeatures(self.data[0].T)

            if self.distance == "Euclidean":
                distanceMethod = EuclideanDistance(data_feat, data_feat)

            elif self.distance == "Manhattan":
                distanceMethod = ManhattanMetric(data_feat, data_feat)

            elif self.distance == "Cosine":
                distanceMethod = CosineDistance(data_feat, data_feat)

            elif self.distance == "Chebyshev":
                distanceMethod = ChebyshewMetric(data_feat, data_feat)

            elif self.distance == "Chi-Square":
                distanceMethod = ChiSquareDistance(data_feat, data_feat)

            elif self.distance == "Tanimoto":
                distanceMethod = TanimotoDistance(data_feat, data_feat)

            elif self.distance == "Minkowski":
                distanceMethod = MinkowskiMetric(data_feat, data_feat,
                                                 self.minkowski_k)

            elif self.distance == "Jensen":
                distanceMethod = JensenMetric(data_feat, data_feat)

            elif self.distance == "Canberra":
                distanceMethod = CanberraMetric(data_feat, data_feat)

            elif self.distance == "Geodesic":
                distanceMethod = GeodesicMetric(data_feat, data_feat)

            else:
                raise ValueError(
                    "Provided distance not supported by the benchmark")

            model = Hierarchical(self.merges, distanceMethod)
            model.train()

        merge_distances = model.get_merge_distances()
        cluster_pairs = model.get_cluster_pairs()

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()
        metric["Merge distances between clusters"] = str(merge_distances)
        metric["Cluster pairings"] = str(cluster_pairs)

        return metric
Exemple #2
0
def distance_canberra (train_fname=traindat,test_fname=testdat):
	from shogun import RealFeatures, CanberraMetric, CSVFile

	feats_train=RealFeatures(CSVFile(train_fname))
	feats_test=RealFeatures(CSVFile(test_fname))

	distance=CanberraMetric(feats_train, feats_train)
	dm_train=distance.get_distance_matrix()

	distance.init(feats_train, feats_test)
	dm_test=distance.get_distance_matrix()
	return distance,dm_train,dm_test
Exemple #3
0
    def metric(self):

        distance = "Euclidean"
        if "distance" in self.method_param:
            distance = str(self.method_param["distance"])

        kernel = "Gaussian"
        if "kernel" in self.method_param:
            kernel = str(self.method_param["kernel"])

        cache_size = 10
        if "cache-size" in self.method_param:
            cache_size = int(self.method_param["cache-size"])

        degree = 2
        if "degree" in self.method_param:
            degree = int(self.method_param["degree"])

        gamma = 2.0
        if "gamma" in self.method_param:
            gamma = float(self.method_param["gamma"])

        coef0 = 1.0
        if "coef0" in self.method_param:
            coef0 = float(self.method_param["coef0"])

        order = 2.0
        if "order" in self.method_param:
            order = float(self.method_param["order"])

        width = 2.0
        if "width" in self.method_param:
            width = float(self.method_param["order"])

        sigma = 1.5
        if "sigma" in self.method_param:
            sigma = float(self.method_param["sigma"])

        const = 2.0
        if "constant" in self.method_param:
            const = float(self.method_param["constant"])

        #Choosing a Distance Function required by some Kernels
        if distance == "Euclidean":
            distanceMethod = EuclideanDistance()

        elif distance == "Chi-Square":
            distanceMethod = ChiSquareDistance()

        elif distance == "Tanimoto":
            distanceMethod = TanimotoDistance()

        elif distance == "Minkowski":
            distanceMethod = MinkowskiMetric()

        elif distance == "Manhattan":
            distanceMethod = ManhattanMetric()

        elif distance == "Jensen":
            distanceMethod = JensenMetric()

        elif distance == "Canberra":
            distanceMethod = CanberraMetric()

        else:
            raise ValueError(
                "distance function not supported by the benchmarks")

        totalTimer = Timer()
        with totalTimer:
            #Choosing a Kernel for the Gaussian Process Classification
            if kernel == "Gaussian":
                kernelMethod = GaussianKernel(width)

            elif kernel == "Polynomial":
                kernelMethod = PolyKernel(cache_size, degree)

            elif kernel == "Sigmoid":
                kernelMethod = SigmoidKernel(cache_size, gamma, coef0)

            elif kernel == "Bessel":
                kernelMethod = BesselKernel(cache_size, order, width, degree,
                                            distanceMethod)

            elif kernel == "Power":
                kernelMethod = PowerKernel(cache_size, degree, distanceMethod)

            elif kernel == "Log":
                kernelMethod = LogKernel(cache_size, degree, distanceMethod)

            elif kernel == "Cauchy":
                kernelMethod = CauchyKernel(cache_size, sigma, distanceMethod)

            elif kernel == "Constant":
                kernelMethod = ConstKernel(const)

            elif kernel == "Diagonal":
                kernelMethod = DiagKernel(cache_size, const)

            else:
                raise ValueError("kernel not supported by the benchmarks")

            mean_function = ConstMean()
            likelihood = SoftMaxLikelihood()
            inference_method = MultiLaplaceInferenceMethod(
                kernelMethod, self.train_features, mean_function,
                self.train_labels, likelihood)

            #Create the model
            model = SGPC(inference_method)

            #Train model
            model.train()
            if len(self.data) >= 2:
                predictions = model.apply_multiclass(
                    self.test_features).get_labels()

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        if len(self.data) >= 2:
            predictions = label_decoder(predictions, self.label_map)

        if len(self.data) >= 3:
            confusionMatrix = Metrics.ConfusionMatrix(self.data[2],
                                                      predictions)
            metric['ACC'] = Metrics.AverageAccuracy(confusionMatrix)
            metric['MCC'] = Metrics.MCCMultiClass(confusionMatrix)
            metric['Precision'] = Metrics.AvgPrecision(confusionMatrix)
            metric['Recall'] = Metrics.AvgRecall(confusionMatrix)
            metric['MSE'] = Metrics.SimpleMeanSquaredError(
                self.data[2], predictions)

        return metric
Exemple #4
0
    def metric(self):
        totalTimer = Timer()

        with totalTimer:
            if self.seed:
                Math_init_random(self.seed)

            data_feat = RealFeatures(self.data[0].T)
            if self.distance == "Euclidean":
                distanceMethod = EuclideanDistance(data_feat, data_feat)

            elif self.distance == "Bray-Curtis":
                distanceMethod = BrayCurtisDistance(data_feat, data_feat)

            elif self.distance == "Chi-Square":
                distanceMethod = ChiSquareDistance(data_feat, data_feat)

            elif self.distance == "Mahalanobis":
                distanceMethod = MahalanobisDistance(data_feat, data_feat)

            elif self.distance == "Cosine":
                distanceMethod = CosineDistance(data_feat, data_feat)

            elif self.distance == "Tanimoto":
                distanceMethod = TanimotoDistance(data_feat, data_feat)

            elif self.distance == "Minkowski":
                distanceMethod = MinkowskiMetric(data_feat, data_feat,
                                                 self.minkowski_k)

            elif self.distance == "Manhattan":
                distanceMethod = ManhattanMetric(data_feat, data_feat)

            elif self.distance == "Jensen":
                distanceMethod = JensenMetric(data_feat, data_feat)

            elif self.distance == "Chebyshev":
                distanceMethod = ChebyshewMetric(data_feat, data_feat)

            elif self.distance == "Canberra":
                distanceMethod = CanberraMetric(data_feat, data_feat)

            elif self.distance == "Geodesic":
                distanceMethod = GeodesicMetric(data_feat, data_feat)

            else:
                raise ValueError(
                    "Provided distance not supported by benchmark")

            model = KMeans(self.k, distanceMethod, self.use_kmpp)

            if self.max_iter:
                model.set_max_iter(self.max_iter)
            model.train()

            labels = model.apply().get_labels()
            centers = model.get_cluster_centers()

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        return metric
Exemple #5
0
    def metric(self):
        totalTimer = Timer()

        with totalTimer:
            if self.distance == "Euclidean":
                distanceMethod = EuclideanDistance(self.train_feat,
                                                   self.train_feat)

            elif self.distance == "Bray-Curtis":
                distanceMethod = BrayCurtisDistance(self.train_feat,
                                                    self.train_feat)

            elif self.distance == "Chi-Square":
                distanceMethod = ChiSquareDistance(self.train_feat,
                                                   self.train_feat)

            elif self.distance == "Mahalanobis":
                distanceMethod = MahalanobisDistance(self.train_feat,
                                                     self.train_feat)

            elif self.distance == "Cosine":
                distanceMethod = CosineDistance(self.train_feat,
                                                self.train_feat)

            elif self.distance == "Tanimoto":
                distanceMethod = TanimotoDistance(self.train_feat,
                                                  self.train_feat)

            elif self.distance == "Minkowski":
                distanceMethod = MinkowskiMetric(self.train_feat,
                                                 self.train_feat, self.degree)

            elif self.distance == "Manhattan":
                distanceMethod = ManhattanMetric(self.train_feat,
                                                 self.train_feat)

            elif self.distance == "Jensen":
                distanceMethod = JensenMetric(self.train_feat, self.train_feat)

            elif self.distance == "Chebyshev":
                distanceMethod = ChebyshewMetric(self.train_feat,
                                                 self.train_feat)

            elif self.distance == "Canberra":
                distanceMethod = CanberraMetric(self.train_feat,
                                                self.train_feat)

            elif self.distance == "Geodesic":
                distanceMethod = GeodesicMetric(self.train_feat,
                                                self.train_feat)

            else:
                raise ValueError(
                    "Provided distance not supported by benchmark")

            if self.solver == "Brute":
                solverFlag = KNN_BRUTE

            elif self.solver == "KD-Tree":
                solverFlag = KNN_KDTREE

            elif self.solver == "Cover-Tree":
                solverFlag = KNN_COVER_TREE

            elif self.solver == "LSH":
                solverFlag = KNN_LSH

            else:
                raise ValueError("Provided solver not supported by benchmark")

            model = SKNN(self.k, distanceMethod, self.train_labels, solverFlag)
            model.train()

            if len(self.data) >= 2:
                predictions = model.apply_multiclass(
                    self.test_feat).get_labels()

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        if len(self.data) >= 2:
            predictions = label_decoder(predictions, self.label_map)

        if len(self.data) >= 3:
            confusionMatrix = Metrics.ConfusionMatrix(self.data[2],
                                                      predictions)
            metric['ACC'] = Metrics.AverageAccuracy(confusionMatrix)
            metric['MCC'] = Metrics.MCCMultiClass(confusionMatrix)
            metric['Precision'] = Metrics.AvgPrecision(confusionMatrix)
            metric['Recall'] = Metrics.AvgRecall(confusionMatrix)
            metric['MSE'] = Metrics.SimpleMeanSquaredError(
                self.data[2], predictions)

        return metric
Exemple #6
0
    def metric(self):
        totalTimer = Timer()
        with totalTimer:

            if self.distance == "Euclidean":
                distanceMethod = EuclideanDistance()

            elif self.distance == "Bray-Curtis":
                distanceMethod = BrayCurtisDistance()

            elif self.distance == "Chi-Square":
                distanceMethod = ChiSquareDistance()

            elif self.distance == "MahalanobisDistance":
                distanceMethod = MahalanobisDistance()

            elif self.distance == "Cosine":
                distanceMethod = CosineDistance()

            elif self.distance == "Tanimoto":
                distanceMethod = TanimotoDistance()

            elif self.distance == "Minkowski":
                distanceMethod = MinkowskiMetric()

            elif self.distance == "Jensen":
                distanceMethod = JensenMetric()

            elif self.distance == "Chebyshev":
                distanceMethod = ChebyshewMetric()

            elif self.distance == "Canberra":
                distanceMethod = CanberraMetric()

            elif self.distance == "Geodesic":
                distanceMethod = GeodesicMetric()

            else:
                raise ValueError(
                    "Provided distance is not supported by benchmark")

            if self.kernel == "Polynomial":
                kernelMethod = PolyKernel(self.train_feat, self.train_feat,
                                          self.degree, True, self.cache_size)

            elif self.kernel == "Gaussian":
                kernelMethod = GaussianKernel(self.train_feat, self.train_feat,
                                              self.width, self.cache_size)

            elif self.kernel == "Linear":
                kernelMethod = LinearKernel(self.train_feat, self.train_feat)

            elif self.kernel == "Hyptan" or self.kernel == "Sigmoid":
                kernelMethod = SigmoidKernel(self.train_feat, self.train_feat,
                                             self.cache_size, self.gamma,
                                             self.coef0)

            elif self.kernel == "Power":
                kernelMethod = PowerKernel(self.train_feat, self.train_feat,
                                           self.degree, distanceMethod)

            elif self.kernel == "Log":
                kernelMethod = LogKernel(self.train_feat, self.train_feat,
                                         self.degree, distanceMethod)

            elif self.kernel == "Cauchy":
                kernelMethod = CauchyKernel(self.train_feat, self.train_feat,
                                            self.sigma, distanceMethod)

            elif self.kernel == "Constant":
                kernelMethod = ConstKernel(self.train_feat, self.train_feat,
                                           self.const)

            elif self.kernel == "Diagonal":
                kernelMethod = DiagKernel(self.train_feat, self.train_feat,
                                          self.const)

            else:
                raise ValueError(
                    "Provided Kernel not supported by current benchmark")

            if self.solver_type == "epsilon":
                model = LibSVR(self.C, self.svr_param, kernelMethod,
                               self.train_labels, LIBSVR_EPSILON_SVR)

            elif self.solver_type == "nu":
                model = LibSVR(self.C, self.svr_param, kernelMethod,
                               self.train_labels, LIBSVR_NU_SVR)

            else:
                raise ValueError("Unknown solver type")

            model.train()

            if len(self.data) >= 2:
                predictions = model.apply(self.test_feat).get_labels()

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        if len(self.data) >= 3:
            metric['MSE'] = Metrics.SimpleMeanSquaredError(
                self.data[2], predictions)

        return metric
Exemple #7
0
    def metric(self):
        totalTimer = Timer()
        with totalTimer:
            data_feat = RealFeatures(self.data[0].T)

            if self.distance == "Euclidean":
                distanceMethod = EuclideanDistance()

            elif self.distance == "Bray-Curtis":
                distanceMethod = BrayCurtisDistance()

            elif self.distance == "Chi-Square":
                distanceMethod = ChiSquareDistance()

            elif self.distance == "MahalanobisDistance":
                distanceMethod = MahalanobisDistance()

            elif self.distance == "Cosine":
                distanceMethod = CosineDistance()

            elif self.distance == "Tanimoto":
                distanceMethod = TanimotoDistance()

            elif self.distance == "Minkowski":
                distanceMethod = MinkowskiMetric()

            elif self.distance == "Manhattan":
                distanceMethod = ManhattanMetric()

            elif self.distance == "Jensen":
                distanceMethod = JensenMetric()

            elif self.distance == "Chebyshev":
                distanceMethod = ChebyshewMetric()

            elif self.distance == "Canberra":
                distanceMethod = CanberraMetric()

            elif self.distance == "Geodesic":
                distanceMethod = GeodesicMetric()

            else:
                raise ValueError(
                    "Provided distance is not supported by benchmark")

            if self.kernel == "Polynomial":
                kernelMethod = PolyKernel(data_feat, data_feat, self.degree,
                                          True, self.cache_size)

            elif self.kernel == "Gaussian":
                kernelMethod = GaussianKernel(data_feat, data_feat, self.width,
                                              self.cache_size)

            elif self.kernel == "Linear":
                kernelMethod = LinearKernel(data_feat, data_feat)

            elif self.kernel == "Hyptan" or self.kernel == "Sigmoid":
                kernelMethod = SigmoidKernel(data_feat, data_feat,
                                             self.cache_size, self.gamma,
                                             self.coef0)

            elif self.kernel == "Bessel":
                kernelMethod = BesselKernel(data_feat, data_feat, self.order,
                                            self.width, self.degree,
                                            distanceMethod, self.cache_size)

            elif self.kernel == "Power":
                kernelMethod = PowerKernel(data_feat, data_feat, self.degree,
                                           distanceMethod)

            elif self.kernel == "Log":
                kernelMethod = LogKernel(data_feat, data_feat, self.degree,
                                         distanceMethod)

            elif self.kernel == "Cauchy":
                kernelMethod = CauchyKernel(data_feat, data_feat, self.sigma,
                                            distanceMethod)

            elif self.kernel == "Constant":
                kernelMethod = ConstKernel(data_feat, data_feat, self.const)

            elif self.kernel == "Diagonal":
                kernelMethod = DiagKernel(data_feat, data_feat, self.const)
            else:
                raise ValueError(
                    "Provided Kernel not supported by current benchmark")

            model = KernelPCA(kernelMethod)
            model.set_target_dim(self.d)
            model.init(data_feat)
            model.apply_to_feature_matrix(data_feat)

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()

        return metric
Exemple #8
0
  def metric(self):
    totalTimer = Timer()

    with totalTimer:

      if self.distance == "Euclidean":
        distanceMethod = EuclideanDistance()

      elif self.distance == "Bray-Curtis":
        distanceMethod = BrayCurtisDistance()

      elif self.distance == "Chi-Square":
        distanceMethod = ChiSquareDistance()

      elif self.distance == "MahalanobisDistance":
        distanceMethod = MahalanobisDistance()

      elif self.distance == "Cosine":
        distanceMethod = CosineDistance()

      elif self.distance == "Tanimoto":
        distanceMethod = TanimotoDistance()

      elif self.distance == "Minkowski":
        distanceMethod = MinkowskiMetric()

      elif self.distance == "Manhattan":
        distanceMethod = ManhattanMetric()

      elif self.distance == "Jensen":
        distanceMethod = JensenMetric()

      elif self.distance == "Chebyshev":
        distanceMethod = ChebyshewMetric()

      elif self.distance == "Canberra":
        distanceMethod = CanberraMetric()

      elif self.distance == "Geodesic":
        distanceMethod = GeodesicMetric()

      else:
        raise ValueError("Provided distance is not supported by benchmark")

      if self.kernel == "Polynomial":
        kernelMethod = PolyKernel(self.train_feat, self.train_feat, self.degree, True,
            self.cache_size)

      elif self.kernel == "Gaussian":
        kernelMethod = GaussianKernel(self.train_feat, self.train_feat, self.width,
            self.cache_size)

      elif self.kernel == "Linear":
        kernelMethod = LinearKernel(self.train_feat, self.train_feat)

      elif self.kernel == "Hyptan" or self.kernel == "Sigmoid":
        kernelMethod = SigmoidKernel(self.train_feat, self.train_feat, self.cache_size,
            self.gamma, self.coef0)

      elif self.kernel == "Power":
        kernelMethod = PowerKernel(self.train_feat, self.train_feat, self.degree,
          distanceMethod)

      elif self.kernel == "Log":
        kernelMethod = LogKernel(self.train_feat, self.train_feat, self.degree,
            distanceMethod)

      elif self.kernel == "Cauchy":
        kernelMethod = CauchyKernel(self.train_feat, self.train_feat, self.sigma,
            distanceMethod)

      elif self.kernel == "Constant":
        kernelMethod = ConstKernel(self.train_feat, self.train_feat, self.const)

      elif self.kernel == "Diagonal":
        kernelMethod = DiagKernel(self.train_feat, self.train_feat, self.const)

      else:
        raise ValueError("Provided Kernel not supported by current benchmark")

      model = MC_LSVM(self.C, kernelMethod, self.train_labels)
      model.train()

      if len(self.data) >= 2:
        predictions = model.apply_multiclass(self.test_feat).get_labels()

    metric = {}
    metric["runtime"] = totalTimer.ElapsedTime()

    if len(self.data) >= 2:
      predictions = label_decoder(predictions, self.label_map)

    if len(self.data) == 3:
      confusionMatrix = Metrics.ConfusionMatrix(self.data[2], predictions)
      metric['ACC'] = Metrics.AverageAccuracy(confusionMatrix)
      metric['MCC'] = Metrics.MCCMultiClass(confusionMatrix)
      metric['Precision'] = Metrics.AvgPrecision(confusionMatrix)
      metric['Recall'] = Metrics.AvgRecall(confusionMatrix)
      metric['MSE'] = Metrics.SimpleMeanSquaredError(self.data[2], predictions)

    return metric