示例#1
0
def mad_matrix(examples,
               clf,
               featuredataset,
               examplegenerator,
               attribute_name='car'):
    """
    run examples experiments to see how cars are declaired
    the same or different by the clf classifier.abs

    examples(int): number of trials
    clf(classifier): classifier to make same/different distinciton
    fd(featureDataset) : allows joining of chip to features
    eg(experimentGenerator): makes expermients for testing
    """

    ddg = defaultdict(int)
    ddb = defaultdict(int)

    for _ in tnrange(examples):
        cameras_test = examplegenerator.generate()
        match_id = get_match_id(cameras_test)
        goods, bads = make_good_bad(cameras_test, match_id)
        good0 = goods[0]
        good1 = goods[1]
        bad0 = bads[0]
        bad1 = bads[1]

        eval_good_bad(good0, good1, clf, featuredataset, ddg, ddb,
                      attribute_name)
        eval_good_bad(bad0, bad1, clf, featuredataset, ddb, ddg,
                      attribute_name)

    return (ddg, ddb)
示例#2
0
def test_svm(examples, clf_train, fd_test, eg_test):
    """
    score the trained SVM against test features

    examples(int): number of examples to run
    clf_train(modle): model for evaluating testing data
    fd_test(featureDataset): testing dataset
    eg_test(experimentGenerator): generated experiments from testing dataset

    out(int): score from the model
    """
    lessons_test = list()
    outcomes_test = list()

    for _ in tnrange(examples):
        cameras_test = eg_test.generate()
        match_id = get_match_id(cameras_test)
        goods, bads = make_good_bad(cameras_test, match_id)
        make_work(fd_test, lessons_test, outcomes_test, goods, 1)
        make_work(fd_test, lessons_test, outcomes_test, bads, 0)

    print('scoring')
    start = time.time()
    out = clf_train.score(lessons_test, outcomes_test)
    end = time.time()
    print('scoring took {} seconds'.format(end - start))
    return out
示例#3
0
def train_svm(examples, fd_train, eg_train):
    """
    train a support vector machine

    examples(int): number of examples to generate
    fd_train(featureDataset): where to join features to chips
    eg_train(experimentGenerator): makes experiments

    clf(SVM): scm classifier trainined on the input examples
    """
    lessons_train = list()
    outcomes_train = list()
    for _ in tnrange(examples):
        cameras_train = eg_train.generate()
        match_id = get_match_id(cameras_train)
        goods, bads = make_good_bad(cameras_train, match_id)
        make_work(fd_train, lessons_train, outcomes_train, goods, 1)
        make_work(fd_train, lessons_train, outcomes_train, bads, 0)

    clf = svm.SVC()

    print('fitting')
    start = time.time()
    clf.fit(lessons_train, outcomes_train)
    end = time.time()
    print('fitting took {} seconds'.format(end - start))
    return clf
示例#4
0
def make_examples(gen, examples):
    workitems = []

    for _ in range(examples):
        cameras = gen.generate()
        match_id = get_match_id(cameras)
        goods, bads = make_good_bad(cameras, match_id)

        makework(workitems, goods)
        makework(workitems, bads)

    print('made', len(workitems))
    return workitems
示例#5
0
def search(examples, fd_train, eg_train, iterations):
    """
    beginnnings of hyperparameter search for svm
    """
    param_grid = {'C': sp_rand()}
    lessons_train = list()
    outcomes_train = list()
    for _ in tnrange(examples):
        cameras_train = eg_train.generate()
        match_id = get_match_id(cameras_train)
        goods, bads = make_good_bad(cameras_train, match_id)
        make_work(fd_train, lessons_train, outcomes_train, goods, 1)
        make_work(fd_train, lessons_train, outcomes_train, bads, 0)
    clf = svm.SVC()
    print('searching')
    start = time.time()
    rsearch = RandomizedSearchCV(
        estimator=clf, param_distributions=param_grid, n_iter=iterations)
    rsearch.fit(lessons_train, outcomes_train)
    end = time.time()
    print('searching took {} seconds'.format(end - start))
    print(rsearch.best_score_)
    print(rsearch.best_estimator_.C)