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


Python learners.FrankWolfeSSVM类代码示例

本文整理汇总了Python中pystruct.learners.FrankWolfeSSVM的典型用法代码示例。如果您正苦于以下问题:Python FrankWolfeSSVM类的具体用法?Python FrankWolfeSSVM怎么用?Python FrankWolfeSSVM使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: CRFTrainer

class CRFTrainer(object):
    def __init__(self, c_value, classifier_name='ChainCRF'):
        self.c_value = c_value
        self.classifier_name = classifier_name

        if self.classifier_name == 'ChainCRF':
            model = ChainCRF()
            self.clf = FrankWolfeSSVM(model=model, C=self.c_value, max_iter=50) 
        else:
            raise TypeError('Invalid classifier type')

    def load_data(self):
        letters = load_letters()
        X, y, folds = letters['data'], letters['labels'], letters['folds']
        X, y = np.array(X), np.array(y)
        return X, y, folds

    # X is a numpy array of samples where each sample
    # has the shape (n_letters, n_features) 
    def train(self, X_train, y_train):
        self.clf.fit(X_train, y_train)

    def evaluate(self, X_test, y_test):
        return self.clf.score(X_test, y_test)

    # Run the classifier on input data
    def classify(self, input_data):
        return self.clf.predict(input_data)[0]
开发者ID:Eyobtaf,项目名称:Python-Machine-Learning-Cookbook,代码行数:28,代码来源:crf.py

示例2: test_multinomial_blocks_frankwolfe_batch

def test_multinomial_blocks_frankwolfe_batch():
    X, Y = generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0)
    crf = GridCRF(inference_method='qpbo')
    clf = FrankWolfeSSVM(model=crf, C=1, max_iter=500, batch_mode=True)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
开发者ID:pystruct,项目名称:pystruct,代码行数:7,代码来源:test_frankwolfe_svm.py

示例3: CRFTrainer

class CRFTrainer(object):

    def __init__(self, c_value, classifier_name='ChainCRF'):
        self.c_value = c_value
        self.classifier_name = classifier_name

        if self.classifier_name == 'ChainCRF':
            model = ChainCRF()
            self.clf = FrankWolfeSSVM(model=model, C=self.c_value, max_iter=50)
        else:
            raise TypeError('Invalid classifier type')

    def load_data(self):
        letters = load_letters()

        X, y, folds = letters['data'], letters['labels'], letters['folds']
        X, y = np.array(X), np.array(y)
        return X, y, folds

    # X是一个由样本组成的numpy数组,每个样本为(字母,数值)
    def train(self, X_train, y_train):
        self.clf.fit(X_train, y_train)

    def evaluate(self, X_test, y_test):
        return self.clf.score(X_test, y_test)

    # 对输入数据运行分类器
    def classify(self, input_data):
        return self.clf.predict(input_data)[0]
开发者ID:Tian-Yu,项目名称:Python-box,代码行数:29,代码来源:crf.py

示例4: n_cross_valid_crf

def n_cross_valid_crf(X, Y, K, command):
    # cross validation for crf

    if command == 'write_results':
        list_write = list()

    cv = KFold(len(X), K, shuffle=True, random_state=0)
    for traincv, testcv in cv:
        x_train, x_test = X[traincv], X[testcv]
        y_train, y_test = Y[traincv], Y[testcv]

        crf = ChainCRF(inference_method='max-product', directed=False, class_weight=None)
        ssvm = FrankWolfeSSVM(model=crf, C=1.0, max_iter=100)
        ssvm.fit(x_train, y_train)
        y_pred = ssvm.predict(x_test)

        print 'Accuracy of linear-crf %f:' % ssvm.score(x_test, y_test)
        if command == 'metrics_F1':
            metrics_crf(y_test, y_pred)
        elif command == 'confusion_matrix':
            confusion_matrix_CRF(y_test, y_pred)
        elif command == 'write_results':
            list_write += write_results_CRF(testcv, y_test, y_pred)

        print '------------------------------------------------------'
        print '------------------------------------------------------'

    if command == 'write_results':
        list_write = sorted(list_write, key=itemgetter(0))  # sorted list based on index
        for value in list_write:
            pred_list = value[1]
            test_list = value[2]

            for i in range(0, len(pred_list)):
                print str(pred_list[i]) + '\t' + str(test_list[i])
开发者ID:hvdthong,项目名称:Transportation_NEC,代码行数:35,代码来源:feature_crf.py

示例5: train_SSVM

def train_SSVM(X_train, y_train):

    #print X_train.shape, X_train[0].shape

    # splitting the 8 sub-arrays into further:
    #X_train = np.concatenate([np.array_split(x, 100) for x in X_train])
    #y_train = np.concatenate([np.array_split(y, 100) for y in y_train])

    #X_test = np.concatenate([np.array_split(x, 30) for x in X_test])
    #y_test = np.concatenate([np.array_split(y, 30) for y in y_test])

    #print X_train.shape
    #print X_train[0].shape
    #print y_train[0].shape
    #exit()
    #Train using linear chain CRF
    #https://groups.google.com/forum/#!topic/pystruct/KIkF7fzCyDI

    model = ChainCRF()
    #ssvm = NSlackSSVM(model=model, C=.1, max_iter=11) # almost similar to FrankWolfeSSVM
    ssvm = FrankWolfeSSVM(model=model, C=0.001, max_iter=11)
    # c=0.2 -> 62.86 % accuracy <==> c=0.1

    #ssvm = OneSlackSSVM(model=model) #doesn't work as well
    ssvm.fit(X_train, y_train)
    print "Learning complete..."

    return ssvm
开发者ID:ppegusii,项目名称:cs689-final,代码行数:28,代码来源:ssvm.py

示例6: test_multinomial_blocks_frankwolfe

def test_multinomial_blocks_frankwolfe():
    X, Y = generate_blocks_multinomial(n_samples=50, noise=0.5,
                                       seed=0)
    crf = GridCRF(inference_method='qpbo')
    clf = FrankWolfeSSVM(model=crf, C=1, line_search=True,
                         batch_mode=False, check_dual_every=500)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    assert_array_equal(Y, Y_pred)
开发者ID:DerThorsten,项目名称:pystruct,代码行数:9,代码来源:test_frankwolfe_svm.py

示例7: main

def main():
    parser = argparse.ArgumentParser(
        description="learn to segment and tokenize (really, any labeling)",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "--untokfile", "-u", nargs="?", type=argparse.FileType("r"), default=sys.stdin, help="untok file"
    )
    parser.add_argument(
        "--biofile",
        "-b",
        nargs="?",
        type=argparse.FileType("r"),
        default=sys.stdin,
        help="bio file. must match untok file and be space separated",
    )
    parser.add_argument("--outfile", "-o", nargs="?", type=argparse.FileType("wb"), default=None, help="output file")
    parser.add_argument("--debug", "-d", action="store_true", default=False, help="debug mode")

    try:
        args = parser.parse_args()
    except IOError as msg:
        parser.error(str(msg))

    untokfile = prepfile(args.untokfile, "r")
    biofile = prepfile(args.biofile, "r")

    data, labels, datamap, labelmap = prepdata(untokfile, biofile, args.debug)

    #  print(data)
    #  print(labels)
    model = ChainCRF()
    # ssvm = SubgradientSSVM(model=model, C=.1)#, show_loss_every=5)
    ssvm = FrankWolfeSSVM(model=model, max_iter=100, C=0.1)  # , show_loss_every=5)
    ssvm.fit(data, labels)
    #  curve = ssvm.loss_curve_
    # TONT
    # print("TONT score with chain CRF: %f" % ssvm.score(data, labels))

    ret = {}
    ret["model"] = ssvm
    ret["feats"] = datamap
    ret["labels"] = labelmap
    if args.outfile is not None:
        pickle.dump(ret, args.outfile)
开发者ID:isi-nlp,项目名称:unitok,代码行数:45,代码来源:learntok.py

示例8: test_svm_as_crf_pickling_batch

def test_svm_as_crf_pickling_batch():

    iris = load_iris()
    X, y = iris.data, iris.target

    X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X]
    Y = y.reshape(-1, 1)

    X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1)
    _, file_name = mkstemp()

    pbl = GraphCRF(n_features=4, n_states=3, inference_method='unary')
    logger = SaveLogger(file_name)
    svm = FrankWolfeSSVM(pbl, C=10, logger=logger, max_iter=50, batch_mode=False)
    svm.fit(X_train, y_train)

    assert_less(.97, svm.score(X_test, y_test))
    assert_less(.97, logger.load().score(X_test, y_test))
开发者ID:pystruct,项目名称:pystruct,代码行数:18,代码来源:test_frankwolfe_svm.py

示例9: __init__

    def __init__(self, c_value, classifier_name='ChainCRF'):
        self.c_value = c_value
        self.classifier_name = classifier_name

        if self.classifier_name == 'ChainCRF':
            model = ChainCRF()
            self.clf = FrankWolfeSSVM(model=model, C=self.c_value, max_iter=50)
        else:
            raise TypeError('Invalid classifier type')
开发者ID:Tian-Yu,项目名称:Python-box,代码行数:9,代码来源:crf.py

示例10: n_cross_valid_crf_candidate

def n_cross_valid_crf_candidate(list_line, X, Y, K):
    list_text = []
    for i in range(0, len(list_line), 3):
        split_first = 0
        split_second = 0

        if i % 3 == 0:
            split_first = list_line[i].strip().split('\t')
        list_text.append(split_first)

    list_text = np.array(list_text)

    cv = KFold(len(X), K, shuffle=True, random_state=0)
    list_write = []
    for traincv, testcv in cv:
        x_train, x_test = X[traincv], X[testcv]
        y_train, y_test = Y[traincv], Y[testcv]
        list_text_train, list_text_test = list_text[traincv], list_text[testcv]

        crf = ChainCRF(inference_method='max-product', directed=False, class_weight=None)
        ssvm = FrankWolfeSSVM(model=crf, C=1.0, max_iter=10)
        ssvm.fit(x_train, y_train)
        y_pred = ssvm.predict(x_test)
        list_wrong = metrics_crf_candidate(list_text_test, y_test, y_pred)
        if len(list_write) == 0:
            list_write = list_wrong
        else:
            for i in range(0, len(list_wrong)):
                svc = list_wrong[0]
                road = list_wrong[1]
                busstop = list_wrong[2]

                list_write[0] = list_write[0] + svc
                list_write[1] = list_write[1] + road
                list_write[2] = list_write[2] + busstop

    # write_file('d:/', 'wrong_svc', list_write[0])
    # write_file('d:/', 'wrong_road', list_write[1])
    # write_file('d:/', 'wrong_busstop', list_write[2])

    write_file('d:/', 'good_svc', list_write[0])
    write_file('d:/', 'good_road', list_write[1])
    write_file('d:/', 'good_busstop', list_write[2])
开发者ID:hvdthong,项目名称:Transportation_NEC,代码行数:43,代码来源:feature_crf.py

示例11: results_CRFs

def results_CRFs(X_training, Y_training, X_testing, Y_testing, command):
    crf = ChainCRF(inference_method='max-product', directed=False, class_weight=None)
    ssvm = FrankWolfeSSVM(model=crf, C=1.0, max_iter=100)
    ssvm.fit(X_training, Y_training)
    y_pred = ssvm.predict(X_testing)

    list_write = list()
    print 'Accuracy of linear-crf %f:' % ssvm.score(X_testing, Y_testing)
    if command == 'metrics_F1':
        metrics_crf(Y_testing, y_pred)
    elif command == 'confusion_matrix':
        confusion_matrix_CRF(Y_testing, y_pred)
    elif command == 'write_results':
        list_write = write_CRFs_compare(Y_testing, y_pred)
        for value in list_write:
            pred_list = value[0]
            test_list = value[1]

            for i in range(0, len(pred_list)):
                print str(pred_list[i]) + '\t' + str(test_list[i])
开发者ID:hvdthong,项目名称:Transportation_NEC,代码行数:20,代码来源:CRF_model_cmp.py

示例12: chaincrf_test

def chaincrf_test():
	num_pics = 3000
	X, Y= load_pictures(num_pics)
	X = np.array(X)
	Y = np.array(Y)

	print X.shape
	print Y.shape

	# 0: pixel, 1: row, 2: picture
	mode = 0
	outstr = "Test score with data arranged by "

	if mode == 0:
		X, Y = arrange_by_pixel(X, Y)
		outstr += "pixel:"
	elif mode == 1:
		X, Y = arrange_by_row(X, Y)
		outstr += "row:"
	elif mode == 2:
		X, Y = arrange_by_picture(X, Y)
		outstr += "picture:"

	print X.shape
	print Y.shape

	#print X.shape, Y.shape
	train_pct = 0.66
	test_pct = 1 - train_pct
	X_train = X[0:math.floor(train_pct * num_pics)]
	X_test = X[math.floor(test_pct*num_pics):]
	Y_train = Y[0:math.floor(train_pct * num_pics)]
	Y_test = Y[math.floor(test_pct*num_pics):]

	model = ChainCRF()
	ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10)
	# #print X_train.shape, Y_train.shape
	ssvm.fit(X_train, Y_train)
	results = ssvm.score(X_test, Y_test)
	print outstr
	print results
开发者ID:nikhilch,项目名称:eecs349-final-project,代码行数:41,代码来源:tiftest.py

示例13: __init__

 def __init__(self, do_train=False, trained_model_name="passage_crf_model", algorithm="crf"):
   self.trained_model_name = trained_model_name
   self.fp = FeatureProcessing()
   self.do_train = do_train
   self.algorithm = algorithm
   if algorithm == "crf":
     if do_train:
       self.trainer = Trainer()
     else:
       self.tagger = Tagger()
   else:
     if do_train:
       model = ChainCRF()
       self.trainer = FrankWolfeSSVM(model=model)
       self.feat_index = {}
       self.label_index = {}
     else:
       self.tagger = pickle.load(open(self.trained_model_name, "rb"))
       self.feat_index = pickle.load(open("ssvm_feat_index.pkl", "rb"))
       label_index = pickle.load(open("ssvm_label_index.pkl", "rb"))
       self.rev_label_index = {i: x for x, i in label_index.items()}
开发者ID:BMKEG,项目名称:exp-parser,代码行数:21,代码来源:passage_tagger.py

示例14: build_models

def build_models(X_train, y_train):
    '''
    PURPOSE:    ouput model objects which have been fitted with training data
    INPUT:      X_train (np.array) - features matrix
                y_train (np.array) - label matrix
    OUTPUT:     nmb (MultinomialNB obj) - model trained on X_train, y_train
                svm (LinearSVC obj) - model trained on X_train, y_train
                ssvm (PyStruct chainCRF object) - trained Chain CRF model
    '''
    # Multinomial Naive Bayes Classifier:
    nmb = MultinomialNB()
    nmb.fit(np.vstack(X_train), np.hstack(y_train))

    # Support Vector Machine Classifier
    svm = LinearSVC(dual=False, C=.1)
    svm.fit(np.vstack(X_train), np.hstack(y_train))

    # Chain Conditional Random Field Classifier
    model = ChainCRF()
    ssvm = FrankWolfeSSVM(model=model, C=0.5, max_iter=15)
    ssvm.fit(X_train, y_train)
    return nmb, svm, ssvm
开发者ID:han928,项目名称:PinkSlipper,代码行数:22,代码来源:build_model.py

示例15: CRF_pred_label

def CRF_pred_label(X, Y, command):
    texts = load_demo_text(command)
    if command == 'twitter':
        convert_texts = filterText_demo(texts, 'removeLink', command)
        X_ftr = load_demo_ftr(command)
        print len(convert_texts), len(X_ftr)
        path_write = 'D:/Project/Transportation_SMU-NEC_collaboration/Data_demo_Dec_2015/twitter'
        name_write = 'pred_label_' + command

    elif command == 'sgforums':
        convert_texts = filterText_demo(texts, 'removePunc', command)
        X_ftr = load_demo_ftr(command)
        print len(convert_texts), len(X_ftr)
        path_write = 'D:/Project/Transportation_SMU-NEC_collaboration/Data_demo_Dec_2015/sgforums'
        name_write = 'pred_label_' + command

    elif command == 'facebook':
        convert_texts = filterText_demo(texts, 'removeLink', command)
        X_ftr = load_demo_ftr(command)
        print len(convert_texts), len(X_ftr)
        path_write = 'D:/Project/Transportation_SMU-NEC_collaboration/Data_demo_Dec_2015/facebook'
        name_write = 'pred_label_' + command

    crf = ChainCRF(inference_method='max-product', directed=False, class_weight=None)
    ssvm = FrankWolfeSSVM(model=crf, C=1.0, max_iter=100)
    ssvm.fit(X, Y)
    y_pred = ssvm.predict(X_ftr)

    list_write = list()
    for line in y_pred:
        labels = ''
        for label in line:
            labels += str(label) + '\t'
        list_write.append(labels.strip())

    write_file(path_write, name_write, list_write)
开发者ID:hvdthong,项目名称:Transportation_NEC,代码行数:36,代码来源:CRF_model_pred.py


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