Classification Example with Ridge Classifier in Python

   The Ridge Classifier,  based on Ridge regression method, converts the label data into [-1, 1] and solves the problem with regression method. The highest value in prediction is accepted as a target class and for multiclass data muilti-output regression is applied.
   In this tutorial, we'll briefly learn how to classify data by using Scikit-learn's RidgeClassifier class in Python. The tutorial covers:
  1. Preparing the data
  2. Training the model
  3. Predicting and accuracy check
  4. Iris dataset classification example
  5. Source code listing
   We'll start by loading the required libraries.

from sklearn.linear_model import RidgeClassifier
from sklearn.datasets import load_iris
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split from sklearn.model_selection import cross_val_score from sklearn.metrics import confusion_matrix from sklearn.metrics import classification_report

Preparing the data

    First, we'll generate random classification dataset with make_classification() function. The dataset contains 3 classes with 10 features and the number of samples is 5000.

x, y = make_classification(n_samples=5000, n_features=10, 
                           n_classes=3, 
                           n_clusters_per_class=1)

Then, we'll split the data into train and test parts. Here, we'll extract 15 percent of it as test data.

xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.15)


Training the model

     Next, we'll define the classifier by using the RidgeClassifier class. We can use the default parameters of the class. The parameters can be changed according to the classification data content.

rc = RidgeClassifier()
print(rc)

RidgeClassifier(alpha=1.0, class_weight=None, copy_X=True, fit_intercept=True, max_iter=None, normalize=True, random_state=None, solver='auto', tol=0.001)

Then, we'll fit the model on train data and check the model accuracy score.

rc.fit(xtrain, ytrain)
score = rc.score(xtrain, ytrain)
print("Score: ", score)

Score:  0.8272941176470588

We can also apply a cross-validation training method to the model and check the training score.

cv_scores = cross_val_score(rc, xtrain, ytrain, cv=10)
print("CV average score: %.2f" % cv_scores.mean())
CV average score: 0.83


Predicting and accuracy check

     Now, we can predict the test data by using the trained model. After the prediction, we'll check the accuracy level by using the confusion matrix function.

ypred = rc.predict(xtest)

cm = confusion_matrix(ytest, ypred)
print(cm)

[[227  37   0]
 [  0 190  59]
 [  0  34 203]]

We can also create a classification report by using classification_report() function on predicted data to check the other accuracy metrics.

cr = classification_report(ytest, ypred)
print(cr)

              precision    recall  f1-score   support
           
           0 1.00 0.86 0.92 264 1 0.73 0.76 0.75 249 2 0.77 0.86 0.81 237 accuracy 0.83 750 macro avg 0.83 0.83 0.83 750 weighted avg 0.84 0.83 0.83 750


Iris dataset classification example

    We'll load the Iris dataset with load_iris() function, extract the x and y parts, then split into the train and test parts.

print("Iris dataset classification with SVC")

iris = load_iris() x, y = iris.data, iris.target
xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)

Then, we'll use the same method mentioned above.

rc = RidgeClassifier()
print(rc) rc.fit(xtrain, ytrain) score = rc.score(xtrain, ytrain) print("Score: ", score) cv_scores = cross_val_score(lsvc, xtrain, ytrain, cv=10) print("CV average score: %.2f" % cv_scores.mean()) ypred = rc.predict(xtest) cm = confusion_matrix(ytest, ypred) print(cm) cr = classification_report(ytest, ypred) print(cr)

Iris dataset classification with SVC RidgeClassifier(alpha=1.0, class_weight=None, copy_X=True, fit_intercept=True, max_iter=None, normalize=False, random_state=None, solver='auto', tol=0.001) Score: 0.8818897637795275 CV average score: 0.87 [[7 0 0] [0 5 4] [0 1 6]] precision recall f1-score support 0 1.00 1.00 1.00 7 1 0.83 0.56 0.67 9 2 0.60 0.86 0.71 7 accuracy 0.78 23 macro avg 0.81 0.80 0.79 23 weighted avg 0.81 0.78 0.78 23

    In this tutorial, we've briefly learned how to classify data by using Scikit-learn's RidgeClassifier class in Python. The full source code is listed below.


Source code listing

from sklearn.linear_model import RidgeClassifier
from sklearn.datasets import load_iris
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report

x, y = make_classification(n_samples=5000, n_features=10, 
                           n_classes=3, 
                           n_clusters_per_class=1)

xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)

rc = RidgeClassifier()
print(rc)

rc.fit(xtrain, ytrain)
score = rc.score(xtrain, ytrain)
print("Score: ", score)

cv_scores = cross_val_score(rc, xtrain, ytrain, cv=10)
print("CV average score: %.2f" % cv_scores.mean())

ypred = rc.predict(xtest)

cm = confusion_matrix(ytest, ypred)
print(cm)

cr = classification_report(ytest, ypred)
print(cr) 


# Iris dataset classification
print("Iris dataset classification with SVC")
iris = load_iris()
x, y = iris.data, iris.target
xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)

rc = RidgeClassifier()
print(rc)

rc.fit(xtrain, ytrain)
score = rc.score(xtrain, ytrain)
print("Score: ", score)

cv_scores = cross_val_score(rc, xtrain, ytrain, cv=10)
print("CV average score: %.2f" % cv_scores.mean())

ypred = rc.predict(xtest)

cm = confusion_matrix(ytest, ypred)
print(cm)

cr = classification_report(ytest, ypred)
print(cr)  


References:

No comments:

Post a Comment