<jupyter_start><jupyter_text>Importing  Packages<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Reading the image data with pixel values in a dataframe<jupyter_code>digits = pd.read_csv('train.csv')
pd.set_option('display.max_columns',500)
digits.head()
digits.describe()
digits.info()
digits.shape<jupyter_output><empty_output><jupyter_text>Cleaning the data<jupyter_code>digits.isnull().sum()
digits['label'].astype('category').value_counts()<jupyter_output><empty_output><jupyter_text>Plotting a image<jupyter_code>four = digits.iloc[3,1:].values
four =four.reshape(28,28)
plt.imshow(four,'gray')
X = digits.drop(['label'],axis=1)
Y = digits['label']
from sklearn.preprocessing import scale
X = scale(X)<jupyter_output>C:\Users\spars\miniconda3\lib\site-packages\ipykernel_launcher.py:4: DataConversionWarning: Data with input dtype int64 were all converted to float64 by the scale function.
  after removing the cwd from sys.path.<jupyter_text>Splitting the data into training and testing set<jupyter_code>from sklearn.model_selection import train_test_split as tt
X_train , X_test , Y_train ,Y_test = tt(X,Y,test_size=0.3,random_state=100)
print(X_test[1].shape)<jupyter_output>(784,)<jupyter_text>KFold Cross Validation to find the best model<jupyter_code>from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC
n_folds = KFold(n_splits = 10,shuffle= True ,random_state =100)
svm = SVC(C=1,kernel='linear', random_state =100)
accuracy = cross_val_score(svm ,X_train,Y_train,cv=n_folds,scoring ='accuracy')
print(accuracy)
print()
print(accuracy.mean())
from sklearn.neighbors import KNeighborsClassifier
n_folds = KFold(n_splits = 10,shuffle= True ,random_state =100)
knn = KNeighborsClassifier()
accuracy = cross_val_score(knn ,X_train,Y_train,cv=n_folds,scoring ='accuracy')
print(accuracy)
print()
print(accuracy.mean())
from sklearn.svm import SVC
n_folds = KFold(n_splits = 10,shuffle= True ,random_state =100)
svm = SVC(C=1,kernel='rbf', random_state =100)
accuracy = cross_val_score(svm ,X_train,Y_train,cv=n_folds,scoring ='accuracy')
print(accuracy)
print()
print(accuracy.mean())<jupyter_output>C:\Users\spars\miniconda3\lib\site-packages\sklearn\svm\base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)
C:\Users\spars\miniconda3\lib\site-packages\sklearn\svm\base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)
C:\Users\spars\miniconda3\lib\site-packages\sklearn\svm\base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)
C:\Users\spars\miniconda3\lib\site-packages\sklearn\svm\base.py:196: FutureW[...]<jupyter_text>Best Model is SVM with kernel='rbf'  from sklearn.model_selection import GridSearchCV model = SVC(kernel='rbf') parameters = {'C': [0.1,1,10,100] , 'gamma' : [1e-1 ,1e-2 ,1e-3]} grid_search = GridSearchCV(estimator=model,param_grid= parameters,scoring = 'accuracy') grid_search.fit(X_train , Y_train) print(grid_search.cv_results_)  Final Model<jupyter_code>from sklearn.svm import SVC
svm_final = SVC(C=1.0,kernel='rbf',random_state=100 )
svm_final.fit(X_train,Y_train)
Y_pred = svm_final.predict(X_test)

from sklearn.metrics import confusion_matrix,accuracy_score,classification_report
print(confusion_matrix(Y_test , Y_pred))
print()
print(accuracy_score(Y_test , Y_pred))
print()
print(classification_report(Y_test , Y_pred))<jupyter_output>C:\Users\spars\miniconda3\lib\site-packages\sklearn\svm\base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)<jupyter_text>Use of OpenCV to predict our own Handwritten Digit Image drawn on the Paint Window.    Saving the Final Model<jupyter_code>import pickle
# Dump the trained decision tree classifier with Pickle
svm_file = 'svm.pkl'
# Open the file to save as pkl file
svm_save_model_pkl = open(svm_file, 'wb')
pickle.dump(svm_final, svm_save_model_pkl)
# Close the pickle instances
svm_save_model_pkl.close()
# Loading the saved decision tree model pickle 
svm_load_model_pkl = open(svm_file, 'rb')
svm_loaded = pickle.load(svm_load_model_pkl)
print("Loaded Decision tree model :: ", svm_loaded)
import cv2
import numpy as np
drawing = False # true if mouse is pressed
# mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1

# mouse callback function
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            cv2.circle(img,(x,y),5,(255,255,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        cv2.circle(img,(x,y),5,(255,255,255),-1)

img = np.zeros((200,200,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1)
    if k == 27:
        cv2.imwrite('drawing.jpg',img)
        break

cv2.destroyAllWindows()
print(img.shape)
imggray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgresize = cv2.resize(imggray,(28,28))
# cv2.imread('drawing.jpg',imgresize)
print(imggray.shape)
print(imgresize.shape)
# cv2.imwrite('drawing.jpg',imgresize)
imgreshape = imgresize.ravel()
print(imgreshape.shape)
# final = cv2.imread('drawing.jpg',0)
# cv2.imshow('Final',final)
# cv2.destroyAllWindows()
imgreshape = np.array(imgreshape)
imgreshape = imgreshape.reshape(1,-1)
print(svm_final.predict(imgreshape))<jupyter_output>(200, 200, 3)
(200, 200)
(28, 28)
(784,)
[2]
{'fraction_non_alphanumeric': 0.12200919030264618, 'fraction_numerical': 0.02915544287751545, 'mean_word_length': 6.172727272727273, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '273939', 'license': 'MIT', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2279, 'n_words': 522, 'path': 'Digit_Recognition_2.ipynb', 'repo_name': 'Sparsh-Bansal/Digit_recognition_bestModel', 'size': '6311'}