{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30732,"isInternetEnabled":false,"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 seaborn as sns\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:01.922563Z","iopub.execute_input":"2024-06-14T09:15:01.922994Z","iopub.status.idle":"2024-06-14T09:15:02.985386Z","shell.execute_reply.started":"2024-06-14T09:15:01.922957Z","shell.execute_reply":"2024-06-14T09:15:02.984147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntestset=pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:02.987513Z","iopub.execute_input":"2024-06-14T09:15:02.988148Z","iopub.status.idle":"2024-06-14T09:15:07.800675Z","shell.execute_reply.started":"2024-06-14T09:15:02.988105Z","shell.execute_reply":"2024-06-14T09:15:07.799575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset=testset.drop(columns=['id'])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:07.801910Z","iopub.execute_input":"2024-06-14T09:15:07.802226Z","iopub.status.idle":"2024-06-14T09:15:07.824191Z","shell.execute_reply.started":"2024-06-14T09:15:07.802202Z","shell.execute_reply":"2024-06-14T09:15:07.823016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:07.827155Z","iopub.execute_input":"2024-06-14T09:15:07.828261Z","iopub.status.idle":"2024-06-14T09:15:07.855257Z","shell.execute_reply.started":"2024-06-14T09:15:07.828225Z","shell.execute_reply":"2024-06-14T09:15:07.854150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:07.856437Z","iopub.execute_input":"2024-06-14T09:15:07.856729Z","iopub.status.idle":"2024-06-14T09:15:07.862922Z","shell.execute_reply.started":"2024-06-14T09:15:07.856704Z","shell.execute_reply":"2024-06-14T09:15:07.861970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:07.864324Z","iopub.execute_input":"2024-06-14T09:15:07.864715Z","iopub.status.idle":"2024-06-14T09:15:07.917500Z","shell.execute_reply.started":"2024-06-14T09:15:07.864679Z","shell.execute_reply":"2024-06-14T09:15:07.916490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=np.array(df.drop(columns=['label']))\nY=df['label']","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:07.918898Z","iopub.execute_input":"2024-06-14T09:15:07.919310Z","iopub.status.idle":"2024-06-14T09:15:08.246167Z","shell.execute_reply.started":"2024-06-14T09:15:07.919273Z","shell.execute_reply":"2024-06-14T09:15:08.245065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:08.247476Z","iopub.execute_input":"2024-06-14T09:15:08.247820Z","iopub.status.idle":"2024-06-14T09:15:08.254529Z","shell.execute_reply.started":"2024-06-14T09:15:08.247792Z","shell.execute_reply":"2024-06-14T09:15:08.253423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[0].reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:08.256057Z","iopub.execute_input":"2024-06-14T09:15:08.256441Z","iopub.status.idle":"2024-06-14T09:15:08.501129Z","shell.execute_reply.started":"2024-06-14T09:15:08.256405Z","shell.execute_reply":"2024-06-14T09:15:08.499992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.randint(10)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:08.505957Z","iopub.execute_input":"2024-06-14T09:15:08.506314Z","iopub.status.idle":"2024-06-14T09:15:08.512677Z","shell.execute_reply.started":"2024-06-14T09:15:08.506285Z","shell.execute_reply":"2024-06-14T09:15:08.511714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(3,3,figsize=(12,6))\n\nm,_=X.shape\nax=ax.flatten()\n\nfor i in range(len(ax)):\n    plot=np.random.randint(m)\n    reshape_plot=X[plot].reshape(28,28)\n    ax[i].imshow(reshape_plot)\n    ax[i].set_axis_off()\n    ax[i].set_title(Y[i])\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:08.514153Z","iopub.execute_input":"2024-06-14T09:15:08.515076Z","iopub.status.idle":"2024-06-14T09:15:09.251927Z","shell.execute_reply.started":"2024-06-14T09:15:08.515037Z","shell.execute_reply":"2024-06-14T09:15:09.250794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(df,x='label')","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:09.253891Z","iopub.execute_input":"2024-06-14T09:15:09.254339Z","iopub.status.idle":"2024-06-14T09:15:09.530683Z","shell.execute_reply.started":"2024-06-14T09:15:09.254300Z","shell.execute_reply":"2024-06-14T09:15:09.529340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nfrom sklearn.pipeline import Pipeline\n\nscaler=MinMaxScaler()\npl=Pipeline([('norm',MinMaxScaler())])\n\nX=pl.fit_transform(X)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:09.532145Z","iopub.execute_input":"2024-06-14T09:15:09.532556Z","iopub.status.idle":"2024-06-14T09:15:10.086887Z","shell.execute_reply.started":"2024-06-14T09:15:09.532516Z","shell.execute_reply":"2024-06-14T09:15:10.085866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testset)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.088143Z","iopub.execute_input":"2024-06-14T09:15:10.088459Z","iopub.status.idle":"2024-06-14T09:15:10.095529Z","shell.execute_reply.started":"2024-06-14T09:15:10.088433Z","shell.execute_reply":"2024-06-14T09:15:10.094298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset=pl.transform(np.array(testset))","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.097039Z","iopub.execute_input":"2024-06-14T09:15:10.097978Z","iopub.status.idle":"2024-06-14T09:15:10.131194Z","shell.execute_reply.started":"2024-06-14T09:15:10.097940Z","shell.execute_reply":"2024-06-14T09:15:10.129838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testset)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.132665Z","iopub.execute_input":"2024-06-14T09:15:10.133121Z","iopub.status.idle":"2024-06-14T09:15:10.140075Z","shell.execute_reply.started":"2024-06-14T09:15:10.133049Z","shell.execute_reply":"2024-06-14T09:15:10.138895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X.reshape(-1,28,28,1)\ntestset=testset.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.141514Z","iopub.execute_input":"2024-06-14T09:15:10.141870Z","iopub.status.idle":"2024-06-14T09:15:10.149785Z","shell.execute_reply.started":"2024-06-14T09:15:10.141841Z","shell.execute_reply":"2024-06-14T09:15:10.148680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.151198Z","iopub.execute_input":"2024-06-14T09:15:10.151699Z","iopub.status.idle":"2024-06-14T09:15:10.163502Z","shell.execute_reply.started":"2024-06-14T09:15:10.151655Z","shell.execute_reply":"2024-06-14T09:15:10.162398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\n\nY=to_categorical(Y)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:10.165012Z","iopub.execute_input":"2024-06-14T09:15:10.165400Z","iopub.status.idle":"2024-06-14T09:15:23.642819Z","shell.execute_reply.started":"2024-06-14T09:15:10.165372Z","shell.execute_reply":"2024-06-14T09:15:23.641633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,Y_train,Y_test=train_test_split(X,Y,random_state=42,test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:23.644279Z","iopub.execute_input":"2024-06-14T09:15:23.645109Z","iopub.status.idle":"2024-06-14T09:15:24.392992Z","shell.execute_reply.started":"2024-06-14T09:15:23.645048Z","shell.execute_reply":"2024-06-14T09:15:24.391658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.395022Z","iopub.execute_input":"2024-06-14T09:15:24.395504Z","iopub.status.idle":"2024-06-14T09:15:24.403339Z","shell.execute_reply.started":"2024-06-14T09:15:24.395460Z","shell.execute_reply":"2024-06-14T09:15:24.402173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.405104Z","iopub.execute_input":"2024-06-14T09:15:24.405583Z","iopub.status.idle":"2024-06-14T09:15:24.416193Z","shell.execute_reply.started":"2024-06-14T09:15:24.405549Z","shell.execute_reply":"2024-06-14T09:15:24.414988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation , Flatten\nfrom keras.layers import Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.417938Z","iopub.execute_input":"2024-06-14T09:15:24.418304Z","iopub.status.idle":"2024-06-14T09:15:24.430229Z","shell.execute_reply.started":"2024-06-14T09:15:24.418275Z","shell.execute_reply":"2024-06-14T09:15:24.428901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CNN model\n\nmodel=Sequential([\n    Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)),\n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n    Flatten(),\n    Dense(256,activation='relu'),\n    Dropout(0.25),\n    Dense(10,activation='softmax')\n    \n    \n    \n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.431599Z","iopub.execute_input":"2024-06-14T09:15:24.431932Z","iopub.status.idle":"2024-06-14T09:15:24.572169Z","shell.execute_reply.started":"2024-06-14T09:15:24.431905Z","shell.execute_reply":"2024-06-14T09:15:24.571136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\nmodel.compile(Adam(),loss=['categorical_crossentropy'],metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.573319Z","iopub.execute_input":"2024-06-14T09:15:24.573619Z","iopub.status.idle":"2024-06-14T09:15:24.590829Z","shell.execute_reply.started":"2024-06-14T09:15:24.573593Z","shell.execute_reply":"2024-06-14T09:15:24.589866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\nmodel.fit(X_train,Y_train,epochs=5,validation_split=0.2,validation_data=(X_test,Y_test),callbacks=EarlyStopping(patience=3))","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:15:24.591934Z","iopub.execute_input":"2024-06-14T09:15:24.592260Z","iopub.status.idle":"2024-06-14T09:21:53.846846Z","shell.execute_reply.started":"2024-06-14T09:15:24.592233Z","shell.execute_reply":"2024-06-14T09:21:53.845789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model.predict(testset.reshape(-1,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:53.848208Z","iopub.execute_input":"2024-06-14T09:21:53.848566Z","iopub.status.idle":"2024-06-14T09:21:56.340558Z","shell.execute_reply.started":"2024-06-14T09:21:53.848506Z","shell.execute_reply":"2024-06-14T09:21:56.339426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testset)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:56.345712Z","iopub.execute_input":"2024-06-14T09:21:56.346076Z","iopub.status.idle":"2024-06-14T09:21:56.353307Z","shell.execute_reply.started":"2024-06-14T09:21:56.346045Z","shell.execute_reply":"2024-06-14T09:21:56.352045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsub=np.argmax(predictions,axis=1)\nlen(sub)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:56.354468Z","iopub.execute_input":"2024-06-14T09:21:56.354772Z","iopub.status.idle":"2024-06-14T09:21:56.367031Z","shell.execute_reply.started":"2024-06-14T09:21:56.354746Z","shell.execute_reply":"2024-06-14T09:21:56.365806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\n            ","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:56.368557Z","iopub.execute_input":"2024-06-14T09:21:56.368981Z","iopub.status.idle":"2024-06-14T09:21:56.668561Z","shell.execute_reply.started":"2024-06-14T09:21:56.368945Z","shell.execute_reply":"2024-06-14T09:21:56.667583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub=pd.DataFrame()\nsample_sub['id']=test['id']\nsample_sub['label']=sub\n","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:56.669846Z","iopub.execute_input":"2024-06-14T09:21:56.670190Z","iopub.status.idle":"2024-06-14T09:21:56.678734Z","shell.execute_reply.started":"2024-06-14T09:21:56.670163Z","shell.execute_reply":"2024-06-14T09:21:56.677645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-14T09:21:56.679922Z","iopub.execute_input":"2024-06-14T09:21:56.680268Z","iopub.status.idle":"2024-06-14T09:21:56.697599Z","shell.execute_reply.started":"2024-06-14T09:21:56.680242Z","shell.execute_reply":"2024-06-14T09:21:56.696386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}