{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30775,"isInternetEnabled":false,"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-10-02T20:12:17.722007Z","iopub.execute_input":"2024-10-02T20:12:17.722511Z","iopub.status.idle":"2024-10-02T20:12:17.735144Z","shell.execute_reply.started":"2024-10-02T20:12:17.722465Z","shell.execute_reply":"2024-10-02T20:12:17.733580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv(r'/kaggle/input/digit-recognizer/train.csv')\n\nX_train=df_train.iloc[:,1:].to_numpy()\ny_train=df_train.iloc[:,0].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:17.738002Z","iopub.execute_input":"2024-10-02T20:12:17.738564Z","iopub.status.idle":"2024-10-02T20:12:20.881648Z","shell.execute_reply.started":"2024-10-02T20:12:17.738485Z","shell.execute_reply":"2024-10-02T20:12:20.878381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.883404Z","iopub.status.idle":"2024-10-02T20:12:20.883997Z","shell.execute_reply.started":"2024-10-02T20:12:20.883713Z","shell.execute_reply":"2024-10-02T20:12:20.883741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.885642Z","iopub.status.idle":"2024-10-02T20:12:20.886160Z","shell.execute_reply.started":"2024-10-02T20:12:20.885921Z","shell.execute_reply":"2024-10-02T20:12:20.885946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit=X_train[0].reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.887675Z","iopub.status.idle":"2024-10-02T20:12:20.888138Z","shell.execute_reply.started":"2024-10-02T20:12:20.887912Z","shell.execute_reply":"2024-10-02T20:12:20.887934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(digit)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.891125Z","iopub.status.idle":"2024-10-02T20:12:20.891937Z","shell.execute_reply.started":"2024-10-02T20:12:20.891533Z","shell.execute_reply":"2024-10-02T20:12:20.891570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train/255.0 #normalizing","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.894448Z","iopub.status.idle":"2024-10-02T20:12:20.895191Z","shell.execute_reply.started":"2024-10-02T20:12:20.894842Z","shell.execute_reply":"2024-10-02T20:12:20.894879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.897074Z","iopub.status.idle":"2024-10-02T20:12:20.897781Z","shell.execute_reply.started":"2024-10-02T20:12:20.897434Z","shell.execute_reply":"2024-10-02T20:12:20.897467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Dense(256,activation='relu',input_dim=784))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))\n\n\nfrom tensorflow.keras.optimizers import Adam\noptimizer = Adam(learning_rate=0.001)  # Try a smaller learning rate\nmodel.compile(loss='sparse_categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.900681Z","iopub.status.idle":"2024-10-02T20:12:20.901241Z","shell.execute_reply.started":"2024-10-02T20:12:20.900987Z","shell.execute_reply":"2024-10-02T20:12:20.901015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,y_train,epochs=15,validation_split=0.2,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.903522Z","iopub.status.idle":"2024-10-02T20:12:20.904062Z","shell.execute_reply.started":"2024-10-02T20:12:20.903814Z","shell.execute_reply":"2024-10-02T20:12:20.903839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'],label='train loss',color='red')\nplt.plot(history.history['val_loss'],label='val loss',color='black')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.906049Z","iopub.status.idle":"2024-10-02T20:12:20.906502Z","shell.execute_reply.started":"2024-10-02T20:12:20.906287Z","shell.execute_reply":"2024-10-02T20:12:20.906308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],label='train accuracy',color='red')\nplt.plot(history.history['val_accuracy'],label='val accuracy',color='black')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.908069Z","iopub.status.idle":"2024-10-02T20:12:20.908512Z","shell.execute_reply.started":"2024-10-02T20:12:20.908296Z","shell.execute_reply":"2024-10-02T20:12:20.908319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test :","metadata":{}},{"cell_type":"code","source":"df_test=pd.read_csv(r'/kaggle/input/digit-recognizer/test.csv')\nX_test=df_test.to_numpy()/255.0","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.913461Z","iopub.status.idle":"2024-10-02T20:12:20.914277Z","shell.execute_reply.started":"2024-10-02T20:12:20.913912Z","shell.execute_reply":"2024-10-02T20:12:20.913950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.917122Z","iopub.status.idle":"2024-10-02T20:12:20.917896Z","shell.execute_reply.started":"2024-10-02T20:12:20.917517Z","shell.execute_reply":"2024-10-02T20:12:20.917552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_probability = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.920449Z","iopub.status.idle":"2024-10-02T20:12:20.921553Z","shell.execute_reply.started":"2024-10-02T20:12:20.921259Z","shell.execute_reply":"2024-10-02T20:12:20.921292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = y_probability.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.923294Z","iopub.status.idle":"2024-10-02T20:12:20.923786Z","shell.execute_reply.started":"2024-10-02T20:12:20.923531Z","shell.execute_reply":"2024-10-02T20:12:20.923554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here's what happens:\n\nModel Output (y_probability): When you pass an image through your trained neural network model, the output is a vector of probabilities for each of the 10 classes (digits 0-9). For example, if the model predicts [0.1, 0.05, 0.8, 0.05, ..., 0.0], it indicates that the model believes there's an 80% chance that the image represents the digit '2'.\n\nUsing argmax(axis=1): The argmax(axis=1) function finds the index of the maximum probability in each row (each image's probability vector). In the MNIST dataset:\n\nFor each image, this function picks the index of the highest probability value.\nThe index corresponds to the predicted digit.\n\n(in pandas axis = 1 is column wise but in numpy it's row-wise. )\n\n(argmax is a function provided by NumPy that returns the indices of the maximum values along a specified axis.)","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': np.arange(1, len(y_prediction) + 1),\n    'Label': y_prediction\n})\n \nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T20:12:20.925456Z","iopub.status.idle":"2024-10-02T20:12:20.925950Z","shell.execute_reply.started":"2024-10-02T20:12:20.925714Z","shell.execute_reply":"2024-10-02T20:12:20.925737Z"},"trusted":true},"execution_count":null,"outputs":[]}]}