<jupyter_start><jupyter_text>Predicting Digit in Images We have a dataset containing images of different Digits ranging from 0 to 9. We are going to analyze this datasetand build a prediction function that can predict digit by taking an image as input.  1. Importing libraries and their associated methods<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>2. Loading the Dataset<jupyter_code>from sklearn.datasets import load_digits
digits = load_digits()<jupyter_output><empty_output><jupyter_text>3. Exploring Dataset  3.1 - Determining the total number of images and labels<jupyter_code>print("Image Data Shape",digits.data.shape)
print("Label Data Shape",digits.target.shape)<jupyter_output>Image Data Shape (1797, 64)
Label Data Shape (1797,)<jupyter_text>3.2 - Displaying some of the images and labels<jupyter_code>plt.figure(figsize=(20,4))
for index, (image,label) in enumerate(zip(digits.data[0:5], digits.target[0:5])):
    plt.subplot(1, 5, index+1)
    plt.imshow(np.reshape(image, (8,8)), cmap=plt.cm.gray)
    plt.title("Image: %i\n"% label, fontsize = 20)<jupyter_output><empty_output><jupyter_text>4. Dividing dataset into Training and Test set<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(digits.data , digits.target, test_size=0.23, random_state=2)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)<jupyter_output>(414,)<jupyter_text>5. Model Training  5.1 - import the Logistic Regression model<jupyter_code>from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>5.2 - Making an instance of the model and training it<jupyter_code>regressor = LogisticRegression(solver = "liblinear", multi_class = "ovr")
regressor.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>5.3 - Predicting the output of the first element of the test set<jupyter_code># Returns a Numpy Array
# Predict for one Observation (image)

print(regressor.predict(X_test[0].reshape(1, -1)))<jupyter_output>[4]<jupyter_text>5.4 - Predicting the output of the first 10 elements of the test set<jupyter_code>print(regressor.predict(X_test[0:10]))<jupyter_output>[4 0 9 1 8 7 1 5 1 6]<jupyter_text>5.5 - Predicting for the entire dataset<jupyter_code>predictions = regressor.predict(X_test)<jupyter_output><empty_output><jupyter_text>5.6 - Determining the accuracy of the model<jupyter_code>score = regressor.score(X_test, y_test)
print(score)<jupyter_output>0.9420289855072463<jupyter_text>5.7 - Printing the confusion Matrix<jupyter_code>from sklearn import metrics 

confusionMatrix = metrics.confusion_matrix(y_test, predictions)
print(confusionMatrix)<jupyter_output>[[38  0  0  0  0  0  0  0  0  0]
 [ 0 44  0  1  0  0  0  0  2  1]
 [ 0  0 43  0  0  0  0  0  0  0]
 [ 0  0  0 37  0  0  0  3  2  0]
 [ 0  2  0  0 34  0  0  0  1  1]
 [ 0  0  0  0  0 46  0  0  0  0]
 [ 0  0  0  0  0  0 40  0  1  0]
 [ 0  0  0  0  0  0  0 45  1  0]
 [ 0  2  0  0  0  0  0  0 35  1]
 [ 0  0  0  1  0  1  0  1  3 28]]<jupyter_text>5.8 - Representing the confusion matrix in a heat map<jupyter_code>plt.figure(figsize=(10, 10))
ax = sns.heatmap(confusionMatrix, annot=True, linewidths=.5, square = True, cmap = 'Blues_r');
plt.ylabel("Actual label");
plt.xlabel("Predicted label");
all_sample_title = "Accuracy Score: {0}".format(score)
plt.title(all_sample_title, size = 15);
# ax.get_ylim()
ax.set_ylim(10.0, 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10575296108291032, 'fraction_numerical': 0.06091370558375635, 'mean_word_length': 5.168695652173913, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '52678', 'license': 'MIT', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1355, 'n_words': 360, 'path': 'Predicting Digit From Image Notebook.ipynb', 'repo_name': 'RitikGajjar3/Predicting-Digit-in-Images', 'size': '3546'}