(conts, k, nsteps=30, window_size=1)
return w, means, covars, priors
k expected classes,
    m data points,
    each with dim dimensions
k expected classes,
    m data points,
    each with dim dimensions
k expected classes m data points each with dim dimensions
def lac(conts, k, nsteps=30, window_size=1):
    """
    k expected classes,
    m data points,
    each with dim dimensions
    """
    import sys

    dim = len(conts)

    np.random.seed(42)
    # Initialize parameters
    priors = np.ones(k) / k

    print("Initializing")
    sys.stdout.flush()
    means, covars = initialize_random(conts, k)
    #means, covars = initialize_kmeans(conts, k)
    print("Done")

    w = [np.empty((k, len(c[0]),)) for c in conts]
    active = np.ones(k, dtype=bool)

    for i in range(1, nsteps + 1):
        for l, (c, cw) in enumerate(conts):
            lower = l - window_size if l - window_size >= 0 else None
            upper = l + window_size + 1 if l + window_size + 1 <= dim else None
            dims = slice(lower, upper)
            active_dim = min(l, window_size)

            x = c

            # E step
            for j in range(k):
                if any(np.abs(covars[j, dims]) < 1e-15):
                    active[j] = 0

                if active[j]:
                    det = covars[j, dims].prod()
                    inv_covars = 1. / covars[j, dims]
                    xn = x - means[j, dims]
                    factor = (2.0 * np.pi) ** (x.shape[1]/ 2.0) * det ** 0.5
                    w[l][j] = \
                        priors[j] \
                        * np.exp(np.sum(xn * inv_covars * xn, axis=1) * -.5) \
                        / factor
                else:
                    w[l][j] = 0
            w[l][active] /= w[l][active].sum(axis=0)

            # M step
            n = np.sum(w[l], axis=1)
            priors = n / np.sum(n)
            for j in range(k):
                if n[j]:
                    mu = np.dot(w[l][j, :] * cw, x[:, active_dim]) / (w[l][j, :] * cw).sum()

                    xn = x[:, active_dim] - mu
                    sigma = np.sum(xn ** 2 * w[l][j] * cw, axis=0) / (w[l][j, :] * cw).sum()

                    if np.isnan(mu).any() or np.isnan(sigma).any():
                        return w, means, covars, priors
                else:
                    active[j] = 0
                    mu = 0.
                    sigma = 0.
                means[j, l] = mu
                covars[j, l] = sigma

    # w = np.zeros((k, m))
    # for j in range(k):
    #     if active[j]:
    #         det = covars[j].prod()
    #         inv_covars = 1. / covars[j]
    #         xn = X - means[j]
    #         factor = (2.0 * np.pi) ** (xn.shape[1] / 2.0) * det ** 0.5
    #         w[j] = priors[j] * exp(-.5 * np.sum(xn * inv_covars * xn, axis=1)) / factor
    # w[active] /= w[active].sum(axis=0)

    return w, means, covars, priors
def lac ( conts , k , nsteps = 30 , window_size = 1 ) : import sys dim = len ( conts ) np . random . seed ( 42 ) # Initialize parameters priors = np . ones ( k ) / k print ( "Initializing" ) sys . stdout . flush ( ) means , covars = initialize_random ( conts , k ) #means, covars = initialize_kmeans(conts, k) print ( "Done" ) w = [ np . empty ( ( k , len ( c [ 0 ] ) , ) ) for c in conts ] active = np . ones ( k , dtype = bool ) for i in range ( 1 , nsteps + 1 ) : for l , ( c , cw ) in enumerate ( conts ) : lower = l - window_size if l - window_size >= 0 else None upper = l + window_size + 1 if l + window_size + 1 <= dim else None dims = slice ( lower , upper ) active_dim = min ( l , window_size ) x = c # E step for j in range ( k ) : if any ( np . abs ( covars [ j , dims ] ) < 1e-15 ) : active [ j ] = 0 if active [ j ] : det = covars [ j , dims ] . prod ( ) inv_covars = 1. / covars [ j , dims ] xn = x - means [ j , dims ] factor = ( 2.0 * np . pi ) ** ( x . shape [ 1 ] / 2.0 ) * det ** 0.5 w [ l ] [ j ] = priors [ j ] * np . exp ( np . sum ( xn * inv_covars * xn , axis = 1 ) * - .5 ) / factor else : w [ l ] [ j ] = 0 w [ l ] [ active ] /= w [ l ] [ active ] . sum ( axis = 0 ) # M step n = np . sum ( w [ l ] , axis = 1 ) priors = n / np . sum ( n ) for j in range ( k ) : if n [ j ] : mu = np . dot ( w [ l ] [ j , : ] * cw , x [ : , active_dim ] ) / ( w [ l ] [ j , : ] * cw ) . sum ( ) xn = x [ : , active_dim ] - mu sigma = np . sum ( xn ** 2 * w [ l ] [ j ] * cw , axis = 0 ) / ( w [ l ] [ j , : ] * cw ) . sum ( ) if np . isnan ( mu ) . any ( ) or np . isnan ( sigma ) . any ( ) : return w , means , covars , priors else : active [ j ] = 0 mu = 0. sigma = 0. means [ j , l ] = mu covars [ j , l ] = sigma # w = np.zeros((k, m)) # for j in range(k): #     if active[j]: #         det = covars[j].prod() #         inv_covars = 1. / covars[j] #         xn = X - means[j] #         factor = (2.0 * np.pi) ** (xn.shape[1] / 2.0) * det ** 0.5 #         w[j] = priors[j] * exp(-.5 * np.sum(xn * inv_covars * xn, axis=1)) / factor # w[active] /= w[active].sum(axis=0) return w , means , covars , priors