Extracting confidence measurements

It would be nice to know the confidence with which we classify unknown data. When a new datapoint is classified into a known category, we can train the SVM to compute the confidence level of this output as well.

How to do it…

  1. The full code is given in the svm_confidence.py file already provided to you. We will only discuss the core of the recipe here. Let's define some input data:
    # Measure distance from the boundary
    input_datapoints = np.array([[2, 1.5], [8, 9], [4.8, 5.2], [4, 4], [2.5, 7], [7.6, 2], [5.4, 5.9]])
  2. Let's measure the distance from the boundary:
    print "
    Distance from the boundary:"
    for i in input_datapoints:
        print i, '-->', classifier.decision_function(i)[0]
  3. You will see the following printed on your Terminal:
    How to do it…
  4. Distance from the boundary gives us some information about the datapoint, but it doesn't exactly tell us how confident the classifier is about the output tag. To do this, we need Platt scaling. This is a method that converts the distance measure into probability measure between classes. You can check out the following tutorial to learn more about Platt scaling: http://fastml.com/classifier-calibration-with-platts-scaling-and-isotonic-regression. Let's go ahead and train an SVM using Platt scaling:
    # Confidence measure
    params = {'kernel': 'rbf', 'probability': True}
    classifier = SVC(**params)

    The probability parameter tells the SVM that it should train to compute the probabilities as well.

  5. Let's train the classifier:
    classifier.fit(X_train, y_train)
  6. Let's compute the confidence measurements for these input datapoints:
    print "
    Confidence measure:"
    for i in input_datapoints:
        print i, '-->', classifier.predict_proba(i)[0]

    The predict_proba function measures the confidence value.

  7. You will see the following on your Terminal:
    How to do it…
  8. Let's see where the points are with respect to the boundary:
    utilities.plot_classifier(classifier, input_datapoints, [0]*len(input_datapoints), 'Input datapoints', 'True')
  9. If you run this, you will get the following figure:
    How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.252