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

Gradient Descent with RSME

Optimization Alorithms Ex : G.D. , S.D. ,  Adam, RMS prop , momentum , adelta Gradient Descent is an  optimization algorithm that find a best fit line and local minima of a differentiable function for given training data set. S imply used to find the coefficients (weights) and intercept (bias) that minimize a cost function as far as possible.  There are three types of  g radient descent techniques:   Regular Batch GD (Gradient Descent) -  Studiously descend the curve in one path towards one minima ; every hop calculates the cost function for entire training data. If training data is large, one should not use this. Random GD (Stochastic GD) -   Calculates the Cost function for only one (randomly selected) training data per hop ; tend to jump all over the place due to randomness but due to it actually jump across minima’s.  Mini Batch gradient descent - Somewhere midway between the above 2. Does the calculation for a bunch of random data poin...

Why python ? What is Python?

Python is a generally interpreted and  interactive dynamic symmetric   high-level  object oriented programming language. It is widely used in Machine Learning today. Pretty easy to understand, learn, code and explain because it has very crisp and clear syntaxes than other languages.  Guido van Rossum made Python in 1991, named his programming language after the television show Monty Python's Flying Circus. Python has got features or derived features from ABC named programming language. Interactive - The result will be printed on the screen, immediately return, in the next line as we entered. High-level - Humans can easy to interpret; the source code contains easy-to-read syntax that is later converted into a low-level language (0 , 1) Dynamic-symmetric – Don’t need to clarify the data type. It Allows the type casting. Type Casting –  We can transform the one data type in another data type Object Oriented – language is focused on Object...