本文整理汇总了Python中pystruct.learners.FrankWolfeSSVM.score方法的典型用法代码示例。如果您正苦于以下问题:Python FrankWolfeSSVM.score方法的具体用法?Python FrankWolfeSSVM.score怎么用?Python FrankWolfeSSVM.score使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pystruct.learners.FrankWolfeSSVM
的用法示例。
在下文中一共展示了FrankWolfeSSVM.score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CRFTrainer
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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]
示例2: n_cross_valid_crf
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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])
示例3: CRFTrainer
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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]
示例4: test_svm_as_crf_pickling_batch
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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))
示例5: results_CRFs
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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])
示例6: chaincrf_test
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
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
示例7: LinearRegression
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
chain_ssvm.fit(nn_predictions_train, y_train)
# # Create linear regression object
# regr = LinearRegression()
# # Train the model using the training sets
# regr.fit(np.vstack(nn_predictions_train), np.hstack(y_train))
# print("Test score with linear regression: %f" % regr.score(np.vstack(nn_predictions_test),
# np.hstack(y_test)))
print("Test score with linear NN: 84.15%")
print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test),
np.hstack(y_test)))
print("Test score with CRF: %f" % ssvm.score(nn_predictions_test, y_test))
print("Test score with Linear Chain CRF: %f" % chain_ssvm.score(nn_predictions_test, y_test))
# # plot some word sequenced
# n_words = 4
# rnd = np.random.RandomState(1)
# selected = rnd.randint(len(y_test), size=n_words)
# max_word_len = max([len(y_) for y_ in y_test[selected]])
# fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10))
# fig.subplots_adjust(wspace=0)
# fig.text(0.2, 0.05, 'GT', color="#00AA00", size=25)
# fig.text(0.4, 0.05, 'SVM', color="#5555FF", size=25)
# fig.text(0.6, 0.05, 'LCCRF', color="#FF5555", size=25)
# fig.text(0.8, 0.05, 'CRF', color="#FFD700", size=25)
示例8: print
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
# print("Shuffle results")
# features, labels = util.shuffle(features, labels)
trsize = int(0.7*len(labels))
X_train = features[1:trsize]
y_train = labels[1:trsize]
X_test = features[trsize+1:]
y_test = labels[trsize+1:]
# X_train = X_test = features
# y_train = y_test = labels
# trsize = len(labels)
# Evaluate the chain
model = ChainCRF()
C=0.0001
max_iter=50
ssvm = FrankWolfeSSVM(model=model, C=C, max_iter=max_iter, verbose=True)
print(ssvm)
print(ssvm.fit(X_train, y_train))
print(ssvm.w)
trscore = ssvm.score(X_train,y_train)
# testscore = ssvm.score(X_test,y_test)
print("Training score: {0}".format(trscore))
# print("Test score: {0}".format(testscore))
# Save the result
# util.saveToSQL(featureset, C, max_iter, trsize, trscore, 2)
示例9: LinearSVC
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
# convenient
X, y = np.array(X), np.array(y)
X_train, X_test = X[folds == 1], X[folds != 1]
y_train, y_test = y[folds == 1], y[folds != 1]
# Train linear SVM
svm = LinearSVC(dual=False, C=0.1)
# flatten input
svm.fit(np.vstack(X_train), np.hstack(y_train))
# Train linear chain CRF
model = ChainCRF()
ssvm = FrankWolfeSSVM(model=model, C=0.1, max_iter=11)
ssvm.fit(X_train, y_train)
print("Test score with chain CRF: %f" % ssvm.score(X_test, y_test))
print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test), np.hstack(y_test)))
# plot some word sequenced
n_words = 4
rnd = np.random.RandomState(1)
selected = rnd.randint(len(y_test), size=n_words)
max_word_len = max([len(y_) for y_ in y_test[selected]])
fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10))
fig.subplots_adjust(wspace=0)
for ind, axes_row in zip(selected, axes):
y_pred_svm = svm.predict(X_test[ind])
y_pred_chain = ssvm.predict([X_test[ind]])[0]
for i, (a, image, y_true, y_svm, y_chain) in enumerate(
zip(axes_row, X_test[ind], y_test[ind], y_pred_svm, y_pred_chain)
示例10: range
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
X = X[:100]
y = y[:100]
#Add edges
for i in range(X.shape[0]):
X[i] = [X[i], np.vstack([(0,1),(2,2)])]
model = GraphCRF(directed=True, inference_method="max-product")
X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(X,y, test_size =0.5, random_state=0)
ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10)
ssvm.fit(X_train,y_train)
print ssvm.score(X_test, y_test)
print ssvm.predict(X_test)
print y_test
'''
for i in range(X.shape[0]):
X_train, X_test = X[]
X_test = X[i]
y_test = y[i]
X_train = np.delete(X,i)
y_train = np.delete(y,i)
ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10)
ssvm.fit(X_train,y_train)
示例11: run_crf
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
def run_crf(w2v, words_before, words_after, shallow_parse):
pmids_dict, pmids, abstracts, lbls, vectorizer, groups_map, one_hot, dicts = \
parse_summerscales.get_tokens_and_lbls(
make_pmids_dict=True, sen=True)
"""
Create model
"""
model = ChainCRF(directed=False)
ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=30)
all_pmids = pmids_dict.keys()
n = len(all_pmids)
n_folds = 5
kf = KFold(n, random_state=1337, shuffle=True, n_folds=n_folds)
fold_gi = []
for fold_idx, (train, test) in enumerate(kf):
print("on fold %s" % fold_idx)
train_pmids = [all_pmids[pmid_idx] for pmid_idx in train]
test_pmids = [all_pmids[pmid_idx] for pmid_idx in test]
print('loading data...')
train_x, train_y = abstract2features(pmids_dict, words_before, w2v, shallow_parse)
test_x, test_y = abstract2features(pmids_dict, words_after, w2v, shallow_parse)
print('loaded data...')
print 'training...'
ssvm.fit(train_x, train_y)
print ssvm.score(test_x, test_y)
for i, (pmid, x, y) in enumerate(zip(test_pmids, test_x, test_y)):
abstract_words, _, _= pmids_dict[pmid]
print(pmid)
# predict() takes in a list returns another list
prediction = ssvm.predict([x]).pop(0)
predicted = ''
output = ''
if len(prediction) > 0:
for p in prediction:
if p == 1:
print "word: {}".format(abstract_words[p])
if n == 0:
predicted += abstract_words[p]
else:
predicted += ' ' + abstract_words[p]
if not predicted == '':
output = 'predicted: {}'.format(predicted)
else:
output = 'Predicted nothing!'
else:
output = 'Predicted nothing!'
print output
示例12: ChainCRF
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
# Train CRF
model = ChainCRF(directed=True)
ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=11)
ssvm.fit(np.vstack(X_train).reshape((5375, 1, 128)), np.hstack(y_train).reshape(5375, 1))
# Train linear chain CRF
chain_model = ChainCRF(directed=True)
chain_ssvm = FrankWolfeSSVM(model=chain_model, C=.1, max_iter=11, verbose=0)
chain_ssvm.fit(X_train, y_train)
print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test),
np.hstack(y_test)))
print("Test score with CRF: %f" % ssvm.score(X_test, y_test))
print("Test score with Linear Chain CRF: %f" % chain_ssvm.score(X_test, y_test))
# plot some word sequenced
n_words = 4
rnd = np.random.RandomState(1)
selected = rnd.randint(len(y_test), size=n_words)
max_word_len = max([len(y_) for y_ in y_test[selected]])
fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10))
fig.subplots_adjust(wspace=0)
fig.text(0.2, 0.05, 'GT', color="#00AA00", size=25)
fig.text(0.4, 0.05, 'SVM', color="#5555FF", size=25)
fig.text(0.6, 0.05, 'LCCRF', color="#FF5555", size=25)
fig.text(0.8, 0.05, 'CRF', color="#FFD700", size=25)
示例13: range
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
y_half_train = np.ones_like(X_half_train)
for ind in range(0, X_train.shape[0]):
# n_letters = 2 #fixed len of word
n_letters = int(np.floor(X_train[ind].shape[0] / 2))
X_half_train[2*ind] = X_train[ind][0:n_letters]
X_half_train[2*ind+1] = X_train[ind][n_letters:]
y_half_train[2*ind] = y_train[ind][0:n_letters]
y_half_train[2*ind+1] = y_train[ind][n_letters:]
# Train the model
half_ssvm.fit(X_half_train, y_half_train)
print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test),
np.hstack(y_test)))
print("Test score with FULL LCCRF: %f" % ssvm.score(X_test, y_test))
print("Test score with HALF LCCRF: %f" % half_ssvm.score(X_test, y_test))
# plot some word sequenced
n_words = 4
rnd = np.random.RandomState(1)
selected = rnd.randint(len(y_test), size=n_words)
max_word_len = max([len(y_) for y_ in y_test[selected]])
fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10))
fig.subplots_adjust(wspace=0)
fig.text(0.2, 0.05, 'GT', color="#00AA00", size=25)
fig.text(0.4, 0.05, 'SVM', color="#5555FF", size=25)
fig.text(0.6, 0.05, 'HALF-LCCRF', color="#FF5555", size=25)
fig.text(0.8, 0.05, 'FULL-LCCRF', color="#FFD700", size=25)
示例14: ChainCRF
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
# Train directed chain CRF
model = ChainCRF(directed=True)
ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=11)
ssvm.fit(X_train, y_train)
# Train undirected chain CRF
undirected_model = ChainCRF(directed=False)
undirected_ssvm = FrankWolfeSSVM(model=undirected_model, C=.1, max_iter=11, verbose=0)
undirected_ssvm.fit(X_train, y_train)
print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test),
np.hstack(y_test)))
print("Test score with directed LCCRF: %f" % ssvm.score(X_test, y_test))
print("Test score with undirected LCCRF: %f" % undirected_ssvm.score(X_test, y_test))
# plot some word sequenced
n_words = 4
rnd = np.random.RandomState(1)
selected = rnd.randint(len(y_test), size=n_words)
max_word_len = max([len(y_) for y_ in y_test[selected]])
fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10))
fig.subplots_adjust(wspace=0)
fig.text(0.2, 0.05, 'GT', color="#00AA00", size=25)
fig.text(0.4, 0.05, 'SVM', color="#5555FF", size=25)
fig.text(0.6, 0.05, 'UD-LCCRF', color="#FF5555", size=25)
fig.text(0.8, 0.05, 'D-LCCRF', color="#FFD700", size=25)
示例15: print
# 需要导入模块: from pystruct.learners import FrankWolfeSSVM [as 别名]
# 或者: from pystruct.learners.FrankWolfeSSVM import score [as 别名]
import loader
import util
from sklearn import preprocessing
directory = "/Users/thijs/dev/boilerplate/src/main/resources/dataset/"
featureset = "features10"
print("Load files")
features, labels = \
loader.loadBinary(featureset+'.csv', 'labels.csv', directory)
# print("Shuffle results")
# features, labels = util.shuffle(features, labels)
print("Loaded")
# print(labels)
# features = preprocessing.scale(features)
from pystruct.models import BinaryClf
from pystruct.learners import (NSlackSSVM, OneSlackSSVM,
SubgradientSSVM, FrankWolfeSSVM)
clf = FrankWolfeSSVM(BinaryClf(),verbose=True)
# print(clf)
clf.fit(features,labels)
trscore = clf.score(features,labels)
# print("Training score: {0}".format(trscore))
print("Klaar")