Building a nonlinear classifier using SVMs

An SVM provides a variety of options to build a nonlinear classifier. We need to build a nonlinear classifier using various kernels. For the sake of simplicity, let's consider two cases here. When we want to represent a curvy boundary between two sets of points, we can either do this using a polynomial function or a radial basis function.

How to do it…

  1. For the first case, let's use a polynomial kernel to build a nonlinear classifier. In the same Python file, search for the following line:
    params = {'kernel': 'linear'}

    Replace this line with the following:

    params = {'kernel': 'poly', 'degree': 3}

    This means that we use a polynomial function with degree 3. If you increase the degree, this means we allow the polynomial to be curvier. However, curviness comes at a cost in the sense that it will take more time to train because it's more computationally expensive.

  2. If you run this code now, you will get the following figure:
    How to do it…
  3. You will also see the following classification report printed on your Terminal:
    How to do it…
  4. We can also use a radial basis function kernel to build a nonlinear classifier. In the same Python file, search for the following line:
    params = {'kernel': 'poly', 'degree': 3}

    Replace this line with the following one:

    params = {'kernel': 'rbf'} 
  5. If you run this code now, you will get the following figure:
    How to do it…
  6. You will also see the following classification report printed on your Terminal:
    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
18.117.91.2