Commit 2b06e488 authored by Uwe Köckemann's avatar Uwe Köckemann
Browse files

Working on examples

parent 60e74a47
from moving_target_cplex import MovingTargetClsCplex, MovingTargetRegCplex
from moving_target_smt import MovingTargetClsSMT, MovingTargetRegSMT
def get_problem(problem_type, solver='cplex'):
if problem_type == 'classification':
if solver == 'cplex':
return MovingTargetClsCplex
elif solver == 'smt':
return MovingTargetClsSMT
else:
raise ValueError("Solver " + str(solver) + " not recognized!")
elif problem_type == 'regression':
if solver == 'cplex':
return MovingTargetRegCplex
elif solver == 'smt':
return MovingTargetRegSMT
else:
raise ValueError("Solver " + str(solver) + " not recognized!")
else:
raise ValueError("Problem type not understood: " + str(problem_type))
......@@ -108,8 +108,7 @@ class MovingTarget(ABC):
constraints = current[Symbolic('constraints')]
features = [att[0].string_value() for att in attributes if att[0] != label]
print("attributes ", attributes)
print("features ", features)
C = list()
for c in constraints:
......
......@@ -200,7 +200,7 @@ class MovingTargetClsCplex(MovingTarget):
self.variables = [
'y_%d_%d' % (i, j) for i in range(self.n_points) for j in range(self.n_classes)
]
print("Variables added: " + str(self.variables))
# print("Variables added: " + str(self.variables))
return mod
......@@ -234,7 +234,6 @@ class MovingTargetRegCplex(MovingTarget):
]
"""
for c in C:
print(c)
ctype = c[0]
# Translate the constraint to CPLEX language.
......@@ -368,7 +367,7 @@ class MovingTargetRegCplex(MovingTarget):
# Store variable names.
self.variables = ['y_%d' %i for i in idx_var]
print("Variables added: " + str(self.variables))
# print("Variables added: " + str(self.variables))
return mod
......
......@@ -283,7 +283,7 @@ class MovingTargetRegSMT(MovingTarget):
# Store variable names.
self.variables = ['y_%d' %i for i in idx_var]
print("Variables added: " + str(self.variables))
# print("Variables added: " + str(self.variables))
return mod
......@@ -447,6 +447,6 @@ class MovingTargetClsSMT(MovingTarget):
self.variables = [
'y_%d_%d' % (i, j) for i in range(self.n_points) for j in range(self.n_classes)
]
print("Variables added: " + str(self.variables))
# print("Variables added: " + str(self.variables))
return mod
......@@ -50,57 +50,6 @@ class SciKitLearnFunction(Function):
def predict_proba(self, x):
return self.model.predict_proba(x)
class RandomForestLearner(Function):
def __init__(self):
self.n_estimators = 50
self.max_depth = 5
self.model = RandomForestClassifier(n_estimators=self.n_estimators, max_depth=self.max_depth)
def configure(self, cfg, freg):
self.n_estimators = cfg.get_or_default(Symbolic("n_estimators"), Integer(self.n_estimators)).int_value()
self.n_estimators = cfg.get_or_default(Symbolic("max_depth"), Integer(self.max_depth)).int_value()
self.model = RandomForestClassifier(n_estimators=self.n_estimators, max_depth=self.max_depth)
def apply(self, mlProblem):
x, y = splitAiddlMlProblem(mlProblem)
self.model.fit(x, y)
y_p = self.model.predict(x)
r = []
for e in y_p:
r.append(parse_term(str(e)))
return List(r)
# def fit(self, x, y):
# self.model.fit(x, y)
# def predict(self, x):
# return self.model.predict(x)
# def predict_proba(self, x):
# return self.model.predict_proba(x)
class GBTree(Function):
def __init__(self):
self.n_estimators = 50
self.min_samples_leaf = 5
self.model = GradientBoostingRegressor(n_estimators=self.n_estimators,
min_samples_leaf=self.min_samples_leaf)
def configure(self, cfg, freg):
self.n_estimators = cfg.get_or_default(Symbolic("n_estimators"), Integer(self.n_estimators)).int_value()
self.n_estimators = cfg.get_or_default(Symbolic("min_samples_leaf"), Integer(self.min_samples_leaf)).int_value()
self.model = GradientBoostingRegressor(n_estimators=self.n_estimators,
min_samples_leaf=self.min_samples_leaf)
def apply(self, mlProblem):
x, y = splitAiddlMlProblem(mlProblem)
self.model.fit(x, y)
y_p = self.model.predict(x)
print(y_p)
r = []
for e in y_p:
r.append(parse_term(str(e)))
return List(r)
def splitAiddlMlProblem(mlProblem):
atts = mlProblem[Symbolic("attributes")]
label = mlProblem[Symbolic("label")]
......
import numpy as np
from moving_target_cplex import MovingTargetClsCplex, MovingTargetRegCplex
from moving_target_smt import MovingTargetClsSMT, MovingTargetRegSMT
def didi_r(x, y, pfeat):
"""
Compute the disparate impact discrimination index of a given dataset.
......@@ -47,21 +43,3 @@ def didi_c(x, y, pfeat):
return tot
def get_problem(problem_type, solver='cplex'):
if problem_type == 'classification':
if solver == 'cplex':
return MovingTargetClsCplex
elif solver == 'smt':
return MovingTargetClsSMT
else:
raise ValueError("Solver " + str(solver) + " not recognized!")
elif problem_type == 'regression':
if solver == 'cplex':
return MovingTargetRegCplex
elif solver == 'smt':
return MovingTargetRegSMT
else:
raise ValueError("Solver " + str(solver) + " not recognized!")
else:
raise ValueError("Problem type not understood: " + str(problem_type))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment