Exemple #1
0
    def metric(self):
        totalTimer = Timer()
        with totalTimer:
            if self.method_param["kernel"] == "polynomial":
                if "degree" in self.method_param:
                    degree = int(self.method_param["degree"])
                else:
                    degree = 1

                kernel = mlpy.kernel_polynomial(self.data[0],
                                                self.data[0],
                                                d=degree)
            elif self.method_param["kernel"] == "gaussian":
                kernel = mlpy.kernel_gaussian(self.data[0],
                                              self.data[0],
                                              sigma=2)
            elif self.method_param["kernel"] == "linear":
                kernel = mlpy.kernel_linear(self.data[0], self.data[0])
            elif self.method_param["kernel"] == "hyptan":
                kernel = mlpy.kernel_sigmoid(self.data[0], self.data[0])

            model = mlpy.KPCA()
            model.learn(kernel)
            out = model.transform(kernel, **self.build_opts)

        metric = {}
        metric["runtime"] = totalTimer.ElapsedTime()
        return metric
Exemple #2
0
    def RunKPCAMlpy():
      totalTimer = Timer()

      # Load input dataset.
      Log.Info("Loading dataset", self.verbose)
      data = np.genfromtxt(self.dataset, delimiter=',')

      try:
        with totalTimer:
          # Get the new dimensionality, if it is necessary.
          if "new_dimensionality" in options:
            d = int(options.pop("new_dimensionality"))
            if (d > data.shape[1]):
              Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater "
                + "than existing dimensionality (" + str(data.shape[1]) + ")!")
              return -1
          else:
            d = data.shape[0]

          # Get the kernel type and make sure it is valid.
          if not "kernel" in options:
            Log.Fatal("Choose kernel type, valid choices are 'polynomial', " +
                  "'gaussian', 'linear' and 'hyptan'.")
            return -1

          if options["kernel"] == "polynomial":
            if "degree" in options:
              degree = int(options.pop("degree"))
            else:
              degree = 1

            kernel = mlpy.kernel_polynomial(data, data, d=degree)
          elif options["kernel"] == "gaussian":
            kernel = mlpy.kernel_gaussian(data, data, sigma=2)
          elif options["kernel"] == "linear":
            kernel = mlpy.kernel_linear(data, data)
          elif options["kernel"] == "hyptan":
            kernel = mlpy.kernel_sigmoid(data, data)
          else:
            Log.Fatal("Invalid kernel type (" + kernel.group(1) + "); valid " +
                    "choices are 'polynomial', 'gaussian', 'linear' and 'hyptan'.")
            return -1

          options.pop("kernel")
          if len(options) > 0:
            Log.Fatal("Unknown parameters: " + str(options))
            raise Exception("unknown parameters")

          # Perform Kernel Principal Components Analysis.
          model = mlpy.KPCA()
          model.learn(kernel)
          out = model.transform(kernel, k=d)
      except Exception as e:
        Log.Fatal("Exception: " + str(e))
        return -1

      return totalTimer.ElapsedTime()
    def RunKPCAMlpy(q):
      totalTimer = Timer()

      # Load input dataset.
      Log.Info("Loading dataset", self.verbose)
      data = np.genfromtxt(self.dataset, delimiter=',')

      try:
        with totalTimer:
          # Get the new dimensionality, if it is necessary.
          dimension = re.search('-d (\d+)', options)
          if not dimension:
            d = data.shape[0]
          else:
            d = int(dimension.group(1))
            if (d > data.shape[1]):
              Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater "
                + "than existing dimensionality (" + str(data.shape[1]) + ")!")
              q.put(-1)
              return -1

          # Get the kernel type and make sure it is valid.
          kernel = re.search("-k ([^\s]+)", options)
          if not kernel:
              Log.Fatal("Choose kernel type, valid choices are 'polynomial', " +
                    "'gaussian', 'linear' and 'hyptan'.")
              q.put(-1)
              return -1
          elif kernel.group(1) == "polynomial":
            degree = re.search('-D (\d+)', options)
            degree = 1 if not degree else int(degree.group(1))

            kernel = mlpy.kernel_polynomial(data, data, d=degree)
          elif kernel.group(1) == "gaussian":
            kernel = mlpy.kernel_gaussian(data, data, sigma=2)
          elif kernel.group(1) == "linear":
            kernel = mlpy.kernel_linear(data, data)
          elif kernel.group(1) == "hyptan":
            kernel = mlpy.kernel_sigmoid(data, data)
          else:
            Log.Fatal("Invalid kernel type (" + kernel.group(1) + "); valid " +
                    "choices are 'polynomial', 'gaussian', 'linear' and 'hyptan'.")
            q.put(-1)
            return -1

          # Perform Kernel Principal Components Analysis.
          model = mlpy.KPCA()
          model.learn(kernel)
          out = model.transform(kernel, k=d)
      except Exception as e:
        q.put(-1)
        return -1

      time = totalTimer.ElapsedTime()
      q.put(time)
      return time
Exemple #4
0
m2 = np.sum(K2, axis=1) / float(n2)
d = (m1 - m2)

#M
M = np.dot(d.reshape(-1, 1), d.reshape(1, -1))

#alpha
alpha = np.linalg.solve(N, d)
#alpha
b = - np.dot(alpha, (n1 * m1 + n2 * m2) / float(n))


# ----- comparare  yoonsang's Alpha and package's Alpha -----
import mlpy # mlpy is must be installed with command line function (pip, easy_install)
# Using linear kernel
Kl = mlpy.kernel_linear(X, X) # compute the kernel matrix
linear_kfda = mlpy.KFDA(lmb=0.001)
linear_kfda.learn(Kl, y) # compute the tranformation vector
zl = linear_kfda.transform(Kl) # embedded x into the kernel fisher space

# Using Gaussian kernel
sig = 1
Kg = mlpy.kernel_gaussian(X, X, sigma=sig) # compute the kernel matrix
gaussian_kfda = mlpy.KFDA(lmb=0.001)
gaussian_kfda.learn(Kg, y) # compute the tranformation vector
zg = gaussian_kfda.transform(Kg) # embedded x into the kernel fisher space
gaussian_kfda._coeff # alpha

# Using sigmoid kernel
gam=0.1
Ks = mlpy.kernel_sigmoid(X, X, gamma=gam, b=1.0) # compute the kernel matrix
Exemple #5
0
        def RunKPCAMlpy(q):
            totalTimer = Timer()

            # Load input dataset.
            Log.Info("Loading dataset", self.verbose)
            data = np.genfromtxt(self.dataset, delimiter=',')

            try:
                with totalTimer:
                    # Get the new dimensionality, if it is necessary.
                    dimension = re.search('-d (\d+)', options)
                    if not dimension:
                        d = data.shape[0]
                    else:
                        d = int(dimension.group(1))
                        if (d > data.shape[1]):
                            Log.Fatal("New dimensionality (" + str(d) +
                                      ") cannot be greater " +
                                      "than existing dimensionality (" +
                                      str(data.shape[1]) + ")!")
                            q.put(-1)
                            return -1

                    # Get the kernel type and make sure it is valid.
                    kernel = re.search("-k ([^\s]+)", options)
                    if not kernel:
                        Log.Fatal(
                            "Choose kernel type, valid choices are 'polynomial', "
                            + "'gaussian', 'linear' and 'hyptan'.")
                        q.put(-1)
                        return -1
                    elif kernel.group(1) == "polynomial":
                        degree = re.search('-D (\d+)', options)
                        degree = 1 if not degree else int(degree.group(1))

                        kernel = mlpy.kernel_polynomial(data, data, d=degree)
                    elif kernel.group(1) == "gaussian":
                        kernel = mlpy.kernel_gaussian(data, data, sigma=2)
                    elif kernel.group(1) == "linear":
                        kernel = mlpy.kernel_linear(data, data)
                    elif kernel.group(1) == "hyptan":
                        kernel = mlpy.kernel_sigmoid(data, data)
                    else:
                        Log.Fatal(
                            "Invalid kernel type (" + kernel.group(1) +
                            "); valid " +
                            "choices are 'polynomial', 'gaussian', 'linear' and 'hyptan'."
                        )
                        q.put(-1)
                        return -1

                    # Perform Kernel Principal Components Analysis.
                    model = mlpy.KPCA()
                    model.learn(kernel)
                    out = model.transform(kernel, k=d)
            except Exception as e:
                q.put(-1)
                return -1

            time = totalTimer.ElapsedTime()
            q.put(time)
            return time