Ejemplo n.º 1
0
 def __call__(self, config: dict, budget: float = 1) -> float:
     layered_dict = layering_config(config)
     AS_HP = layered_dict['classifier'].copy()
     AS, HP = AS_HP.popitem()
     ML_model = eval(AS)(**HP)
     # scores = cross_val_score(ML_model, self.X, self.y, cv=self.cv, scoring=self.metric)
     # -------------------------------------------------------------
     # 采用在对【 5折交叉验证中的训练集 】进行采样的方法,采样率为 budget
     sample_ratio = budget
     scores = []
     for i, (train_ix, valid_ix) in enumerate(self.cv.split(X, y)):
         rng = np.random.RandomState(i)
         size = int(train_ix.size * sample_ratio)
         train_ix = rng.choice(train_ix, size, replace=False)
         X_train = X[train_ix, :]
         y_train = y[train_ix]
         X_valid = X[valid_ix, :]
         y_valid = y[valid_ix]
         ML_model.fit(X_train, y_train)
         y_pred = ML_model.predict(X_valid)
         score = eval(f"sklearn.metrics.{self.metric}_score")(y_valid, y_pred)
         scores.append(score)
     # -------------------------------------------------------------
     score = np.mean(scores)
     return 1 - score
Ejemplo n.º 2
0
 def __call__(self, config):
     layered_config = layering_config(config)
     layered_config_ = deepcopy(layered_config)
     df = self.df
     for component in ['scaler', 'selector', 'learner']:
         sub_config = layered_config_[component]
         if sub_config is None:
             df_ = df.loc[df[component] == "None"]
             df = df_
             continue
         AS, HP = sub_config.popitem()
         df_ = df.loc[df[component] == AS, :]
         df = df_
         for k, v in HP.items():
             name = f"{component}.{AS}.{k}"
             # 对于浮点数考虑精度误差
             if isinstance(v, float):
                 df_ = df.loc[np.abs(df[name] - v) < 1e-8, :]
             else:
                 df_ = df.loc[df[name] == v, :]
             df = df_
     assert df.shape[0] == 1
     loss = 1 - float(df[self.metric].values[0])
     self.losses.append(loss)
     return loss
Ejemplo n.º 3
0
 def __call__(self, config: dict) -> float:
     layered_dict = layering_config(config)
     AS_HP = layered_dict['classifier'].copy()
     AS, HP = AS_HP.popitem()
     ML_model = eval(AS)(**HP)
     scores = cross_val_score(ML_model,
                              self.X,
                              self.y,
                              cv=self.cv,
                              scoring=self.metric)
     score = scores.mean()
     return 1 - score
Ejemplo n.º 4
0
 def test_conditional_space(self):
     # 测试conditional
     cs = HDL2CS()({
         "model(choice)": {
             "linearsvc": {
                 "max_iter": {"_type": "int_quniform", "_value": [300, 3000, 100], "_default": 600},
                 "penalty": {"_type": "choice", "_value": ["l1", "l2"], "_default": "l2"},
                 "dual": {"_type": "choice", "_value": [True, False], "_default": False},
                 "loss": {"_type": "choice", "_value": ["hinge", "squared_hinge"], "_default": "squared_hinge"},
                 "C": {"_type": "loguniform", "_value": [0.01, 10000], "_default": 1.0},
                 "multi_class": "ovr",
                 "random_state": 42,
                 "__forbidden": [
                     {"penalty": "l1", "loss": "hinge"},
                     {"penalty": "l2", "dual": False, "loss": "hinge"},
                     {"penalty": "l1", "dual": False},
                     {"penalty": "l1", "dual": True, "loss": "squared_hinge"},
                 ]
             },
             "svc": {
                 "C": {"_type": "loguniform", "_value": [0.01, 10000], "_default": 1.0},
                 "kernel": {"_type": "choice", "_value": ["rbf", "poly", "sigmoid"], "_default": "rbf"},
                 "degree": {"_type": "int_uniform", "_value": [2, 5], "_default": 3},
                 "gamma": {"_type": "loguniform", "_value": [1e-05, 8], "_default": 0.1},
                 "coef0": {"_type": "quniform", "_value": [-1, 1], "_default": 0},
                 "shrinking": {"_type": "choice", "_value": [True, False], "_default": True},
                 "class_weight": None,
                 "probability": True,
                 "decision_function_shape": "ovr",
                 "__activate": {
                     "kernel": {
                         "rbf": ["gamma"],
                         "sigmoid": ["gamma", "coef0"],
                         "poly": ["degree", "gamma", "coef0"]
                     }
                 },
                 "random_state": 42
             },
         }
     })
     print(cs)
     configs=cs.sample_configuration(200)
     res=[layering_config(config) for config in configs]
     print(len(res))
     print("OK")
Ejemplo n.º 5
0
def evaluation(config: Configuration):
    dict_ = layering_config(config)
    choice, sub_config = dict_['parent'].popitem()
    coef = choice2coef[int(choice[-1])]
    # print(choice)
    return -df2.loc["".join([sub_config.get(f"X{i}") for i in range(4)]), 'Fitness'] * coef