Skip to content
Permalink
d54f9b9bdc
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
68 lines (53 sloc) 1.62 KB
#!/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)