kernel/mismatch.py
def mismatch_kernel(k: int = 4, m: int = 2):
    """
    Substrings of len k with at most m mismatches
    """

    @memoize_id
    def mismatch_kernel_inner(X0: list, X1: list):
        # Same as spectrum kernel basically with a different feature vector
        symmetric = X0 is X1

        n0 = len(X0)
        X0_dict = feature_vectors(X0, k, m)

        if symmetric:
            K = np.zeros(shape=(n0, n0))
            for i in tqdm(range(n0), desc=f"Mismatch kernel (k={k}, m={m})"):
                X0i = X0_dict[i]
                for j in range(i, n0):
                    X0j = X0_dict[j]
                    K[i, j] = sum(count * X0j[substr] for substr, count in X0i.items())
                    K[j, i] = K[i, j]

            return normalize_kernel(K)

        else:
            n1 = len(X1)
            X1_dict = feature_vectors(X1, k, m)

            # Compute dot products
            K = np.zeros(shape=(n0, n1))
            for i in tqdm(range(n0), desc=f"Mismatch kernel (k={k}, m={m})"):
                X0i = X0_dict[i]
                for j in range(n1):
                    X1j = X1_dict[j]
                    K[i, j] = sum(count * X1j[substr] for substr, count in X0i.items())

            # Computes K(x, x) and K(y, y) for normalization
            rows = np.zeros(shape=n0)
            for i in range(n0):
                rows[i] = sum(count ** 2 for count in X0_dict[i].values())

            columns = np.zeros(shape=n1)
            for j in range(n1):
                columns[j] = sum(count ** 2 for count in X1_dict[j].values())

            return normalize_kernel(K, rows=rows, columns=columns)

    return mismatch_kernel_inner

    Substrings of len k with at most m mismatches
    
Substrings of len k with at most m mismatches
Substrings of len k with at most m mismatches
def mismatch_kernel(k: int = 4, m: int = 2):
    @memoize_id
    def mismatch_kernel_inner(X0: list, X1: list):
        symmetric = X0 is X1
        n0 = len(X0)
        X0_dict = feature_vectors(X0, k, m)
        if symmetric:
            K = np.zeros(shape=(n0, n0))
            for i in tqdm(range(n0), desc=f"Mismatch kernel (k={k}, m={m})"):
                X0i = X0_dict[i]
                for j in range(i, n0):
                    X0j = X0_dict[j]
                    K[i, j] = sum(count * X0j[substr] for substr, count in X0i.items())
                    K[j, i] = K[i, j]
            return normalize_kernel(K)
        else:
            n1 = len(X1)
            X1_dict = feature_vectors(X1, k, m)
            K = np.zeros(shape=(n0, n1))
            for i in tqdm(range(n0), desc=f"Mismatch kernel (k={k}, m={m})"):
                X0i = X0_dict[i]
                for j in range(n1):
                    X1j = X1_dict[j]
                    K[i, j] = sum(count * X1j[substr] for substr, count in X0i.items())
            rows = np.zeros(shape=n0)
            for i in range(n0):
                rows[i] = sum(count ** 2 for count in X0_dict[i].values())
            columns = np.zeros(shape=n1)
            for j in range(n1):
                columns[j] = sum(count ** 2 for count in X1_dict[j].values())
            return normalize_kernel(K, rows=rows, columns=columns)
    return mismatch_kernel_inner
def mismatch_kernel ( k : int = 4 , m : int = 2 ) : @ memoize_id def mismatch_kernel_inner ( X0 : list , X1 : list ) : symmetric = X0 is X1 n0 = len ( X0 ) X0_dict = feature_vectors ( X0 , k , m ) if symmetric : K = np . zeros ( shape = ( n0 , n0 ) ) for i in tqdm ( range ( n0 ) , desc = f"Mismatch kernel (k={k}, m={m})" ) : X0i = X0_dict [ i ] for j in range ( i , n0 ) : X0j = X0_dict [ j ] K [ i , j ] = sum ( count * X0j [ substr ] for substr , count in X0i . items ( ) ) K [ j , i ] = K [ i , j ] return normalize_kernel ( K ) else : n1 = len ( X1 ) X1_dict = feature_vectors ( X1 , k , m ) K = np . zeros ( shape = ( n0 , n1 ) ) for i in tqdm ( range ( n0 ) , desc = f"Mismatch kernel (k={k}, m={m})" ) : X0i = X0_dict [ i ] for j in range ( n1 ) : X1j = X1_dict [ j ] K [ i , j ] = sum ( count * X1j [ substr ] for substr , count in X0i . items ( ) ) rows = np . zeros ( shape = n0 ) for i in range ( n0 ) : rows [ i ] = sum ( count ** 2 for count in X0_dict [ i ] . values ( ) ) columns = np . zeros ( shape = n1 ) for j in range ( n1 ) : columns [ j ] = sum ( count ** 2 for count in X1_dict [ j ] . values ( ) ) return normalize_kernel ( K , rows = rows , columns = columns ) return mismatch_kernel_inner
Substrings of len k with at most m mismatches
Substrings of len k with at most m mismatches
"""
    Substrings of len k with at most m mismatches
    """ # Same as spectrum kernel basically with a different feature vector # Compute dot products # Computes K(x, x) and K(y, y) for normalization
k
int
m
int