{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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"},{"sourceId":22678333,"sourceType":"kernelVersion"}],"dockerImageVersionId":31040,"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","trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:02.049149Z","iopub.execute_input":"2025-07-02T15:30:02.049445Z","iopub.status.idle":"2025-07-02T15:30:02.566271Z","shell.execute_reply.started":"2025-07-02T15:30:02.049422Z","shell.execute_reply":"2025-07-02T15:30:02.564981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Flatten","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:06.008895Z","iopub.execute_input":"2025-07-02T15:30:06.009214Z","iopub.status.idle":"2025-07-02T15:30:26.074477Z","shell.execute_reply.started":"2025-07-02T15:30:06.009188Z","shell.execute_reply":"2025-07-02T15:30:26.073449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:26.076125Z","iopub.execute_input":"2025-07-02T15:30:26.076820Z","iopub.status.idle":"2025-07-02T15:30:29.521980Z","shell.execute_reply.started":"2025-07-02T15:30:26.076792Z","shell.execute_reply":"2025-07-02T15:30:29.520830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:32.950168Z","iopub.execute_input":"2025-07-02T15:30:32.950553Z","iopub.status.idle":"2025-07-02T15:30:32.999933Z","shell.execute_reply.started":"2025-07-02T15:30:32.950521Z","shell.execute_reply":"2025-07-02T15:30:32.998918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=data.drop(\"label\",axis=1)\ny=data.label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:34.029563Z","iopub.execute_input":"2025-07-02T15:30:34.029972Z","iopub.status.idle":"2025-07-02T15:30:34.224034Z","shell.execute_reply.started":"2025-07-02T15:30:34.029940Z","shell.execute_reply":"2025-07-02T15:30:34.222980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape ### it showing that 42000 image with the pizel value of 784","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:35.972663Z","iopub.execute_input":"2025-07-02T15:30:35.973614Z","iopub.status.idle":"2025-07-02T15:30:35.980734Z","shell.execute_reply.started":"2025-07-02T15:30:35.973578Z","shell.execute_reply":"2025-07-02T15:30:35.979472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:37.729626Z","iopub.execute_input":"2025-07-02T15:30:37.729990Z","iopub.status.idle":"2025-07-02T15:30:37.739208Z","shell.execute_reply.started":"2025-07-02T15:30:37.729963Z","shell.execute_reply":"2025-07-02T15:30:37.737952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:39.292105Z","iopub.execute_input":"2025-07-02T15:30:39.292505Z","iopub.status.idle":"2025-07-02T15:30:39.298542Z","shell.execute_reply.started":"2025-07-02T15:30:39.292472Z","shell.execute_reply":"2025-07-02T15:30:39.297120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.iloc[0].values.reshape(28,28)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:40.856109Z","iopub.execute_input":"2025-07-02T15:30:40.856519Z","iopub.status.idle":"2025-07-02T15:30:40.865707Z","shell.execute_reply.started":"2025-07-02T15:30:40.856488Z","shell.execute_reply":"2025-07-02T15:30:40.864688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(X.iloc[1].values.reshape(28,28))\n# iloc for the row so we taking 2nd rows values\n# now these values are 784 straight array but for image we need them in l,b\n# so 28,28 is use to reperest 784 pizel \n# each row has 28 pixel and there are 28 pixel in col","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:44.169334Z","iopub.execute_input":"2025-07-02T15:30:44.169777Z","iopub.status.idle":"2025-07-02T15:30:44.484736Z","shell.execute_reply.started":"2025-07-02T15:30:44.169746Z","shell.execute_reply":"2025-07-02T15:30:44.483659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:46.069649Z","iopub.execute_input":"2025-07-02T15:30:46.070175Z","iopub.status.idle":"2025-07-02T15:30:46.288871Z","shell.execute_reply.started":"2025-07-02T15:30:46.070136Z","shell.execute_reply":"2025-07-02T15:30:46.287165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# the pixel value are lie from 0 to 255 we need to scale them between 0 to 1 so\nX=X/255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:47.589802Z","iopub.execute_input":"2025-07-02T15:30:47.590203Z","iopub.status.idle":"2025-07-02T15:30:47.714781Z","shell.execute_reply.started":"2025-07-02T15:30:47.590172Z","shell.execute_reply":"2025-07-02T15:30:47.713324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=123)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:48.829440Z","iopub.execute_input":"2025-07-02T15:30:48.829809Z","iopub.status.idle":"2025-07-02T15:30:49.184866Z","shell.execute_reply.started":"2025-07-02T15:30:48.829785Z","shell.execute_reply":"2025-07-02T15:30:49.183706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:50.109314Z","iopub.execute_input":"2025-07-02T15:30:50.109771Z","iopub.status.idle":"2025-07-02T15:30:50.117633Z","shell.execute_reply.started":"2025-07-02T15:30:50.109736Z","shell.execute_reply":"2025-07-02T15:30:50.116249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=Sequential()\n\n#model.add(Flatten(input_shape=(28,28))) as over data is alread flatten we not need to do this\nmodel.add(Dense(128,activation='relu',input_dim=784))\nmodel.add(Dense(32,activation='relu'))\nmodel.add(Dense(10,activation='softmax')) # as multi classification problem we use softmax","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:51.475176Z","iopub.execute_input":"2025-07-02T15:30:51.475976Z","iopub.status.idle":"2025-07-02T15:30:51.628531Z","shell.execute_reply.started":"2025-07-02T15:30:51.475938Z","shell.execute_reply":"2025-07-02T15:30:51.627041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:30:57.370169Z","iopub.execute_input":"2025-07-02T15:30:57.371446Z","iopub.status.idle":"2025-07-02T15:30:57.393931Z","shell.execute_reply.started":"2025-07-02T15:30:57.371402Z","shell.execute_reply":"2025-07-02T15:30:57.393061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:31:25.919495Z","iopub.execute_input":"2025-07-02T15:31:25.919809Z","iopub.status.idle":"2025-07-02T15:31:25.937206Z","shell.execute_reply.started":"2025-07-02T15:31:25.919786Z","shell.execute_reply":"2025-07-02T15:31:25.936237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history= model.fit(X_train,y_train,epochs=20,validation_split=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:31:28.529986Z","iopub.execute_input":"2025-07-02T15:31:28.531189Z","iopub.status.idle":"2025-07-02T15:32:41.654583Z","shell.execute_reply.started":"2025-07-02T15:31:28.531143Z","shell.execute_reply":"2025-07-02T15:32:41.653597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_prob=model.predict(X_test)\ny_prob\n# here we are gettin probability of 1 image \n# we getting 10 probability of resembling that image with the 10 digits","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:34:04.069846Z","iopub.execute_input":"2025-07-02T15:34:04.070239Z","iopub.status.idle":"2025-07-02T15:34:04.910177Z","shell.execute_reply.started":"2025-07-02T15:34:04.070210Z","shell.execute_reply":"2025-07-02T15:34:04.909188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# we need a single value so we take the highest probability index value\ny_pred=y_prob.argmax(axis=1)\ny_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:35:10.893922Z","iopub.execute_input":"2025-07-02T15:35:10.894306Z","iopub.status.idle":"2025-07-02T15:35:10.903076Z","shell.execute_reply.started":"2025-07-02T15:35:10.894279Z","shell.execute_reply":"2025-07-02T15:35:10.902122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:35:38.454917Z","iopub.execute_input":"2025-07-02T15:35:38.455310Z","iopub.status.idle":"2025-07-02T15:35:38.465839Z","shell.execute_reply.started":"2025-07-02T15:35:38.455282Z","shell.execute_reply":"2025-07-02T15:35:38.464635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:35:56.613206Z","iopub.execute_input":"2025-07-02T15:35:56.613995Z","iopub.status.idle":"2025-07-02T15:35:56.618888Z","shell.execute_reply.started":"2025-07-02T15:35:56.613962Z","shell.execute_reply":"2025-07-02T15:35:56.617813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:36:46.597208Z","iopub.execute_input":"2025-07-02T15:36:46.597689Z","iopub.status.idle":"2025-07-02T15:36:46.796127Z","shell.execute_reply.started":"2025-07-02T15:36:46.597660Z","shell.execute_reply":"2025-07-02T15:36:46.795181Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"it showing a little bit of overfittijng","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:37:19.333831Z","iopub.execute_input":"2025-07-02T15:37:19.334206Z","iopub.status.idle":"2025-07-02T15:37:19.529145Z","shell.execute_reply.started":"2025-07-02T15:37:19.334182Z","shell.execute_reply":"2025-07-02T15:37:19.528050Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(X_train.iloc[2].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:38:01.953110Z","iopub.execute_input":"2025-07-02T15:38:01.953688Z","iopub.status.idle":"2025-07-02T15:38:02.179504Z","shell.execute_reply.started":"2025-07-02T15:38:01.953655Z","shell.execute_reply":"2025-07-02T15:38:02.178438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.predict(X_train.iloc[2].values.reshape(1,784)).argmax(axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:47:20.213307Z","iopub.execute_input":"2025-07-02T15:47:20.213762Z","iopub.status.idle":"2025-07-02T15:47:20.337547Z","shell.execute_reply.started":"2025-07-02T15:47:20.213729Z","shell.execute_reply":"2025-07-02T15:47:20.336541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.iloc[2].values.reshape(28,28)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:43:15.613253Z","iopub.execute_input":"2025-07-02T15:43:15.613741Z","iopub.status.idle":"2025-07-02T15:43:15.626609Z","shell.execute_reply.started":"2025-07-02T15:43:15.613711Z","shell.execute_reply":"2025-07-02T15:43:15.625579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-02T15:45:48.634741Z","iopub.execute_input":"2025-07-02T15:45:48.635156Z","iopub.status.idle":"2025-07-02T15:45:48.642504Z","shell.execute_reply.started":"2025-07-02T15:45:48.635127Z","shell.execute_reply":"2025-07-02T15:45:48.641608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}