{"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":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n#from keras.utils.np_utils import to_categorical\nfrom tensorflow.keras.utils import to_categorical\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization\nfrom sklearn.metrics import confusion_matrix","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:36.906715Z","iopub.execute_input":"2024-11-27T13:26:36.907240Z","iopub.status.idle":"2024-11-27T13:26:36.915052Z","shell.execute_reply.started":"2024-11-27T13:26:36.907199Z","shell.execute_reply":"2024-11-27T13:26:36.913649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:36.924086Z","iopub.execute_input":"2024-11-27T13:26:36.924972Z","iopub.status.idle":"2024-11-27T13:26:36.935309Z","shell.execute_reply.started":"2024-11-27T13:26:36.924923Z","shell.execute_reply":"2024-11-27T13:26:36.934140Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train= pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')\nvalidate = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:36.937418Z","iopub.execute_input":"2024-11-27T13:26:36.937862Z","iopub.status.idle":"2024-11-27T13:26:38.098273Z","shell.execute_reply.started":"2024-11-27T13:26:36.937823Z","shell.execute_reply":"2024-11-27T13:26:38.096951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.100163Z","iopub.execute_input":"2024-11-27T13:26:38.100500Z","iopub.status.idle":"2024-11-27T13:26:38.117769Z","shell.execute_reply.started":"2024-11-27T13:26:38.100466Z","shell.execute_reply":"2024-11-27T13:26:38.116535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images=train.drop('label',axis =1)\ntrain_labels = train.label\nval_images=validate.drop('label',axis=1)\nval_labels=validate.label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.119224Z","iopub.execute_input":"2024-11-27T13:26:38.119699Z","iopub.status.idle":"2024-11-27T13:26:38.179766Z","shell.execute_reply.started":"2024-11-27T13:26:38.119634Z","shell.execute_reply":"2024-11-27T13:26:38.178557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.182815Z","iopub.execute_input":"2024-11-27T13:26:38.183273Z","iopub.status.idle":"2024-11-27T13:26:38.191611Z","shell.execute_reply.started":"2024-11-27T13:26:38.183224Z","shell.execute_reply":"2024-11-27T13:26:38.190506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images = train_images/255.0\nval_images = val_images/255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.192782Z","iopub.execute_input":"2024-11-27T13:26:38.193063Z","iopub.status.idle":"2024-11-27T13:26:38.241642Z","shell.execute_reply.started":"2024-11-27T13:26:38.193034Z","shell.execute_reply":"2024-11-27T13:26:38.240561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images=train_images.values.reshape(-1,28,28,1)\nval_images=val_images.values.reshape(-1,28,28,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.243107Z","iopub.execute_input":"2024-11-27T13:26:38.243554Z","iopub.status.idle":"2024-11-27T13:26:38.249728Z","shell.execute_reply.started":"2024-11-27T13:26:38.243504Z","shell.execute_reply":"2024-11-27T13:26:38.248470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('The shape of train set now is',train_images.shape)\nprint('The shape of test set now is',val_images.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.251127Z","iopub.execute_input":"2024-11-27T13:26:38.251476Z","iopub.status.idle":"2024-11-27T13:26:38.260361Z","shell.execute_reply.started":"2024-11-27T13:26:38.251443Z","shell.execute_reply":"2024-11-27T13:26:38.259392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = train_images[0,:,:,:]\nimage.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.261849Z","iopub.execute_input":"2024-11-27T13:26:38.262231Z","iopub.status.idle":"2024-11-27T13:26:38.276820Z","shell.execute_reply.started":"2024-11-27T13:26:38.262198Z","shell.execute_reply":"2024-11-27T13:26:38.275774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(image)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.278224Z","iopub.execute_input":"2024-11-27T13:26:38.278696Z","iopub.status.idle":"2024-11-27T13:26:38.500563Z","shell.execute_reply.started":"2024-11-27T13:26:38.278620Z","shell.execute_reply":"2024-11-27T13:26:38.499212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels = to_categorical(train_labels)\nval_labels = to_categorical(val_labels)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.505743Z","iopub.execute_input":"2024-11-27T13:26:38.506150Z","iopub.status.idle":"2024-11-27T13:26:38.512148Z","shell.execute_reply.started":"2024-11-27T13:26:38.506113Z","shell.execute_reply":"2024-11-27T13:26:38.511074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels[0,:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.513382Z","iopub.execute_input":"2024-11-27T13:26:38.513739Z","iopub.status.idle":"2024-11-27T13:26:38.527301Z","shell.execute_reply.started":"2024-11-27T13:26:38.513706Z","shell.execute_reply":"2024-11-27T13:26:38.526205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images,test_images,train_labels,test_labels = train_test_split(train_images,train_labels, random_state=0, test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.528773Z","iopub.execute_input":"2024-11-27T13:26:38.529220Z","iopub.status.idle":"2024-11-27T13:26:38.634142Z","shell.execute_reply.started":"2024-11-27T13:26:38.529174Z","shell.execute_reply":"2024-11-27T13:26:38.632969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters =32,kernel_size =(5,5),padding ='Same',\n                 activation ='relu',input_shape =(28,28,1)))\nmodel.add(BatchNormalization(momentum=0.1))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters =64,kernel_size =(3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization(momentum=0.1))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.635578Z","iopub.execute_input":"2024-11-27T13:26:38.636055Z","iopub.status.idle":"2024-11-27T13:26:38.978612Z","shell.execute_reply.started":"2024-11-27T13:26:38.636008Z","shell.execute_reply":"2024-11-27T13:26:38.977310Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.add(Conv2D(filters =32,kernel_size =(5,5),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization(momentum=0.1))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.4))\nmodel.add(Flatten())#np.reval()\nmodel.add(Dense(10,activation=\"softmax\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:38.979827Z","iopub.execute_input":"2024-11-27T13:26:38.980129Z","iopub.status.idle":"2024-11-27T13:26:39.056144Z","shell.execute_reply.started":"2024-11-27T13:26:38.980098Z","shell.execute_reply":"2024-11-27T13:26:39.054992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:39.057613Z","iopub.execute_input":"2024-11-27T13:26:39.058467Z","iopub.status.idle":"2024-11-27T13:26:39.088864Z","shell.execute_reply.started":"2024-11-27T13:26:39.058415Z","shell.execute_reply":"2024-11-27T13:26:39.087740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer=tf.keras.optimizers.Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.999)\nmodel.compile(optimizer=optimizer,loss=['categorical_crossentropy'],metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:39.090302Z","iopub.execute_input":"2024-11-27T13:26:39.090772Z","iopub.status.idle":"2024-11-27T13:26:39.101515Z","shell.execute_reply.started":"2024-11-27T13:26:39.090723Z","shell.execute_reply":"2024-11-27T13:26:39.100324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs=5\nbatch_size=64","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:39.103233Z","iopub.execute_input":"2024-11-27T13:26:39.104266Z","iopub.status.idle":"2024-11-27T13:26:39.109865Z","shell.execute_reply.started":"2024-11-27T13:26:39.104221Z","shell.execute_reply":"2024-11-27T13:26:39.108791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_images,train_labels, batch_size=batch_size, epochs = epochs)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:26:39.111411Z","iopub.execute_input":"2024-11-27T13:26:39.111929Z","iopub.status.idle":"2024-11-27T13:27:27.368987Z","shell.execute_reply.started":"2024-11-27T13:26:39.111881Z","shell.execute_reply":"2024-11-27T13:27:27.367878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig,ax=plt.subplots(2,1)\nfig.set\nx=range(1,1+epochs)\nax[0].plot(x,history.history['loss'],color='red')\nax[1].plot(x,history.history['accuracy'],color='red')\nplt.xlabel('Number of epochs')\nplt.ylabel('accuracy')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:27.370688Z","iopub.execute_input":"2024-11-27T13:27:27.371138Z","iopub.status.idle":"2024-11-27T13:27:27.738459Z","shell.execute_reply.started":"2024-11-27T13:27:27.371091Z","shell.execute_reply":"2024-11-27T13:27:27.737402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = model.predict(test_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:27.739930Z","iopub.execute_input":"2024-11-27T13:27:27.740384Z","iopub.status.idle":"2024-11-27T13:27:28.616700Z","shell.execute_reply.started":"2024-11-27T13:27:27.740336Z","shell.execute_reply":"2024-11-27T13:27:28.615716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:28.618305Z","iopub.execute_input":"2024-11-27T13:27:28.618824Z","iopub.status.idle":"2024-11-27T13:27:28.627499Z","shell.execute_reply.started":"2024-11-27T13:27:28.618773Z","shell.execute_reply":"2024-11-27T13:27:28.626547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions=[]\nfor item in prediction:\n    result = item.argmax()\n    predictions.append(result)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:28.628970Z","iopub.execute_input":"2024-11-27T13:27:28.629327Z","iopub.status.idle":"2024-11-27T13:27:28.640342Z","shell.execute_reply.started":"2024-11-27T13:27:28.629279Z","shell.execute_reply":"2024-11-27T13:27:28.639071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions[0:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:28.641767Z","iopub.execute_input":"2024-11-27T13:27:28.642191Z","iopub.status.idle":"2024-11-27T13:27:28.651236Z","shell.execute_reply.started":"2024-11-27T13:27:28.642142Z","shell.execute_reply":"2024-11-27T13:27:28.650247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions[0].argmax()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:28.652317Z","iopub.execute_input":"2024-11-27T13:27:28.652650Z","iopub.status.idle":"2024-11-27T13:27:28.664076Z","shell.execute_reply.started":"2024-11-27T13:27:28.652618Z","shell.execute_reply":"2024-11-27T13:27:28.662885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = test_images[0,:,:,:]\nplt.imshow(image)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T13:27:28.665402Z","iopub.execute_input":"2024-11-27T13:27:28.665880Z","iopub.status.idle":"2024-11-27T13:27:28.830156Z","shell.execute_reply.started":"2024-11-27T13:27:28.665829Z","shell.execute_reply":"2024-11-27T13:27:28.828951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}