Python | Classify handwritten numbers with Tensorflow

NumPy | Python Methods and Functions | String Variables

Modules required:

Matplotlib :

 $ pip install matplotlib 



Steps to follow

Step 1: Import the entire dependency

Step 2: Importing a dataset using MNIST data

import numpy as np

import matplotlib.pyplot as plt

import tensorflow as tf

 

learn  = tf.contrib.learn

 
tf.logging.set_verbosity (tf.logging.ERROR)

mnist = learn.datasets.load_dataset ( `mnist ` )

data = mnist.train.images

labels = np.asarray (mnist.train.labels, dtype = np.int32)

test_data = mnist.test.images

test_labels = np.asarray (mnist.test.labels, dtype = np.int32)

after this step the set will be loaded data mnist. 
output:

 Extracting MNIST-data / train-images-idx3-ubyte.gz Extracting MNIST-data / train-labels-idx1-ubyte.gz Extracting MNIST -data / t10k-images-idx3-ubyte.gz Extracting MNIST-data / t10k-labels-idx1-ubyte.gz 

Step 3: Create the dataset

max_examples = 10000

data = data [: max_examples]

labels = labels [: max_examples]

Step 4: Displaying the dataset using MatplotLib

def  display (i):

img = test_data [i]

plt.title ( `label: {}` . format (test_labels [i]))

plt.imshow (img.reshape (( 28 , 28 )))

 
# TensorFlow image - 28 by 28 px

display ( 0 )

To display data, we can use this function —  display (0)
exit:

Step 5: Fitting the data using a linear classifier

feature_columns = learn.infer_real_valued_columns_from_input (data)

classifier = learn.LinearClassifier (n_classes = 10

feature_columns = feature_columns)

classifier.fit (data, labels, batch_size = 100 , steps = 1000 )

Step 6: Assess accuracy

classifier. evaluate (test_data, test_labels)

print (classifier.evaluate (test_data, test_labels) [ "accuracy" ])

Output:

 0.9137 

Step 7: Forecasting data

prediction = classi fier.predict (np.array ([test_data [ 0 ]], 

dtype = float ), 

as_iterable = False )

print ( " prediction: {}, label: {} " . format (prediction, 

  test_labels [ 0 ]))

Output:

 prediction: [ 7], label: 7 

Complete code for handwritten classification

# importing libraries

import numpy as np

import matplotlib.pyplot as plt

import tensorflow as tf

 

learn = tf.contrib.learn

tf.logging.set_verbosity (tf.logging.ERROR)

 
# importing the dataset using MNIST
# this is how mnist is used mnist contains the dataset test and train

mnist = learn. dat asets.load_dataset ( `mnist` )

data = mnist.train.images

labels = np.asarray (mnist.train.labels, dtype = np.int32)

test_data = mnist.test.images

test_labels = np.asarray (mnist.test.labels, dtype = np. int32)

 

max_examples = 10000

data = data [: max_examp les]

labels = labels [: max_examples]

 
# displaying the dataset with Matplotlib

def display (i):

img = test_data [i]

plt.title ( `label: {}` . format (test_labels [i]))

plt.imshow (img.reshape (( 28 , 28 )))

 
# img in tf - 28 by 28 px

# approximate linear classifier

feature_columns = learn.infer_real_valued_columns_from_input (data)

classifier = learn.LinearClassifier (n_classes = 10

feature_columns = feature_columns)

classifier.fit (data, labels, batch_size = 100 , steps = 1000 )

 
# Evaluate the accuracy
classifier.evaluate (test_data, test_labels)

print (classifier.evaluate (test_data, test_labels) [ "accuracy" ] )

 

prediction = classifier.predict (np.array ([test_data [ 0 ]], 

  dtype = float ), 

as_iterable = False )

print ( "prediction: {} , label : {} " . format (prediction, 

test_labels [ 0 ]))

 

if prediction = = test_labels [ 0 ]:

  display ( 0 )





Get Solution for free from DataCamp guru