Gadgets 4 Students Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
4.5k 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

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

2 Answers

0 like 0 dislike
by (278 points)

GO_STP_6266

https://www.linkedin.com/posts/sahil-parmar-4099391bb_google-colaboratory-activity-6810449047736975360-T7CN

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

395 comments

4.5k users

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

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

 

Free Online Directory
...