<jupyter_start><jupyter_text>Task 01 - Mnist Dataset  Loading the Dataset Here I'm Importing the mnist dataset from keras library using load_data() which splits data into the trainig set (60000 digits) and test set (10000 digits)<jupyter_code>from tensorflow.keras.datasets import mnist
(train_X, train_y), (test_X, test_y) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>-----------  Showing Random Numbers from the Dataset Here I'm displaying 10 random images from the training set and 5 random numbers from the test set with the number displayed at the top of each image<jupyter_code>import matplotlib.pyplot as plt
images_2_show = []
titles_2_show = []
for i in range(0, 10):
    r = random.randint(1, 60000)
    images_2_show.append(train_X[r])
    titles_2_show.append('training image [' + str(r) + '] = ' + str(train_y[r]))    

for i in range(0, 5):
    r = random.randint(1, 10000)
    images_2_show.append(test_X[r])        
    titles_2_show.append('test image [' + str(r) + '] = ' + str(test_y[r]))    

cols = 5
rows = int(len(images_2_show)/cols) + 1
plt.figure(figsize=(30, 20))
index = 1
for x in zip(images_2_show, titles_2_show):
    image = x[0]
    title_text = x[1]
    plt.subplot(rows, cols, index)
    plt.imshow(image, cmap=plt.cm.gray)
    if (title_text != ''):
        plt.title(title_text, fontsize=15)
    index += 1<jupyter_output><empty_output><jupyter_text>-----------  Importing the Logistic Regressor and K Nearest Neighbor Classifier now we're using the LogisticRegression Class to Binary classify one digit from the dataset, along side with the KNeighborsClassifier to classify all digits<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier<jupyter_output><empty_output><jupyter_text>The Max Iteration Here is set at 500 (at random) just to make the model converge (it throughs error otherwise)<jupyter_code>LogesticRegressor = LogisticRegression(max_iter=500)<jupyter_output><empty_output><jupyter_text>In this step, we set the training data = 0 if the digit is not equal to **digit**<jupyter_code>digit = 3
y_test_is_digit = train_y == 0
y_test_is_digit = y_is_zero.astype(np.uint8)
y_test_is_digit<jupyter_output><empty_output><jupyter_text>Here we flatten the features variable each image is a 2D (28 x 28) pixels, so after flattening it would be of 1D size (784) for each image<jupyter_code>flattened_train_X = train_X.reshape((len(train_X), -1))
flattened_train_X.shape
LogesticRegressor.fit(X_train_scaled, y_test_is_digit)
predictions = test.predict(test_X.reshape(len(test_X), -1))<jupyter_output><empty_output><jupyter_text>The Confusion Matrix Represents the difference between the predected values from the true ones \False Positives: Predicted value is true but the real value is false \False Negative: Predicted value is false but the real value is true \Right Predictions: Predicted value is true when the real value is true and vice versa<jupyter_code>from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

test_y_digit = test_y == digit

test_y_digit = test_y_zero.astype(np.uint8)

cm = confusion_matrix(test_y_digit, predictions)

disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()
KNearestNeighborClassifier = KNeighborsClassifier()

KNearestNeighborClassifier.fit(train_X.reshape(len(train_X), -1), train_y)
predictions2 = test2.predict(test_X.reshape(len(test_X), -1))
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

cm = confusion_matrix(test_y, predictions2)

disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09718809718809719, 'fraction_numerical': 0.02184002184002184, 'mean_word_length': 5.797773654916512, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '478860', 'license': 'MIT', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1231, 'n_words': 339, 'path': 'Task 01/main.ipynb', 'repo_name': 'RamadanIbrahem98/ML-Algos', 'size': '3663'}