Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
2.4k views
in Python Programming by Goeduhub's Expert (2.2k points)

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

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

2 Answers

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

import numpy as np

import sklearn

import matplotlib.pyplot as plt

from sklearn.datasets import load_digits

import seaborn as sns

digits=load_digits()

print(digits)

digits.keys()

df=pd.DataFrame(digits.data)

print(df.head())

print(df.shape)

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

df['target']=digits.target

df.head()
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()

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...