optunity包python optunity.maximize_structured问题

时间:2018-02-23 12:49:40

标签: python pandas scikit-learn

我正在附加代码。我试图复制链接link中给出的代码。我在我的数据上尝试此代码。下面是我得到的代码和错误。很抱歉问题的形成不好我试图以正确的方式插入代码,但它似乎不适用于某些命令。

search = {'algorithm': {'k-nn': {'n_neighbors': [1, 5]},
                        'SVM': {'kernel': {'linear': {'C': [0, 2]},
                                           'rbf': {'gamma': [0, 1], 'C': [0, 10]},
                                           'poly': {'degree': [2, 5], 'C': [0, 50], 'coef0': [0, 1]}
                                           }
                                },
                        'naive-bayes': None,
                        'random-forest': {'n_estimators': [10, 30],
                                          'max_features': [5, 20]}
                        }
         }

@optunity.cross_validated(x=data, y=labels, num_folds=5)

def performance(x_train, y_train, x_test, y_test,
                algorithm, n_neighbors=None, n_estimators=None, max_features=None,
                kernel=None, C=None, gamma=None, degree=None, coef0=None):
    # fit the model
    if algorithm == 'k-nn':
        model = KNeighborsClassifier(n_neighbors=int(n_neighbors))
        model.fit(x_train, y_train)
    elif algorithm == 'naive-bayes':
        model = GaussianNB()
        model.fit(x_train, y_train)
    elif algorithm == 'random-forest':
        model = RandomForestClassifier(n_estimators=int(n_estimators),
                                       max_features=int(max_features))
        model.fit(x_train, y_train)
 # predict the test set
    if algorithm == 'k-nn':
        predictions =  model.predict_proba(x_test)[:, 1]
    else:
        predictions = model.predict_proba(x_test)[:, 1]

    return optunity.metrics.roc_auc(y_test, predictions, positive=True)


optimal_configuration, info, _ = optunity.maximize_structured(performance,
                                                              search_space=search,
                                                              num_evals=300)

这是我收到的错误消息

TypeError                                 Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\optunity\cross_validation.py in select(collection, indices)
     76     try:
---> 77         return collection[indices, ...]
     78     except IndexError: # caused by scipy.sparse in some versions

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   1963         else:
-> 1964             return self._getitem_column(key)
   1965 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   1970         if self.columns.is_unique:
-> 1971             return self._get_item_cache(key)
   1972 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1642         cache = self._item_cache
-> 1643         res = cache.get(item)
   1644         if res is None:

TypeError: unhashable type: 'list'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2441             try:
-> 2442                 return self._engine.get_loc(key)
   2443             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 26770

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-153-3d5c9d0e2047> in <module>()
      1 optimal_configuration, info, _ = optunity.maximize_structured(performance,
      2                                                               search_space=search,
----> 3                                                               num_evals=300)

C:\ProgramData\Anaconda3\lib\site-packages\optunity\api.py in maximize_structured(f, search_space, num_evals, pmap)
    368     solver = make_solver(**suggestion)
    369     solution, details = optimize(solver, f, maximize=True, max_evals=num_evals,
--> 370                                  pmap=pmap, decoder=tree.decode)
    371     return solution, details, suggestion
    372 

C:\ProgramData\Anaconda3\lib\site-packages\optunity\api.py in optimize(solver, func, maximize, max_evals, pmap, decoder)
    243     time = timeit.default_timer()
    244     try:
--> 245         solution, report = solver.optimize(f, maximize, pmap=pmap)
    246     except fun.MaximumEvaluationsException:
    247         # early stopping because maximum number of evaluations is reached

C:\ProgramData\Anaconda3\lib\site-packages\optunity\solvers\ParticleSwarm.py in optimize(self, f, maximize, pmap)
    269         for g in range(self.num_generations):
    270             fitnesses = pmap(evaluate, list(map(self.particle2dict, pop)))
--> 271             for part, fitness in zip(pop, fitnesses):
    272                 part.fitness = fit * util.score(fitness)
    273                 if not part.best or part.best_fitness < part.fitness:

C:\ProgramData\Anaconda3\lib\site-packages\optunity\solvers\ParticleSwarm.py in evaluate(d)
    257         @functools.wraps(f)
    258         def evaluate(d):
--> 259             return f(**d)
    260 
    261         if maximize:

C:\ProgramData\Anaconda3\lib\site-packages\optunity\functions.py in wrapped_f(*args, **kwargs)
    354             else:
    355                 wrapped_f.num_evals += 1
--> 356                 return f(*args, **kwargs)
    357         wrapped_f.num_evals = 0
    358         return wrapped_f

C:\ProgramData\Anaconda3\lib\site-packages\optunity\constraints.py in wrapped_f(*args, **kwargs)
    149         def wrapped_f(*args, **kwargs):
    150             try:
--> 151                 return f(*args, **kwargs)
    152             except ConstraintViolation:
    153                 return default

C:\ProgramData\Anaconda3\lib\site-packages\optunity\constraints.py in wrapped_f(*args, **kwargs)
    127             if violations:
    128                 raise ConstraintViolation(violations, *args, **kwargs)
--> 129             return f(*args, **kwargs)
    130         wrapped_f.constraints = constraints
    131         return wrapped_f

C:\ProgramData\Anaconda3\lib\site-packages\optunity\constraints.py in func(*args, **kwargs)
    264         @functions.wraps(f)
    265         def func(*args, **kwargs):
--> 266             return f(*args, **kwargs)
    267     return func
    268 

C:\ProgramData\Anaconda3\lib\site-packages\optunity\search_spaces.py in wrapped(**kwargs)
    324         def wrapped(**kwargs):
    325             decoded = self.decode(kwargs)
--> 326             return f(**decoded)
    327         return wrapped
    328 

C:\ProgramData\Anaconda3\lib\site-packages\optunity\functions.py in wrapped_f(*args, **kwargs)
    299         value = wrapped_f.call_log.get(*args, **kwargs)
    300         if value is None:
--> 301             value = f(*args, **kwargs)
    302             wrapped_f.call_log.insert(value, *args, **kwargs)
    303         return value

C:\ProgramData\Anaconda3\lib\site-packages\optunity\cross_validation.py in __call__(self, *args, **kwargs)
    396                                                     for i in range(self.num_folds)
    397                                                     if not i == fold]))
--> 398                 kwargs['x_train'] = select(self.x, rows_train)
    399                 kwargs['x_test'] = select(self.x, rows_test)
    400                 if not self.y is None:  # dealing with a supervised algorithm

C:\ProgramData\Anaconda3\lib\site-packages\optunity\cross_validation.py in select(collection, indices)
     82             indexset = set(indices)
     83             return collection.zipWithIndex().filter(lambda x: x[1] in indexset).map(lambda x: x[0])
---> 84         return [collection[i] for i in indices]
     85 
     86 

C:\ProgramData\Anaconda3\lib\site-packages\optunity\cross_validation.py in <listcomp>(.0)
     82             indexset = set(indices)
     83             return collection.zipWithIndex().filter(lambda x: x[1] in indexset).map(lambda x: x[0])
---> 84         return [collection[i] for i in indices]
     85 
     86 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   1962             return self._getitem_multilevel(key)
   1963         else:
-> 1964             return self._getitem_column(key)
   1965 
   1966     def _getitem_column(self, key):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   1969         # get column
   1970         if self.columns.is_unique:
-> 1971             return self._get_item_cache(key)
   1972 
   1973         # duplicate columns & possible reduce dimensionality

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1643         res = cache.get(item)
   1644         if res is None:
-> 1645             values = self._data.get(item)
   1646             res = self._box_item_values(item, values)
   1647             cache[item] = res

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   3588 
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2442                 return self._engine.get_loc(key)
   2443             except KeyError:
-> 2444                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2445 
   2446         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 26770

1 个答案:

答案 0 :(得分:0)

所以我得到了答案。输入数据集的格式需要在数组中,标签作为布尔列表。