def structure_discrete_hmsvm_bmrm(m_data_dict=data_dict):
    from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
    from modshogun import StructuredAccuracy, SMT_TWO_STATE
    try:
        from modshogun import DualLibQPBMSOSVM
    except ImportError:
        print("DualLibQPBMSOSVM not available")
        exit(0)

    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)
def structure_discrete_hmsvm_bmrm (m_data_dict=data_dict):
	from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
	from modshogun import StructuredAccuracy, SMT_TWO_STATE
	try:
		from modshogun import DualLibQPBMSOSVM
	except ImportError:
		print("DualLibQPBMSOSVM not available")
		exit(0)

	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)
Beispiel #3
0
def structure_plif_hmsvm_bmrm (num_examples, example_length, num_features, num_noise_features):
	from modshogun import RealMatrixFeatures, TwoStateModel, DualLibQPBMSOSVM, 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 modshogun import RealMatrixFeatures, TwoStateModel, StructuredAccuracy

	try:
		from modshogun 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_plif_hmsvm_bmrm (num_examples, example_length, num_features, num_noise_features):
	from modshogun import RealMatrixFeatures, TwoStateModel, StructuredAccuracy
	try:
		from modshogun import DualLibQPBMSOSVM
	except ImportError:
		print("DualLibQPBMSOSVM not available")
		exit(0)

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

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

	predicted = sosvm.apply(model.get_features())
	evaluator = StructuredAccuracy()
	acc = evaluator.evaluate(predicted, model.get_labels())
Beispiel #6
0
def structure_plif_hmsvm_mosek(num_examples, example_length, num_features,
                               num_noise_features):
    from modshogun import RealMatrixFeatures, TwoStateModel, StructuredAccuracy

    try:
        from modshogun 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_hierarchical_multilabel_classification(train_file_name,
                                                     test_file_name):
    train_file = open(train_file_name)
    test_file = open(test_file_name)

    train_features, train_labels, train_taxonomy = get_features_labels(
        train_file)

    model = HierarchicalMultilabelModel(train_features, train_labels,
                                        train_taxonomy)
    sgd = StochasticSOSVM(model, train_labels)
    t1 = time.time()
    sgd.train()
    print('>>> Took %f time for training' % (time.time() - t1))

    test_features, test_labels, test_taxonomy = get_features_labels(test_file)
    assert(test_taxonomy.all() == train_taxonomy.all())

    evaluator = StructuredAccuracy()
    outlabel = LabelsFactory.to_structured(sgd.apply(test_features))

    print('>>> Accuracy of classification = %f' % evaluator.evaluate(
        outlabel, test_labels))
Beispiel #8
0
	### prepare training, test data and evaluator

	train_data_file = 'hmsvm_%d_distort_data_fold' % distort
	train_num_examples_fold = 20
	train_num_folds = 5
	train_labels, train_features = utils.unfold_data(train_data_file)

	test_data_file = 'hmsvm_%d_distort_data_test' % distort
	test_num_examples = 100
	test_labels, test_features = utils.read_mat_file(test_data_file, test_num_examples)

	### train ML-HMM and evaluate in training data

	model = HMSVMModel(train_features, train_labels, SMT_TWO_STATE)
	model.set_use_plifs(True)
	mlhmm = MLHMM(model)
	mlhmm.train()

	prediction = mlhmm.apply()
	accuracy = evaluator.evaluate(prediction, train_labels)
	print '\ttraining accuracy:\t' + str(accuracy*100) + '%'
	utils.print_statistics(train_labels, prediction)

	### evaluate in test data

	prediction = mlhmm.apply(test_features)
	accuracy = evaluator.evaluate(prediction, test_labels)
	print '\ttest accuracy:\t\t' + str(accuracy*100) + '%'
	utils.print_statistics(test_labels, prediction)
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")
Beispiel #10
0
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')
Beispiel #11
0
evaluator = StructuredAccuracy()

### train ML-HMM and evaluate in training data

print 'training ML-HMM'
model = HMSVMModel(train_features, train_labels, SMT_TWO_STATE)
model.set_use_plifs(True)
mlhmm = MLHMM(model)
mlhmm.train()
'''
print '\n\tmodel parameters:'
print '\t- transition scores: ' + str(numpy.exp(mlhmm.transition_scores))
print '\t- feature scores:'
for s,f in product(xrange(mlhmm.num_free_states), xrange(mlhmm.num_features)):
	print '\t\tstate %d feature %d:\n%s' % (s, f, str(numpy.exp(mlhmm.feature_scores[f,s,:])))
'''

prediction = mlhmm.apply()
accuracy = evaluator.evaluate(prediction, train_labels)
print '\n\ttraining accuracy: ' + str(accuracy * 100) + '%'
utils.print_statistics(train_labels, prediction)

### evaluate in test data

print 'testing ML-HMM'
prediction = mlhmm.apply(test_features)
accuracy = evaluator.evaluate(prediction, test_labels)
print '\ttest accuracy: ' + str(accuracy * 100) + '%'
utils.print_statistics(test_labels, prediction)