def noise_learning_test(): import numpy from mclp import LPBoostMulticlassClassifier # do all of the setup implicitly this time c = LPBoostMulticlassClassifier(3, 0.1) # ok to use numpy arrays c.add_multiclass_classifier(numpy.eye(3)) # junk / noise c.add_multiclass_classifier([[1.0, 0.4, 0.2], [0.4, 0.4, 0.4], [0.6, 0.1, 0.2]]) # junk / noise c.add_multiclass_classifier([[0.3, 0.3, 0.3], [0.4, 0.4, 0.4], [0.5, 0.5, 0.5]]) c.update() assert(c.weights[0][0] - 1.0 < 1e-8) assert(c.rho - 1.0 < 1e-8)
def simple_learning_test(): import numpy from mclp import * c = LPBoostMulticlassClassifier(3, 0.1) c.initialize_boosting([0,1,2], False, "clp") c.add_multiclass_classifier([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) c.update() assert(c.weights[0][0] - 1.0 < 1e-8) assert(c.rho - 1.0 < 1e-8)
fmean = train_features.mean(1) train_features -= fmean[:, None] test_features -= fmean[:, None] fstd = train_features.std(1) train_features /= fstd[:, None] test_features /= fstd[:, None] from mclp import LPBoostMulticlassClassifier lp = LPBoostMulticlassClassifier(2, 0.1) # lp.initialize_boosting([0,1], False, "clp") for tf in train_features: lp.add_multiclass_classifier([tf - 1, tf]) lp.update() print "rho:", lp.rho train_predictions = sp.zeros(train_labels.shape) for i in xrange(len(train_features)): # print i, lp.weights[0][i] train_predictions += lp.weights[0][i] * train_features[i] print "train perf=", 100.0 * (sp.sign(train_predictions) == train_labels).sum() / train_labels.size test_predictions = sp.zeros(test_labels.shape) for i in xrange(len(test_features)): # print i, lp.weights[0][i] test_predictions += lp.weights[0][i] * test_features[i] print "test perf=", 100.0 * (sp.sign(test_predictions) == test_labels).sum() / test_labels.size