Ejemplo n.º 1
0
def structure_hmsvm_bmrm(m_data_dict=data_dict):
    from shogun.Features import RealMatrixFeatures
    from shogun.Loss import HingeLoss
    from shogun.Structure import HMSVMLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
    from shogun.Evaluation import StructuredAccuracy
    from shogun.Structure import DualLibQPBMSOSVM

    labels_array = m_data_dict['label'][0]

    idxs = numpy.nonzero(labels_array == -1)
    labels_array[idxs] = 0

    labels = HMSVMLabels(labels_array, 250, 500, 2)
    features = RealMatrixFeatures(m_data_dict['signal'].astype(float), 250,
                                  500)

    loss = HingeLoss()
    model = HMSVMModel(features, labels, SMT_TWO_STATE, 4)

    sosvm = DualLibQPBMSOSVM(model, loss, labels, 5000.0)
    sosvm.train()

    #print sosvm.get_w()

    predicted = sosvm.apply()
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, labels)
Ejemplo n.º 2
0
def structure_hmsvm_bmrm (m_data_dict=data_dict):
	from shogun.Features   import RealMatrixFeatures
	from shogun.Loss       import HingeLoss
	from shogun.Structure  import SequenceLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
	from shogun.Evaluation import StructuredAccuracy
	from shogun.Structure  import DualLibQPBMSOSVM

	labels_array = m_data_dict['label'][0]

	idxs = numpy.nonzero(labels_array == -1)
	labels_array[idxs] = 0

	labels = SequenceLabels(labels_array, 250, 500, 2)
	features = RealMatrixFeatures(m_data_dict['signal'].astype(float), 250, 500)

	loss = HingeLoss()
	model = HMSVMModel(features, labels, SMT_TWO_STATE, 4)

	sosvm = DualLibQPBMSOSVM(model, loss, labels, 5000.0)
	sosvm.train()

	#print sosvm.get_w()

	predicted = sosvm.apply()
	evaluator = StructuredAccuracy()
	acc = evaluator.evaluate(predicted, labels)
Ejemplo n.º 3
0
def structure_plif_hmsvm_bmrm(num_examples, example_length, num_features, num_noise_features):
    from shogun.Features import RealMatrixFeatures
    from shogun.Structure import TwoStateModel, DualLibQPBMSOSVM
    from shogun.Evaluation import StructuredAccuracy

    model = TwoStateModel.simulate_data(num_examples, example_length, num_features, num_noise_features)
    sosvm = DualLibQPBMSOSVM(model, model.get_labels(), 5000.0)

    sosvm.train()
    # print sosvm.get_w()

    predicted = sosvm.apply(model.get_features())
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, model.get_labels())
Ejemplo n.º 4
0
def structure_discrete_hmsvm_bmrm(m_data_dict=data_dict):
    from shogun.Features import RealMatrixFeatures
    from shogun.Structure import SequenceLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
    from shogun.Evaluation import StructuredAccuracy
    from shogun.Structure import DualLibQPBMSOSVM

    labels_array = m_data_dict["label"][0]

    idxs = numpy.nonzero(labels_array == -1)
    labels_array[idxs] = 0

    labels = SequenceLabels(labels_array, 250, 500, 2)
    features = RealMatrixFeatures(m_data_dict["signal"].astype(float), 250, 500)

    num_obs = 4  # given by the data file used
    model = HMSVMModel(features, labels, SMT_TWO_STATE, num_obs)

    sosvm = DualLibQPBMSOSVM(model, labels, 5000.0)
    sosvm.train()
    # print sosvm.get_w()

    predicted = sosvm.apply(features)
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, labels)
Ejemplo n.º 5
0
dim = 2

X, y = gen_data()

cnt = 250

X2, y2 = fill_data(cnt, np.min(X), np.max(X))

labels = MulticlassSOLabels(y)
features = RealFeatures(X.T)

model = MulticlassModel(features, labels)
loss = HingeLoss()

lambda_ = 1e1
sosvm = DualLibQPBMSOSVM(model, loss, labels, lambda_)

sosvm.set_cleanAfter(
    10
)  # number of iterations that cutting plane has to be inactive for to be removed
sosvm.set_cleanICP(True)  # enables inactive cutting plane removal feature
sosvm.set_TolRel(0.001)  # set relative tolerance
sosvm.set_verbose(True)  # enables verbosity of the solver
sosvm.set_cp_models(16)  # set number of cutting plane models
sosvm.set_solver(BMRM)  # select training algorithm
#sosvm.set_solver(PPBMRM)
#sosvm.set_solver(P3BMRM)

sosvm.train()

res = sosvm.get_result()
Ejemplo n.º 6
0
# Dimension of the data
dim = 2

X, y = gen_data()

cnt = 250

X2, y2 = fill_data(cnt, np.min(X), np.max(X))

labels = MulticlassSOLabels(y)
features = RealFeatures(X.T)

model = MulticlassModel(features, labels)

lambda_ = 1e1
sosvm = DualLibQPBMSOSVM(model, labels, lambda_)

sosvm.set_cleanAfter(10)	# number of iterations that cutting plane has to be inactive for to be removed
sosvm.set_cleanICP(True)	# enables inactive cutting plane removal feature
sosvm.set_TolRel(0.001)		# set relative tolerance
sosvm.set_verbose(True)		# enables verbosity of the solver
sosvm.set_cp_models(16)		# set number of cutting plane models
sosvm.set_solver(BMRM)		# select training algorithm
#sosvm.set_solver(PPBMRM)
#sosvm.set_solver(P3BMRM)

sosvm.train()

res = sosvm.get_result()
Fps = np.array(res.get_hist_Fp_vector())
Fds = np.array(res.get_hist_Fp_vector())
Ejemplo n.º 7
0
#!/usr/bin/env python

import numpy
import scipy

from scipy             import io
from shogun.Features   import RealMatrixFeatures
from shogun.Loss       import HingeLoss
from shogun.Structure  import HMSVMLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
from shogun.Evaluation import StructuredAccuracy
from shogun.Structure  import DualLibQPBMSOSVM

data_dict = scipy.io.loadmat('../data/hmsvm_data_large_integer.mat')
labels_array = data_dict['label'][0]
idxs = numpy.nonzero(labels_array == -1)
labels_array[idxs] = 0
labels = HMSVMLabels(labels_array, 250, 500, 2)
features = RealMatrixFeatures(data_dict['signal'].astype(float), 250, 500)
loss = HingeLoss()
model = HMSVMModel(features, labels, SMT_TWO_STATE, 4)
sosvm = DualLibQPBMSOSVM(model, loss, labels, 5000.0)
sosvm.train()
print sosvm.get_w()
predicted = sosvm.apply()
evaluator = StructuredAccuracy()
acc = evaluator.evaluate(predicted, labels)
print('Accuracy = %.4f' % acc)