Exemplo n.º 1
0
def part1():
    points = get_input()
    sparse = get_sparse_matrix(points).tocsc()
    dense = np.asarray(sparse.toarray(), dtype=str)
    for coord, _ in np.ndenumerate(dense):
        v = closest_node_value(coord, sparse)
        dense[coord] = v
    borders = [dense[0], dense[-1], dense[:, 0], dense[:, -1]]
    infinite = set()
    for line in borders:
        for c in line:
            infinite.add(c)
    char, counts = np.unique(dense, return_counts=True)
    possible = {c: cnt for c, cnt in zip(char, counts) if c not in infinite}
    return max(possible.items(), key=lambda x: x[1])
Exemplo n.º 2
0
def entropy2(x, y):
    """Joint entropy of paired samples X and Y"""
    #
    # Bin each image into 256 gray levels
    #
    x = (stretch(x) * 255).astype(int)
    y = (stretch(y) * 255).astype(int)
    #
    # create an image where each pixel with the same X & Y gets
    # the same value
    #
    xy = 256 * x + y
    xy = xy.flatten()
    sparse = scipy.sparse.coo_matrix((np.ones(xy.shape), (xy, np.zeros(xy.shape))))
    histogram = sparse.toarray()
    n = np.sum(histogram)
    if n > 0 and np.max(histogram) > 0:
        histogram = histogram[histogram > 0]
        return np.log2(n) - old_div(np.sum(histogram * np.log2(histogram)), n)
    else:
        return 0
Exemplo n.º 3
0
def entropy2(x,y):
    '''Joint entropy of paired samples X and Y'''
    #
    # Bin each image into 256 gray levels
    #
    x = (stretch(x) * 255).astype(int)
    y = (stretch(y) * 255).astype(int)
    #
    # create an image where each pixel with the same X & Y gets
    # the same value
    #
    xy = 256*x+y
    xy = xy.flatten()
    sparse = scipy.sparse.coo_matrix((np.ones(xy.shape), 
                                      (xy,np.zeros(xy.shape))))
    histogram = sparse.toarray()
    n=np.sum(histogram)
    if n > 0 and np.max(histogram) > 0:
        histogram = histogram[histogram>0]
        return np.log2(n) - np.sum(histogram * np.log2(histogram)) / n
    else:
        return 0