My Brain Cells

Easiest (and best) learning materials for anyone with a curiosity for machine learning and artificial intelligence, Deep learning, Programming, and other fun life hacks.

Digits Recognition using SVM

What is SVM?

SVM is short form for support vector machines. It is a supervised learning models for classification and regression analysis. This was developed by AT&T bell lab. This SVM maps training examples to points in space so as to maximise the width of the gap between the two categories. if at all the new examples come to the picture then they are mapped into the same space and predicted to belong to a category based on which since of the gap they fall. SVMs can efficiently perform a non-linear classification using what is called the kernel trick, implicitly mapping their inputs into high-dimensional feature spaces.

Why do we use SVM for digital recognition?

We have different types to kernal in the SVM such as

  • Linear
  • RBF – Radial Basis Function
  • Polunomial Kernal*Ha7EfcfB5mY2RIKsXaTRkA.png

By this SVM algorithm, which makes easy to classify the digits and recognise it very quickly. SVM has different types of kernals which is used in classification and regression analysis.


Code in Python

We have taken the data from the sklearn dataset. Always the image data is converted into numpy array where the image data gets converted into numbers i.e. it represents as 0 to 255. and also try to flatten the image data.

from sklearn import datasets
digits = datasets.load_digits()
# Bunch : Dictionary like object of Sklearn
# {'key':'values}[0]
# data is 1 Dimension
# images is 2 Dimension
# data is flattened version of images
import matplotlib.pyplot as plt
# DataFrame containing : 
# Rows: 1797 
# Columns : 64 input + 1 output = 65 

import pandas as pd
df = pd.DataFrame(
df['Target'] =
x =
y =
import numpy as np
# splitting the data
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,random_state=0,stratify=y)
# Model
from sklearn.svm import SVC
model = SVC(C=10),y_train)
y_pred = model.predict(x_test)
# Evaluation
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
# finding the best parameters
from sklearn.model_selection import GridSearchCV
svc = SVC()
parameters = {
cv = GridSearchCV(svc,parameters,cv =5),y_train)
OUTPUT: {'C': 10, 'kernel': 'rbf'}


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top