def _convert_space_hop_skopt(space): dimensions = [] for name, specs in space.items(): specs = str(specs).split('\n') method = specs[3].split(' ')[-1] bounds = specs[4:] if len(bounds) == 1: bounds = bounds[0].split('range')[-1] bounds = bounds.replace('(', '').replace(')', '').replace('}', '') low, high = [float(v) for v in bounds.split(',')] else: vals = [ float( b.split('Literal')[-1].replace('}', '').replace('{', '')) for b in bounds ] low = min(vals) high = max(vals) if method == 'randint': dimensions.append(skopt.space.Integer(low, high, name=name)) elif method == 'uniform': dimensions.append( skopt.space.Real(low, high, name=name, prior='uniform')) elif method == 'loguniform': low, high = np.exp(low), np.exp(high) dimensions.append( skopt.space.Real(low, high, name=name, prior='log-uniform')) else: raise NotImplementedError skopt_space = skopt.Space(dimensions) return skopt_space
def _convert_to_param_space(df, param_cols, param_types): dimensions = [] for colname, col_type in zip(param_cols, param_types): if col_type == str: dimensions.append( skopt.space.Categorical(categories=df[colname].unique(), name=colname)) elif col_type == float: low, high = df[colname].min(), df[colname].max() dimensions.append(skopt.space.Real(low, high, name=colname)) else: raise NotImplementedError skopt_space = skopt.Space(dimensions) return skopt_space
def dispatch(self, *args, **kwargs): import skopt minim = self._obj self._ensure_object(minim) if len(args) > 0: raise ValueError( f"{minim.__class__.__name__}.to.skopt only accepts keyword arguments" ) # First create the Space variable def skoptReal(v): low, high = v.bounds return skopt.space.Real(low, high, transform="identity", name=v.name) space = skopt.Space(list(map(skoptReal, self.variables))) # Extract sampled data-points if "x" in kwargs: Xi = kwargs.pop("x") yi = kwargs.pop("y") else: Xi = np.array(self.data.x) yi = np.array(self.data.y) if "models" not in kwargs: import sklearn # We can't use categorial (SVC) since these are regression models # fast, but should not be as accurate? #model = sklearn.svm.LinearSVR() # much slower, but more versatile # I don't know which one is better ;) model = sklearn.svm.SVR(cache_size=500) #model = sklearn.svm.NuSVR(kernel="poly", cache_size=500) # we need to fit to create auxiliary data warnings.warn( f"Converting to skopt without a 'models' argument forces " f"{minim.__class__.__name__} to train a model for the sampled data. " f"This may be slow depending on the number of samples...") model.fit(Xi, yi) kwargs["models"] = [model] result = skopt.utils.create_result(Xi, yi, space=space, **kwargs) return result