|
|
@ -18,6 +18,7 @@ class PrimalSolutionComponent(Component):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
A component that predicts primal solutions.
|
|
|
|
A component that predicts primal solutions.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
def __init__(self,
|
|
|
|
classifier=AdaptiveClassifier(),
|
|
|
|
classifier=AdaptiveClassifier(),
|
|
|
|
mode="exact",
|
|
|
|
mode="exact",
|
|
|
@ -73,7 +74,7 @@ class PrimalSolutionComponent(Component):
|
|
|
|
proba = pred.predict_proba(x_train)
|
|
|
|
proba = pred.predict_proba(x_train)
|
|
|
|
assert isinstance(proba, np.ndarray), \
|
|
|
|
assert isinstance(proba, np.ndarray), \
|
|
|
|
"classifier should return numpy array"
|
|
|
|
"classifier should return numpy array"
|
|
|
|
assert proba.shape == (x_train.shape[0], 2),\
|
|
|
|
assert proba.shape == (x_train.shape[0], 2), \
|
|
|
|
"classifier should return (%d,%d)-shaped array, not %s" % (
|
|
|
|
"classifier should return (%d,%d)-shaped array, not %s" % (
|
|
|
|
x_train.shape[0], 2, str(proba.shape))
|
|
|
|
x_train.shape[0], 2, str(proba.shape))
|
|
|
|
|
|
|
|
|
|
|
@ -89,7 +90,7 @@ class PrimalSolutionComponent(Component):
|
|
|
|
if thresholds[k + 1] < self.min_threshold[label]:
|
|
|
|
if thresholds[k + 1] < self.min_threshold[label]:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
k = k + 1
|
|
|
|
k = k + 1
|
|
|
|
logger.debug(" Setting threshold to %.4f (fpr=%.4f, tpr=%.4f)"%
|
|
|
|
logger.debug(" Setting threshold to %.4f (fpr=%.4f, tpr=%.4f)" %
|
|
|
|
(thresholds[k], fpr[k], tpr[k]))
|
|
|
|
(thresholds[k], fpr[k], tpr[k]))
|
|
|
|
self.thresholds[category, label] = thresholds[k]
|
|
|
|
self.thresholds[category, label] = thresholds[k]
|
|
|
|
|
|
|
|
|
|
|
@ -113,7 +114,8 @@ class PrimalSolutionComponent(Component):
|
|
|
|
return solution
|
|
|
|
return solution
|
|
|
|
|
|
|
|
|
|
|
|
def evaluate(self, instances):
|
|
|
|
def evaluate(self, instances):
|
|
|
|
ev = {}
|
|
|
|
ev = {"Fix zero": {},
|
|
|
|
|
|
|
|
"Fix one": {}}
|
|
|
|
for instance_idx in tqdm(range(len(instances))):
|
|
|
|
for instance_idx in tqdm(range(len(instances))):
|
|
|
|
instance = instances[instance_idx]
|
|
|
|
instance = instances[instance_idx]
|
|
|
|
solution_actual = instance.solution
|
|
|
|
solution_actual = instance.solution
|
|
|
@ -146,8 +148,6 @@ class PrimalSolutionComponent(Component):
|
|
|
|
tn_one = len(pred_one_negative & vars_zero)
|
|
|
|
tn_one = len(pred_one_negative & vars_zero)
|
|
|
|
fn_one = len(pred_one_negative & vars_one)
|
|
|
|
fn_one = len(pred_one_negative & vars_one)
|
|
|
|
|
|
|
|
|
|
|
|
ev[instance_idx] = {
|
|
|
|
ev["Fix zero"][instance_idx] = classifier_evaluation_dict(tp_zero, tn_zero, fp_zero, fn_zero)
|
|
|
|
"Fix zero": classifier_evaluation_dict(tp_zero, tn_zero, fp_zero, fn_zero),
|
|
|
|
ev["Fix one"][instance_idx] = classifier_evaluation_dict(tp_one, tn_one, fp_one, fn_one)
|
|
|
|
"Fix one": classifier_evaluation_dict(tp_one, tn_one, fp_one, fn_one),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev
|
|
|
|
return ev
|
|
|
|