Skip to main content

Digit Recognition

Here you can import digit dataset from scikit learn library which is in-built, So you don't need to download from other else

Note: If you use visual code, I recommend you to turn your color theme to Monokai because it has a few extra and important keyword and attractive colors than other theme.


 # %%  Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random 

# %%   Load dataset
from sklearn.datasets import load_digits
dataset = load_digits()
dataset.keys()

output: dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])

You have to check all to direct print them
Here DESCR is a description of dataset

# %%   divide the dataset into input and target
inputs = dataset.data
target = dataset.target

# %%   Visualization

images = dataset.images
for i in range(6):
    plt.figure(figsize=(6,4), num=0)
    plt.subplot(2,3, i+1)
    plt.axis('off')
    plt.imshow(images[i], cmap='gray')
    plt.title(dataset.target[i])





# %%   Split the dataset into training and testing set

from sklearn.model_selection import train_test_split
train_f, test_f, train_t, test_t = train_test_split(input, target, test_size=0.2random_state=21)

# %%    model fitting

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()
model.fit(train_f, train_t)

# %%   Score

model.score(test_f, test_t)
# %%    Prediction

num = 10
test_ar = test_f[num]
mark = model.predict([test_ar])

image = test_ar.reshape(8,8)
plt.imshow(image, cmap='gray')
plt.title('Digit is {}'.format(mark))
plt.show()

'''Now you can make real life application to saving model. There is the reason behind
save model is you don't have to run your already done model everytime. It consume your
important time and impairs the model performance.'''

# %%  Save the model

from sklearn.externals import joblib

joblib.dump(model, '../Digit_Recnogition_App')


You have to put your digit image in front of webcam and see result




Comments

Popular posts from this blog

Multiple classification from many of directories

  # %%  Import nessacary libraries import  numpy  as  np import  pandas  as  pd import  cv2 import  matplotlib.pyplot  as  plt import  os import  glob # %%   Keras Tensorflow libraries from  keras  import  layers from  keras.models  import  Model from  keras.optimizers  import  RMSprop , Adam , Nadam from  keras.preprocessing.image  import  ImageDataGenerator from  keras.layers  import  Input, BatchNormalization, Dense, Dropout, Conv2D, Flatten, GlobalAveragePooling2D, LeakyReLU from  keras.preprocessing.image  import  ImageDataGenerator, img_to_array, load_img # %%  Path path  =   r 'G:/Machine Learning/Project/Lego Mnifigures Classification/dataset' open_dir  =  os....

Classification & Confusion Matrix & Accuracy Paradox

Classification  work on voting the object belongs from which classes has more probability  There are two types of classification : Binary classification : There are two classes we have ex: male-female , cat-dog , yes-not  Multiple classification :   There are classes more than two we have ex: traffic signs , face recognition , flower race  , Digit Recognition Confusion matrix :  Confusion matrix is one type of technique to evaluate the model accuracy for classification problem. In this technique we consider how many of positive and negative data points we predict correctly. The main consideration terms are accuracy, precision and recall The accuracy was an appealing matric, because it was a single number. Here precision and recall(sensitivity) are two numbers. So to get the final score (accuracy) of our model we use F1 score, so that we have a single number. Here is the F1 score's mathematical formula: F1 = 2x precision x recall / (precision ...