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

Updated all easy clf

parent 36d90e15
No related branches found
No related tags found
No related merge requests found
Showing
with 239 additions and 509 deletions
from sklearn.svm import SVC
class SVCClassifier(SVC):
def __init__(self, random_state=None, kernel='rbf', C=1.0, degree=3, **kwargs):
super(SVCClassifier, self).__init__(
C=C,
kernel=kernel,
degree=degree,
probability=True,
max_iter=1000,
random_state=random_state
)
self.classed_params = None
self.weird_strings = {}
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def getInterpret(self, directory):
interpretString = ""
return interpretString
\ No newline at end of file
......@@ -46,65 +46,3 @@ def paramsToSet(nIter, random_state):
paramsSet.append({"n_estimators": random_state.randint(1, 500),
"base_estimator": None})
return paramsSet
# def canProbas():
# return True
#
#
# def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
# """Used to fit the monoview classifier with the args stored in kwargs"""
# classifier = AdaBoostClassifier(n_estimators=kwargs['n_estimators'],
# base_estimator=kwargs['base_estimator'],
# random_state=randomState)
# classifier.fit(DATASET, CLASS_LABELS)
# return classifier
#
#
# def paramsToSet(nIter, randomState):
# """Used for weighted linear early fusion to generate random search sets"""
# paramsSet = []
# for _ in range(nIter):
# paramsSet.append({"n_estimators": randomState.randint(1, 15),
# "base_estimator": DecisionTreeClassifier()})
# return paramsSet
#
#
# def getKWARGS(args):
# """Used to format kwargs for the parsed args"""
# kwargsDict = {}
# kwargsDict['n_estimators'] = args.Ada_n_est
# kwargsDict['base_estimator'] = DecisionTreeClassifier() #args.Ada_b_est
# return kwargsDict
#
#
# def genPipeline():
# return Pipeline([('classifier', AdaBoostClassifier())])
#
#
# def genParamsDict(randomState):
# return {"classifier__n_estimators": np.arange(150)+1,
# "classifier__base_estimator": [DecisionTreeClassifier()]}
#
#
# def genBestParams(detector):
# return {"n_estimators": detector.best_params_["classifier__n_estimators"],
# "base_estimator": detector.best_params_["classifier__base_estimator"]}
#
#
# def genParamsFromDetector(detector):
# nIter = len(detector.cv_results_['param_classifier__n_estimators'])
# return [("baseEstimators", np.array(["DecisionTree" for _ in range(nIter)])),
# ("nEstimators", np.array(detector.cv_results_['param_classifier__n_estimators']))]
#
#
# def getConfig(config):
# if type(config) is not dict: # Used in late fusion when config is a classifier
# return "\n\t\t- Adaboost with num_esimators : " + str(config.n_estimators) + ", base_estimators : " + str(
# config.base_estimator)
# else:
# return "\n\t\t- Adaboost with n_estimators : " + str(config["n_estimators"]) + ", base_estimator : " + str(
# config["base_estimator"])
#
#
# def getInterpret(classifier, directory):
# interpretString = getFeatureImportance(classifier, directory)
# return interpretString
\ No newline at end of file
from sklearn import tree
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
# import graphviz
# import cPickle
from sklearn.tree import DecisionTreeClassifier
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.MonoviewUtils import CustomRandint, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
class DecisionTree(DecisionTreeClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, max_depth=None,
criterion='gini', splitter='best', **kwargs):
super(DecisionTree, self).__init__(
max_depth=max_depth,
criterion=criterion,
splitter=splitter,
random_state=random_state
)
self.param_names = ["max_depth", "criterion", "splitter",]
self.classed_params = None
self.distribs = [CustomRandint(low=1, high=300),
["gini", "entropy"],
["best", "random"], ]
self.weird_strings = {}
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def getInterpret(self, directory):
interpretString = ""
interpretString += self.getFeatureImportance(directory)
return interpretString
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = tree.DecisionTreeClassifier(max_depth=kwargs['max_depth'], criterion=kwargs['criterion'],
splitter=kwargs['splitter'], random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"max_depth": args.DT_depth,
"criterion": args.DT_criterion,
"splitter": args.DT_splitter}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -32,46 +49,3 @@ def paramsToSet(nIter, randomState):
"criterion": randomState.choice(["gini", "entropy"]),
"splitter": randomState.choice(["best", "random"])})
return paramsSet
\ No newline at end of file
def getKWARGS(args):
kwargsDict = {"max_depth": args.DT_depth, "criterion": args.DT_criterion, "splitter": args.DT_splitter}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', tree.DecisionTreeClassifier())])
def genParamsDict(randomState):
return {"classifier__max_depth": np.arange(1, 300),
"classifier__criterion": ["gini", "entropy"],
"classifier__splitter": ["best", "random"]}
def genBestParams(detector):
return {"max_depth": detector.best_params_["classifier__max_depth"],
"criterion": detector.best_params_["classifier__criterion"],
"splitter": detector.best_params_["classifier__splitter"]}
def genParamsFromDetector(detector):
return [("maxDepth", np.array(detector.cv_results_['param_classifier__max_depth'])),
("criterion", np.array(detector.cv_results_['param_classifier__criterion'])),
("splitter", np.array(detector.cv_results_['param_classifier__splitter']))]
def getConfig(config):
if type(config) is not dict:
return "\n\t\t- Decision Tree with max_depth : " + str(
config.max_depth) + ", criterion : " + config.criterion + ", splitter : " + config.splitter
else:
return "\n\t\t- Decision Tree with max_depth : " + str(config["max_depth"]) + ", criterion : " + config[
"criterion"] + ", splitter : " + config["splitter"]
def getInterpret(classifier, directory):
dot_data = tree.export_graphviz(classifier, out_file=None)
# graph = graphviz.Source(dot_data)
# graph.render(directory+"-tree.pdf")
interpretString = getFeatureImportance(classifier, directory)
return interpretString
\ No newline at end of file
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.MonoviewUtils import CustomRandint, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
class KNN(KNeighborsClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, n_neighbors=50,
weights='uniform', algorithm='auto', p=2, **kwargs):
super(KNN, self).__init__(
n_neighbors=n_neighbors,
weights=weights,
algorithm=algorithm,
p=p
)
self.param_names = ["n_neighbors", "weights", "algorithm", "p"]
self.classed_params = None
self.distribs = [CustomRandint(low=1, high=20), ["uniform", "distance"],
["auto", "ball_tree", "kd_tree", "brute"], [1, 2]]
self.weird_strings = {}
self.random_state=random_state
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def getInterpret(self, directory):
interpretString = ""
return interpretString
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = KNeighborsClassifier(n_neighbors=kwargs["n_neighbors"],
weights=kwargs["weights"],
algorithm=kwargs["algorithm"],
p=kwargs["p"],
n_jobs=NB_CORES, )
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"n_neighbors": args.KNN_neigh,
"weights":args.KNN_weights,
"algorithm":args.KNN_algo,
"p":args.KNN_p}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -34,49 +50,3 @@ def paramsToSet(nIter, randomState):
"algorithm": randomState.choice(["auto", "ball_tree", "kd_tree", "brute"]),
"p": randomState.choice([1, 2])})
return paramsSet
def getKWARGS(args):
kwargsDict = {"n_neighbors": args.KNN_neigh,
"weights":args.KNN_weights,
"algorithm":args.KNN_algo,
"p":args.KNN_p}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', KNeighborsClassifier())])
def genParamsDict(randomState):
return {"classifier__n_neighbors": np.arange(1, 20),
"classifier__weights": ["uniform", "distance"],
"classifier__algorithm": ["auto", "ball_tree", "kd_tree", "brute"],
"classifier__p": [1, 2]}
def genBestParams(detector):
return {"n_neighbors": detector.best_params_["classifier__n_neighbors"],
"weights": detector.best_params_["classifier__weights"],
"algorithm": detector.best_params_["classifier__algorithm"],
"p": detector.best_params_["classifier__p"]}
def genParamsFromDetector(detector):
return [("nNeighbors", np.array(detector.cv_results_['param_classifier__n_neighbors'])),
("weights", np.array(detector.cv_results_['param_classifier__weights'])),
("algorithm", np.array(detector.cv_results_['param_classifier__algorithm'])),
("p", np.array(detector.cv_results_['param_classifier__p']))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- K nearest Neighbors with n_neighbors : " + str(config.n_neighbors) + \
", weights : " + config.weights + ", algorithm : " + config.algorithm + ", p : " + \
str(config.p)
else:
return "\n\t\t- K nearest Neighbors with n_neighbors : " + str(config["n_neighbors"]) + \
", weights : " + config["weights"] + ", algorithm : " + config["algorithm"] + \
", p : " + str(config["p"])
def getInterpret(classifier, directory):
return ""
\ No newline at end of file
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
from ..Monoview.MonoviewUtils import CustomRandint, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
class RandomForest(RandomForestClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, n_estimators=None,
max_depth='gini', criterion='best', **kwargs):
super(RandomForest, self).__init__(
n_estimators=n_estimators,
max_depth=max_depth,
criterion=criterion,
random_state=random_state
)
self.param_names = ["n_estimators", "max_depth", "criterion",]
self.classed_params = None
self.distribs = [CustomRandint(low=1, high=300),
CustomRandint(low=1, high=300),
["gini", "entropy"], ]
self.weird_strings = {}
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def getInterpret(self, directory):
interpretString = ""
interpretString += self.getFeatureImportance(directory)
return interpretString
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = RandomForestClassifier(n_estimators=kwargs['n_estimators'],
max_depth=kwargs['max_depth'],
criterion=kwargs['criterion'],
n_jobs=NB_CORES, random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"n_estimators": args.RF_trees,
"max_depth": args.RF_max_depth,
"criterion": args.RF_criterion}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -29,46 +49,3 @@ def paramsToSet(nIter, randomState):
"max_depth": randomState.randint(1, 300),
"criterion": randomState.choice(["gini", "entropy"])})
return paramsSet
def getKWARGS(args):
kwargsDict = {"n_estimators": args.RF_trees,
"max_depth": args.RF_max_depth,
"criterion": args.RF_criterion}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', RandomForestClassifier())])
def genParamsDict(randomState):
return {"classifier__n_estimators": np.arange(1, 300),
"classifier__max_depth": np.arange(1, 300),
"classifier__criterion": ["gini", "entropy"]}
def genBestParams(detector):
return {"n_estimators": detector.best_params_["classifier__n_estimators"],
"max_depth": detector.best_params_["classifier__max_depth"],
"criterion": detector.best_params_["classifier__criterion"]}
def genParamsFromDetector(detector):
return [("nEstimators", np.array(detector.cv_results_['param_classifier__n_estimators'])),
("maxDepth", np.array(detector.cv_results_['param_classifier__max_depth'])),
("criterion", np.array(detector.cv_results_['param_classifier__criterion']))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- Random Forest with num_esimators : " + str(config.n_estimators) + ", max_depth : " + str(
config.max_depth) + ", criterion : " + config.criterion
else:
return "\n\t\t- Random Forest with num_esimators : " + str(config["n_estimators"]) + \
", max_depth : " + str(config["max_depth"]) + ", criterion : " + config["criterion"]
def getInterpret(classifier, directory):
interpretString = getFeatureImportance(classifier, directory)
return interpretString
import numpy as np
from sklearn.externals.six import iteritems
from pyscm.scm import SetCoveringMachineClassifier as scm
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.pipeline import Pipeline
from sklearn.model_selection import RandomizedSearchCV
from sklearn.externals.six import iteritems
from scipy.stats import uniform, randint
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.MonoviewUtils import CustomRandint, CustomUniform, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
class DecisionStumpSCMNew(BaseEstimator, ClassifierMixin):
"""docstring for SCM
A hands on class of SCM using decision stump, built with sklearn format in order to use sklearn function on SCM like
......@@ -26,9 +19,9 @@ class DecisionStumpSCMNew(BaseEstimator, ClassifierMixin):
self.p = p
self.max_rules = max_rules
self.random_state = random_state
self.clf = scm(model_type=self.model_type, max_rules=self.max_rules, p=self.p, random_state=self.random_state)
def fit(self, X, y):
self.clf = scm(model_type=self.model_type, max_rules=self.max_rules, p=self.p, random_state=self.random_state)
self.clf.fit(X=X, y=y)
def predict(self, X):
......@@ -47,65 +40,44 @@ class DecisionStumpSCMNew(BaseEstimator, ClassifierMixin):
return {"Binary_attributes": self.clf.model_.rules}
def canProbas():
return False
class SCM(DecisionStumpSCMNew, BaseMonoviewClassifier):
def __init__(self, random_state=None, model_type=50,
max_rules=None, p=1.0, **kwargs):
super(SCM, self).__init__(
random_state=random_state,
model_type=model_type,
max_rules=max_rules,
p=p
)
self.param_names = ["model_type", "max_rules", "p"]
self.distribs = [["conjunction", "disjunction"],
CustomRandint(low=1, high=15),
CustomUniform(loc=0, state=1)]
self.classed_params = None
self.weird_strings = {}
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = DecisionStumpSCMNew(model_type=kwargs['model_type'],
max_rules=kwargs['max_rules'],
p=kwargs['p'],
random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append({"model_type": randomState.choice(["conjunction", "disjunction"]),
"max_rules": randomState.randint(1, 15),
"p": randomState.random_sample()})
return paramsSet
def getInterpret(self, directory):
interpretString = "Model used : " + str(self.clf.model_)
return interpretString
def getKWARGS(args):
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"model_type": args.SCM_model_type,
"p": args.SCM_p,
"max_rules": args.SCM_max_rules}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', DecisionStumpSCMNew())])
def genParamsDict(randomState):
return {"classifier__model_type": ['conjunction', 'disjunction'],
"classifier__p": uniform(),
"classifier__max_rules": np.arange(1,30)}
def genBestParams(detector):
return {"model_type": detector.best_params_["classifier__model_type"],
"p": detector.best_params_["classifier__p"],
"max_rules": detector.best_params_["classifier__max_rules"]}
def genParamsFromDetector(detector):
return [("model_type", np.array(detector.cv_results_['param_classifier__model_type'])),
("maxRules", np.array(detector.cv_results_['param_classifier__max_rules'])),
("p", np.array(detector.cv_results_['param_classifier__p']))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SCM with model_type: " + config.model_type + ", max_rules : " + str(config.max_rules) +\
", p : " + str(config.p)
else:
return "\n\t\t- SCM with model_type: " + config["model_type"] + ", max_rules : " + str(config["max_rules"]) + ", p : " + \
str(config["p"])
def getInterpret(classifier, directory):
return "Model used : " + str(classifier.clf.model_)
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append({"model_type": randomState.choice(["conjunction", "disjunction"]),
"max_rules": randomState.randint(1, 15),
"p": randomState.random_sample()})
return paramsSet
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
import numpy as np
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.MonoviewUtils import CustomUniform, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
class SGD(SGDClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, loss=None,
penalty='gini', alpha='best', **kwargs):
super(SGD, self).__init__(
loss=loss,
penalty=penalty,
alpha=alpha,
random_state=random_state
)
self.param_names = ["loss", "penalty", "alpha",]
self.classed_params = None
self.distribs = [['log', 'modified_huber'],
["l1", "l2", "elasticnet"],
CustomUniform(loc=0, state=1), ]
self.weird_strings = {}
def canProbas(self):
"""Used to know if the classifier can return label probabilities"""
return True
def getInterpret(self, directory):
interpretString = ""
return interpretString
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = SGDClassifier(loss=kwargs['loss'],
penalty=kwargs['penalty'],
alpha=kwargs['alpha'],
random_state=randomState, n_jobs=NB_CORES)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"loss": args.SGD_loss,
"penalty": args.SGD_penalty,
"alpha": args.SGD_alpha}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -32,48 +48,3 @@ def paramsToSet(nIter, randomState):
"penalty": randomState.choice(["l1", "l2", "elasticnet"]),
"alpha": randomState.random_sample()})
return paramsSet
\ No newline at end of file
def getKWARGS(args):
kwargsDict = {"loss": args.SGD_loss,
"penalty": args.SGD_penalty,
"alpha": args.SGD_alpha}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', SGDClassifier())])
def genParamsDict(randomState):
losses = ['log', 'modified_huber']
penalties = ["l1", "l2", "elasticnet"]
alphas = uniform()
return {"classifier__loss": losses, "classifier__penalty": penalties,
"classifier__alpha": alphas}
def genBestParams(detector):
return {"loss": detector.best_params_["classifier__loss"],
"penalty": detector.best_params_["classifier__penalty"],
"alpha": detector.best_params_["classifier__alpha"]}
def genParamsFromDetector(detector):
return [("loss", np.array(detector.cv_results_['param_classifier__loss'])),
("penalty", np.array(detector.cv_results_['param_classifier__penalty'])),
("aplha", np.array(detector.cv_results_['param_classifier__alpha']))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SGDClassifier with loss : " + config.loss + ", penalty : " + \
config.penalty + ", alpha : " + str(config.alpha)
else:
return "\n\t\t- SGDClassifier with loss : " + config["loss"] + ", penalty : " + \
config["penalty"] + ", alpha : " + str(config["alpha"])
def getInterpret(classifier, directory):
# TODO : coeffs
return ""
#
\ No newline at end of file
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.Additions.SVCClassifier import SVCClassifier
from ..Monoview.MonoviewUtils import CustomUniform, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
return True
class SVMLinear(SVCClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, C=1.0, **kwargs):
super(SVMLinear, self).__init__(
C=C,
kernel='linear',
random_state=random_state
)
self.param_names = ["C",]
self.distribs = [CustomUniform(loc=0, state=1), ]
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = SVC(C=kwargs['C'], kernel='linear', probability=True, max_iter=1000, random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"C": args.SVML_C, }
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -28,37 +30,3 @@ def paramsToSet(nIter, randomState):
for _ in range(nIter):
paramsSet.append({"C": randomState.randint(1, 10000), })
return paramsSet
def getKWARGS(args):
kwargsDict = {"C":args.SVML_C, }
return kwargsDict
def genPipeline():
return Pipeline([('classifier', SVC(kernel="linear", max_iter=1000))])
def genParamsDict(randomState):
return {"classifier__C": np.arange(1, 10000)}
def genBestParams(detector):
return {"C": detector.best_params_["classifier__C"]}
def genParamsFromDetector(detector):
nIter = len(detector.cv_results_['param_classifier__C'])
return [("C", np.array(detector.cv_results_['param_classifier__C'])),
("control", np.array(["control" for _ in range(nIter)]))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM Linear with C : " + str(config.C)
else:
return "\n\t\t- SVM Linear with C : " + str(config["C"])
def getInterpret(classifier, directory):
# TODO : coeffs
return ""
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.Additions.SVCClassifier import SVCClassifier
from ..Monoview.MonoviewUtils import CustomUniform, CustomRandint, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
return True
class SVMPoly(SVCClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, C=1.0, degree=3, **kwargs):
super(SVMPoly, self).__init__(
C=C,
kernel='poly',
degree=degree,
random_state=random_state
)
self.param_names = ["C", "degree"]
self.distribs = [CustomUniform(loc=0, state=1), CustomRandint(low=2, high=30)]
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = SVC(C=kwargs['C'], kernel='poly', degree=kwargs["degree"], probability=True, max_iter=1000, random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"C": args.SVMPoly_C, "degree": args.SVMPoly_deg}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -29,36 +31,3 @@ def paramsToSet(nIter, randomState):
paramsSet.append({"C": randomState.randint(1, 10000), "degree": randomState.randint(1, 30)})
return paramsSet
def getKWARGS(args):
kwargsDict = {"C": args.SVMPoly_C, "degree": args.SVMPoly_deg}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', SVC(kernel="poly", max_iter=1000))])
def genParamsDict(randomState):
return {"classifier__C": np.arange(1, 10000),
"classifier__degree": np.arange(1, 30)}
def genBestParams(detector):
return {"C": detector.best_params_["classifier__C"],
"degree": detector.best_params_["classifier__degree"]}
def genParamsFromDetector(detector):
return [("c", np.array(detector.cv_results_['param_classifier__C'])),
("degree", np.array(detector.cv_results_['param_classifier__degree']))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM Poly with C : " + str(config.C) + ", degree : " + str(config.degree)
else:
return "\n\t\t- SVM Poly with C : " + str(config["C"]) + ", degree : " + str(config["degree"])
def getInterpret(classifier, directory):
return ""
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline # Pipelining in classification
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
import numpy as np
from .. import Metrics
from ..utils.HyperParameterSearch import genHeatMaps
from ..Monoview.Additions.SVCClassifier import SVCClassifier
from ..Monoview.MonoviewUtils import CustomUniform, CustomRandint, BaseMonoviewClassifier
# Author-Info
__author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
return True
class SVMRBF(SVCClassifier, BaseMonoviewClassifier):
def __init__(self, random_state=None, C=1.0, **kwargs):
super(SVMRBF, self).__init__(
C=C,
kernel='rbf',
random_state=random_state
)
self.param_names = ["C",]
self.distribs = [CustomUniform(loc=0, state=1),]
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
classifier = SVC(C=kwargs['C'], kernel='rbf', probability=True, max_iter=1000, random_state=randomState)
classifier.fit(DATASET, CLASS_LABELS)
return classifier
def formatCmdArgs(args):
"""Used to format kwargs for the parsed args"""
kwargsDict = {"C": args.SVMRBF_C}
return kwargsDict
def paramsToSet(nIter, randomState):
......@@ -28,37 +29,3 @@ def paramsToSet(nIter, randomState):
for _ in range(nIter):
paramsSet.append({"C": randomState.randint(1, 10000), })
return paramsSet
\ No newline at end of file
def getKWARGS(args):
kwargsDict = {"C": args.SVMRBF_C}
return kwargsDict
def genPipeline():
return Pipeline([('classifier', SVC(kernel="rbf", max_iter=1000))])
def genParamsDict(randomState):
return {"classifier__C": np.arange(1, 10000)}
def genBestParams(detector):
return {'C': detector.best_params_["classifier__C"]}
def genParamsFromDetector(detector):
nIter = len(detector.cv_results_['param_classifier__C'])
return [("c", np.array(detector.cv_results_['param_classifier__C'])),
("control", np.array(["control" for _ in range(nIter)]))]
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM RBF with C : " + str(config.C)
else:
return "\n\t\t- SVM RBF with C : " + str(config["C"])
def getInterpret(classifier, directory):
return ""
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment