def structure_hmsvm_mosek(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 try: from shogun.Structure import PrimalMosekSOSVM except ImportError: print "Mosek not available" import sys sys.exit(0) 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 = PrimalMosekSOSVM(model, loss, labels) sosvm.train() print sosvm.get_w() predicted = sosvm.apply() evaluator = StructuredAccuracy() acc = evaluator.evaluate(predicted, labels) print('Accuracy = %.4f' % acc)
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) print('Accuracy = %.4f' % acc)
def structure_hmsvm_mosek (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 try: from shogun.Structure import PrimalMosekSOSVM except ImportError: print "Mosek not available" return 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 = PrimalMosekSOSVM(model, loss, labels) sosvm.train() print sosvm.get_w() predicted = sosvm.apply() evaluator = StructuredAccuracy() acc = evaluator.evaluate(predicted, labels) print('Accuracy = %.4f' % acc)
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)
def structure_discrete_hmsvm_mosek (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 try: from shogun.Structure import PrimalMosekSOSVM except ImportError: print("Mosek not available") return 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 = PrimalMosekSOSVM(model, labels) sosvm.train() #print(sosvm.get_w()) predicted = sosvm.apply() evaluator = StructuredAccuracy() acc = evaluator.evaluate(predicted, labels)
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())
def structure_plif_hmsvm_mosek (num_examples, example_length, num_features, num_noise_features): from shogun.Features import RealMatrixFeatures from shogun.Structure import TwoStateModel from shogun.Evaluation import StructuredAccuracy try: from shogun.Structure import PrimalMosekSOSVM except ImportError: print("Mosek not available") return model = TwoStateModel.simulate_data(num_examples, example_length, num_features, num_noise_features) sosvm = PrimalMosekSOSVM(model, model.get_labels()) sosvm.train() #print(sosvm.get_w()) predicted = sosvm.apply(model.get_features()) evaluator = StructuredAccuracy() acc = evaluator.evaluate(predicted, model.get_labels())
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)
wdists = np.array(res.hist_wdist) plt.figure() plt.subplot(221) plt.title('Fp and Fd history') plt.plot(xrange(res.nIter), Fps, hold=True) plt.plot(xrange(res.nIter), Fds, hold=True) plt.subplot(222) plt.title('w dist history') plt.plot(xrange(res.nIter), wdists) # Evaluation out = sosvm.apply() Evaluation = StructuredAccuracy() acc = Evaluation.evaluate(out, labels) print "Correct classification rate: %0.4f%%" % (100.0 * acc) # show figure Z = get_so_labels(sosvm.apply(RealFeatures(X2))) x = (X2[0, :]).reshape(cnt, cnt) y = (X2[1, :]).reshape(cnt, cnt) z = Z.reshape(cnt, cnt) plt.subplot(223) plt.pcolor(x, y, z, shading='interp') plt.contour(x, y, z, linewidths=1, colors='black', hold=True) plt.plot(X[:, 0], X[:, 1], 'yo') plt.axis('tight') plt.title('Classification')
wdists = np.array(res.get_hist_wdist_vector()) plt.figure() plt.subplot(221) plt.title('Fp and Fd history') plt.plot(xrange(res.get_n_iters()), Fps, hold=True) plt.plot(xrange(res.get_n_iters()), Fds, hold=True) plt.subplot(222) plt.title('w dist history') plt.plot(xrange(res.get_n_iters()), wdists) # Evaluation out = sosvm.apply() Evaluation = StructuredAccuracy() acc = Evaluation.evaluate(out, labels) print "Correct classification rate: %0.4f%%" % ( 100.0*acc ) # show figure Z = get_so_labels(sosvm.apply(RealFeatures(X2))) x = (X2[0,:]).reshape(cnt, cnt) y = (X2[1,:]).reshape(cnt, cnt) z = Z.reshape(cnt, cnt) plt.subplot(223) plt.pcolor(x, y, z, shading='interp') plt.contour(x, y, z, linewidths=1, colors='black', hold=True) plt.plot(X[:,0], X[:,1], 'yo') plt.axis('tight') plt.title('Classification')
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 try: from shogun.Structure import PrimalMosekSOSVM except ImportError: print "Mosek not available" import sys sys.exit(0) 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 = PrimalMosekSOSVM(model, loss, labels, features) sosvm.train() print sosvm.get_w() predicted = sosvm.apply() evaluator = StructuredAccuracy() acc = evaluator.evaluate(predicted, labels) print('Accuracy = %.4f' % acc)