Esempio n. 1
0
    def fit(self, X, y):
        """
        @param X : an n-by-m array-like object containing n examples with m
                   features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._X = np.asmatrix(X)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._X))
        else:
            C = self.C

        K, H, f, A, b, lb, ub = self._setup_svm(self._X, self._y, C)

        # Solve QP
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Esempio n. 2
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        bs = BagSplitter([elem for elem in map(np.asmatrix, bags)],
                         np.asmatrix(y).reshape((-1, 1)))
        self._bags = bs.neg_inst_as_bags + bs.pos_bags
        self._y = np.matrix(
            np.vstack([-np.ones((bs.L_n, 1)),
                       np.ones((bs.X_p, 1))]))
        if self.scale_C:
            iC = float(self.C) / bs.L_n
            bC = float(self.C) / bs.X_p
        else:
            iC = self.C
            bC = self.C
        C = np.vstack([iC * np.ones((bs.L_n, 1)), bC * np.ones((bs.X_p, 1))])

        if self.verbose:
            print('Setup QP...')
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Adjust f with balancing terms
        factors = np.vstack([
            np.matrix(np.ones((bs.L_n, 1))),
            np.matrix([2.0 / len(bag) - 1.0 for bag in bs.pos_bags]).T
        ])
        f = np.multiply(f, factors)

        if self.verbose:
            print('Solving QP...')
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)

        # Recompute predictions for full bags
        self._bag_predictions = self.predict(bags)
Esempio n. 3
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        bs = BagSplitter(map(np.asmatrix, bags),
                         np.asmatrix(y).reshape((-1, 1)))
        self._bags = bs.neg_inst_as_bags + bs.pos_bags
        self._y = np.matrix(np.vstack([-np.ones((bs.L_n, 1)),
                                       np.ones((bs.X_p, 1))]))
        if self.scale_C:
            iC = float(self.C) / bs.L_n
            bC = float(self.C) / bs.X_p
        else:
            iC = self.C
            bC = self.C
        C = np.vstack([iC * np.ones((bs.L_n, 1)),
                       bC * np.ones((bs.X_p, 1))])

        if self.verbose:
            print('Setup QP...')
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Adjust f with balancing terms
        factors = np.vstack([np.matrix(np.ones((bs.L_n, 1))),
                             np.matrix([2.0 / len(bag) - 1.0
                                        for bag in bs.pos_bags]).T])
        f = np.multiply(f, factors)

        if self.verbose:
            print('Solving QP...')
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)

        # Recompute predictions for full bags
        self._bag_predictions = self.predict(bags)
Esempio n. 4
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._bags = map(np.asmatrix, bags)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._bags))
        else:
            C = self.C

        if self.verbose:
            print('Setup QP...')
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Solve QP
        if self.verbose:
            print('Solving QP...')
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Esempio n. 5
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._bags = list(map(np.asmatrix, bags))
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._bags))
        else:
            C = self.C

        if self.verbose:
            print('Setup QP...')
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Solve QP
        if self.verbose:
            print('Solving QP...')
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)