def RunMetrics(self, options):
    Log.Info("Perform Linear Ridge Regression.", self.verbose)

    results = self.LinearRidgeRegressionShogun(options)
    if results < 0:
      return results

    metrics = {'Runtime' : results}

    if len(self.dataset) >= 3:

      X, y = SplitTrainData(self.dataset)
      tau = re.search("-t (\d+)", options)
      tau = 1.0 if not tau else int(tau.group(1))
      model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
      model.train()

      testData = LoadDataset(self.dataset[1])
      truelabels = LoadDataset(self.dataset[2])

      predictedlabels = model.apply_regression(RealFeatures(testData.T)).get_labels()

      SimpleMSE = Metrics.SimpleMeanSquaredError(truelabels, predictedlabels)
      metrics_dict = {}
      metrics_dict['Simple MSE'] = SimpleMSE
      return metrics_dict

    else:
      Log.Fatal("This method requires three datasets!")
    def RunLinearRidgeRegressionShogun(q):
      totalTimer = Timer()

      # Load input dataset.
      # If the dataset contains two files then the second file is the responses
      # file.
      
      Log.Info("Loading dataset", self.verbose)
      if len(self.dataset) >= 2:
        testSet = np.genfromtxt(self.dataset[1], delimiter=',')

      # Use the last row of the training set as the responses.
      X, y = SplitTrainData(self.dataset)
      tau = re.search("-t (\d+)", options)
      tau = 1.0 if not tau else int(tau.group(1))

      try:
        with totalTimer:
          # Perform linear ridge regression.
          model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
          model.train()

          if len(self.dataset) >= 2:
            model.apply_regression(RealFeatures(testSet.T))

      except Exception as e:
        q.put(-1)
        return -1

      time = totalTimer.ElapsedTime()
      q.put(time)
      return time
예제 #3
0
  def RunMetrics(self, options):
    Log.Info("Perform Linear Ridge Regression.", self.verbose)

    results = self.LinearRidgeRegressionShogun(options)
    if results < 0:
      return results

    metrics = {'Runtime' : results}

    if len(self.dataset) >= 3:

      X, y = SplitTrainData(self.dataset)
      tau = re.search("-t (\d+)", options)
      tau = 1.0 if not tau else int(tau.group(1))
      model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
      model.train()

      testData = LoadDataset(self.dataset[1])
      truelabels = LoadDataset(self.dataset[2])

      predictedlabels = model.apply_regression(RealFeatures(testData.T)).get_labels()

      SimpleMSE = Metrics.SimpleMeanSquaredError(truelabels, predictedlabels)
      metrics['Simple MSE'] = SimpleMSE
      return metrics

    else:
      Log.Fatal("This method requires three datasets!")
예제 #4
0
    def RunLinearRidgeRegressionShogun(q):
      totalTimer = Timer()

      # Load input dataset.
      # If the dataset contains two files then the second file is the responses
      # file.
      Log.Info("Loading dataset", self.verbose)
      if len(self.dataset) >= 2:
        testSet = np.genfromtxt(self.dataset[1], delimiter=',')

      # Use the last row of the training set as the responses.
      X, y = SplitTrainData(self.dataset)
      tau = re.search("-t (\d+)", options)
      tau = 1.0 if not tau else int(tau.group(1))

      try:
        with totalTimer:
          # Perform linear ridge regression.
          model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
          model.train()

          if len(self.dataset) >= 2:
            model.apply_regression(RealFeatures(testSet.T))

      except Exception as e:
        q.put(-1)
        return -1

      time = totalTimer.ElapsedTime()
      q.put(time)
      return time
예제 #5
0
    def RunLinearRidgeRegressionShogun():
      totalTimer = Timer()

      # Load input dataset.
      # If the dataset contains two files then the second file is the responses
      # file.
      Log.Info("Loading dataset", self.verbose)
      if len(self.dataset) >= 2:
        testSet = np.genfromtxt(self.dataset[1], delimiter=',')

      # Use the last row of the training set as the responses.
      X, y = SplitTrainData(self.dataset)
      if "alpha" in options:
        tau = float(options.pop("alpha"))
      else:
        Log.Fatal("Required parameter 'alpha' not specified!")
        raise Exception("missing parameter")

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

      try:
        with totalTimer:
          # Perform linear ridge regression.
          model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
          model.train()

          if len(self.dataset) >= 2:
            model.apply_regression(RealFeatures(testSet.T))

      except Exception as e:
        return [-1]

      return [totalTimer.ElapsedTime(), model]
def regression_linear_ridge_modular(fm_train=traindat, fm_test=testdat, label_train=label_traindat, tau=1e-6):

    from modshogun import RegressionLabels, RealFeatures
    from modshogun import LinearRidgeRegression

    rr = LinearRidgeRegression(tau, RealFeatures(traindat), RegressionLabels(label_train))
    rr.train()
    out = rr.apply(RealFeatures(fm_test)).get_labels()
    return out, rr
예제 #7
0
def regression_linear_ridge_modular(fm_train=traindat,
                                    fm_test=testdat,
                                    label_train=label_traindat,
                                    tau=1e-6):

    from modshogun import RegressionLabels, RealFeatures
    from modshogun import LinearRidgeRegression

    rr = LinearRidgeRegression(tau, RealFeatures(traindat),
                               RegressionLabels(label_train))
    rr.train()
    out = rr.apply(RealFeatures(fm_test)).get_labels()
    return out, rr
  def RunMetrics(self, options):
    if len(self.dataset) >= 3:

      X, y = SplitTrainData(self.dataset)
      tau = re.search("-t (\d+)", options)
      tau = 1.0 if not tau else int(tau.group(1))
      model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
      model.train()
      
      testData = LoadDataset(self.dataset[1])
      truelabels = LoadDataset(self.dataset[2])

      predictedlabels = model.apply_regression(RealFeatures(testData.T)).get_labels()

      SimpleMSE = Metrics.SimpleMeanSquaredError(truelabels, predictedlabels)
      metrics_dict = {}
      metrics_dict['Simple MSE'] = SimpleMSE
      return metrics_dict

    else:
      Log.Fatal("This method requires three datasets!")
예제 #9
0
        def RunLinearRidgeRegressionShogun(q):
            totalTimer = Timer()

            # Load input dataset.
            # If the dataset contains two files then the second file is the responses
            # file.
            Log.Info("Loading dataset", self.verbose)
            if len(self.dataset) >= 2:
                testSet = np.genfromtxt(self.dataset[1], delimiter=',')

            # Use the last row of the training set as the responses.
            X, y = SplitTrainData(self.dataset)
            if "alpha" in options:
                tau = float(options.pop("alpha"))
            else:
                Log.Fatal("Required parameter 'alpha' not specified!")
                raise Exception("missing parameter")

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

            try:
                with totalTimer:
                    # Perform linear ridge regression.
                    model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
                    model.train()

                    if len(self.dataset) >= 2:
                        model.apply_regression(RealFeatures(testSet.T))

            except Exception as e:
                q.put(-1)
                return -1

            time = totalTimer.ElapsedTime()
            q.put(time)
            return time
    def RunMetrics(self, options):
        if len(self.dataset) >= 3:

            X, y = SplitTrainData(self.dataset)
            tau = re.search("-t (\d+)", options)
            tau = 1.0 if not tau else int(tau.group(1))
            model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
            model.train()

            testData = LoadDataset(self.dataset[1])
            truelabels = LoadDataset(self.dataset[2])

            predictedlabels = model.apply_regression(RealFeatures(
                testData.T)).get_labels()

            SimpleMSE = Metrics.SimpleMeanSquaredError(truelabels,
                                                       predictedlabels)
            metrics_dict = {}
            metrics_dict['Simple MSE'] = SimpleMSE
            return metrics_dict

        else:
            Log.Fatal("This method requires three datasets!")
예제 #11
0
    def RunMetrics(self, options):
        Log.Info("Perform Linear Ridge Regression.", self.verbose)

        results = self.LinearRidgeRegressionShogun(options)
        if results < 0:
            return results

        metrics = {'Runtime': results}

        if len(self.dataset) >= 3:

            X, y = SplitTrainData(self.dataset)
            if "alpha" in options:
                tau = float(options.pop("alpha"))
            else:
                Log.Fatal("Required parameter 'alpha' not specified!")
                raise Exception("missing parameter")

            if len(options) > 0:
                Log.Fatal("Unknown parameters: " + str(options))
                raise Exception("unknown parameters")
            model = LRR(tau, RealFeatures(X.T), RegressionLabels(y))
            model.train()

            testData = LoadDataset(self.dataset[1])
            truelabels = LoadDataset(self.dataset[2])

            predictedlabels = model.apply_regression(RealFeatures(
                testData.T)).get_labels()

            SimpleMSE = Metrics.SimpleMeanSquaredError(truelabels,
                                                       predictedlabels)
            metrics['Simple MSE'] = SimpleMSE
            return metrics

        else:
            Log.Fatal("This method requires three datasets!")
예제 #12
0
ytest = yall[ntrain:]

# preprocess data
for i in xrange(p):
    X[:, i] -= np.mean(X[:, i])
    X[:, i] /= np.linalg.norm(X[:, i])
y -= np.mean(y)

# train LASSO
LeastAngleRegression = LeastAngleRegression()
LeastAngleRegression.set_labels(RegressionLabels(y))
LeastAngleRegression.train(RealFeatures(X.T))

# train ordinary LSR
if use_ridge:
    lsr = LinearRidgeRegression(0.01, RealFeatures(X.T), Labels(y))
    lsr.train()
else:
    lsr = LeastSquaresRegression()
    lsr.set_labels(RegressionLabels(y))
    lsr.train(RealFeatures(X.T))

# gather LASSO path
path = np.zeros((p, LeastAngleRegression.get_path_size()))
for i in xrange(path.shape[1]):
    path[:, i] = LeastAngleRegression.get_w(i)

evaluator = MeanSquaredError()

# apply on training data
mse_train = np.zeros(LeastAngleRegression.get_path_size())
예제 #13
0
ytest = yall[ntrain:]

# preprocess data
for i in xrange(p):
    X[:, i] -= np.mean(X[:, i])
    X[:, i] /= np.linalg.norm(X[:, i])
y -= np.mean(y)

# train LASSO
LeastAngleRegression = LeastAngleRegression()
LeastAngleRegression.set_labels(RegressionLabels(y))
LeastAngleRegression.train(RealFeatures(X.T))

# train ordinary LSR
if use_ridge:
    lsr = LinearRidgeRegression(0.01, RealFeatures(X.T), Labels(y))
    lsr.train()
else:
    lsr = LeastSquaresRegression()
    lsr.set_labels(RegressionLabels(y))
    lsr.train(RealFeatures(X.T))

# gather LASSO path
path = np.zeros((p, LeastAngleRegression.get_path_size()))
for i in xrange(path.shape[1]):
    path[:, i] = LeastAngleRegression.get_w(i)

evaluator = MeanSquaredError()

# apply on training data
mse_train = np.zeros(LeastAngleRegression.get_path_size())