def calc_SSE(data, means, idx):
    """
    Returns the total squared error.
    """
    K = means.shape[0]
    SSE = 0.0

    # Loop over all clusters
    for j in range(K):

        # Squared distance from the cluster center
        d = (data[idx==j, :] - means[j, :]) ** 2

        # Total squared error
        SSE += d.sum()

    return SSE