{"metadata":{"colab":{"provenance":[],"gpuType":"T4"},"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"},"accelerator":"GPU"},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np","metadata":{"id":"lPnWLC6Ehy2B"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"id":"YXuSPaThfAgL"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{"id":"mXC9FiukiFmu"}},{"cell_type":"code","source":"data = df_train","metadata":{"id":"FmzfC_VPiBdG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"id":"DIOinflAiI3q","outputId":"f7aca320-f0ab-44f1-c589-1dcf2c92d13c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"id":"1fRBNLhoiK9C","outputId":"a94a47da-5091-43a1-b42f-8c7a48aa5b0c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"id":"iZd9VJ_IiRzc","outputId":"e6c0a6bb-b207-48a3-bc90-7c2956c3b064"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.isnull().sum())","metadata":{"id":"l1cE3PHxiVCf","outputId":"0c6e4144-4e07-4623-c6f2-366e8cf73032"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values = data.isnull().sum()\ncolumns_with_missing_values = missing_values[missing_values > 0]\ncolumns_with_missing_values","metadata":{"id":"vyY8TLYA2VGj","outputId":"833ee032-1959-4ea7-9d09-793d1b4d78b1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data['label'].value_counts())","metadata":{"id":"DOlIwVQXm0Go","outputId":"3780630e-2dd4-4cf0-e1b5-fa41505bd540"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\ndata['label'].value_counts().sort_index().plot(kind='bar')\nplt.title('Distribution of Labels')\nplt.xlabel('Digits')\nplt.ylabel('Number of Occurrences')\nplt.show()","metadata":{"id":"k9zhTnCBide8","outputId":"13b16e30-d790-4949-b181-776909a7b9ed"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2. Visualizing Digits\nfig, axes = plt.subplots(2, 5, figsize=(12, 5))\naxes = axes.ravel()\n\nfor i in range(10):\n    digit_data = data[data['label'] == i].sample(1)\n    digit_image = digit_data.drop('label', axis=1).values.reshape(28, 28)\n    axes[i].imshow(digit_image, cmap='gray')\n    axes[i].axis('off')\n    axes[i].set_title(f\"Digit: {i}\")\n\nplt.tight_layout()\nplt.show()","metadata":{"id":"6XTOgRFsinIq","outputId":"de74754a-6d4b-4b5c-d194-d4e15f98b464"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of variations per digit\nvariations = 3\n\nfig, axes = plt.subplots(variations, 10, figsize=(15, 7))  # Adjusted subplot dimensions\naxes = axes.ravel()\n\nfor i in range(10):  # For each digit\n    for j in range(variations):  # For each variation\n        digit_data = data[data['label'] == i].sample(1, replace=True)\n        digit_image = digit_data.drop('label', axis=1).values.reshape(28, 28)\n\n        # Calculate the correct index in axes based on digit and variation number\n        ax_index = i + j * 10\n\n        axes[ax_index].imshow(digit_image, cmap='gray')\n        axes[ax_index].axis('off')\n        if j == 0:  # Add title only once per digit\n            axes[ax_index].set_title(f\"Digit: {i}\")\n\nplt.tight_layout()\nplt.show()","metadata":{"id":"RrY9A5Cyze-p","outputId":"7c861fb4-5e7c-46b6-c05d-8d5451f05bd0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 3. Checking for Missing Values\nmissing_values = data.isnull().sum().sum()\nprint(f\"Number of missing values in the dataset: {missing_values}\")","metadata":{"id":"YW8Dl30OmHKq","outputId":"cfaa0b93-16e0-4293-8bd8-0ff7a656decb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 4. Pixel Intensity Distribution\nplt.figure(figsize=(10, 5))\nsns.distplot(data.drop('label', axis=1).mean(axis=1), kde=False, bins=50, color='blue')\nplt.title('Pixel Intensity Distribution')\nplt.xlabel('Mean Intensity')\nplt.ylabel('Number of Images')\nplt.show()","metadata":{"id":"NgT0h-YZmJvp","outputId":"8cf51ede-7dc7-4f1f-d209-313d9703e88c"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{"id":"eSOm3SL7o4bD"}},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.metrics import accuracy_score","metadata":{"id":"Ils5PN9vpJvO"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop('label', axis=1)\ny = df_train['label']","metadata":{"id":"icdT1JT1pqI6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"id":"A42Vvc9ZpEvQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_accuracy_reports(predictions, labels):\n    print(\"Confusion Matrix:\")\n    print(\"\")\n    print(confusion_matrix(y_val, predictions))\n    print(\"\")\n    print(\"Classification Report:\")\n    print(\"\")\n    print(classification_report(y_val, predictions))","metadata":{"id":"gcss1JkKpZMw"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## submittion generator","metadata":{"id":"PipOAsmdqDmc"}},{"cell_type":"code","source":"def sub_gen(df_test,predictions,path):\n  ImageId = pd.Series(df_test.index+1, name='ImageId')\n  Label = pd.Series(predictions, name='Label')\n\n  submission = pd.concat([ImageId, Label], axis=1)\n  submission.to_csv(path,index=False)","metadata":{"id":"i0bBf22rqC3R"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## KNN","metadata":{"id":"D5lebO-8pbZA"}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"id":"Ro_0Iwca9u8Y"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### normal","metadata":{"id":"7wmsjUbfsY43"}},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train, y_train)\n\nval_predictions = knn.predict(X_val)\nprint_accuracy_reports(val_predictions, y_val)","metadata":{"id":"d2KFTQj1mK5x","outputId":"0e08c35c-21c0-40be-f5fc-cb4cbc3a5934"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = knn.predict(df_test)\nsub_gen(df_test,predictions,\"/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/knn_2.csv\")","metadata":{"id":"WV3MMu6gp0Jb"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### fine tune","metadata":{"id":"uJYzYd52sVJ3"}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\ngrid_params = {'n_neighbors': [1,3,5,7,9], 'weights': [\"uniform\", \"distance\"],'p': [1, 2]}\n\nknn = KNeighborsClassifier()\n\ngrid_search = GridSearchCV(knn, grid_params, cv=3, scoring=\"accuracy\", return_train_score=True, verbose=1)\ngrid_search.fit(X, y)","metadata":{"id":"htPPhgrQsduk","outputId":"42e15968-43b8-47fc-d1b9-067db8dd9014"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.best_params_","metadata":{"id":"OKbUH222DEup","outputId":"5ff9385f-2d38-4f50-854d-3f97d07a1828"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the best estimator/model from the grid search\nbest_knn_model = grid_search.best_estimator_\n\nval_predictions = best_knn_model.predict(X_val)\nprint_accuracy_reports(val_predictions, y_val)","metadata":{"id":"1py-LdinDmHH","outputId":"88b0b109-f099-4668-e806-a09898c9f0cc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors= 3, p= 2, weights= 'distance')\nknn.fit(X_train, y_train)\n\nval_predictions = knn.predict(X_val)\nprint_accuracy_reports(val_predictions, y_val)","metadata":{"id":"tasW-DtoFldQ","outputId":"2746b416-f0a1-409b-c1b5-f17dec0e381c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict using the best model on the test set\npredictions = best_knn_model.predict(df_test)\nsub_gen(df_test,predictions,\"/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/knn_3.csv\")","metadata":{"id":"uqprRIOQq6Vm"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Neural Network","metadata":{"id":"4dIoXe-iFLxP"}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.utils import to_categorical","metadata":{"id":"hIuxSSLYU1SQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize pixel values to [0, 1]\nX = X / 255.0\n\n# One-hot encode the labels\ny_encoded = to_categorical(y)\n\n# Split data into training and validation sets\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X, y_encoded, test_size=0.2, random_state=42)\n\n# 2. Model Building\nmodel = Sequential([\n    Flatten(input_shape=(784,)),  # Flatten input image\n    Dense(128, activation='relu'),  # First hidden layer with 128 neurons\n    Dropout(0.2),  # Dropout layer to prevent overfitting\n    Dense(10, activation='softmax')  # Output layer with 10 neurons (for 10 classes) using softmax activation\n])\n\n# 3. Compilation\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# 4. Training\n\nmodel.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))","metadata":{"id":"6BQNrqMjFAME","outputId":"0aa66f29-4157-4e61-ee3a-290fd0915d3c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_accuracy_reports(predictions, labels):\n    print(\"Confusion Matrix:\")\n    print(\"\")\n    print(confusion_matrix(labels, predictions))\n    print(\"\")\n    print(\"Classification Report:\")\n    print(\"\")\n    print(classification_report(labels, predictions))\n","metadata":{"id":"_Sc6uZY4YTq7"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate and print accuracy reports\npredictions = model.predict(X_val).argmax(axis=1)  # get the class with the highest probability\n\nprint_accuracy_reports(predictions, y_val.argmax(axis=1))  # convert one-hot encoded labels back to original labels","metadata":{"id":"1w45hU-3Xpcp","outputId":"7def5271-d4d2-4864-8002-e22ee1c7565c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = model.predict(df_test.values / 255.0).argmax(axis=1)  # normalize test data\n\ndef sub_gen(df_test, predictions, path):\n    ImageId = pd.Series(df_test.index + 1, name='ImageId')\n    Label = pd.Series(predictions, name='Label')\n    submission = pd.concat([ImageId, Label], axis=1)\n    submission.to_csv(path, index=False)\n\nsub_gen(df_test, test_predictions, \"/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/nn_1.csv\")","metadata":{"id":"1su_ExwGXUMg","outputId":"89822d10-163e-494e-ed20-d276b62227b2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess the data\nX = df_train.drop('label', axis=1).values.reshape(-1, 28, 28, 1) / 255.0  # normalize and reshape\ny = df_train['label'].values\ny_encoded = tf.keras.utils.to_categorical(y)\n\nX_train, X_val, y_train, y_val = train_test_split(X, y_encoded, test_size=0.2, random_state=42)\n\n# Define the CNN model\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (5, 5), padding=\"same\", activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.Conv2D(32, (5, 5), padding=\"same\", activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(64, (3, 3), padding=\"same\", activation='relu'),\n    tf.keras.layers.Conv2D(64, (3, 3), padding=\"same\", activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))\n","metadata":{"id":"wp4wI0GNYZBb","outputId":"ea039061-8b9f-47f4-f3e2-2c41b83bfcdc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate and print accuracy reports\npredictions = model.predict(X_val).argmax(axis=1)  # get the class with highest probability for each sample\n\ndef print_accuracy_reports(predictions, labels):\n    print(\"Confusion Matrix:\")\n    print(\"\")\n    print(confusion_matrix(labels, predictions))\n    print(\"\")\n    print(\"Classification Report:\")\n    print(\"\")\n    print(classification_report(labels, predictions))\n\nprint_accuracy_reports(predictions, y_val.argmax(axis=1))  # y_val.argmax(axis=1) converts one-hot encoded labels back to original labels","metadata":{"id":"8OjpeNdXZvJW","outputId":"0f872221-a315-45a6-802e-e7c4ffb16541"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate predictions for the test set and save to CSV\ntest_predictions = model.predict(df_test.values.reshape(-1, 28, 28, 1) / 255.0).argmax(axis=1)  # normalize and reshape test data\n\ndef sub_gen(df_test, predictions, path):\n    ImageId = pd.Series(df_test.index + 1, name='ImageId')\n    Label = pd.Series(predictions, name='Label')\n    submission = pd.concat([ImageId, Label], axis=1)\n    submission.to_csv(path, index=False)\n\nsub_gen(df_test, test_predictions, \"/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/nn_2.csv\")\n","metadata":{"id":"6czF5AkqZqtq","outputId":"6185c8d9-17e2-4596-cf82-b88f256d7906"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the entire model to a HDF5 file\nmodel.save('/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/nn2_model.h5')","metadata":{"id":"mvwdoRVFbnzk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\n# Load the model\nloaded_model = load_model('/content/drive/MyDrive/Colab Notebooks/Freelancing/Kaggle/Digit recognizer (1)/nn2_model.h5')\n","metadata":{"id":"Q2EU_bqhazHG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model.summary()","metadata":{"id":"2gpp7p553Zl6","outputId":"1294c58b-18e0-4886-f1e3-646ccd679836"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"RpOXFej13dc8"},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}