{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[]},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.activations import linear, relu, sigmoid\nfrom sklearn.model_selection import train_test_split","metadata":{"id":"KfAeuCJUJvAU","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:22:55.954807Z","iopub.execute_input":"2025-01-29T08:22:55.955202Z","iopub.status.idle":"2025-01-29T08:23:13.448792Z","shell.execute_reply.started":"2025-01-29T08:22:55.955172Z","shell.execute_reply":"2025-01-29T08:23:13.447665Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Reading Data","metadata":{"id":"8FcB1p1geEr5"}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"id":"q089wWaNKG1j","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:23:13.450320Z","iopub.execute_input":"2025-01-29T08:23:13.451002Z","iopub.status.idle":"2025-01-29T08:23:18.556174Z","shell.execute_reply.started":"2025-01-29T08:23:13.450947Z","shell.execute_reply":"2025-01-29T08:23:18.555042Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Splitting Data","metadata":{"id":"pEPX3CwHeOjA"}},{"cell_type":"code","source":"x = train.drop('label', axis=1)\ny = train['label']\nx_train,x_val,y_train,y_val = train_test_split(x,y,test_size=0.2,random_state=42)","metadata":{"id":"MitEuHCkKswe","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:23:18.558438Z","iopub.execute_input":"2025-01-29T08:23:18.558880Z","iopub.status.idle":"2025-01-29T08:23:18.950066Z","shell.execute_reply.started":"2025-01-29T08:23:18.558836Z","shell.execute_reply":"2025-01-29T08:23:18.948822Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Building The model","metadata":{"id":"OspV_NaXeTHo"}},{"cell_type":"code","source":"model = Sequential([Dense(25, activation = \"relu\"),\n                    Dense(15, activation = \"relu\"),\n                    Dense(10, activation = \"linear\")])\nmodel.compile(\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n)\nhistory = model.fit(x_train, y_train, epochs=100, validation_data=(x_val, y_val))","metadata":{"id":"4vwOzyYRK8Gb","outputId":"0d137ddd-afe3-4ecd-8f3c-40cd12804199","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:23:18.951842Z","iopub.execute_input":"2025-01-29T08:23:18.952258Z","iopub.status.idle":"2025-01-29T08:26:52.519890Z","shell.execute_reply.started":"2025-01-29T08:23:18.952227Z","shell.execute_reply":"2025-01-29T08:26:52.518632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = test.values / 255.0  # Normalize test data\npredictions = model.predict(test_data)","metadata":{"id":"iR256qp3MJz2","outputId":"5e354229-9f76-4bf9-ddf0-e98a878063be","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:52.521227Z","iopub.execute_input":"2025-01-29T08:26:52.521617Z","iopub.status.idle":"2025-01-29T08:26:54.279007Z","shell.execute_reply.started":"2025-01-29T08:26:52.521575Z","shell.execute_reply":"2025-01-29T08:26:54.277505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_labels = np.argmax(predictions, axis=1)","metadata":{"id":"wMab1aZOOTpW","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:54.280475Z","iopub.execute_input":"2025-01-29T08:26:54.280908Z","iopub.status.idle":"2025-01-29T08:26:54.285889Z","shell.execute_reply.started":"2025-01-29T08:26:54.280868Z","shell.execute_reply":"2025-01-29T08:26:54.284839Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Plotting loss","metadata":{"id":"uzOm8UkIeb8G"}},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"id":"CS25YXKnOXP7","outputId":"48aff87d-5e9a-4e33-e893-dd258a31b07a","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:54.286893Z","iopub.execute_input":"2025-01-29T08:26:54.287225Z","iopub.status.idle":"2025-01-29T08:26:54.577276Z","shell.execute_reply.started":"2025-01-29T08:26:54.287185Z","shell.execute_reply":"2025-01-29T08:26:54.576137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_predictions = model.predict(x_val)\nval_predicted_labels = np.argmax(val_predictions, axis=1)","metadata":{"id":"49kG4K5yYx-S","outputId":"20ce3ee6-3382-4a35-effd-cf90117a44e0","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:54.579501Z","iopub.execute_input":"2025-01-29T08:26:54.579836Z","iopub.status.idle":"2025-01-29T08:26:55.270215Z","shell.execute_reply.started":"2025-01-29T08:26:54.579806Z","shell.execute_reply":"2025-01-29T08:26:55.269056Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Misclassified examples","metadata":{"id":"JqP4Egs_ego5"}},{"cell_type":"code","source":"misclassified_indices = np.where(val_predicted_labels != y_val)[0]\n\nplt.figure(figsize=(10, 10))\nfor i, index in enumerate(misclassified_indices[16:32]):  # Display first 16 misclassified images\n    plt.subplot(4, 4, i + 1)\n    plt.imshow(x_val.iloc[index].values.reshape(28, 28), cmap='gray')\n    plt.title(f\"True: {y_val.iloc[index]}, Pred: {val_predicted_labels[index]}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"id":"dOmi1AUAZsDK","outputId":"46221267-9415-4855-d581-3f06c9cea60e","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:55.271611Z","iopub.execute_input":"2025-01-29T08:26:55.271937Z","iopub.status.idle":"2025-01-29T08:26:56.544441Z","shell.execute_reply.started":"2025-01-29T08:26:55.271909Z","shell.execute_reply":"2025-01-29T08:26:56.543349Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Correctly classified Examples","metadata":{"id":"4ooYZYEfeo26"}},{"cell_type":"code","source":"classified_indices = np.where(val_predicted_labels == y_val)[0]\n\nplt.figure(figsize=(10, 10))\nfor i, index in enumerate(classified_indices[:16]):  # Display first 16 misclassified images\n    plt.subplot(4, 4, i + 1)\n    plt.imshow(x_val.iloc[index].values.reshape(28, 28), cmap='gray')\n    plt.title(f\"True: {y_val.iloc[index]}, Pred: {val_predicted_labels[index]}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"id":"EvrN3qbPbFaq","outputId":"63daa69f-f868-41b4-88a7-72c4289710c3","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T08:26:56.545529Z","iopub.execute_input":"2025-01-29T08:26:56.545907Z","iopub.status.idle":"2025-01-29T08:26:57.826153Z","shell.execute_reply.started":"2025-01-29T08:26:56.545863Z","shell.execute_reply":"2025-01-29T08:26:57.825003Z"}},"outputs":[],"execution_count":null}]}