def __best_solution(training_set, test_set, knn_function, solutions, adaptative=False):

    n_features = len(solutions[0][0])
    best = (0, ([1.0]*n_features, [1]*n_features, 1))

    for s in solutions:
            hit_rate = training_machine(training_set, test_set, knn_function, s, adaptative)
            if hit_rate > best[0]:
                    best = (hit_rate, s)
    return best
Пример #2
0
def __best_solution(training_set, test_set, knn_function, solutions, adaptative=False):

    n_features = len(solutions[0][0])
    best = (0, ([1.0]*n_features, [1]*n_features, 1))

    for s in solutions:
            hit_rate = training_machine(training_set, test_set, knn_function, s, adaptative)
            if hit_rate > best[0]:
                    best = (hit_rate, s)
    return best
def __cost(training_set, test_set, knn_function, solution, adaptative=False):
    """
    Calculates the cost of a given solution.
    In this case, calculates the accuracy rate of a k-NN.

    @type training_set: dict
    @param training_set: the training dataset
    @type test_set: dict
    @param test_set: the test dataset
    @type knn_function: function
    @param knn_function: the function used to classify the examples
    @type solution: tuple
    @param solution: the solution
    @type adaptative: bool
    @param adaptative: indicates when to use adaptative distance

    @rtype: float
    @returns: the cost of the given solution
    """

    # Dado a solucao (configuracao de pesos/caracteristicas), transforma os , calcula o k-NN e retorna a taxa de acerto
    return training_machine(training_set, test_set, knn_function, solution, adaptative)
Пример #4
0
def __cost(training_set, test_set, knn_function, solution, adaptative=False):
    """
    Calculates the cost of a given solution.
    In this case, calculates the accuracy rate of a k-NN.

    @type training_set: dict
    @param training_set: the training dataset
    @type test_set: dict
    @param test_set: the test dataset
    @type knn_function: function
    @param knn_function: the function used to classify the examples
    @type solution: tuple
    @param solution: the solution
    @type adaptative: bool
    @param adaptative: indicates when to use adaptative distance

    @rtype: float
    @returns: the cost of the given solution
    """

    # Dado a solucao (configuracao de pesos/caracteristicas), transforma os , calcula o k-NN e retorna a taxa de acerto
    return training_machine(training_set, test_set, knn_function, solution, adaptative)
    return best

if __name__ ==  '__main__':
  # Tabu search Adaptive
      for path in ['liver/selected/drop3', 'liver/selected/hmn_ei',
          'liver/selected/ib2', 'liver/selected/icf', 'liver/selected/mldb',
          'liver/selected/oss', 'liver/folds/original']:
        path = 'datasets/' + path
        for e in range(5):
          n_features = 6
          solution = ([1.0]*n_features, [1]*n_features, 1)
          training_set = get_data(path + '/training_%i' % e)
          test_set = get_data(path + '/test_%i' % e)

          # Tabu search Adaptive
          with open(path + '/ts-adaptive-%i.log' % e,'w') as f:
                          initial = clock()
                          print 'TS Adaptive Distance'
                          best_solution = tabu_search(training_set, test_set, knn_euclidian, solution, 100, f, adaptative=True)
                          f.write('best solution: %s\n' % str(best_solution))
                          middle = clock()
                          print 'Hits for the best solution'
                          hits = training_machine(training_set, test_set, knn_euclidian, best_solution)
                          print hits
                          final = clock()

                          f.write('Hits for the best solution: %s\n' % hits)

                          f.write('TS Spent time: %s\n' % str(middle - initial))
                          f.write('k-NN Spent time: %s\n' % str(final - middle))
Пример #6
0
        path = 'datasets/' + path
        for e in range(5):
            n_features = 6
            solution = ([1.0] * n_features, [1] * n_features, 1)
            training_set = get_data(path + '/training_%i' % e)
            test_set = get_data(path + '/test_%i' % e)

            # Tabu search Adaptive
            with open(path + '/ts-adaptive-%i.log' % e, 'w') as f:
                initial = clock()
                print 'TS Adaptive Distance'
                best_solution = tabu_search(training_set,
                                            test_set,
                                            knn_euclidian,
                                            solution,
                                            100,
                                            f,
                                            adaptative=True)
                f.write('best solution: %s\n' % str(best_solution))
                middle = clock()
                print 'Hits for the best solution'
                hits = training_machine(training_set, test_set, knn_euclidian,
                                        best_solution)
                print hits
                final = clock()

                f.write('Hits for the best solution: %s\n' % hits)

                f.write('TS Spent time: %s\n' % str(middle - initial))
                f.write('k-NN Spent time: %s\n' % str(final - middle))