Classifier decision functions in Python

Hi, everyone in this tutorial we are going to see about classifier decision functions in brief with Python.

What are the Decision functions?

The Decision Function is used in classification algorithms especially in SVC (support Vector Classifier). The decision function tells us the magnitude of the point in a hyperplane. Once this decision function is set the classifier classifies model within this decision function boundary.

Generally, when there is a need for specified outcomes we use decision functions. This decision function is also used to label the magnitude of the hyperplane (i.e. how close the points are lying in the plane).

Implementation of classifier decision functions in Python

The Sklearn package provides a function called decision_function() which helps us to implement it in Python. Now let us implement this decision_function() in SVC,

The Coding part is done in Google Colab, Copy the code segments to your workspace in Google Colab. Refer to this tutorial Google Colab for Machine Learning to get started with the Google Colab, If you are new to Google Colab.

  1. To import necessary packages and create X,y data and to create an svc model we use the below code segment.
    import numpy as np
    X = np.array([[12,11],[1,1],[2,2],[2,12]])
    y = np.array([1,2,2,2])
    from sklearn.svm import SVC
    mod = SVC(kernel='linear', C = 1.0)
    mod.fit(X, y)
  2. To Visualize the data and the division line,
    weight = mod.coef_[0]
    data = -w[0] / w[1]
    xax=np.linspace(0,12)
    yax=a*xax-mod.intercept_[0] / w[1]
    h0 = plt.plot(xax, yax, 'k-', label="non weighted div")
    plt.scatter(X[:, 0], X[:, 1], c = y)
    plt.legend()
    plt.show()

    classifier DFHere, look at our program as well as the figure.

  3. To set the decision function and to predict the data we use the below code segment.
    print(mod.decision_function(X))
    mod.predict(X)

    Output:

    [-0.99986929  1.19991504  0.99993465  0.99993465]   
    array([1, 2, 2, 2])

    We can say that the decision function has labeled the values according to their presence in the hyperplane. So we did it.

    Hope this tutorial helps!!!

 

One response to “Classifier decision functions in Python”

  1. Dominic P says:

    Hi Infant,
    When trying to run the code snippets given in your article, I have run into several problems as multiple things aren’t defined, including w, a, and plt. I am operating under the assumption that w is supposed to be weight and plt is from an import of matplotlib.pyplot, but the variable a is never declared anywhere.

Leave a Reply