Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
discrete regression with entropy minimisation added
- Loading branch information
Showing
5 changed files
with
130 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from collections import Counter, defaultdict | ||
from copy import copy | ||
import random | ||
import sys | ||
import time | ||
|
||
from crisp import map_to_majority, marginals | ||
from entropy import entropy | ||
|
||
|
||
def regress(X, Y): | ||
# target Y, feature X | ||
max_iterations = 10000 | ||
scx = entropy(X) | ||
len_dom_y = len(set(Y)) | ||
# print scx, | ||
f = map_to_majority(X, Y) | ||
|
||
supp_x = list(set(X)) | ||
supp_y = list(set(Y)) | ||
|
||
pair = zip(X, Y) | ||
res = [y - f[x] for x, y in pair] | ||
cur_res_codelen = entropy(res) | ||
|
||
j = 0 | ||
minimized = True | ||
while j < max_iterations and minimized: | ||
random.shuffle(supp_x) | ||
minimized = False | ||
|
||
for x_to_map in supp_x: | ||
best_res_codelen = sys.float_info.max | ||
best_cand_y = None | ||
|
||
for cand_y in supp_y: | ||
if cand_y == f[x_to_map]: | ||
continue | ||
|
||
res = [y - f[x] if x != x_to_map else y - | ||
cand_y for x, y in pair] | ||
res_codelen = entropy(res) | ||
|
||
if res_codelen < best_res_codelen: | ||
best_res_codelen = res_codelen | ||
best_cand_y = cand_y | ||
|
||
if best_res_codelen < cur_res_codelen: | ||
cur_res_codelen = best_res_codelen | ||
f[x_to_map] = best_cand_y | ||
minimized = True | ||
j += 1 | ||
# print cur_res_codelen | ||
return scx + cur_res_codelen | ||
|
||
|
||
def crispe(X, Y): | ||
sxtoy = regress(X, Y) | ||
sytox = regress(Y, X) | ||
return (sxtoy, sytox) | ||
|
||
|
||
if __name__ == "__main__": | ||
from test_benchmark import load_pair | ||
X, Y = load_pair(99) | ||
print crispe(X, Y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python | ||
"""Computes entropy of a sequence of messages | ||
""" | ||
from __future__ import division | ||
from collections import Counter | ||
from math import log | ||
|
||
|
||
def entropy(sequence): | ||
res = 0 | ||
n = len(sequence) | ||
counts = Counter(sequence).values() | ||
for count in counts: | ||
res -= (count / n) * (log(count, 2) - log(n, 2)) | ||
return res | ||
|
||
|
||
if __name__=="__main__": | ||
print entropy([1, 2, 1, 1, 1, 1]) | ||
print entropy([1, 1, 1, 1, 1, 1]) | ||
print entropy([1, 1, 1, 2, 2, 2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters