Example #1
0
def get_search_space(target):
    """ return search space for optimisation
        target can be string, classifier, pipeline
    """
    # get latest params
    filename = os.path.dirname(__file__)
    filename = os.path.join(filename, "classifiers.yaml")
    with open(filename) as f:
        allparams = yaml.load(f)
    
    # classifier convert to string
    if isinstance(target, BaseEstimator) \
               and not isinstance(target, Pipeline):
        target = get_clfname(target)
        
    # string
    if isinstance(target, str):
        return allparams[target]

    # pipeline
    if isinstance(target, Pipeline):
        # convert to list of (prefix, key) tuples
        target = [(name, get_clfname(clf)) for name, clf in target.steps]

        # process list of (prefix, key) into search space
        search_space = {}
        for prefix, key in target:
            step_space = allparams[key]
            step_space = {"{prefix}__{k}".format(prefix=prefix, k=k):v 
                                     for k,v in step_space.items()}
            search_space.update(step_space)
    return search_space
Example #2
0
 def crossval(self, verbose=0, seed=0, method="predict", **params):
     """ returns crossval score
         sets self.preds
     """
     # track time spent per run
     starttime = time()
     
     np.random.seed(seed)         
 
     # useful for keras but throws exception for others
     if "Keras" in get_clfname(self.clf):
         self.clf.set_params(verbose=verbose)
         
     self.clf.set_params(**params)
     
     self.preds = cross_val_predict(self.clf, self.xtrain, self.ytrain,
                                    method=method)
     score = self.scorer._score_func(self.ytrain, self.preds) \
                     * self.scorer._sign
     
     # log results
     params.update(clf=get_clfname(self.clf),
                   name=self.name,
                   score=score, 
                   elapsed=time()-starttime)
     if self.runs:
         self.runs.append(params, self.preds)
         
     return score
Example #3
0
 def name(self):
     """ label for charts and reporting """
     if hasattr(self, "_name") and self._name is not None:
         return self._name
     if self.clf is not None:
         return get_clfname(self.clf)
     return None
Example #4
0
    def traintest(self, verbose=0, **params):
        """ DEPRECATED. USE CROSSVAL.
            return train/test score """
        xtrain, xtest, ytrain, ytest = train_test_split(
            self.xtrain, self.ytrain, test_size=0.1, random_state=0)
        
        # verbose useful for keras. others throw exception.
        if "Keras" in get_clfname(self.clf):
            params["verbose"] = verbose

        self.clf.fit(xtrain, ytrain, **params)
        return self.scorer(self.clf, xtest, ytest)