Ejemplo n.º 1
0
def radius(K):
    """evaluate the radius of the MEB (Minimum Enclosing Ball) of examples in
    feature space.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    r : np.float64,
        the radius of the minimum enclosing ball of examples in feature space.
    """
    check_squared(K)
    n = K.shape[0]
    P = 2 * matrix(K)
    p = -matrix([K[i, i] for i in range(n)])
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress'] = False
    sol = solvers.qp(P, p, G, h, A, b)
    return np.sqrt(abs(sol['primal objective']))
Ejemplo n.º 2
0
def frobenius(K):
    """return the frobenius-norm of the kernel as input.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    t : np.float64,
        the frobenius-norm of *K*
    """
    check_squared(K)
    return np.sqrt(np.sum(K**2))
Ejemplo n.º 3
0
def trace(K):
    """return the trace of the kernel as input.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    t : np.float64,
        the trace of *K*
    """
    check_squared(K)
    return sum([K[i, i] for i in range(K.shape[0])])
Ejemplo n.º 4
0
def spectral_ratio(K, norm=True):
    """return the spectral ratio of the kernel as input.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.
    norm : bool=True,
           True if we want the normalized spectral ratio.
    
    Returns
    -------
    t : np.float64,
        the spectral ratio of *K*, normalized iif *norm=True*
    """
    check_squared(K)
    n = K.shape[0]
    c = trace(K) / frobenius(K)
    return (c - 1) / (np.sqrt(n) - 1) if norm else c
Ejemplo n.º 5
0
def kernel_centering(K):
    """move a squared kernel at the center of axis

    Parameters
    ----------
    K : (n,n) ndarray,
        the squared kernel matrix.
        
    Returns
    -------
    Kc : ndarray,
         the centered version of *K*.
    """
    K = check_squared(K)
    N = K.shape[0]
    I = np.ones(K.shape)
    C = np.diag(np.ones(N)) - (1.0 / N * I)
    Kc = np.dot(np.dot(C, K), C)
    return Kc
Ejemplo n.º 6
0
def tracenorm(K):
    """normalize the trace of a squared kernel matrix

    Parameters
    ----------
    K : (n,n) ndarray,
        the squared kernel matrix.

    Returns
    -------
    Kt : ndarray,
         the trace-normalized version of *K*.

    Notes
    -----
    In trace-normalization, the kernel is divided by the average of the diagonal.
    """
    K = check_squared(K)
    trn = trace(K) / K.shape[0]
    return K / trn
Ejemplo n.º 7
0
def kernel_normalization(K):
    """normalize a squared kernel matrix

    Parameters
    ----------
    K : (n,n) ndarray,
        the squared kernel matrix.

    Returns
    -------
    Kn : ndarray,
         the normalized version of *K*.

    Notes
    -----
    Given a kernel K, the normalized version is defines as:
    
    .. math:: \hat{k}(x,z) = \frac{k(x,z)}{\sqrt{k(x,x)\cdot k(z,z)}}
    """
    K = check_squared(K)
    n = K.shape[0]
    d = np.array([[K[i, i] for i in range(n)]])
    Kn = K / np.sqrt(np.dot(d.T, d))
    return Kn