Exemplo n.º 1
0
def _mario_helper(bands, s, poly_order, opts, callback):
    # Build the polynomial basis over the bands.
    P = hermvander(bands, poly_order - 1)
    f = P.sum(axis=0)

    if HAS_CVXOPT:
        solvers.options['show_progress'] = opts['disp']
        solvers.options['maxiters'] = opts['maxiter']
        solvers.options['abstol'] = opts['tol']
        solvers.options['reltol'] = opts['tol']
        solvers.options['feastol'] = 1e-100  # For some reason this helps.
        try:
            res = solvers.lp(cvx_matrix(f), cvx_matrix(-P), cvx_matrix(-s))
        except ValueError as e:
            # This can be thrown when poly_order is too large for the data size.
            res = {'status': e.message, 'x': None}
        return res, P

    res = linprog(f,
                  A_ub=-P,
                  b_ub=-s,
                  bounds=(-np.inf, np.inf),
                  options=opts,
                  callback=callback)
    res = {'status': res.message, 'x': res.x if res.success else None}
    return res, P
Exemplo n.º 2
0
def cvxopt_solver(G, h, A, b, c, n):
    # cvxopt doesn't allow redundant constraints in the linear program Ax = b,
    # so we need to do some preprocessing to find and remove any linearly
    # dependent rows in the augmented matrix [A | b].
    #
    # First we do Gaussian elimination to put the augmented matrix into row
    # echelon (reduced row echelon form is not necessary). Since b comes as a
    # numpy array (that is, a row vector), we need to convert it to a numpy
    # matrix before transposing it (that is, to a column vector).
    b = np.mat(b).T
    A_b = np.hstack((A, b))
    A_b, permutation = to_row_echelon(np.hstack((A, b)))
    # Next, we apply the inverse of the permutation applied to compute the row
    # echelon form.
    P = dictionary_to_permutation(permutation)
    A_b = P.I * A_b
    # Trim any rows that are all zeros. The call to np.any returns an array of
    # Booleans that correspond to whether a row in A_b is all zeros. Indexing
    # A_b by an array of Booleans acts as a selector. We need to use np.asarray
    # in order for indexing to work, since it expects a row vector instead of a
    # column vector.
    A_b = A_b[np.any(np.asarray(A_b) != 0, axis=1)]
    # Split the augmented matrix back into a matrix and a vector.
    A, b = A_b[:, :-1], A_b[:, -1]
    # Apply the linear programming solver; cvxopt requires that these are all
    # of a special type of cvx-specific matrix.
    G, h, A, b, c = (cvx_matrix(M) for M in (G, h, A, b, c))
    solution = cvx_solvers.lp(c, G, h, A, b)
    if solution['status'] == 'optimal':
        return True, solution['x']
    # TODO status could be 'unknown' here, but we're currently ignoring that
    return False, None
Exemplo n.º 3
0
def cvxopt_solver(G, h, A, b, c, n):
    # cvxopt doesn't allow redundant constraints in the linear program Ax = b,
    # so we need to do some preprocessing to find and remove any linearly
    # dependent rows in the augmented matrix [A | b].
    #
    # First we do Gaussian elimination to put the augmented matrix into row
    # echelon (reduced row echelon form is not necessary). Since b comes as a
    # numpy array (that is, a row vector), we need to convert it to a numpy
    # matrix before transposing it (that is, to a column vector).
    b = np.mat(b).T
    A_b = np.hstack((A, b))
    A_b, permutation = to_row_echelon(np.hstack((A, b)))
    # Next, we apply the inverse of the permutation applied to compute the row
    # echelon form.
    P = dictionary_to_permutation(permutation)
    A_b = P.I * A_b
    # Trim any rows that are all zeros. The call to np.any returns an array of
    # Booleans that correspond to whether a row in A_b is all zeros. Indexing
    # A_b by an array of Booleans acts as a selector. We need to use np.asarray
    # in order for indexing to work, since it expects a row vector instead of a
    # column vector.
    A_b = A_b[np.any(np.asarray(A_b) != 0, axis=1)]
    # Split the augmented matrix back into a matrix and a vector.
    A, b = A_b[:, :-1], A_b[:, -1]
    # Apply the linear programming solver; cvxopt requires that these are all
    # of a special type of cvx-specific matrix.
    G, h, A, b, c = (cvx_matrix(M) for M in (G, h, A, b, c))
    solution = cvx_solvers.lp(c, G, h, A, b)
    if solution['status'] == 'optimal':
        return True, solution['x']
    # TODO status could be 'unknown' here, but we're currently ignoring that
    return False, None
Exemplo n.º 4
0
def _mario_helper(bands, s, poly_order, opts, callback):
  # Build the polynomial basis over the bands.
  P = hermvander(bands, poly_order-1)
  f = P.sum(axis=0)

  if HAS_CVXOPT:
    solvers.options['show_progress'] = opts['disp']
    solvers.options['maxiters'] = opts['maxiter']
    solvers.options['abstol'] = opts['tol']
    solvers.options['reltol'] = opts['tol']
    solvers.options['feastol'] = 1e-100  # For some reason this helps.
    try:
      res = solvers.lp(cvx_matrix(f), cvx_matrix(-P), cvx_matrix(-s))
    except ValueError as e:
      # This can be thrown when poly_order is too large for the data size.
      res = {'status': e.message, 'x': None}
    return res, P

  res = linprog(f, A_ub=-P, b_ub=-s, bounds=(-np.inf,np.inf), options=opts,
                callback=callback)
  res = {'status': res.message, 'x': res.x if res.success else None}
  return res, P
Exemplo n.º 5
0
def binary_part_b(train, test):
    train = train.loc[(train[784] == 2) | (train[784] == 3)]
    train = train.reset_index(drop=True)
    train_X = train.iloc[:, 0:784]
    train_Y = train.iloc[:, 784]
    train_X = (train_X.as_matrix()) / 255
    train_Y = train_Y.as_matrix()
    train_Y[train_Y == 2] = -1
    train_Y[train_Y == 3] = 1
    test = test.loc[(test[784] == 2) | (test[784] == 3)]
    test = test.reset_index(drop=True)
    test_X = test.iloc[:, 0:784]
    test_Y = test.iloc[:, 784]
    test_X = (test_X.as_matrix()) / 255
    test_Y = test_Y.as_matrix()
    test_Y[test_Y == 2] = -1
    test_Y[test_Y == 3] = 1
    R = pdist(train_X, 'sqeuclidean')
    K = np.exp(-0.05 * sqf(R))
    x = train_Y[:, None]
    y = np.transpose(train_Y[:, None])
    K = x @ y * K
    pts = train_X.shape[0]
    p = cvx_matrix(K)
    q = cvx_matrix(-1 * np.ones((pts, 1)))
    m = np.diag(-1 * np.ones(pts))
    n = np.identity(pts)
    G = cvx_matrix(np.vstack((m, n)))
    h = cvx_matrix(np.hstack((np.zeros(pts), np.ones(pts))))
    A = cvx_matrix(train_Y.reshape(1, -1).astype(np.double))
    b = cvx_matrix(np.zeros(1))
    result = solvers.qp(p, q, G, h, A, b)
    x_val = np.array(result['x']).flatten()
    val = .0001
    y_val = np.arange(len(x_val))[x_val > val]
    alpha = x_val[x_val > val]
    xi = train_X[x_val > val]
    yi = train_Y[x_val > val]
    b = 0
    for i in range(len(yi)):
        b += yi[i]
        temp = K[y_val[i], x_val > val]
        b -= np.sum(alpha * temp * yi)
    b /= len(yi)
    pts = test_X.shape[0]
    pred_y = np.zeros(pts)
    for i in range(pts):
        val = 0
        for j in range(len(alpha)):
            temp = test_X[i] - xi[j]
            temp = np.exp((temp.dot(temp)) * 0.05 * -1)
            val += alpha[j] * yi[j] * temp
        pred_y[i] = val
    pred_y += b
    pred_y[pred_y >= 0] = 1
    pred_y[pred_y < 0] = -1
    accuracy = (np.sum(pred_y == test_Y)) / pts * 100
    print(accuracy)
Exemplo n.º 6
0
def binary_part_a(train, test):
    train = train.loc[(train[784] == 2) | (train[784] == 3)]
    train = train.reset_index(drop=True)
    train_X = train.iloc[:, 0:784]
    train_Y = train.iloc[:, 784]
    train_X = (train_X.as_matrix()) / 255
    train_Y = train_Y.as_matrix()
    train_Y[train_Y == 2] = -1
    train_Y[train_Y == 3] = 1
    test = test.loc[(test[784] == 2) | (test[784] == 3)]
    test = test.reset_index(drop=True)
    test_X = test.iloc[:, 0:784]
    test_Y = test.iloc[:, 784]
    test_X = (test_X.as_matrix()) / 255
    test_Y = test_Y.as_matrix()
    test_Y[test_Y == 2] = -1
    test_Y[test_Y == 3] = 1
    H = train_Y[:, None] * train_X
    K = H @ np.transpose(H)
    pts = train_X.shape[0]
    f = train_X.shape[1]
    P = cvx_matrix(K)
    q = -1 * cvx_matrix(np.ones((pts, 1)))
    m = np.diag(-1 * np.ones(pts))
    n = np.identity(pts)
    G = cvx_matrix(np.vstack((m, n)))
    h = cvx_matrix(np.hstack((np.zeros(pts), np.ones(pts))))
    A = cvx_matrix(train_Y.reshape(1, -1).astype(np.double))
    b = cvx_matrix(np.zeros(1))
    result = solvers.qp(P, q, G, h, A, b)
    x_val = np.array(result['x'])
    w = np.sum(x_val * train_Y[:, None] * train_X, axis=0)
    b = (train_Y[(x_val > 0.0001).reshape(-1)] -
         (train_X[(x_val > 0.0001).reshape(-1)].dot(w)))[0]
    pts = test_Y.shape[0]
    pred_y = test_X.dot(w) + b
    pred_y[pred_y < 0] = -1
    pred_y[pred_y >= 0] = 1
    accuracy = np.sum(pred_y == test_Y) / pts * 100
    print(accuracy)
Exemplo n.º 7
0
def multi_part_a(train, test):
    k = list(itertools.combinations([i for i in range(5)], 2))
    #k=[(0,1),(7,9)]
    total_classes = len(k)
    final_pred = []
    list3 = []
    for i in range(total_classes):
        train_X, train_Y = train_data_format(train, k[i][0], k[i][1])
        test_X, test_Y = test_data_format(test, k[i][0], k[i][1])
        R = pdist(train_X, 'sqeuclidean')
        K = np.exp(-0.05 * sqf(R))
        x = train_Y[:, None]
        y = np.transpose(train_Y[:, None])
        K = x @ y * K
        pts = train_X.shape[0]
        p = cvx_matrix(K)
        q = cvx_matrix(-1 * np.ones((pts, 1)))
        m = np.diag(-1 * np.ones(pts))
        n = np.identity(pts)
        G = cvx_matrix(np.vstack((m, n)))
        h = cvx_matrix(np.hstack((np.zeros(pts), np.ones(pts))))
        A = cvx_matrix(train_Y.reshape(1, -1).astype(np.double))
        b = cvx_matrix(np.zeros(1))
        result = solvers.qp(p, q, G, h, A, b)
        x_val = np.array(result['x']).flatten()
        #print(x_val)
        val = .0001
        y_val = np.arange(len(x_val))[x_val > val]
        alpha = x_val[x_val > val]
        #print(alpha)
        xi = train_X[x_val > val]
        #print(xi)
        yi = train_Y[x_val > val]
        #print(yi)
        b = 0
        for ii in range(len(yi)):
            b += yi[ii]
            temp = K[y_val[ii], x_val > val]
            b -= np.sum(alpha * temp * yi)
        b /= len(yi)
        pts = test_X.shape[0]
        pred_y = np.zeros(pts)
        for iii in range(pts):
            val = 0
            for j in range(len(alpha)):
                temp = test_X[iii] - xi[j]
                temp = np.exp((temp.dot(temp)) * 0.05 * -1)
                val += alpha[j] * yi[j] * temp
            pred_y[iii] = val
        pred_y += b
        pred_y[pred_y >= 0] = 1
        pred_y[pred_y < 0] = -1
        accuracy = (np.sum(pred_y == test_Y)) / pts * 100
        print(accuracy)
        pred_y[pred_y < 0] = k[i][0]
        pred_y[pred_y > 0] = k[i][1]
        final_pred.append(pred_y)
        list2 = []
        for j in final_pred[i]:
            if j == 1:
                list2.append(k[i][1])
            else:
                list2.append(k[i][0])
        list3.append(list2)
        print('value of i is:' + str(i))
    list4 = np.transpose(np.array(list3))
    final_predcition = []
    for it in range(list4.shape[0]):
        val = max(set(list(list4[i])), key=list(list4[i]).count)
        final_predcition.append(val)
    final_predcition = np.array(final_predcition)
    accuracy = (np.sum(final_predcition == test_Y)) / pts * 100
    print(accuracy)
    return final_predcition
Exemplo n.º 8
0
    def fit(self, X, Y):
        n_samples = X.shape[0]
        #		n_features = X.shape[1]

        K = np.zeros((n_samples, n_samples))
        if self.kernel is 'linear':
            for i in range(n_samples):
                for j in range(n_samples):
                    K[i, j] = linear(X[i], X[j])

        elif self.kernel is 'poly':
            for i in range(n_samples):
                for j in range(n_samples):
                    K[i, j] = polynomial(X[i], X[j], self.p)

        elif self.kernel is 'rbf':
            for i in range(n_samples):
                for j in range(n_samples):
                    K[i, j] = gaussian(X[i], X[j], self.gamma)

        else:
            raise Exception('Invalid kernel')

        #Declaring parameters for CVX solvers
        P = cvx_matrix(np.outer(Y, Y) * K)
        q = cvx_matrix(-np.ones((n_samples, 1)))
        Y = Y.astype('double')
        A = cvx_matrix(Y.reshape((1, -1)))
        b = cvx_matrix(0.0)

        if self.C is None:
            G = cvx_matrix(-np.identity(n_samples))
            h = cvx_matrix(np.zeros((n_samples, 1)))
        else:
            t1 = -np.identity(n_samples)
            t2 = np.identity(n_samples)
            G = cvx_matrix(np.vstack((t1, t2)))
            t1 = np.zeros((n_samples, 1))
            t2 = np.ones((n_samples, 1)) * self.C
            h = cvx_matrix(np.vstack((t1, t2)))

        answer = cvx_solvers.qp(P, q, G, h, A, b)
        lag_mul = np.ravel(answer['x'])
        support_vectors = lag_mul > 1e-5
        indexes = np.arange(len(lag_mul))
        lag_indexes = indexes[support_vectors]
        self.lag_mul = lag_mul[support_vectors]
        self.sv_x = X[support_vectors]
        self.support_vectors_ = self.sv_x.astype()
        #		for i in range(self.sv_x.shape[0]):
        #			self.support_vectors_.append(float(self.sv_x[i]))
        print(self.support_vectors_)
        self.sv_y = Y[support_vectors]
        #		print(self.sv_x,self.sv_y)
        print('No. of support vectors = ' + str(len(self.lag_mul)))

        #intercept
        self.b = 0
        for i in range(len(self.lag_mul)):
            #			print(K[lag_indexes[i],lag_indexes].shape)
            self.b = self.sv_y[i] - np.sum(
                self.lag_mul * self.sv_y * K[lag_indexes[i], lag_indexes])


#			print(self.b)
        self.b = self.b / len(self.lag_mul)