{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-22T16:07:14.478934Z","iopub.execute_input":"2024-01-22T16:07:14.479628Z","iopub.status.idle":"2024-01-22T16:07:14.801224Z","shell.execute_reply.started":"2024-01-22T16:07:14.479590Z","shell.execute_reply":"2024-01-22T16:07:14.800383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import preprocessing\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten\nfrom keras.utils import to_categorical\n\n# 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\")\n\n# Split the training data into features (X) and labels (y)\nX = train_data.iloc[:, 1:]\ny = train_data.iloc[:, 0]\n\n# Normalize pixel values to be between 0 and 1\nX = X / 255.0\n\n# 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)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:07:14.802643Z","iopub.execute_input":"2024-01-22T16:07:14.803061Z","iopub.status.idle":"2024-01-22T16:07:32.644582Z","shell.execute_reply.started":"2024-01-22T16:07:14.803033Z","shell.execute_reply":"2024-01-22T16:07:32.643598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert labels to one-hot encoding\ny_train_categorical = to_categorical(y_train)\ny_val_categorical = to_categorical(y_val)\n\n# Build a simple neural network\nmodel = Sequential()\nmodel.add(Flatten(input_shape=(X_train.shape[1],)))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(X_train, y_train_categorical, validation_data=(X_val, y_val_categorical), epochs=10, batch_size=64)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:07:32.645641Z","iopub.execute_input":"2024-01-22T16:07:32.645937Z","iopub.status.idle":"2024-01-22T16:07:50.832805Z","shell.execute_reply.started":"2024-01-22T16:07:32.645910Z","shell.execute_reply":"2024-01-22T16:07:50.832141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize test data\ntest_data_normalized = test_data / 255.0\n\n# Make predictions on the test set\npredictions = model.predict(test_data_normalized)\n\n# Convert predictions to labels\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create a submission DataFrame\nsubmission_df = pd.DataFrame({'ImageId': range(1, len(test_data) + 1), 'Label': predicted_labels})\n\n# Save the submission file\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:07:50.834393Z","iopub.execute_input":"2024-01-22T16:07:50.834902Z","iopub.status.idle":"2024-01-22T16:07:52.586197Z","shell.execute_reply.started":"2024-01-22T16:07:50.834855Z","shell.execute_reply":"2024-01-22T16:07:52.584784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the validation set\nval_loss, val_accuracy = model.evaluate(X_val, y_val_categorical)\nprint(f\"Validation Accuracy: {val_accuracy}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:07:52.587572Z","iopub.execute_input":"2024-01-22T16:07:52.588008Z","iopub.status.idle":"2024-01-22T16:07:53.326500Z","shell.execute_reply.started":"2024-01-22T16:07:52.587970Z","shell.execute_reply":"2024-01-22T16:07:53.325032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom keras.optimizers import Adam\n\n# Define the CNN model\nmodel_cnn = Sequential()\nmodel_cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel_cnn.add(MaxPooling2D(pool_size=(2, 2)))\nmodel_cnn.add(Flatten())\nmodel_cnn.add(Dense(128, activation='relu'))\nmodel_cnn.add(Dense(10, activation='softmax'))\n\n# Compile the CNN model\nmodel_cnn.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Reshape the input data for CNN (assuming grayscale images)\nX_train_cnn = X_train.values.reshape(-1, 28, 28, 1)\nX_val_cnn = X_val.values.reshape(-1, 28, 28, 1)\n\n# Train the CNN model\nmodel_cnn.fit(X_train_cnn, y_train_categorical, validation_data=(X_val_cnn, y_val_categorical), epochs=10, batch_size=64)\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:40:15.373251Z","iopub.execute_input":"2024-01-22T16:40:15.373622Z","iopub.status.idle":"2024-01-22T16:41:36.359951Z","shell.execute_reply.started":"2024-01-22T16:40:15.373596Z","shell.execute_reply":"2024-01-22T16:41:36.358912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set using the CNN model\ntest_data_normalized_cnn = test_data.values.reshape(-1, 28, 28, 1)\npredictions_test_cnn = model_cnn.predict(test_data_normalized_cnn)\n\n# Define predictions_model1 (replace it with your actual model's predictions)\npredictions_model1 = model.predict(test_data_normalized)\n\n# Ensemble with previous model\nensemble_predictions = np.argmax((predictions_test_cnn + predictions_model1) / 2, axis=1)\n\n# Create a submission DataFrame for the ensemble\nsubmission_df_ensemble = pd.DataFrame({'ImageId': range(1, len(test_data) + 1), 'Label': ensemble_predictions})\n\n# Save the ensemble submission file\nsubmission_df_ensemble.to_csv(\"/kaggle/working/submission_ensemble.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:42:32.383332Z","iopub.execute_input":"2024-01-22T16:42:32.383686Z","iopub.status.idle":"2024-01-22T16:42:39.201107Z","shell.execute_reply.started":"2024-01-22T16:42:32.383656Z","shell.execute_reply":"2024-01-22T16:42:39.200032Z"},"trusted":true},"execution_count":null,"outputs":[]}]}