# Support Vector Machine (SVM): Digit Classification with SVM Python using sklearn digits dataset

0 like 0 dislike
4.5k views

Train SVM classifier using sklearn digits dataset (i.e. from sklearn.datasets import load_digits) and then,

1. Measure accuracy of your model using different kernels such as rbf and linear.
2. Tune your model further using regularization and gamma parameters and try to come up with highest accurancy score
3. Use 80% of samples as training data size
 Where can I do online courses From Word's Top Instructors? UDEMY:: Attend All Udemy Courses in Just INR 450[Coupon] Coursera:: Join For FREE

0 like 0 dislike
by (278 points)
0 like 0 dislike
by (130 points)
```import pandas as pd

import numpy as np

import sklearn

import matplotlib.pyplot as plt

import seaborn as sns

```
```digits=load_digits()

print(digits)

```
`digits.keys()`
```df=pd.DataFrame(digits.data)

print(df.shape)

```
`df.columns`
```df.isnull().sum()

```
```df['target']=digits.target

```print(digits.data.shape)

print(digits.target.shape)

```
`df.target`
```df.values

```
```from sklearn.model_selection import train_test_split

x=df.drop(['target'],axis='columns')

y=df.target

x_train,x_test,y_train,y_test= train_test_split(x,y,test_size=0.2,random_state=12)

```
```print(len(x_train))

print(len(x_test))

```
```from sklearn.metrics import accuracy_score

from sklearn.svm import SVC

model1=SVC(kernel='rbf',random_state=0, probability=True)

model1.fit(x_train,y_train)

y_pred_1=model1.predict(x_test)

print("Model Score of Kernal(rbf) :", model1.score(x_test,y_test))

```
```model2=SVC(kernel='linear',random_state=0, probability=True)

model2.fit(x_train,y_train)

y_pred_2=model2.predict(x_test)

print("Model Score of Kernal(linear) :", model2.score(x_test,y_test))

```
```model3=SVC(kernel='poly',random_state=0, probability=True)

model3.fit(x_train,y_train)

y_pred_3=model3.predict(x_test)

print("Model Score of Kernal(poly) :", model3.score(x_test,y_test))

```
```accuracy=accuracy_score(y_test,y_pred_3)

print('ACCURACY is',accuracy)

```
```from sklearn.metrics import confusion_matrix

cm=np.array(confusion_matrix(y_test,y_pred_3))

cm

```
```from sklearn.metrics import mean_squared_error

mse=mean_squared_error(y_test,y_pred_3)

mse

```
```model1_C=SVC(C=3)

model1_C.fit(x_train,y_train)

model1_C.score(x_test,y_test)

```
```model2_C=SVC(C=3)

model2_C.fit(x_train,y_train)

model2_C.score(x_test,y_test)

```
```model3_C=SVC(C=3)

model3_C.fit(x_train,y_train)

model3_C.score(x_test,y_test)

```
```plt.figure(figsize=(5,5))

sns.heatmap(cm, annot=True, fmt=".2f", linewidths=.5, square = True, cmap = 'Blues_r')

plt.ylabel('Actual label')

plt.xlabel('Predicted label')

A=f'Accuracy Score :{accuracy:.2f}'

plt.title(A)

plt.show()```