当前位置: 首页>>代码示例>>Python>>正文


Python Evaluator.score方法代码示例

本文整理汇总了Python中evaluator.Evaluator.score方法的典型用法代码示例。如果您正苦于以下问题:Python Evaluator.score方法的具体用法?Python Evaluator.score怎么用?Python Evaluator.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在evaluator.Evaluator的用法示例。


在下文中一共展示了Evaluator.score方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_function

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import score [as 别名]
    def make_function(data, node_var):
        """ Generates a function.

            Args:
                data: Data object
                node_var: label for variable we are interested in

            Returns:
                sympy Function
        """
        TREE_COUNT = 10
        GENERATIONS = 100
        #GENERATIONS = 50
        #PROB_REPRODUCTION = 0.5
        #PROB_POINT = 0.4
        #PROB_CROSSOVER = 0.1
        PROB_REPRODUCTION = 0.4
        PROB_POINT = 0.25
        PROB_FLOAT = 0.3
        PROB_CROSSOVER = 0.05

        #THRESHOLD = -1e-1
        THRESHOLD = -1e-3

        # Generate a pool of trees
        trees = []
        for _ in range(TREE_COUNT):
            depth = np.random.randint(2, 6+1)
            if np.random.random() < 0.5:
                trees.append(TreeMethods.create_grow_tree(depth))
            else:
                trees.append(TreeMethods.create_full_tree(depth))

        # For animation
        gif_fnames = []

        best_fitnesses = []

        for i in range(GENERATIONS):
            print("Generation: {0}".format(i))

            new_trees = []

            fitnesses = []
            for tree in trees:
                fitness = Evaluator.score(tree, data, node_var)
                fitnesses.append(fitness)
            fitnesses = np.array(fitnesses)

            # Draw pool
            gif_fname = "temp/_iteration_{0}.png".format(i)
            gif_fnames.append(gif_fname)
            Algorithm.draw_pool(trees, gif_fname, fitnesses)

            # Save best fitness
            best_fitness = np.max(fitnesses)
            print("Best fitness: {0}".format(best_fitness))
            best_fitnesses.append(best_fitness)

            # If a fitness is super good just end the algorithm
            if np.max(fitnesses) > THRESHOLD:
                print("Threshold reached! Ending.")
                break

            # Translate and normalize fitnesses until they form a
            # probability distribution
            #fitnesses += -np.min(fitnesses) + 1e-5
            #fitnesses /= np.sum(fitnesses)

            # TODO change this
            # Use an extremely nonlinear probability function
            fitnesses *= -1
            fitnesses += 1e-5
            fitnesses = 1/fitnesses

            # Normalize
            fitnesses /= np.sum(fitnesses)

            while len(new_trees) <= TREE_COUNT:
                r = random()
                if r < PROB_POINT:
                    # Point mutation
                    candidate_tree = np.random.choice(trees, 1, p=fitnesses)[0]
                    tree = Mutations.mutate_point(candidate_tree)
                    new_trees.append(tree)
                elif r < PROB_POINT + PROB_CROSSOVER:
                    # Crossover mutation
                    [ctree1, ctree2] = np.random.choice(trees, 2, p=fitnesses)
                    if len(ctree1.children) == 0 or len(ctree2.children) == 0:
                        # TODO handle this case somehow
                        # Abort, we dun goofed
                        continue
                    tree1, tree2 = Mutations.mutate_crossover(ctree1, ctree2)
                    new_trees.append(tree1)
                    new_trees.append(tree2)
                elif r < PROB_POINT + PROB_CROSSOVER + PROB_FLOAT:
                    # Float mutation
                    candidate_tree = np.random.choice(trees, 1, p=fitnesses)[0]

                    def score_tree(tree_):
#.........这里部分代码省略.........
开发者ID:mlberkeley,项目名称:genetic-algs,代码行数:103,代码来源:algorithm.py

示例2: TwoClassDiscreteFeatureNB

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import score [as 别名]
    for train_index, test_index in kf:
        print "===="
        train_label, test_label = label.ix[train_index], label.ix[test_index]
        train_label.index += 1
        test_label.index += 1
        train_data = spamdata[spamdata["email_id"].isin(train_label.index)]
        test_data = spamdata[spamdata["email_id"].isin(test_label.index)]
        nb = TwoClassDiscreteFeatureNB()
        nb.train(train_data, train_label)
        print test_label
        print "===="
        word_vectors = test_data[test_data["email_id"].isin(test_label.index)]
        print "Predicted:\n"
        pre_label = nb.classify(word_vectors)
        print pre_label
        e = Evaluator(pre_label, test_label, 1)
        print e.score()
        if e.accuracy > max_accuracy:
            max_accuracy = e.accuracy
            best_nv = nb
    total_word_vector = spamdata[spamdata["email_id"].isin(label.index)]
    total_predict = best_nv.classify(total_word_vector)
    e_t = Evaluator(total_predict, label, 1)
    print e_t.score()
    for email_id, df in word_vectors.groupby("email_id"):
        print df
    print words_vector
    print nb.classify(word_vectors)
    for e in test_label.index:
        print test_label.ix[e][0]
开发者ID:AngeloK,项目名称:cs584-hws,代码行数:32,代码来源:main.py

示例3: score_tree

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import score [as 别名]
 def score_tree(tree_):
     return Evaluator.score(tree_, data, node_var)
开发者ID:mlberkeley,项目名称:genetic-algs,代码行数:4,代码来源:algorithm.py


注:本文中的evaluator.Evaluator.score方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。