예제 #1
0
def structure_multiclass_bmrm(fm_train_real=traindat,
                              label_train_multiclass=label_traindat):
    from modshogun import RealFeatures
    from modshogun import SOSVMHelper
    from modshogun import BMRM, PPBMRM, P3BMRM
    from modshogun import MulticlassModel, MulticlassSOLabels, DualLibQPBMSOSVM, RealNumber

    labels = MulticlassSOLabels(label_train_multiclass)
    features = RealFeatures(fm_train_real.T)

    model = MulticlassModel(features, labels)
    sosvm = DualLibQPBMSOSVM(model, labels, 1.0)

    # BMRM
    sosvm.set_solver(BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("BMRM: Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() ))
    #hp = sosvm.get_helper()
    #print hp.get_primal_values()
    #print hp.get_train_errors()

    # PPBMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(PPBMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("PPBMRM: Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() ))

    # P3BMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(P3BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1
예제 #2
0
def structure_multiclass_bmrm(fm_train_real=traindat,label_train_multiclass=label_traindat):
	from modshogun  import RealFeatures
	from modshogun  import SOSVMHelper
	from modshogun  import BMRM, PPBMRM, P3BMRM
	from modshogun	import MulticlassModel, MulticlassSOLabels, DualLibQPBMSOSVM, RealNumber

	labels = MulticlassSOLabels(label_train_multiclass)
	features = RealFeatures(fm_train_real.T)

	model = MulticlassModel(features, labels)
	sosvm = DualLibQPBMSOSVM(model, labels, 1.0)

	# BMRM
	sosvm.set_solver(BMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1

	#print("BMRM: Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() ))
	#hp = sosvm.get_helper()
	#print hp.get_primal_values()
	#print hp.get_train_errors()

	# PPBMRM
	w = np.zeros(model.get_dim())
	sosvm.set_w(w)
	sosvm.set_solver(PPBMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1

	#print("PPBMRM: Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() ))

	# P3BMRM
	w = np.zeros(model.get_dim())
	sosvm.set_w(w)
	sosvm.set_solver(P3BMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1
예제 #3
0
def structure_multiclass_bmrm(fm_train_real=traindat,
                              label_train_multiclass=label_traindat):
    from modshogun import MulticlassSOLabels, LabelsFactory
    from modshogun import RealFeatures
    from modshogun import SOSVMHelper
    try:
        from modshogun import BMRM, PPBMRM, P3BMRM, DualLibQPBMSOSVM
    except ImportError:
        print(
            "At least one of BMRM, PPBMRM, P3BMRM, DualLibQPBMSOSVM not available"
        )
        exit(0)
    from modshogun import MulticlassModel, RealNumber

    labels = MulticlassSOLabels(label_train_multiclass)
    features = RealFeatures(fm_train_real.T)

    model = MulticlassModel(features, labels)
    sosvm = DualLibQPBMSOSVM(model, labels, 1.0)

    # BMRM
    sosvm.set_solver(BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(bmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(bmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("BMRM: Correct classification rate: %0.2f" % ( 100.0*count/bmrm_out.get_num_labels() ))
    #hp = sosvm.get_helper()
    #print hp.get_primal_values()
    #print hp.get_train_errors()

    # PPBMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(PPBMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    ppbmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(ppbmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(ppbmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("PPBMRM: Correct classification rate: %0.2f" % ( 100.0*count/ppbmrm_out.get_num_labels() ))

    # P3BMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(P3BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    p3bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(p3bmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(p3bmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("P3BMRM: Correct classification rate: %0.2f" % ( 100.0*count/p3bmrm_out.get_num_labels() ))
    return bmrm_out, ppbmrm_out, p3bmrm_out