<jupyter_start><jupyter_text>Handwritten Digit Recognition   Importing Libraries<jupyter_code>import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score, ShuffleSplit
from sklearn.metrics import classification_report
from sklearn.datasets import make_classification<jupyter_output><empty_output><jupyter_text>Loading Dataset<jupyter_code>train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")<jupyter_output><empty_output><jupyter_text>To view the datasets<jupyter_code>train.head()
test.head()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>train.shape
test.shape

train.info()
test.info()
train.isna().sum()
for i in train.columns:
    if train[i].isna().sum() > 0:
        print(i)
test.isna().sum()
for i in test.columns:
    if test[i].isna().sum() > 0:
        print(i)<jupyter_output><empty_output><jupyter_text>Proportion of each digit in train dataset<jupyter_code>train['label'].value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Plotting a histogram of the digits in train dataset<jupyter_code>plt.hist(train['label'], bins=20, alpha=0.7, color='#603c8e')
plt.xlabel('Digits')
plt.ylabel('Frequency')
plt.grid(axis='y', alpha=0.5)
plt.show()<jupyter_output><empty_output><jupyter_text>Data Pre-Processing<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(train[train.columns[1:785]], train[train.columns[0]]
                                                    , test_size=0.2)
# Predictor variables of training set
X_train.shape
# Dependent/outcome variable of training set
y_train.shape
X_test.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Training a Multinomial Naive-Bayes Classifier Model<jupyter_code>from sklearn.naive_bayes import MultinomialNB

mnb = MultinomialNB()
mnb.fit(X_train, y_train)
mnb_pred = mnb.predict(X_test)
print(f"Accuracy: {round(metrics.accuracy_score(y_test, mnb_pred)*100, 2)}%")
param_grid = {'alpha': [0.00001, 0.0001, 0.001, 0.01, 0.1, 0.5, 1.0, 10.0],
              'fit_prior': [True,False]}

mnb_GS = GridSearchCV(mnb, param_grid, cv=5, verbose=2, n_jobs=1)
mnb_GS.fit(X_train, y_train)
mnb_GS_pred = mnb_GS.predict(X_test)
print(f"Accuracy: {round(metrics.accuracy_score(y_test, mnb_GS_pred)*100, 2)}%")
mnb_GS.best_params_<jupyter_output><empty_output><jupyter_text>Classification Report:<jupyter_code>mnb_report = classification_report(y_true = y_test, y_pred = mnb_GS_pred)
print(mnb_report)
from sklearn.metrics import f1_score
f1_score(y_test, mnb_pred, average='weighted')<jupyter_output><empty_output><jupyter_text>Training a Random Forest Classifier Model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()
rfc.fit(X_train, y_train)
rfc_pred = rfc.predict(X_test)
rfc_probs = rfc.predict_proba(X_test)[:, 1]<jupyter_output><empty_output><jupyter_text>Classification Report:<jupyter_code>rfc_report = classification_report(
    digits=6,
    y_true=y_test, 
    y_pred=rfc.predict(X_test))
print(rfc_report)
from sklearn.metrics import f1_score
f1_score(y_test, rfc_pred, average='weighted')
test.head()
result = rfc.predict(test)

print(result)
print(result.shape)
x = range(1, 28001)
print(x)
xd = pd.DataFrame(x)
xd.columns = ['ImageId'] 
print(xd.head())
xd.reset_index(drop = True)<jupyter_output><empty_output><jupyter_text>Training a ANN Classifier Model<jupyter_code>import tensorflow as tf
import pandas as pd
import numpy as np
from tensorflow.keras.models import Sequential
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
train.shape, test.shape

train.head()
test.head(3)
x_train = train.iloc[:,1:]
x_train.head(2)
y_train = train.iloc[:,0]
y_train.head(5)
x_train = x_train / 255.0
test = test / 255.0
#reshape

x_train= x_train.values.reshape(-1,28,28,1)
test=test.values.reshape(-1,28,28,1)

x_train.shape
#build simple ANN

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape =(28,28,1)),
    tf.keras.layers.Dense(128, activation = 'relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation = 'softmax')
])
#compile model

model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics= ['accuracy'])
#train model
r = model.fit(x_train, y_train, epochs=15)
test.shape
x_train.shape
results=model.predict(test)
results = np.argmax(results, axis =1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13065548444052086, 'fraction_numerical': 0.024056499668947254, 'mean_word_length': 6.7206132879046, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '154685', 'license': 'MIT', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1721, 'n_words': 297, 'path': 'Digit-Recognizer.ipynb', 'repo_name': 'Divyakathirvel26/HandWritten-Digit-Recognition-', 'size': '4531'}