{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Step 1: Load the Libraries\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.metrics import mean_squared_error, confusion_matrix\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:09.025622Z","iopub.execute_input":"2023-06-03T17:16:09.026585Z","iopub.status.idle":"2023-06-03T17:16:20.263530Z","shell.execute_reply.started":"2023-06-03T17:16:09.026551Z","shell.execute_reply":"2023-06-03T17:16:20.262187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 2: Load the dataset\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:20.265602Z","iopub.execute_input":"2023-06-03T17:16:20.266168Z","iopub.status.idle":"2023-06-03T17:16:24.061080Z","shell.execute_reply.started":"2023-06-03T17:16:20.266142Z","shell.execute_reply":"2023-06-03T17:16:24.059347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3: Split the data into features (X) and labels (y)\nX = train_data.drop('label', axis=1).values\ny = train_data['label'].values","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.062401Z","iopub.execute_input":"2023-06-03T17:16:24.062723Z","iopub.status.idle":"2023-06-03T17:16:24.120534Z","shell.execute_reply.started":"2023-06-03T17:16:24.062696Z","shell.execute_reply":"2023-06-03T17:16:24.119409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 4: Check the shape\nprint(\"Dataset shape:\", X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.123764Z","iopub.execute_input":"2023-06-03T17:16:24.124119Z","iopub.status.idle":"2023-06-03T17:16:24.129985Z","shell.execute_reply.started":"2023-06-03T17:16:24.124092Z","shell.execute_reply":"2023-06-03T17:16:24.128561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 5: Reshape and normalize the input data\nX = X.reshape(-1, 28, 28, 1)\nX = X.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.131748Z","iopub.execute_input":"2023-06-03T17:16:24.132076Z","iopub.status.idle":"2023-06-03T17:16:24.194870Z","shell.execute_reply.started":"2023-06-03T17:16:24.132049Z","shell.execute_reply":"2023-06-03T17:16:24.193576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 6: Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.196419Z","iopub.execute_input":"2023-06-03T17:16:24.196749Z","iopub.status.idle":"2023-06-03T17:16:24.398172Z","shell.execute_reply.started":"2023-06-03T17:16:24.196714Z","shell.execute_reply":"2023-06-03T17:16:24.396733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 7: Display sample images\nnum_samples = 5\nsample_indices = np.random.randint(0, len(X), num_samples)\nfig, axes = plt.subplots(1, num_samples, figsize=(12, 4))\nfor i, index in enumerate(sample_indices):\n    axes[i].imshow(X[index].reshape(28, 28), cmap='gray')\n    axes[i].set_title(\"Label: \" + str(y[index]))\n    axes[i].axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.400164Z","iopub.execute_input":"2023-06-03T17:16:24.400979Z","iopub.status.idle":"2023-06-03T17:16:24.800943Z","shell.execute_reply.started":"2023-06-03T17:16:24.400936Z","shell.execute_reply":"2023-06-03T17:16:24.799936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image preprocessing\n# Perform any necessary preprocessing steps, such as resizing, normalization, or augmentation","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.802174Z","iopub.execute_input":"2023-06-03T17:16:24.802462Z","iopub.status.idle":"2023-06-03T17:16:24.807146Z","shell.execute_reply.started":"2023-06-03T17:16:24.802439Z","shell.execute_reply":"2023-06-03T17:16:24.805731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 8: Class distribution\nclass_counts = np.bincount(y)\nclass_labels = np.arange(len(class_counts))\nplt.figure(figsize=(8, 6))\nplt.bar(class_labels, class_counts)\nplt.xlabel('Class Labels')\nplt.ylabel('Count')\nplt.title('Class Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:24.808579Z","iopub.execute_input":"2023-06-03T17:16:24.808939Z","iopub.status.idle":"2023-06-03T17:16:25.012281Z","shell.execute_reply.started":"2023-06-03T17:16:24.808912Z","shell.execute_reply":"2023-06-03T17:16:25.011002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 9: Visualize image properties\nimage_sizes = np.array([image.shape[:2] for image in X])\nimage_channels = np.array([image.shape[2] if len(image.shape) == 3 else 1 for image in X])\n\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.hist(image_sizes[:, 0], bins=20)\nplt.xlabel('Image Width')\nplt.ylabel('Count')\nplt.title('Distribution of Image Widths')\n\nplt.subplot(1, 2, 2)\nplt.hist(image_channels, bins=10)\nplt.xlabel('Number of Channels')\nplt.ylabel('Count')\nplt.title('Distribution of Image Channels')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:25.015736Z","iopub.execute_input":"2023-06-03T17:16:25.016048Z","iopub.status.idle":"2023-06-03T17:16:25.512208Z","shell.execute_reply.started":"2023-06-03T17:16:25.016022Z","shell.execute_reply":"2023-06-03T17:16:25.510811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 10: Class-specific analysis\nnum_classes = len(class_counts)\nnum_samples_per_class = 5\nfig, axes = plt.subplots(num_classes, num_samples_per_class, figsize=(12, 16))\nfor i in range(num_classes):\n    class_indices = np.where(y == i)[0]\n    sample_indices = np.random.choice(class_indices, num_samples_per_class, replace=False)\n    for j, index in enumerate(sample_indices):\n        axes[i, j].imshow(X[index].reshape(28, 28), cmap='gray')\n        axes[i, j].set_title(\"Label: \" + str(y[index]))\n        axes[i, j].axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:25.513640Z","iopub.execute_input":"2023-06-03T17:16:25.513961Z","iopub.status.idle":"2023-06-03T17:16:28.443731Z","shell.execute_reply.started":"2023-06-03T17:16:25.513935Z","shell.execute_reply":"2023-06-03T17:16:28.442642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 11: Define the neural network model\nmodel = tf.keras.models.Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:28.445335Z","iopub.execute_input":"2023-06-03T17:16:28.445992Z","iopub.status.idle":"2023-06-03T17:16:28.672562Z","shell.execute_reply.started":"2023-06-03T17:16:28.445951Z","shell.execute_reply":"2023-06-03T17:16:28.671574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 12: Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:28.673660Z","iopub.execute_input":"2023-06-03T17:16:28.673938Z","iopub.status.idle":"2023-06-03T17:16:28.692494Z","shell.execute_reply.started":"2023-06-03T17:16:28.673917Z","shell.execute_reply":"2023-06-03T17:16:28.690697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 13: Define early stopping callback\nearly_stopping = EarlyStopping(patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:28.693951Z","iopub.execute_input":"2023-06-03T17:16:28.694282Z","iopub.status.idle":"2023-06-03T17:16:28.700071Z","shell.execute_reply.started":"2023-06-03T17:16:28.694252Z","shell.execute_reply":"2023-06-03T17:16:28.698006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 14: Train the model with early stopping\nhistory = model.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:16:28.701841Z","iopub.execute_input":"2023-06-03T17:16:28.702662Z","iopub.status.idle":"2023-06-03T17:20:39.290463Z","shell.execute_reply.started":"2023-06-03T17:16:28.702592Z","shell.execute_reply":"2023-06-03T17:20:39.288746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 15: Plot the training and validation loss\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:39.292636Z","iopub.execute_input":"2023-06-03T17:20:39.293235Z","iopub.status.idle":"2023-06-03T17:20:39.508548Z","shell.execute_reply.started":"2023-06-03T17:20:39.293199Z","shell.execute_reply":"2023-06-03T17:20:39.507647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 16: Preprocess the test data\ntest_data = test_data.values.reshape(-1, 28, 28, 1)\ntest_data = test_data.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:39.511393Z","iopub.execute_input":"2023-06-03T17:20:39.511691Z","iopub.status.idle":"2023-06-03T17:20:39.550391Z","shell.execute_reply.started":"2023-06-03T17:20:39.511668Z","shell.execute_reply":"2023-06-03T17:20:39.549192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 17: Make predictions on the test set\npredictions = np.argmax(model.predict(test_data), axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:39.551564Z","iopub.execute_input":"2023-06-03T17:20:39.551842Z","iopub.status.idle":"2023-06-03T17:20:44.736224Z","shell.execute_reply.started":"2023-06-03T17:20:39.551819Z","shell.execute_reply":"2023-06-03T17:20:44.735010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 18: Error analysis\n# Assuming you have a validation set `X_val` and `y_val` and the model `model` has been trained\ny_pred_probs = model.predict(X_val)\ny_pred_classes = np.argmax(y_pred_probs, axis=1)\nmisclassified_indices = np.where(y_pred_classes != y_val)[0]\nnum_misclassified = len(misclassified_indices)\nprint(\"Number of misclassified samples:\", num_misclassified)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:44.737487Z","iopub.execute_input":"2023-06-03T17:20:44.738041Z","iopub.status.idle":"2023-06-03T17:20:46.404686Z","shell.execute_reply.started":"2023-06-03T17:20:44.738012Z","shell.execute_reply":"2023-06-03T17:20:46.403164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 19: Display some misclassified images\nnum_samples = 6\nsample_indices = np.random.choice(misclassified_indices, num_samples, replace=False)\nfig, axes = plt.subplots(1, num_samples, figsize=(12, 4))\nfor i, index in enumerate(sample_indices):\n    axes[i].imshow(X_val[index].reshape(28, 28), cmap='gray')\n    axes[i].set_title(\"True: \" + str(y_val[index]) + \"\\nPredicted: \" + str(y_pred_classes[index]))\n    axes[i].axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:46.405999Z","iopub.execute_input":"2023-06-03T17:20:46.406887Z","iopub.status.idle":"2023-06-03T17:20:46.843985Z","shell.execute_reply.started":"2023-06-03T17:20:46.406856Z","shell.execute_reply":"2023-06-03T17:20:46.842624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 20: Dimensionality reduction and visualization\nfrom sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:46.845170Z","iopub.execute_input":"2023-06-03T17:20:46.845478Z","iopub.status.idle":"2023-06-03T17:20:47.006052Z","shell.execute_reply.started":"2023-06-03T17:20:46.845453Z","shell.execute_reply":"2023-06-03T17:20:47.005286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 21: Assuming you have the feature matrix `X` and corresponding labels `y`\nnum_samples, height, width, channels = X.shape\nX_2d = X.reshape(num_samples, height * width * channels)\npca = PCA(n_components=2)\nX_reduced = pca.fit_transform(X_2d)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:47.007250Z","iopub.execute_input":"2023-06-03T17:20:47.007975Z","iopub.status.idle":"2023-06-03T17:20:48.092077Z","shell.execute_reply.started":"2023-06-03T17:20:47.007946Z","shell.execute_reply":"2023-06-03T17:20:48.091279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 22: Plotting the reduced data points\nplt.figure(figsize=(8, 6))\nfor i in range(num_classes):\n    class_indices = np.where(y == i)[0]\n    plt.scatter(X_reduced[class_indices, 0], X_reduced[class_indices, 1], label=str(i))\nplt.xlabel('Dimension 1')\nplt.ylabel('Dimension 2')\nplt.title('2D Visualization of Data Points')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:48.094001Z","iopub.execute_input":"2023-06-03T17:20:48.095191Z","iopub.status.idle":"2023-06-03T17:20:49.603769Z","shell.execute_reply.started":"2023-06-03T17:20:48.095163Z","shell.execute_reply":"2023-06-03T17:20:49.602760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 23: Calculate RMSE on the validation set\nval_predictions = np.argmax(model.predict(X_val), axis=-1)\nval_rmse = np.sqrt(mean_squared_error(y_val, val_predictions))\nprint(\"Validation RMSE:\", val_rmse)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:49.604859Z","iopub.execute_input":"2023-06-03T17:20:49.605558Z","iopub.status.idle":"2023-06-03T17:20:51.111034Z","shell.execute_reply.started":"2023-06-03T17:20:49.605531Z","shell.execute_reply":"2023-06-03T17:20:51.110306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 24: Calculate confusion matrix on the validation set\nval_confusion = confusion_matrix(y_val, val_predictions)\nprint(\"Confusion Matrix:\")\nprint(val_confusion)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:51.112216Z","iopub.execute_input":"2023-06-03T17:20:51.112648Z","iopub.status.idle":"2023-06-03T17:20:51.119880Z","shell.execute_reply.started":"2023-06-03T17:20:51.112625Z","shell.execute_reply":"2023-06-03T17:20:51.118840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 25: Plot confusion matrix as a heatmap\nplt.figure(figsize=(8, 6))\nsns.heatmap(val_confusion, annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:51.121063Z","iopub.execute_input":"2023-06-03T17:20:51.121344Z","iopub.status.idle":"2023-06-03T17:20:51.555661Z","shell.execute_reply.started":"2023-06-03T17:20:51.121323Z","shell.execute_reply":"2023-06-03T17:20:51.554820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 26: Prepare submission file\nsubmission = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1),\n                           'Label': predictions})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)\nprint(submission)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T17:20:51.557404Z","iopub.execute_input":"2023-06-03T17:20:51.557794Z","iopub.status.idle":"2023-06-03T17:20:51.603802Z","shell.execute_reply.started":"2023-06-03T17:20:51.557746Z","shell.execute_reply":"2023-06-03T17:20:51.602063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}