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

It runs with mono and mutliview on adaboost

parent 757c0170
Branches
Tags
No related merge requests found
Showing
with 76 additions and 83 deletions
......@@ -10,7 +10,7 @@ __status__ = "Prototype" # Production, Development, Prototype
class Adaboost(AdaBoostClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, n_estimators=10,
def __init__(self, random_state=None, n_estimators=50,
base_estimator=None, **kwargs):
super(Adaboost, self).__init__(
random_state=random_state,
......@@ -26,14 +26,6 @@ class Adaboost(AdaBoostClassifier, BaseMonoviewClassifier):
"""Used to know if the classifier can return label probabilities"""
return True
def paramsToSrt(self, nIter=1):
"""Used for weighted linear early fusion to generate random search sets"""
paramsSet = []
for _ in range(nIter):
paramsSet.append({"n_estimators": self.random_state.randint(1, 150),
"base_estimator": None})
return paramsSet
def getInterpret(self, directory):
interpretString = ""
interpretString += self.getFeatureImportance(directory)
......@@ -45,6 +37,15 @@ def formatCmdArgs(args):
kwargsDict = {'n_estimators': args.Ada_n_est,
'base_estimator': DecisionTreeClassifier()}
return kwargsDict
def paramsToSet(nIter, random_state):
"""Used for weighted linear early fusion to generate random search sets"""
paramsSet = []
for _ in range(nIter):
paramsSet.append({"n_estimators": random_state.randint(1, 500),
"base_estimator": None})
return paramsSet
# def canProbas():
# return True
#
......
......@@ -3,7 +3,7 @@
import numpy as np
from ....utils.Dataset import getV
from .... import MonoviewClassifiers
class EarlyFusionClassifier(object):
def __init__(self, randomState, monoviewClassifierName, monoviewClassifierConfig, NB_CORES=1):
......@@ -16,8 +16,14 @@ class EarlyFusionClassifier(object):
monoviewClassifierConfig = dict((str(configIndex), config[0]) for configIndex, config in
enumerate(monoviewClassifierConfig
))
monoviewClassifierModule = getattr(MonoviewClassifiers, self.monoviewClassifierName)
if monoviewClassifierConfig is not None:
self.monoviewClassifier = getattr(monoviewClassifierModule, self.monoviewClassifierName)(
random_state=randomState, **monoviewClassifierConfig)
else:
self.monoviewClassifier = getattr(monoviewClassifierModule, self.monoviewClassifierName)(
random_state=randomState)
self.monoviewClassifiersConfig = monoviewClassifierConfig
self.monoviewClassifier = None
self.nbCores = NB_CORES
self.monoviewData = None
self.randomState = randomState
......
......@@ -70,6 +70,9 @@ class WeightedLinear(EarlyFusionClassifier):
self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float)
else:
self.weights = np.array(map(float, kwargs['fusionMethodConfig']))
self.weights /= float(max(self.weights))
def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None):
if type(viewsIndices) == type(None):
......@@ -78,12 +81,11 @@ class WeightedLinear(EarlyFusionClassifier):
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
self.weights /= float(max(self.weights))
self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=trainIndices, viewsIndices=viewsIndices)
monoviewClassifierModule = getattr(MonoviewClassifiers, self.monoviewClassifierName)
self.monoviewClassifier = monoviewClassifierModule.fit(self.monoviewData,
labels[trainIndices],
self.randomState,
NB_CORES=self.nbCores,
**self.monoviewClassifiersConfig)
# monoviewClassifierModule = getattr(MonoviewClassifiers, self.monoviewClassifierName)
self.monoviewClassifier.fit(self.monoviewData, labels[trainIndices])
# self.randomState,
# NB_CORES=self.nbCores,
# **self.monoviewClassifiersConfig)
def setParams(self, paramsSet):
self.weights = paramsSet[0]
......@@ -110,8 +112,8 @@ class WeightedLinear(EarlyFusionClassifier):
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with weighted concatenation, using weights : " + ", ".join(map(str, self.weights)) + \
" with monoview classifier : "
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifiersNames)
configString += monoviewClassifierModule.getConfig(self.monoviewClassifiersConfig)
# monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifiersNames)
configString += self.monoviewClassifier.getConfig()
return configString
def gridSearch(self, classificationKWARGS):
......
......@@ -12,30 +12,28 @@ from .... import Metrics
from ....utils.Dataset import getV
def canProbasClassifier(classifierConfig):
try:
_ = getattr(classifierConfig, "predict_proba")
return True
except AttributeError:
return False
# def canProbasClassifier(classifierConfig):
# try:
# _ = getattr(classifierConfig, "predict_proba")
# return True
# except AttributeError:
# return False
def fitMonoviewClassifier(classifierName, data, labels, classifierConfig, needProbas, randomState, nbCores=1):
if type(classifierConfig) == dict:
monoviewClassifier = getattr(MonoviewClassifiers, classifierName)
def fitMonoviewClassifier(monoviewClassifier, data, labels, needProbas, randomState, nbCores=1):
if needProbas and not monoviewClassifier.canProbas():
monoviewClassifier = getattr(MonoviewClassifiers, "DecisionTree")
DTConfig = {"max_depth": 300, "criterion": "entropy", "splitter": "random"}
classifier = monoviewClassifier.fit(data, labels, randomState, nbCores, **DTConfig)
monoviewClassifier = getattr(MonoviewClassifiers, "DecisionTree")(random_state=randomState, **DTConfig)
classifier = monoviewClassifier.fit(data, labels)
return classifier
else:
if type(classifierConfig) is dict:
pass
else:
classifierConfig = dict((str(configIndex), config)
for configIndex, config in enumerate(classifierConfig))
# if type(classifierConfig) is dict:
# pass
# else:
# classifierConfig = dict((str(configIndex), config)
# for configIndex, config in enumerate(classifierConfig))
classifier = monoviewClassifier.fit(data, labels, randomState, nbCores, **classifierConfig)
classifier = monoviewClassifier.fit(data, labels)
return classifier
......@@ -119,11 +117,18 @@ class LateFusionClassifier(object):
def __init__(self, randomState, monoviewClassifiersNames, monoviewClassifiersConfigs, monoviewSelection,
NB_CORES=1):
self.monoviewClassifiersNames = monoviewClassifiersNames
monoviewClassifiersModules = [getattr(MonoviewClassifiers, classifierName)
for classifierName in self.monoviewClassifiersNames]
if type(monoviewClassifiersConfigs[0]) == dict:
self.monoviewClassifiersConfigs = monoviewClassifiersConfigs
self.monoviewClassifiers = []
self.monoviewClassifiers = [
getattr(monoviewClassifiersModule, classifierName)(random_state=randomState, **config)
for monoviewClassifiersModule, config, classifierName
in zip(monoviewClassifiersModules, monoviewClassifiersConfigs, monoviewClassifiersNames)]
else:
self.monoviewClassifiersConfigs = monoviewClassifiersConfigs
self.monoviewClassifiers = [
getattr(monoviewClassifiersModule, classifierName)(random_state=randomState,)
for monoviewClassifiersModule, config, classifierName
in zip(monoviewClassifiersModules, monoviewClassifiersConfigs, monoviewClassifiersNames)]
self.nbCores = NB_CORES
self.accuracies = np.zeros(len(monoviewClassifiersNames))
self.needProbas = False
......@@ -136,8 +141,8 @@ class LateFusionClassifier(object):
if trainIndices is None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
self.monoviewClassifiers = Parallel(n_jobs=self.nbCores)(
delayed(fitMonoviewClassifier)(self.monoviewClassifiersNames[index],
delayed(fitMonoviewClassifier)(self.monoviewClassifiers[index],
getV(DATASET, viewIndex, trainIndices),
labels[trainIndices],
self.monoviewClassifiersConfigs[index], self.needProbas, self.randomState)
self.needProbas, self.randomState)
for index, viewIndex in enumerate(viewsIndices))
......@@ -87,9 +87,8 @@ class BayesianInference(LateFusionClassifier):
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with Bayesian Inference using a weight for each view : " + ", ".join(map(str, self.weights)) + \
"\n\t-With monoview classifiers : "
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs,
monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
for monoviewClassifier in self.monoviewClassifiers:
configString += monoviewClassifier.getConfig()
configString += "\n\t -Method used to select monoview classifiers : " + self.monoviewSelection
return configString
......@@ -100,8 +100,7 @@ class MajorityVoting(LateFusionClassifier):
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with Majority Voting \n\t-With weights : " + str(
self.weights) + "\n\t-With monoview classifiers : "
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs,
monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
for monoviewClassifier in self.monoviewClassifiers:
configString += monoviewClassifier.getConfig()
return configString
......@@ -119,12 +119,8 @@ class SCMForLinear(LateFusionClassifier):
if trainIndices is None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
for index, viewIndex in enumerate(viewsIndices):
monoviewClassifier = getattr(MonoviewClassifiers, self.monoviewClassifiersNames[index])
self.monoviewClassifiers.append(
monoviewClassifier.fit(getV(DATASET, viewIndex, trainIndices),
labels[trainIndices], self.randomState,
NB_CORES=self.nbCores,
**self.monoviewClassifiersConfigs[index]))
self.monoviewClassifiers[index].fit(getV(DATASET, viewIndex, trainIndices),
labels[trainIndices])
self.SCMForLinearFusionFit(DATASET, labels, usedIndices=trainIndices, viewsIndices=viewsIndices)
def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None):
......@@ -184,8 +180,7 @@ class SCMForLinear(LateFusionClassifier):
configString = "with SCM for linear with max_attributes : " + str(self.maxAttributes) + ", p : " + str(self.p) + \
" model_type : " + str(self.modelType) + " order : " + str(self.order)+ " has chosen " + \
str(0.1) + " rule(s) \n\t-With monoview classifiers : "
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs,
monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
for monoviewClassifier in self.monoviewClassifiers:
configString += monoviewClassifier.getConfig()
return configString
......@@ -62,21 +62,9 @@ class SVMForLinear(LateFusionClassifier):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if trainIndices is None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if type(self.monoviewClassifiersConfigs[0]) == dict:
for index, viewIndex in enumerate(viewsIndices):
monoviewClassifier = getattr(MonoviewClassifiers, self.monoviewClassifiersNames[index])
if type(self.monoviewClassifiersConfigs[index]) is dict:
pass
else:
self.monoviewClassifiersConfigs[index] = dict((str(configIndex), config)
for configIndex, config in enumerate(self.monoviewClassifiersConfigs[index]))
self.monoviewClassifiers.append(
monoviewClassifier.fit(getV(DATASET, viewIndex, trainIndices),
labels[trainIndices], self.randomState,
NB_CORES=self.nbCores,
**self.monoviewClassifiersConfigs[index]))
else:
self.monoviewClassifiers = self.monoviewClassifiersConfigs
self.monoviewClassifiers[index].fit(getV(DATASET, viewIndex, trainIndices),
labels[trainIndices])
self.SVMForLinearFusionFit(DATASET, labels, usedIndices=trainIndices, viewsIndices=viewsIndices)
def setParams(self, paramsSet):
......@@ -109,8 +97,7 @@ class SVMForLinear(LateFusionClassifier):
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with SVM for linear \n\t-With monoview classifiers : "
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs,
monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
for monoviewClassifier in self.monoviewClassifiers:
configString += monoviewClassifier.getConfig()
return configString
......@@ -85,8 +85,7 @@ class WeightedLinear(LateFusionClassifier):
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with Weighted linear using a weight for each view : " + ", ".join(map(str, self.weights)) + \
"\n\t-With monoview classifiers : "
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs,
monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
for monoviewClassifier in self.monoviewClassifiers:
configString += monoviewClassifier.getConfig()
return configString
......@@ -61,7 +61,7 @@ def parseTheArgs(arguments):
'selected', default=["yes", "no"])
groupClass.add_argument('--CL_type', metavar='STRING', action='store', nargs="+",
help='Determine whether to use Multiview and/or Monoview, or Benchmark classification',
default=['Benchmark'])
default=['Monoview', 'Multiview'])
groupClass.add_argument('--CL_algos_monoview', metavar='STRING', action='store', nargs="+",
help='Determine which monoview classifier to use if empty, considering all',
default=[''])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment