Skip to content
Snippets Groups Projects
Commit ac3cf5ed authored by Baptiste Bauvin's avatar Baptiste Bauvin
Browse files

Wroking copy

parent a87e1266
No related branches found
No related tags found
No related merge requests found
Showing
with 49 additions and 36 deletions
...@@ -696,7 +696,7 @@ def get_accuracy_graph(train_accuracies, classifier_name, file_name, name="Accur ...@@ -696,7 +696,7 @@ def get_accuracy_graph(train_accuracies, classifier_name, file_name, name="Accur
class BaseBoost(object): class BaseBoost(object):
def __init__(self): def __init__(self):
self.n_stumps = 1 self.n_stumps_per_attribute = 1
def _collect_probas(self, X): def _collect_probas(self, X):
return np.asarray([clf.predict_proba(X) for clf in self.estimators_generator.estimators_]) return np.asarray([clf.predict_proba(X) for clf in self.estimators_generator.estimators_])
......
...@@ -32,7 +32,7 @@ class ColumnGenerationClassifier(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -32,7 +32,7 @@ class ColumnGenerationClassifier(BaseEstimator, ClassifierMixin, BaseBoost):
y[y == 0] = -1 y[y == 0] = -1
if self.estimators_generator is None: if self.estimators_generator is None:
self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps, self_complemented=True) self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps_per_attribute, self_complemented=True)
self.estimators_generator.fit(X, y) self.estimators_generator.fit(X, y)
self.classification_matrix = self._binary_classification_matrix(X) self.classification_matrix = self._binary_classification_matrix(X)
......
...@@ -6,7 +6,6 @@ from collections import defaultdict ...@@ -6,7 +6,6 @@ from collections import defaultdict
import math import math
from sklearn.utils.validation import check_is_fitted from sklearn.utils.validation import check_is_fitted
from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.metrics import accuracy_score
import time import time
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -15,14 +14,13 @@ from ... import Metrics ...@@ -15,14 +14,13 @@ from ... import Metrics
class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
def __init__(self, n_max_iterations=350, estimators_generator=None, dual_constraint_rhs=0, def __init__(self, n_max_iterations=350, estimators_generator=None,
random_state=42, self_complemented=True, twice_the_same=False, old_fashioned=False, random_state=42, self_complemented=True, twice_the_same=False, old_fashioned=False,
previous_vote_weighted=True, c_bound_choice = True, random_start = True, previous_vote_weighted=True, c_bound_choice = True, random_start = True,
two_wieghts_problem=False, divided_ponderation=True, n_stumps_per_attribute=None): two_wieghts_problem=False, divided_ponderation=True, n_stumps_per_attribute=None, use_r=True, plotted_metric=Metrics.zero_one_loss):
super(ColumnGenerationClassifierQar, self).__init__() super(ColumnGenerationClassifierQar, self).__init__()
self.n_max_iterations = n_max_iterations self.n_max_iterations = n_max_iterations
self.estimators_generator = estimators_generator self.estimators_generator = estimators_generator
self.dual_constraint_rhs = dual_constraint_rhs
if type(random_state) is int: if type(random_state) is int:
self.random_state = np.random.RandomState(random_state) self.random_state = np.random.RandomState(random_state)
else: else:
...@@ -36,13 +34,13 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -36,13 +34,13 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
self.random_start = random_start self.random_start = random_start
self.two_wieghts_problem = two_wieghts_problem self.two_wieghts_problem = two_wieghts_problem
self.divided_ponderation = divided_ponderation self.divided_ponderation = divided_ponderation
self.plotted_metric = Metrics.zero_one_loss self.plotted_metric = plotted_metric
if n_stumps_per_attribute: if n_stumps_per_attribute:
self.n_stumps = n_stumps_per_attribute self.n_stumps_per_attribute = n_stumps_per_attribute
self.use_r = use_r
self.printed_args_name_list = ["n_max_iterations", "self_complemented", "twice_the_same", "old_fashioned", self.printed_args_name_list = ["n_max_iterations", "self_complemented", "twice_the_same", "old_fashioned",
"previous_vote_weighted", "c_bound_choice", "random_start", "previous_vote_weighted", "c_bound_choice", "random_start",
"two_wieghts_problem", "divided_ponderation", "n_stumps"] "two_wieghts_problem", "divided_ponderation", "n_stumps", "use_r"]
def set_params(self, **params): def set_params(self, **params):
self.self_complemented = params["self_complemented"] self.self_complemented = params["self_complemented"]
...@@ -60,7 +58,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -60,7 +58,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
X = np.array(X.todense()) X = np.array(X.todense())
if self.estimators_generator is None: if self.estimators_generator is None:
self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps, self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps_per_attribute,
self_complemented=self.self_complemented) self_complemented=self.self_complemented)
# Initialization # Initialization
y[y == 0] = -1 y[y == 0] = -1
...@@ -93,7 +91,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -93,7 +91,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
self.n_total_hypotheses_ = n self.n_total_hypotheses_ = n
self.n_total_examples = m self.n_total_examples = m
self.n_max_iterations = 100 self.n_max_iterations = n
self.break_cause = " the maximum number of iterations was attained." self.break_cause = " the maximum number of iterations was attained."
for k in range(min(n, self.n_max_iterations if self.n_max_iterations is not None else np.inf)): for k in range(min(n, self.n_max_iterations if self.n_max_iterations is not None else np.inf)):
...@@ -134,12 +132,22 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -134,12 +132,22 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
# Generate the new weight for the new voter # Generate the new weight for the new voter
epsilon = self._compute_epsilon(y) epsilon = self._compute_epsilon(y)
self.epsilons.append(epsilon) self.epsilons.append(epsilon)
r = self._compute_r(y)
if epsilon == 0. or math.log((1 - epsilon) / epsilon) == math.inf: if epsilon == 0. or math.log((1 - epsilon) / epsilon) == math.inf:
self.chosen_columns_.pop() self.chosen_columns_.pop()
self.break_cause = " epsilon was too small." self.break_cause = " epsilon was too small."
break break
if self.divided_ponderation: if self.divided_ponderation:
if self.use_r:
self.q = (1 / (self.n_max_iterations - k)) * 0.5*math.log((1+r)/(1-r))
else:
self.q = (1/(self.n_max_iterations-k))*math.log((1 - epsilon) / epsilon) self.q = (1/(self.n_max_iterations-k))*math.log((1 - epsilon) / epsilon)
else:
if self.use_r:
self.q = 0.5*math.log((1+r)/(1-r))
else: else:
self.q = math.log((1 - epsilon) / epsilon) self.q = math.log((1 - epsilon) / epsilon)
self.weights_.append(self.q) self.weights_.append(self.q)
...@@ -156,7 +164,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -156,7 +164,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
self.previous_margins.append(np.multiply(y, self.previous_vote)) self.previous_margins.append(np.multiply(y, self.previous_vote))
self.train_metrics.append(self.plotted_metric.score(y, np.sign(self.previous_vote))) self.train_metrics.append(self.plotted_metric.score(y, np.sign(self.previous_vote)))
# self.bounds.append(np.prod(np.sqrt(1-4*np.square(0.5-np.array(self.epsilons))))) # self.bounds.append(np.prod(np.sqrt(1-4*np.square(0.5-np.array(self.epsilons)))))
r = self._compute_r(y)
if k!=0: if k!=0:
self.bounds.append(self.bounds[-1]*math.sqrt(1-r**2)) self.bounds.append(self.bounds[-1]*math.sqrt(1-r**2))
else: else:
...@@ -322,7 +330,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -322,7 +330,7 @@ class ColumnGenerationClassifierQar(BaseEstimator, ClassifierMixin, BaseBoost):
if C1 == 0: if C1 == 0:
return ['break', "the derivate was constant"] return ['break', "the derivate was constant"]
else : else :
is_acceptable, sol = self._analyze_solutions_one_weight(np.array(float(C0)/C1)) is_acceptable, sol = self._analyze_solutions_one_weight(np.array(float(C0)/C1).reshape((1,1)))
if is_acceptable: if is_acceptable:
return np.array([sol]) return np.array([sol])
try: try:
......
...@@ -28,7 +28,7 @@ class ColumnGenerationClassifierv21(BaseEstimator, ClassifierMixin, BaseBoost): ...@@ -28,7 +28,7 @@ class ColumnGenerationClassifierv21(BaseEstimator, ClassifierMixin, BaseBoost):
X = np.array(X.todense()) X = np.array(X.todense())
if self.estimators_generator is None: if self.estimators_generator is None:
self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps, self_complemented=True) self.estimators_generator = StumpsClassifiersGenerator(n_stumps_per_attribute=self.n_stumps_per_attribute, self_complemented=True)
y[y == 0] = -1 y[y == 0] = -1
......
...@@ -3,7 +3,6 @@ from ..Monoview.Additions.BoostUtils import getInterpretBase ...@@ -3,7 +3,6 @@ from ..Monoview.Additions.BoostUtils import getInterpretBase
from ..Monoview.Additions.QarBoostUtils import ColumnGenerationClassifierQar from ..Monoview.Additions.QarBoostUtils import ColumnGenerationClassifierQar
class QarBoostNC(ColumnGenerationClassifierQar, BaseMonoviewClassifier): class QarBoostNC(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
def __init__(self, random_state=None, **kwargs): def __init__(self, random_state=None, **kwargs):
...@@ -13,11 +12,12 @@ class QarBoostNC(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -13,11 +12,12 @@ class QarBoostNC(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
twice_the_same=False, twice_the_same=False,
old_fashioned=False, old_fashioned=False,
previous_vote_weighted=False, previous_vote_weighted=False,
c_bound_choice=False, c_bound_choice=True,
random_start=True, random_start=False,
two_wieghts_problem=True, two_wieghts_problem=False,
divided_ponderation=False, divided_ponderation=False,
n_stumps_per_attribute=100 n_stumps_per_attribute=1,
use_r=True
) )
self.param_names = [] self.param_names = []
self.distribs = [] self.distribs = []
......
...@@ -8,14 +8,16 @@ class QarBoostNC2(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -8,14 +8,16 @@ class QarBoostNC2(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
def __init__(self, random_state=None, **kwargs): def __init__(self, random_state=None, **kwargs):
super(QarBoostNC2, self).__init__( super(QarBoostNC2, self).__init__(
random_state=random_state, random_state=random_state,
self_complemented=False, self_complemented=True,
twice_the_same=False, twice_the_same=False,
old_fashioned=False, old_fashioned=False,
previous_vote_weighted=False, previous_vote_weighted=False,
c_bound_choice=True, c_bound_choice=True,
random_start=True, random_start=True,
two_wieghts_problem=False, two_wieghts_problem=False,
divided_ponderation=True divided_ponderation=False,
n_stumps_per_attribute=10,
use_r=True
) )
self.param_names = [] self.param_names = []
self.distribs = [] self.distribs = []
......
...@@ -15,8 +15,9 @@ class QarBoostNC3(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -15,8 +15,9 @@ class QarBoostNC3(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
c_bound_choice=True, c_bound_choice=True,
random_start=True, random_start=True,
two_wieghts_problem=False, two_wieghts_problem=False,
divided_ponderation=False, divided_ponderation=True,
n_stumps_per_attribute=10 n_stumps_per_attribute=1,
use_r=True
) )
self.param_names = [] self.param_names = []
self.distribs = [] self.distribs = []
...@@ -31,7 +32,7 @@ class QarBoostNC3(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -31,7 +32,7 @@ class QarBoostNC3(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
return self.getInterpretQar(directory) return self.getInterpretQar(directory)
def get_name_for_fusion(self): def get_name_for_fusion(self):
return "QBN2" return "QBN3"
def formatCmdArgs(args): def formatCmdArgs(args):
......
...@@ -8,15 +8,16 @@ class QarBoostv2(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -8,15 +8,16 @@ class QarBoostv2(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
def __init__(self, random_state=None, **kwargs): def __init__(self, random_state=None, **kwargs):
super(QarBoostv2, self).__init__( super(QarBoostv2, self).__init__(
random_state=random_state, random_state=random_state,
self_complemented=True, self_complemented=False,
twice_the_same=False, twice_the_same=False,
old_fashioned=False, old_fashioned=False,
previous_vote_weighted=False, previous_vote_weighted=False,
c_bound_choice=True, c_bound_choice=True,
random_start=True, random_start=False,
two_wieghts_problem=False, two_wieghts_problem=False,
divided_ponderation=False, divided_ponderation=False,
n_stumps_per_attribute=10 n_stumps_per_attribute=1,
use_r=True
) )
self.param_names = [] self.param_names = []
self.distribs = [] self.distribs = []
...@@ -28,7 +29,7 @@ class QarBoostv2(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -28,7 +29,7 @@ class QarBoostv2(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
return True return True
def getInterpret(self, directory): def getInterpret(self, directory):
return getInterpretBase(self, directory, "QarBoostv2", self.weights_, self.break_cause) return self.getInterpretQar(directory)
def get_name_for_fusion(self): def get_name_for_fusion(self):
return "QBv2" return "QBv2"
......
...@@ -8,15 +8,16 @@ class QarBoostv3(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -8,15 +8,16 @@ class QarBoostv3(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
def __init__(self, random_state=None, **kwargs): def __init__(self, random_state=None, **kwargs):
super(QarBoostv3, self).__init__( super(QarBoostv3, self).__init__(
random_state=random_state, random_state=random_state,
self_complemented=True, self_complemented=False,
twice_the_same=False, twice_the_same=False,
old_fashioned=False, old_fashioned=False,
previous_vote_weighted=False, previous_vote_weighted=False,
c_bound_choice=True, c_bound_choice=True,
random_start=True, random_start=True,
two_wieghts_problem=False, two_wieghts_problem=False,
divided_ponderation=False, divided_ponderation=True,
n_stumps_per_attribute=1 n_stumps_per_attribute=1,
use_r=False
) )
self.param_names = [] self.param_names = []
self.distribs = [] self.distribs = []
...@@ -28,7 +29,7 @@ class QarBoostv3(ColumnGenerationClassifierQar, BaseMonoviewClassifier): ...@@ -28,7 +29,7 @@ class QarBoostv3(ColumnGenerationClassifierQar, BaseMonoviewClassifier):
return True return True
def getInterpret(self, directory): def getInterpret(self, directory):
return getInterpretBase(self, directory, "QarBoostv3", self.weights_, self.break_cause) return self.getInterpretQar(directory)
def get_name_for_fusion(self): def get_name_for_fusion(self):
return "QBv3" return "QBv3"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment