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


Python Evaluator.evaluate方法代码示例

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


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

示例1: NERValidator

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
class NERValidator(object):
    def __init__(self, recbysns, classifiers):
        self.recbysns = recbysns
        self.db = self.recbysns.db
        self.classifiers = classifiers
        self.evaluator = Evaluator()
        self.confusion_matrixes = {str(classifier):
                                   [] for classifier in self.classifiers}

    def validate(self):
        entities = [NEREntity(self.recbysns, entity)
                    for entity in self.db.select_table('recbysns_entity', '1')]
        for classifier in self.classifiers:
            self.test(classifier, entities)
        self.evaluator.evaluate(self.confusion_matrixes)

    def test(self, classifier, entities):
        confusion_matrix = {
                NER_BOOK:   {NER_BOOK: float(0), NER_MOVIE: float(0),
                             NER_VIDEO: float(0), NER_OTHERS: float(0)},
                NER_MOVIE:  {NER_BOOK: float(0), NER_MOVIE: float(0),
                             NER_VIDEO: float(0), NER_OTHERS: float(0)},
                NER_VIDEO:  {NER_BOOK: float(0), NER_MOVIE: float(0),
                             NER_VIDEO: float(0), NER_OTHERS: float(0)},
                NER_OTHERS: {NER_BOOK: float(0), NER_MOVIE: float(0),
                             NER_VIDEO: float(0), NER_OTHERS: float(0)},
        }
        for entity in entities:
            # predicted ner class
            p_ner_class = classifier.predict(entity)
            ner_class = entity.ner_class()
            confusion_matrix[ner_class][p_ner_class] += 1
        print confusion_matrix
        self.confusion_matrixes[str(classifier)].append(confusion_matrix)
开发者ID:jjyao,项目名称:recbysns,代码行数:36,代码来源:ner.py

示例2: SAValidator

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
class SAValidator(object):
    def __init__(self, recbysns, classifiers):
        self.recbysns = recbysns
        self.db = self.recbysns.db
        self.classifiers = classifiers
        self.evaluator = Evaluator()
        self.confusion_matrixes = {str(classifier): []
                                   for classifier in self.classifiers}

    def validate(self):
        entities = [SAEntity(self.recbysns, entity)
                    for entity in self.db.select_table('recbysns_entity',
                    "type in (%d,%d,%d)" % (NER_BOOK, NER_MOVIE, NER_VIDEO))]
        for classifier in self.classifiers:
            self.test(classifier, entities)
        self.evaluator.evaluate(self.confusion_matrixes)

    def test(self, classifier, entities):
        confusion_matrix = {
                SA_POSITIVE: {SA_POSITIVE: float(0), SA_NETURAL: float(0),
                              SA_NEGATIVE: float(0)},
                SA_NETURAL:  {SA_POSITIVE: float(0), SA_NETURAL: float(0),
                              SA_NEGATIVE: float(0)},
                SA_NEGATIVE: {SA_POSITIVE: float(0), SA_NETURAL: float(0),
                              SA_NEGATIVE: float(0)},
        }
        for entity in entities:
            # predicted sa_class
            p_sa_class = classifier.predict(entity)
            # actual sa_class
            sa_class = entity.sa_class()
            confusion_matrix[sa_class][p_sa_class] += 1
        print confusion_matrix
        self.confusion_matrixes[str(classifier)].append(confusion_matrix)
开发者ID:jjyao,项目名称:recbysns,代码行数:36,代码来源:sa.py

示例3: test_end_to_end

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
    def test_end_to_end(self):
        with open("unit_tests/fixtures/tests_games.json") as file:
            data = json.loads(file.read())

        # Update player's source placeholder with actual code
        with open("unit_tests/fixtures/Snakes/Snakes.cpp") as source:
            data["source"] = source.read()

        # Update opponent's source placeholder with actual code
        with open("unit_tests/fixtures/Snakes/Opponent.cpp") as source:
            data["matches"][0]["source"] = source.read()

        evaluator = Evaluator(data)
        evaluator.evaluate()
开发者ID:espr1t,项目名称:action,代码行数:16,代码来源:test_games.py

示例4: evaluate_classifier

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def evaluate_classifier(clf_wrapper,
                        params,
                        train_images_codelabels, train_labels,
                        test_images_codelabels, test_labels):

    print "==\nevaluate_classifier (test size: {})\n{}".format(len(test_labels), clf_wrapper)

    print "training classifier {}".format(clf_wrapper.clf)
    start_time = time.time()
    clf_wrapper.fit(X=train_images_codelabels, labels=train_labels)
    et = (time.time() - start_time) * 1000.0
    print "finished training classifier - took {}ms".format(et)

    # evaluate
    print "proceeding to evaluate classifier on test set {}".format(len(test_labels))
    encoded_test_labels = clf_wrapper.label_encoder.transform(test_labels)

    evaluator = Evaluator(
        clf=clf_wrapper.clf,
        label_encoder=clf_wrapper.label_encoder,
        params=params,
        output_filepath="../results/evaluation_results_{}.json".format(clf_wrapper)
    )

    evaluator.results["classifier"] = "{}".format(clf_wrapper.clf)
    evaluator.results["classifier_training_time"] = "{}".format(et)

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:31,代码来源:model_tuning.py

示例5: detect

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
	def detect(self, img_path=None, output_file_prefix='', num_ft=100, offset=0, scaling_factor = 1.2, scaling_iters=3, nms=0.5, clf=None, templates=None, linear_scaling=False):
		
		#=====[ Load our classifier and templates ]=====
		clf = pickle.load(open(clf)) if clf else pickle.load(open('classifiers/top_ft_classifier_100_200', 'r'))
		templates = pickle.load(open(templates)) if templates else pickle.load(open('processed_data/top_templates_1000.p','r'))
	
		#=====[ Get top templates ]=====
		templates = templates[:num_ft]

		#=====[ Instantiate our feature generator ]=====
		fg = FeatureGenerator(templates)

		#=====[ Instantiate our detector ]=====
		if linear_scaling:
			self.detector = LinearScaleDetector(clf.clf, fg,scaling_factor=scaling_factor,scaling_iters=scaling_iters, nms=nms)
		else:
			self.detector = Detector(clf.clf, fg,scaling_factor=scaling_factor,scaling_iters=scaling_iters, nms=nms)

		#=====[ If a specific image path is given, then we do not evaluate, just detect the pedestrian and draw bounding boxes ]=====
		if img_path:
			_, bbs = self.detector.detect_pedestrians(img_path)
			self._draw_bbs(img_path, bbs)
		
		else:
			#=====[ Instantiate our evaluator and evaluate ]=====
			evaluator = Evaluator('INRIAPerson/Test', self.detector)
			FPPI, miss_rate = evaluator.evaluate(output_file_prefix,offset)

			print '\nFPPI: {}\nMiss rate: {}\n'.format(FPPI, miss_rate)
开发者ID:sigberto,项目名称:informed-haar,代码行数:31,代码来源:pipeline.py

示例6: window_overlap_test

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def window_overlap_test(window_overlap=2.):
    """  """

    train_labels, train_images, test_labels, test_images = get_training_and_test_data()

    # split to make experimentation quicker
    train_labels, train_images = get_subset_of_training_data(train_labels, train_images, split=0.5)

    training_size = len(train_labels)

    desc = "testing influence of window_overlap, set to {}. NB training size = {}".format(
        window_overlap,
        training_size
    )

    print desc

    selected_labels = list(set(train_labels))

    params = build_params(num_classes=len(selected_labels),
                          training_size=len(train_images),
                          test_size=len(test_images),
                          window_overlap=window_overlap,
                          fn_prefix="winoverlap-{}".format(window_overlap))

    trainer = SketchRecognitionTrainer(
        file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
        run_parallel_processors=True,
        params=params
    )

    classifier = trainer.train_and_build_classifier(train_labels, train_images)
    encoded_test_labels = classifier.le.transform(test_labels)

    test_images_codelabels = trainer.code_labels_for_image_descriptors(
        trainer.extract_image_descriptors(test_images)
    )

    evaluator = Evaluator(
        clf=classifier.clf,
        label_encoder=classifier.le,
        params=params,
        output_filepath=SketchRecognitionTrainer.get_evaluation_filename_for_params(params=params)
    )

    # add timings to output
    evaluator.results["timings"] = {}
    for key, value in trainer.timings.iteritems():
        evaluator.results["timings"][key] = value

    # add comment
    evaluator.results["desc"] = desc

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:57,代码来源:feature_engineering_tuning.py

示例7: main

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def main():
    """Requests infix expressions, translates them to postfix,
    and evaluates them, until the user enters nothing."""
    while True:
        sourceStr = input("Enter an infix expression: ")
        if sourceStr == "": break
        try:
            scanner = Scanner(sourceStr)
            translator = Translator(scanner)
            postfix = translator.translate()
            evaluator = Evaluator(postfix)
            print("Value:", evaluator.evaluate())
        except Exception as e:
            print("Error:", e, translator.translationStatus())
开发者ID:Billy-The-Squid,项目名称:Cayley,代码行数:16,代码来源:evaluatorapp.py

示例8: solve

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
    def solve(self, instance, startpoint=None):
        if startpoint is None:
            startpoint = Random().solve(instance)

        e = Evaluator(instance)
        current = (startpoint, e.evaluate(startpoint))

        while True:
            next_step = self.select_step(e, current)
            if not next_step:
                break
            else:
                current = next_step

        return current[0]
开发者ID:powerllamas,项目名称:MiOIB,代码行数:17,代码来源:local_search.py

示例9: training_size_test

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def training_size_test(split=0.5):
    """
    see what effect the training size has on the performance, initially take off 50%
    """
    print "test_2"

    train_labels, train_images, test_labels, test_images = get_training_and_test_data()

    train_labels, train_images = get_subset_of_training_data(train_labels, train_images, split=split)

    selected_labels = list(set(train_labels))

    params = build_params(num_classes=len(selected_labels),
                          training_size=len(train_images),
                          test_size=len(test_images))

    trainer = SketchRecognitionTrainer(
        file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
        run_parallel_processors=True,
        params=params
    )

    classifier = trainer.train_and_build_classifier(train_labels, train_images)
    encoded_test_labels = classifier.le.transform(test_labels)

    test_images_codelabels = trainer.code_labels_for_image_descriptors(
        trainer.extract_image_descriptors(test_images)
    )

    evaluator = Evaluator(
        clf=classifier.clf,
        label_encoder=classifier.le,
        params=params,
        output_filepath=SketchRecognitionTrainer.get_evaluation_filename_for_params(params=params)
    )

    # add timings to output
    evaluator.results["timings"] = {}
    for key, value in trainer.timings.iteritems():
        evaluator.results["timings"][key] = value

    # add comment
    evaluator.results["desc"] = "After many iterations, this is a baseline for which tuning will benchmark from"

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:48,代码来源:feature_engineering_tuning.py

示例10: sanity_check

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def sanity_check():
    """ baseline test, all all parameters from experimentation """

    train_labels, train_images, test_labels, test_images = get_training_and_test_data()

    train_labels, train_images = get_subset_of_training_data(train_labels, train_images, split=0.05)

    selected_labels = list(set(train_labels))

    params = build_params(
        num_classes=len(selected_labels),
        training_size=len(train_images),
        test_size=len(test_images),
        fn_prefix="sanitycheck"
    )

    trainer = SketchRecognitionTrainer(
        file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
        run_parallel_processors=True,
        params=params
    )

    classifier = trainer.train_and_build_classifier(train_labels, train_images)
    encoded_test_labels = classifier.le.transform(test_labels)

    test_images_codelabels = trainer.code_labels_for_image_descriptors(
        trainer.extract_image_descriptors(test_images)
    )

    evaluator = Evaluator(
        clf=classifier.clf,
        label_encoder=classifier.le,
        params=params,
        output_filepath=SketchRecognitionTrainer.get_evaluation_filename_for_params(params=params)
    )

    # add timings to output
    evaluator.results["timings"] = {}
    for key, value in trainer.timings.iteritems():
        evaluator.results["timings"][key] = value

    # add comment
    evaluator.results["desc"] = "After many iterations, this is a baseline for which tuning will benchmark from"

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:48,代码来源:feature_engineering_tuning.py

示例11: cluster_size_test

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def cluster_size_test(num_clusters=200):
    """  """

    train_labels, train_images, test_labels, test_images = get_training_and_test_data()

    selected_labels = list(set(train_labels))

    params = build_params(num_classes=len(selected_labels),
                          training_size=len(train_images),
                          test_size=len(test_images),
                          num_clusters=num_clusters)

    trainer = SketchRecognitionTrainer(
        file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
        run_parallel_processors=True,
        params=params
    )

    classifier = trainer.train_and_build_classifier(train_labels, train_images)
    encoded_test_labels = classifier.le.transform(test_labels)

    test_images_codelabels = trainer.code_labels_for_image_descriptors(
        trainer.extract_image_descriptors(test_images)
    )

    evaluator = Evaluator(
        clf=classifier.clf,
        label_encoder=classifier.le,
        params=params,
        output_filepath=SketchRecognitionTrainer.get_evaluation_filename_for_params(params=params)
    )

    # add timings to output
    evaluator.results["timings"] = {}
    for key, value in trainer.timings.iteritems():
        evaluator.results["timings"][key] = value

    # add comment
    evaluator.results["desc"] = "testing influence of num_clusters, set to {}".format(num_clusters)

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:44,代码来源:feature_engineering_tuning.py

示例12: start

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def start():
    movies = load_dataset()
    model = MatrixPreferenceDataModel(movies['data'])
    option = int(input("Enter: \n 1 for User Based Recommender \n 2 for Item Based Recommender \n"))
    if option != 1 and option != 2:
        print("Invalid Input")
        return
    if option == 1:
        similarity = UserSimilarity(model, cosine_distances)
        neighborhood = NearestNeighborsStrategy()
        recsys = UserBasedRecommender(model, similarity, neighborhood)

    if option == 2:
        similarity = ItemSimilarity(model, cosine_distances)
        neighborhood = ItemsNeighborhoodStrategy()
        recsys = ItemBasedRecommender(model, similarity, neighborhood)

    evaluator = Evaluator()
    all_scores = evaluator.evaluate(recsys, permutation=False)
    print all_scores
开发者ID:Sandy4321,项目名称:MovieRecommendationEngine,代码行数:22,代码来源:main.py

示例13: __init__

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
class Population:

    def __init__(self, popSize=10, populate=False):
        self.__size = popSize
        self.__members = [None] * self.__size
        self.__eval = Evaluator()
        self.__BITS = self.__eval.getBits()
        if (populate):
            self.__populate(1, self.__BITS)

    def __populate(self, numChromosomes, numGenes, seed=-1):
        if (seed > 0):
            random.seed(seed)
        for i in range(self.__size):
            m = Member()
            for j in range(numChromosomes):
                m.chromosomes.append(random.randrange(0, 1 << numGenes))
            self.__members[i] = m

    def nextGeneration(self):
        nextGen = self.__select()
        children = []
        for i in range(int(self.__size / 2)):
            p1 = random.randrange(len(nextGen))
            p2 = random.randrange(len(nextGen))
            while(p2 == p1):
                p2 = random.randrange(len(nextGen))
            m = self.crossover(nextGen[p1], nextGen[p2], random.randrange(self.__BITS - 1), self.__BITS)[1]
            if (random.randrange(10) == 0):
                m.chromosomes[0] = self.mutate(m.chromosomes[0], random.randrange(self.__BITS))
            children.append(m)
        self.__members = nextGen + children

    def __select(self):
        evals = {}
        for i in range(len(self.__members)):
            #print(self.__eval.evaluate(self.__members[i]))
            evals.update({i:self.__eval.evaluate(self.__members[i])})
        sort = sorted(evals.items(), key=operator.itemgetter(1))
        newPop = []
        for i in range(int(self.__size / 2)):
            newPop.append(self.__members[sort.pop()[0]])
        return newPop



    def crossover(self, m1, m2, radix, bits):
        # Convert all of the chromosomes in each member to a single binary number
        n1 = n2 = 0
        for i in range(len(m1.chromosomes)): # Each member should have the same number of chromosomes
            n1 += m1.chromosomes[i] * (2 ** (i * bits))
            n2 += m2.chromosomes[i] * (2 ** (i * bits))
        totalBits = bits * len(m1.chromosomes)
        if ((n1 > 2 ** totalBits) or (n2 > 2 ** totalBits)):
            return -1
        mask = (2 ** totalBits) - 1
        lm = mask >> totalBits - radix
        um = mask << radix
        cn1 = (n1 & lm) | (n2 & um)
        cn2 = (n1 & um) | (n2 & lm)
        # Convert back to members
        cm1 = Member()
        cm2 = Member()
        mask = (2 ** bits) - 1
        for i in range(len(m1.chromosomes)):
            cm1.chromosomes.append(cn1 & mask)
            cm2.chromosomes.append(cn2 & mask)
            cn1 >>= bits
            cn2 >>= bits
        return [cm1, cm2]

    def mutate(self, n, radix):
        mask = 2 ** radix
        if (n & mask == 0):
            return (n + mask)
        else:
            return (n - mask)

    def getSize(self):
        return self.__size

    def getMember(self, index):
        return self.__members[index]

    def setMember(self, index, member):
        if (member is Member):
            self.__members[index] = member

    def getEvaluator(self):
        return self.__eval

    def getBits(self):
        return self.__BITS
开发者ID:Kuragama,项目名称:GeneOne,代码行数:95,代码来源:population.py

示例14: clustering_algorithm_test

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
def clustering_algorithm_test(clustering='kmeans'):
    """  """

    from sklearn.cluster import KMeans
    from sklearn.cluster import MiniBatchKMeans
    import multiprocessing

    train_labels, train_images, test_labels, test_images = get_training_and_test_data()

    # split to make experimentation quicker
    train_labels, train_images = get_subset_of_training_data(train_labels, train_images, split=0.5)

    training_size = len(train_labels)

    desc = "testing influence of different clustering algorithms, using {} for a training size of {}".format(
        clustering,
        training_size
    )

    print desc

    selected_labels = list(set(train_labels))

    params = build_params(num_classes=len(selected_labels),
                          training_size=len(train_images),
                          test_size=len(test_images),
                          fn_prefix="clustering-{}".format(clustering))

    trainer = SketchRecognitionTrainer(
        file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
        run_parallel_processors=True,
        params=params
    )

    if clustering == "kmeans":
        trainer.clustering = KMeans(
            init='k-means++',
            n_clusters=params[ParamKeys.NUM_CLUSTERS],
            n_init=10,
            max_iter=10,
            tol=1.0,
            n_jobs=multiprocessing.cpu_count() if trainer.run_parallel_processors else 1
        )
    elif clustering == "minibatchkmeans":
        trainer.clustering = MiniBatchKMeans(
            init='k-means++',
            n_clusters=params[ParamKeys.NUM_CLUSTERS],
            batch_size=100,
            n_init=10,
            max_no_improvement=10,
            verbose=0
        )
    elif clustering == "meanshift":
        trainer = MeanShiftSketchRecognitionTrainer(
            file_path=SketchRecognitionTrainer.get_cookbook_filename_for_params(params=params),
            run_parallel_processors=True,
            params=params
        )


    classifier = trainer.train_and_build_classifier(train_labels, train_images)
    encoded_test_labels = classifier.le.transform(test_labels)

    test_images_codelabels = trainer.code_labels_for_image_descriptors(
        trainer.extract_image_descriptors(test_images)
    )

    evaluator = Evaluator(
        clf=classifier.clf,
        label_encoder=classifier.le,
        params=params,
        output_filepath=SketchRecognitionTrainer.get_evaluation_filename_for_params(params=params)
    )

    # add timings to output
    evaluator.results["timings"] = {}
    for key, value in trainer.timings.iteritems():
        evaluator.results["timings"][key] = value

    # add comment
    evaluator.results["desc"] = desc

    evaluation_results = evaluator.evaluate(X=test_images_codelabels, y=encoded_test_labels)
    print evaluation_results
开发者ID:joshnewnham,项目名称:udacity_machine_learning_engineer_nanodegree_capstone,代码行数:86,代码来源:feature_engineering_tuning.py

示例15: run_offline

# 需要导入模块: from evaluator import Evaluator [as 别名]
# 或者: from evaluator.Evaluator import evaluate [as 别名]
    def run_offline(self):
        LogUtil.log('INFO', 'cv_tag(%s)' % self.cv_tag)
        # load feature matrix
        offline_features = Feature.load_all(self.config.get('DIRECTORY', 'feature_pt'),
                                            self.config.get('FEATURE', 'feature_selected').split(),
                                            self.config.get('MODEL', 'offline_rawset_name'),
                                            self.config.get('FEATURE', 'will_save'))
        # load labels
        offline_labels = DataUtil.load_vector('%s/%s.label' % (self.config.get('DIRECTORY', 'label_pt'),
                                                               self.config.get('MODEL', 'offline_rawset_name')),
                                              True)
        # generate index file
        if '' == self.cv_tag:
            self.cv_tag = self.out_tag
            self.__generate_index(offline_features.shape[0])
        # cross validation
        offline_valid_preds_all = [0.] * offline_features.shape[0]
        offline_test_preds_all = [0.] * offline_features.shape[0]
        for fold_id in range(self.cv_num):
            LogUtil.log('INFO', 'cross validation fold_id(%d) begin' % fold_id)

            # generate training data set
            offline_train_pos_rate = float(self.config.get('MODEL', 'train_pos_rate'))
            offline_train_indexs_fp = '%s/cv_tag%s_n%d_f%d_train.%s.index' % (self.config.get('DIRECTORY', 'index_pt'),
                                                                              self.cv_tag,
                                                                              self.cv_num,
                                                                              fold_id,
                                                                              self.config.get('MODEL',
                                                                                              'offline_rawset_name'))
            offline_train_indexs = DataUtil.load_vector(offline_train_indexs_fp, 'int')
            offline_train_features, offline_train_labels, offline_train_balanced_indexs = \
                CrossValidation.__generate_data(offline_train_indexs,
                                                offline_labels,
                                                offline_features,
                                                offline_train_pos_rate)
            LogUtil.log('INFO', 'offline train data generation done')

            # generate validation data set
            offline_valid_pos_rate = float(self.config.get('MODEL', 'valid_pos_rate'))
            offline_valid_indexs_fp = '%s/cv_tag%s_n%d_f%d_valid.%s.index' % (self.config.get('DIRECTORY', 'index_pt'),
                                                                              self.cv_tag,
                                                                              self.cv_num,
                                                                              fold_id,
                                                                              self.config.get('MODEL',
                                                                                              'offline_rawset_name'))
            offline_valid_indexs = DataUtil.load_vector(offline_valid_indexs_fp, 'int')
            offline_valid_features, offline_valid_labels, offline_valid_balanced_indexs = \
                CrossValidation.__generate_data(offline_valid_indexs,
                                                offline_labels,
                                                offline_features,
                                                offline_valid_pos_rate)
            LogUtil.log('INFO', 'offline valid data generation done')

            # generate test data set
            offline_test_pos_rate = float(self.config.get('MODEL', 'test_pos_rate'))
            offline_test_indexs_fp = '%s/cv_tag%s_n%d_f%d_test.%s.index' % (self.config.get('DIRECTORY', 'index_pt'),
                                                                            self.cv_tag,
                                                                            self.cv_num,
                                                                            fold_id,
                                                                            self.config.get('MODEL',
                                                                                            'offline_rawset_name'))
            offline_test_indexs = DataUtil.load_vector(offline_test_indexs_fp, 'int')
            offline_test_features, offline_test_labels, offline_test_balanced_indexs = \
                CrossValidation.__generate_data(offline_test_indexs,
                                                offline_labels,
                                                offline_features,
                                                offline_test_pos_rate)
            LogUtil.log('INFO', 'offline test data generation done')

            model = Model.new(self.config.get('MODEL', 'model_name'), self.config)
            model_fp = self.config.get('DIRECTORY', 'model_pt') + '/cv_n%d_f%d.%s.model' % \
                                                                  (self.cv_num,
                                                                   fold_id,
                                                                   self.config.get('MODEL', 'model_name'))
            model.save(model_fp)
            offline_train_preds, offline_valid_preds, offline_test_preds = model.fit(offline_train_features,
                                                                                     offline_train_labels,
                                                                                     offline_valid_features,
                                                                                     offline_valid_labels,
                                                                                     offline_test_features,
                                                                                     offline_test_labels)
            offline_train_score = Evaluator.evaluate(self.config.get('MODEL', 'evaluator_name'),
                                                     offline_train_labels,
                                                     offline_train_preds)
            offline_valid_score = Evaluator.evaluate(self.config.get('MODEL', 'evaluator_name'),
                                                     offline_valid_labels,
                                                     offline_valid_preds)
            offline_test_score = Evaluator.evaluate(self.config.get('MODEL', 'evaluator_name'),
                                                    offline_test_labels,
                                                    offline_test_preds)
            score_fp = '%s/%s.score' % (self.config.get('DIRECTORY', 'score_pt'), 'cv')
            score_file = open(score_fp, 'a')
            score_file.write('fold:%d\ttrain:%s\tvalid:%s\ttest:%s\n' % (fold_id,
                                                                         offline_train_score,
                                                                         offline_valid_score,
                                                                         offline_test_score))
            score_file.close()
            # merge prediction results
            for index in range(len(offline_valid_balanced_indexs)):
                offline_valid_preds_all[offline_valid_balanced_indexs[index]] = offline_valid_preds[index]
#.........这里部分代码省略.........
开发者ID:liuzongquan,项目名称:kaggle-quora-question-pairs,代码行数:103,代码来源:runner.py


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