本文整理汇总了Python中pystruct.learners.OneSlackSSVM.score方法的典型用法代码示例。如果您正苦于以下问题:Python OneSlackSSVM.score方法的具体用法?Python OneSlackSSVM.score怎么用?Python OneSlackSSVM.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pystruct.learners.OneSlackSSVM
的用法示例。
在下文中一共展示了OneSlackSSVM.score方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: syntetic
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
def syntetic():
# train model on a single set
models_basedir = 'models/syntetic/'
crf = EdgeCRF(n_states=10, n_features=10, n_edge_features=2,
inference_method='gco')
clf = OneSlackSSVM(crf, max_iter=10000, C=0.01, verbose=2,
tol=0.1, n_jobs=4, inference_cache=100)
X, Y = load_syntetic(1)
x_train, x_test, y_train, y_test = train_test_split(X, Y,
train_size=100,
random_state=179)
start = time()
clf.fit(x_train, y_train)
stop = time()
np.savetxt(models_basedir + 'syntetic_full.csv', clf.w)
with open(models_basedir + 'syntetic_full' + '.pickle', 'w') as f:
cPickle.dump(clf, f)
y_pred = clf.predict(x_test)
print 'Error on test set: %f' % compute_error(y_test, y_pred)
print 'Score on test set: %f' % clf.score(x_test, y_test)
print 'Score on train set: %f' % clf.score(x_train, y_train)
print 'Norm of weight vector: |w|=%f' % np.linalg.norm(clf.w)
print 'Elapsed time: %f s' % (stop - start)
return clf
示例2: test_constraint_removal
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
def test_constraint_removal():
digits = load_digits()
X, y = digits.data, digits.target
y = 2 * (y % 2) - 1 # even vs odd as +1 vs -1
X = X / 16.
pbl = BinaryClf(n_features=X.shape[1])
clf_no_removal = OneSlackSSVM(model=pbl, max_iter=500, C=1,
inactive_window=0, tol=0.01)
clf_no_removal.fit(X, y)
clf = OneSlackSSVM(model=pbl, max_iter=500, C=1, tol=0.01,
inactive_threshold=1e-8)
clf.fit(X, y)
# check that we learned something
assert_greater(clf.score(X, y), .92)
# results are mostly equal
# if we decrease tol, they will get more similar
assert_less(np.mean(clf.predict(X) != clf_no_removal.predict(X)), 0.02)
# without removal, have as many constraints as iterations
assert_equal(len(clf_no_removal.objective_curve_),
len(clf_no_removal.constraints_))
# with removal, there are less constraints than iterations
assert_less(len(clf.constraints_),
len(clf.objective_curve_))
示例3: msrc
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
def msrc():
models_basedir = 'models/msrc/'
crf = EdgeCRF(n_states=24, n_features=2028, n_edge_features=4,
inference_method='gco')
clf = OneSlackSSVM(crf, max_iter=10000, C=0.01, verbose=2,
tol=0.1, n_jobs=4,
inference_cache=100)
X, Y = load_msrc('train')
Y = remove_areas(Y)
start = time()
clf.fit(X, Y)
stop = time()
np.savetxt(models_basedir + 'msrc_full.csv', clf.w)
with open(models_basedir + 'msrc_full' + '.pickle', 'w') as f:
pickle.dump(clf, f)
X, Y = load_msrc('test')
Y = remove_areas(Y)
Y_pred = clf.predict(X)
print('Error on test set: %f' % compute_error(Y, Y_pred))
print('Score on test set: %f' % clf.score(X, Y))
print('Norm of weight vector: |w|=%f' % np.linalg.norm(clf.w))
print('Elapsed time: %f s' % (stop - start))
return clf
示例4: test_svm_as_crf_pickling
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
def test_svm_as_crf_pickling():
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 = OneSlackSSVM(pbl, check_constraints=True, C=1, n_jobs=1, logger=logger)
svm.fit(X_train, y_train)
assert_less(0.97, svm.score(X_test, y_test))
assert_less(0.97, logger.load().score(X_test, y_test))
示例5: test_one_slack_repellent_potentials
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
def test_one_slack_repellent_potentials():
# test non-submodular problem with and without submodularity constraint
# dataset is checkerboard
X, Y = generate_checker()
crf = GridCRF(inference_method=inference_method)
clf = OneSlackSSVM(model=crf, max_iter=10, C=0.01, check_constraints=True)
clf.fit(X, Y)
Y_pred = clf.predict(X)
# standard crf can predict perfectly
assert_array_equal(Y, Y_pred)
submodular_clf = OneSlackSSVM(
model=crf, max_iter=10, C=0.01, check_constraints=True, negativity_constraint=[4, 5, 6]
)
submodular_clf.fit(X, Y)
Y_pred = submodular_clf.predict(X)
assert_less(submodular_clf.score(X, Y), 0.99)
# submodular crf can not do better than unaries
for i, x in enumerate(X):
y_pred_unaries = crf.inference(x, np.array([1, 0, 0, 1, 0, 0, 0]))
assert_array_equal(y_pred_unaries, Y_pred[i])
示例6: load_letters
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
"""
================================
Sequence classifcation benchmark
================================
This is a stripped-down version of the "plot_letters.py" example
targetted to benchmark inference and learning algorithms on chains.
"""
import numpy as np
from pystruct.datasets import load_letters
from pystruct.models import ChainCRF
from pystruct.learners import OneSlackSSVM
abc = "abcdefghijklmnopqrstuvwxyz"
letters = load_letters()
X, y, folds = letters['data'], letters['labels'], letters['folds']
# we convert the lists to object arrays, as that makes slicing much more
# 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 chain CRF
model = ChainCRF()
ssvm = OneSlackSSVM(model=model, C=.1, tol=0.1, verbose=3, max_iter=20)
ssvm.fit(X_train, y_train)
print("Test score with chain CRF: %f" % ssvm.score(X_test, y_test))
示例7: time
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
X_train_bias = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
X_test_bias = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
# n-slack cutting plane ssvm
start = time()
n_slack_svm.fit(X_train_bias, y_train)
time_n_slack_svm = time() - start
acc_n_slack = n_slack_svm.score(X_test_bias, y_test)
print("Score with pystruct n-slack ssvm: %f (took %f seconds)"
% (acc_n_slack, time_n_slack_svm))
## 1-slack cutting plane ssvm
start = time()
one_slack_svm.fit(X_train_bias, y_train)
time_one_slack_svm = time() - start
acc_one_slack = one_slack_svm.score(X_test_bias, y_test)
print("Score with pystruct 1-slack ssvm: %f (took %f seconds)"
% (acc_one_slack, time_one_slack_svm))
# online subgradient ssvm
start = time()
subgradient_svm.fit(X_train_bias, y_train)
time_subgradient_svm = time() - start
acc_subgradient = subgradient_svm.score(X_test_bias, y_test)
print("Score with pystruct subgradient ssvm: %f (took %f seconds)"
% (acc_subgradient, time_subgradient_svm))
libsvm = SVC(kernel='linear', C=10)
start = time()
libsvm.fit(X_train, y_train)
示例8: fetch_mldata
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
from sklearn.utils import shuffle
from pystruct.problems import CrammerSingerSVMProblem
#from pystruct.learners import SubgradientStructuredSVM
#from pystruct.learners import StructuredSVM
from pystruct.learners import OneSlackSSVM
mnist = fetch_mldata("MNIST original")
X, y = mnist.data, mnist.target
X = X / 255.
X_train, y_train = X[:60000], y[:60000]
X_test, y_test = X[60000:], y[60000:]
X_train, y_train = shuffle(X_train, y_train)
pblm = CrammerSingerSVMProblem(n_classes=10, n_features=28 ** 2)
#svm = SubgradientStructuredSVM(pblm, verbose=10, n_jobs=1, plot=True,
#max_iter=10, batch=False, learning_rate=0.0001,
#momentum=0)
#svm = SubgradientStructuredSVM(pblm, verbose=10, n_jobs=1, plot=True,
#max_iter=2, batch=False, momentum=.9,
#learning_rate=0.001, show_loss='true', C=1000)
svm = OneSlackSSVM(pblm, verbose=2, n_jobs=1, plot=True, max_iter=2, C=1000)
#svm = StructuredSVM(pblm, verbose=50, n_jobs=1, plot=True, max_iter=10,
#C=1000)
svm.fit(X_train, y_train)
print(svm.score(X_train, y_train))
print(svm.score(X_test, y_test))
示例9: range
# 需要导入模块: from pystruct.learners import OneSlackSSVM [as 别名]
# 或者: from pystruct.learners.OneSlackSSVM import score [as 别名]
total_correct = 0
total = 0
precision = {0: [0, 0], 1: [0, 0], 2: [0, 0]}
recall = {0: [0, 0], 1: [0, 0], 2: [0, 0]}
for fold in range(5):
print 'fold ' + str(fold) + "--------------------------"
X = []
Y = []
for i in range(5):
if i == fold:
continue
X.extend(folds[i]['X'])
Y.extend(folds[i]['Y'])
print "Training Size: ", len(X), len(Y)
ssvm.fit(X, Y)
print 'Train Score: ' + str(ssvm.score(X, Y))
# w = Weight(ssvm.w, node_features, edge_features, dictLength)
fold_correct = 0
fold_total = 0
testX = folds[fold]['X']
testY = folds[fold]['Y']
for i in range(len(testX)):
print "Instance: " + str(i)
print folds[fold]['threads'][i].id
Yi = testY[i]
print list(Yi)
infY = crf.inference(testX[i], ssvm.w)
print list(infY)
for py in range(len(Yi)):
recall[Yi[py]][1] += 1