names = ('ridge regression', 'IRLS', 'gradient descent', 'conjugate gradient', 'BGFS') # This threshold node will be appended to the trained classifier node, to transform the probabilities output by the logistic regression to the set {0, 1} threshold_node = ThresholdNode(threshold=.5, output_values=[0, 1]) # Loop over the different training algorithms and train and test them for node, name in zip(nodes, names): # Construct the flow flow = mdp.Flow([node, threshold_node]) # Train the regression_node flow.train([[[xtrain, ytrain]], []]) # Apply to the test set y = flow.execute(xtest) # Compute the discriminant line y1 = (node.w[0] * r[0] + node.b - 0.5) / -node.w[1] y2 = (node.w[0] * r[1] + node.b - 0.5) / -node.w[1] # Plot the discriminant line pylab.plot(r, [y1, y2], label=name) # Calculate the error print "error rate %s: %.3f" % (name, loss_01(y, ytest)) pylab.legend() pylab.show()
threshold_node = ThresholdNode(threshold=.5, output_values=[0, 1]) # Loop over the different training algorithms and train and test them for node, name in zip(nodes, names): # Construct the flow flow = mdp.Flow([node, threshold_node]) # Train the regression_node flow.train([[[xtrain, ytrain]], []]) # Apply to the test set y = flow.execute(xtest) # Compute the discriminant line if isinstance(node, RidgeRegressionNode): y1 = (node.beta[1] * r[0] + node.beta[0] - .5) / -node.beta[2] y2 = (node.beta[1] * r[1] + node.beta[0] - .5) / -node.beta[2] else: y1 = (node.w[0] * r[0] + node.b) / -node.w[1] y2 = (node.w[0] * r[1] + node.b) / -node.w[1] # Plot the discriminant line pylab.plot(r, [y1, y2], label=name) # Calculate the error print "error rate %s: %.3f" % (name, loss_01(y, ytest)) pylab.legend() pylab.show()
def test_loss_01(): ''' Test zero one loss on simple case ''' assert utils.loss_01(np.array([[1], [2], [3]]), np.array([[1], [2], [4]])) == 1. / 3
def test_loss_01(): ''' Test zero one loss on simple case ''' assert utils.loss_01(np.array([[1, 2, 3]]).T, np.array([[1, 2, 4]]).T) == 1. / 3