Beispiel #1
0
def alignment(K1, K2):
    """evaluate the kernel alignment between two kernels.

    Parameters
    ----------
    K1 : (n,n) ndarray,
          the first kernel used to evaluate the alignment.
    K2 : (n,n) ndarray,
         the last kernel usedto evaluate the alignment.

    Returns
    -------
    v : np.float64,
        the value of kernel alignment between *K1* and *K2*.
    """
    K1 = check_squared(K1)
    K2 = check_squared(K2)
    #def ff(k1,k2):
    #    n = len(k1)
    #    s = 0
    #    for i in xrange(n):
    #        for j in xrange(n):
    #            s += k1[i,j]*k2[i,j]
    #    return s
    f0 = np.sum(K1 * K2)  #ff(K1,K2)
    f1 = np.sum(K1 * K1)  #ff(K1,K1)
    f2 = np.sum(K2 * K2)  #ff(K2,K2)
    return (f0 / np.sqrt(f1 * f2))
Beispiel #2
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']))
Beispiel #3
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']))
Beispiel #4
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))
Beispiel #5
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])])
Beispiel #6
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))
Beispiel #7
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])])
Beispiel #8
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
Beispiel #9
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
Beispiel #10
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
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
Beispiel #12
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
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
Beispiel #14
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
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