{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30615,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:32.518634Z","iopub.execute_input":"2023-12-22T03:44:32.519294Z","iopub.status.idle":"2023-12-22T03:44:33.089984Z","shell.execute_reply.started":"2023-12-22T03:44:32.519266Z","shell.execute_reply":"2023-12-22T03:44:33.089194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:33.091939Z","iopub.execute_input":"2023-12-22T03:44:33.092403Z","iopub.status.idle":"2023-12-22T03:44:36.908356Z","shell.execute_reply.started":"2023-12-22T03:44:33.092370Z","shell.execute_reply":"2023-12-22T03:44:36.907363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:36.909783Z","iopub.execute_input":"2023-12-22T03:44:36.910721Z","iopub.status.idle":"2023-12-22T03:44:36.917508Z","shell.execute_reply.started":"2023-12-22T03:44:36.910683Z","shell.execute_reply":"2023-12-22T03:44:36.916303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SVM, KNN, CNN, LR","metadata":{"execution":{"iopub.status.busy":"2023-12-13T15:10:01.559729Z","iopub.execute_input":"2023-12-13T15:10:01.560240Z","iopub.status.idle":"2023-12-13T15:10:01.567225Z","shell.execute_reply.started":"2023-12-13T15:10:01.560197Z","shell.execute_reply":"2023-12-13T15:10:01.564851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(index):\n    plt.imshow(np.array(train_df.drop(columns=['label']).iloc[index]).reshape(28,28))\n    plt.title(train_df['label'].iloc[index])\nshow_image(100)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:49:49.010629Z","iopub.execute_input":"2023-12-18T02:49:49.012712Z","iopub.status.idle":"2023-12-18T02:49:49.564482Z","shell.execute_reply.started":"2023-12-18T02:49:49.012670Z","shell.execute_reply":"2023-12-18T02:49:49.562983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-----\n# Standard Scaling and PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:10:49.497005Z","iopub.execute_input":"2023-12-22T04:10:49.497439Z","iopub.status.idle":"2023-12-22T04:10:49.624825Z","shell.execute_reply.started":"2023-12-22T04:10:49.497406Z","shell.execute_reply":"2023-12-22T04:10:49.624072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardScaling = StandardScaler()\nstandardScaling.fit(train_df.drop(columns = ['label']),train_df['label'])\nX_std = standardScaling.transform(train_df.drop(columns=['label']))\n\npca = PCA()\npca.fit(X_std,train_df['label'])","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:10:50.912063Z","iopub.execute_input":"2023-12-22T04:10:50.912984Z","iopub.status.idle":"2023-12-22T04:10:56.051186Z","shell.execute_reply.started":"2023-12-22T04:10:50.912951Z","shell.execute_reply":"2023-12-22T04:10:56.050096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\nind = np.arange(len(pca.explained_variance_ratio_)) \nplt.bar(ind,pca.explained_variance_ratio_)\nplt.xlabel('n_components')\nplt.ylabel('Variance')","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:10:58.057085Z","iopub.execute_input":"2023-12-22T04:10:58.057457Z","iopub.status.idle":"2023-12-22T04:10:59.624181Z","shell.execute_reply.started":"2023-12-22T04:10:58.057426Z","shell.execute_reply":"2023-12-22T04:10:59.623245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_comp=len(pca.explained_variance_ratio_.cumsum()[pca.explained_variance_ratio_.cumsum() <= 0.90])\nprint(\"Keeping 90% Info with \",n_comp,\" components\")\npca_best = PCA(n_components=n_comp)\nX_red_std = pca_best.fit_transform(X_std)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:11:03.658801Z","iopub.execute_input":"2023-12-22T04:11:03.659173Z","iopub.status.idle":"2023-12-22T04:11:09.399749Z","shell.execute_reply.started":"2023-12-22T04:11:03.659143Z","shell.execute_reply":"2023-12-22T04:11:09.398739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Shape Originally: {train_df.shape}\")\nprint(f\"Shape after pca: {X_red_std.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:11:09.586290Z","iopub.execute_input":"2023-12-22T04:11:09.586614Z","iopub.status.idle":"2023-12-22T04:11:09.592046Z","shell.execute_reply.started":"2023-12-22T04:11:09.586587Z","shell.execute_reply":"2023-12-22T04:11:09.590976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score,classification_report\nX_train,X_test,y_train,y_test = train_test_split(X_red_std,train_df['label'],test_size=0.25,random_state=1234)\nX_train,X_test,y_train,y_test = np.array(X_train),np.array(X_test),np.array(y_train),np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:11:12.418718Z","iopub.execute_input":"2023-12-22T04:11:12.419447Z","iopub.status.idle":"2023-12-22T04:11:12.478641Z","shell.execute_reply.started":"2023-12-22T04:11:12.419411Z","shell.execute_reply":"2023-12-22T04:11:12.477622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"------\n# Implementing KNN","metadata":{}},{"cell_type":"code","source":"%time\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:22:24.341038Z","iopub.execute_input":"2023-12-22T04:22:24.341436Z","iopub.status.idle":"2023-12-22T04:22:24.347235Z","shell.execute_reply.started":"2023-12-22T04:22:24.341407Z","shell.execute_reply":"2023-12-22T04:22:24.346276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nbase_model = KNeighborsClassifier()\nbase_model.fit(X_train,y_train)\naccuracy_score(y_test,base_model.predict(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:22:25.277810Z","iopub.execute_input":"2023-12-22T04:22:25.278200Z","iopub.status.idle":"2023-12-22T04:22:27.683172Z","shell.execute_reply.started":"2023-12-22T04:22:25.278168Z","shell.execute_reply":"2023-12-22T04:22:27.682085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nThe weight should be based on distance as points closer must have a larger influence on the final answer compared to others, \nusing BallTree algorithm because we have lower dimensional data\nKeeping distance metric to be euclidean\n\n\"\"\"\nKNN_inference = KNeighborsClassifier(weights='distance',algorithm=\"ball_tree\",n_neighbors=10)\nKNN_inference","metadata":{"execution":{"iopub.status.busy":"2023-12-13T15:10:18.475655Z","iopub.execute_input":"2023-12-13T15:10:18.476221Z","iopub.status.idle":"2023-12-13T15:10:18.488202Z","shell.execute_reply.started":"2023-12-13T15:10:18.476070Z","shell.execute_reply":"2023-12-13T15:10:18.486453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit\nKNN_inference.fit(X_train,y_train)\naccuracy_score(y_test,KNN_inference.predict(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-13T15:10:18.492761Z","iopub.execute_input":"2023-12-13T15:10:18.493376Z","iopub.status.idle":"2023-12-13T15:18:15.742318Z","shell.execute_reply.started":"2023-12-13T15:10:18.493334Z","shell.execute_reply":"2023-12-13T15:18:15.740857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit\nerror_rate = list()\nfor i in range(1,20):\n    print(f\"Fitting to {i}\")\n    knn = KNeighborsClassifier(n_neighbors=i)\n    knn.fit(X_train,y_train)\n    pred_i = knn.predict(X_test)\n    error_rate.append(np.mean(pred_i != y_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:22:34.423871Z","iopub.execute_input":"2023-12-22T04:22:34.424794Z","iopub.status.idle":"2023-12-22T04:23:20.712389Z","shell.execute_reply.started":"2023-12-22T04:22:34.424760Z","shell.execute_reply":"2023-12-22T04:23:20.711502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nplt.plot(range(0,19),error_rate,color='blue', linestyle='dashed', marker='o',\n markerfacecolor='red', markersize=10)\nplt.title('Error Rate vs. K Value')\nplt.xlabel('K')\nplt.ylabel('Error Rate')","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:23:20.714426Z","iopub.execute_input":"2023-12-22T04:23:20.715117Z","iopub.status.idle":"2023-12-22T04:23:21.097160Z","shell.execute_reply.started":"2023-12-22T04:23:20.715077Z","shell.execute_reply":"2023-12-22T04:23:21.096233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Error rate was lowest around index 2, thus best k = 3\n","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:26:15.288952Z","iopub.execute_input":"2023-12-13T17:26:15.289362Z","iopub.status.idle":"2023-12-13T17:26:15.296027Z","shell.execute_reply.started":"2023-12-13T17:26:15.289331Z","shell.execute_reply":"2023-12-13T17:26:15.294373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"Using gridsearch cv to find the best parameters\"\"\"\nfrom sklearn.model_selection import GridSearchCV\nparam_grid = {\"n_neighbors\":[2,3],\"algorithm\":[\"ball_tree\",\"kd_tree\"],\"weights\":[\"distance\",\"uniform\"]}\ngsc = GridSearchCV(estimator=KNeighborsClassifier(),param_grid=param_grid,scoring='accuracy',verbose=3,return_train_score=True,cv=3)\ngsc","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:23:45.124023Z","iopub.execute_input":"2023-12-22T04:23:45.124411Z","iopub.status.idle":"2023-12-22T04:23:45.134865Z","shell.execute_reply.started":"2023-12-22T04:23:45.124379Z","shell.execute_reply":"2023-12-22T04:23:45.133885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gsc.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:43:31.107111Z","iopub.execute_input":"2023-12-22T04:43:31.107964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(gsc.cv_results_)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:42:56.870906Z","iopub.execute_input":"2023-12-22T04:42:56.871290Z","iopub.status.idle":"2023-12-22T04:42:56.905183Z","shell.execute_reply.started":"2023-12-22T04:42:56.871258Z","shell.execute_reply":"2023-12-22T04:42:56.903795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"Best knn\"\"\"\naccuracy_score(y_test,gsc.predict(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:49:52.886903Z","iopub.execute_input":"2023-12-18T02:49:52.887379Z","iopub.status.idle":"2023-12-18T02:49:53.394977Z","shell.execute_reply.started":"2023-12-18T02:49:52.887344Z","shell.execute_reply":"2023-12-18T02:49:53.393355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-----\n# SVM","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC","metadata":{"execution":{"iopub.status.busy":"2023-12-21T12:01:39.220116Z","iopub.execute_input":"2023-12-21T12:01:39.221554Z","iopub.status.idle":"2023-12-21T12:01:39.226912Z","shell.execute_reply.started":"2023-12-21T12:01:39.221495Z","shell.execute_reply":"2023-12-21T12:01:39.225677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"Base SVM\"\"\"\nsvc = SVC()\nsvc.fit(X_train,y_train)\ntest_predictions = svc.predict(X_test)\ntrain_predictions = svc.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T12:01:39.893773Z","iopub.execute_input":"2023-12-21T12:01:39.894432Z","iopub.status.idle":"2023-12-21T12:04:24.862407Z","shell.execute_reply.started":"2023-12-21T12:01:39.894396Z","shell.execute_reply":"2023-12-21T12:04:24.861226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Testing accuracy : {accuracy_score(y_train,train_predictions)} Testing accuracy: {accuracy_score(y_test,test_predictions)}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-21T12:06:29.506538Z","iopub.execute_input":"2023-12-21T12:06:29.507013Z","iopub.status.idle":"2023-12-21T12:06:29.519681Z","shell.execute_reply.started":"2023-12-21T12:06:29.506978Z","shell.execute_reply":"2023-12-21T12:06:29.518317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_test,test_predictions))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T12:06:31.400602Z","iopub.execute_input":"2023-12-21T12:06:31.401036Z","iopub.status.idle":"2023-12-21T12:06:31.440885Z","shell.execute_reply.started":"2023-12-21T12:06:31.401004Z","shell.execute_reply":"2023-12-21T12:06:31.439268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nC is a regularisation parameter and thus i've taken some common numbers \nKernel using rbf and poly as i don't think the data will be plotted in a linear fashion (https://scikit-learn.org/stable/auto_examples/svm/plot_svm_kernels.html#sphx-glr-auto-examples-svm-plot-svm-kernels-py)\n\"\"\"\nfrom sklearn.model_selection import GridSearchCV\nparam_grid = {'kernel':['linear', 'poly'],\n              'degree':[1, 2,],\n              'gamma': [0.01, 0.1],\n              'coef0': [0.5, 1]}\ngrid_svm = GridSearchCV(estimator=SVC(probability = True),param_grid=param_grid,scoring='accuracy',verbose=3,return_train_score=True,cv=3,refit=True)\ngrid_svm.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T12:06:37.798699Z","iopub.execute_input":"2023-12-21T12:06:37.799088Z","iopub.status.idle":"2023-12-21T13:09:22.374335Z","shell.execute_reply.started":"2023-12-21T12:06:37.799056Z","shell.execute_reply":"2023-12-21T13:09:22.372802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(grid_svm.cv_results_)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:09:22.376608Z","iopub.execute_input":"2023-12-21T13:09:22.377097Z","iopub.status.idle":"2023-12-21T13:09:22.418436Z","shell.execute_reply.started":"2023-12-21T13:09:22.377056Z","shell.execute_reply":"2023-12-21T13:09:22.417111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_svm.best_params_","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:09:22.420516Z","iopub.execute_input":"2023-12-21T13:09:22.421605Z","iopub.status.idle":"2023-12-21T13:09:22.429738Z","shell.execute_reply.started":"2023-12-21T13:09:22.421550Z","shell.execute_reply":"2023-12-21T13:09:22.428334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = grid_svm.predict(X_test)\ntrain_predictions = grid_svm.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:09:22.432514Z","iopub.execute_input":"2023-12-21T13:09:22.433007Z","iopub.status.idle":"2023-12-21T13:09:49.731265Z","shell.execute_reply.started":"2023-12-21T13:09:22.432960Z","shell.execute_reply":"2023-12-21T13:09:49.730247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Testing accuracy : {accuracy_score(y_train,train_predictions)} Testing accuracy: {accuracy_score(y_test,test_predictions)}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:09:58.956651Z","iopub.execute_input":"2023-12-21T13:09:58.957134Z","iopub.status.idle":"2023-12-21T13:09:58.969577Z","shell.execute_reply.started":"2023-12-21T13:09:58.957096Z","shell.execute_reply":"2023-12-21T13:09:58.967952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,test_predictions))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:10:05.828437Z","iopub.execute_input":"2023-12-21T13:10:05.828827Z","iopub.status.idle":"2023-12-21T13:10:05.867887Z","shell.execute_reply.started":"2023-12-21T13:10:05.828794Z","shell.execute_reply":"2023-12-21T13:10:05.866315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----\n# Decision Tree and Random Forest\n","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:12:10.981771Z","iopub.execute_input":"2023-12-22T04:12:10.982818Z","iopub.status.idle":"2023-12-22T04:12:11.107179Z","shell.execute_reply.started":"2023-12-22T04:12:10.982787Z","shell.execute_reply":"2023-12-22T04:12:11.106380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----\n## Decision Tree","metadata":{}},{"cell_type":"code","source":"\"\"\"Base model\"\"\"\ndt = DecisionTreeClassifier()\ndt.fit(X_train,y_train)\nprediction_train = dt.predict(X_train)\nprediction_test = dt.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:14:11.519165Z","iopub.execute_input":"2023-12-22T04:14:11.519847Z","iopub.status.idle":"2023-12-22T04:14:28.523266Z","shell.execute_reply.started":"2023-12-22T04:14:11.519816Z","shell.execute_reply":"2023-12-22T04:14:28.522445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training accuracy: {round(accuracy_score(y_train,prediction_train))}\")\nprint(f\"Testing accuracy: {round(accuracy_score(y_test,prediction_test))}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:17:04.970234Z","iopub.execute_input":"2023-12-22T04:17:04.971301Z","iopub.status.idle":"2023-12-22T04:17:04.980047Z","shell.execute_reply.started":"2023-12-22T04:17:04.971254Z","shell.execute_reply":"2023-12-22T04:17:04.979022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----\n## Random Forest","metadata":{}},{"cell_type":"code","source":"\"\"\"Base model\"\"\"\nrf = RandomForestClassifier()\nrf.fit(X_train,y_train)\nprediction_train = rf.predict(X_train)\nprediction_test = rf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:18:51.499564Z","iopub.execute_input":"2023-12-22T04:18:51.499910Z","iopub.status.idle":"2023-12-22T04:19:50.098541Z","shell.execute_reply.started":"2023-12-22T04:18:51.499886Z","shell.execute_reply":"2023-12-22T04:19:50.097719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training accuracy: {round(accuracy_score(y_train,prediction_train))}\")\nprint(f\"Testing accuracy: {round(accuracy_score(y_test,prediction_test))}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:19:50.100297Z","iopub.execute_input":"2023-12-22T04:19:50.101008Z","iopub.status.idle":"2023-12-22T04:19:50.110755Z","shell.execute_reply.started":"2023-12-22T04:19:50.100970Z","shell.execute_reply":"2023-12-22T04:19:50.109812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----\n# CNN","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense,Rescaling\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:36.919831Z","iopub.execute_input":"2023-12-22T03:44:36.920114Z","iopub.status.idle":"2023-12-22T03:44:49.848851Z","shell.execute_reply.started":"2023-12-22T03:44:36.920090Z","shell.execute_reply":"2023-12-22T03:44:49.847975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH,IMG_HEIGHT = 28,28\nepochs = 50\nbatch_size=64\nlearning_rate = 0.001\nIMG_SHAPE = (1,IMG_WIDTH,IMG_HEIGHT)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:49.850037Z","iopub.execute_input":"2023-12-22T03:44:49.850599Z","iopub.status.idle":"2023-12-22T03:44:49.855515Z","shell.execute_reply.started":"2023-12-22T03:44:49.850557Z","shell.execute_reply":"2023-12-22T03:44:49.854577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndf = pd.concat([train_df,test_df],axis=0)\ndf.reset_index(drop=True,inplace=True)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:02:25.794394Z","iopub.execute_input":"2023-12-22T04:02:25.794737Z","iopub.status.idle":"2023-12-22T04:02:27.603947Z","shell.execute_reply.started":"2023-12-22T04:02:25.794711Z","shell.execute_reply":"2023-12-22T04:02:27.602998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\nX,y = np.array(train_df.drop(columns=['label'])).astype(np.float32),np.array(train_df['label']).astype(np.float32)\nX = X.reshape(X.shape[0],1,IMG_WIDTH,IMG_HEIGHT)\n\ny = to_categorical(y)\n\ntest_df = np.array(test_df.astype(np.float32))\ntest_df = test_df.reshape(test_df.shape[0],1,IMG_WIDTH,IMG_HEIGHT)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:49:42.669391Z","iopub.execute_input":"2023-12-22T03:49:42.669759Z","iopub.status.idle":"2023-12-22T03:49:44.589909Z","shell.execute_reply.started":"2023-12-22T03:49:42.669729Z","shell.execute_reply":"2023-12-22T03:49:44.589124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.set_image_data_format(\"channels_first\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:52.079573Z","iopub.execute_input":"2023-12-22T03:44:52.080058Z","iopub.status.idle":"2023-12-22T03:44:52.085767Z","shell.execute_reply.started":"2023-12-22T03:44:52.080013Z","shell.execute_reply":"2023-12-22T03:44:52.084678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential()\n# model.add(Rescaling(1./255))\n# model.add(Conv2D(64,(3,3),activation='relu',input_shape=(16,1,IMG_WIDTH,IMG_HEIGHT),padding='valid',kernel_regularizer = keras.regularizers.l2(0.01)))\n# model.add(MaxPooling2D(pool_size=(2,2)))\n# model.add(Dropout(0.25))\n# model.add(Conv2D(16,(3,3),activation='relu',padding='valid',kernel_regularizer=keras.regularizers.l2(0.01)))\n# model.add(MaxPooling2D(pool_size=(2,2)))\n# model.add(Dropout(0.25))\n# model.add(Flatten())\n# model.add(Dropout(0.5))\n# model.add(Dense(10,activation='relu'))\n# model.add(Flatten())\n# model.add(Dropout(0.5))\n# model.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T16:17:14.984024Z","iopub.execute_input":"2023-12-21T16:17:14.984399Z","iopub.status.idle":"2023-12-21T16:17:15.010571Z","shell.execute_reply.started":"2023-12-21T16:17:14.984370Z","shell.execute_reply":"2023-12-21T16:17:15.009821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Rescaling(1./255))\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(16,1,IMG_WIDTH,IMG_HEIGHT),padding='valid',kernel_regularizer = keras.regularizers.l2(0.01)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,(3,3),activation='relu',padding='valid',kernel_regularizer=keras.regularizers.l2(0.01)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(32,(3,3),activation='relu',padding='valid',kernel_regularizer=keras.regularizers.l2(0.01)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:44:57.455635Z","iopub.execute_input":"2023-12-22T03:44:57.456331Z","iopub.status.idle":"2023-12-22T03:45:01.743699Z","shell.execute_reply.started":"2023-12-22T03:44:57.456296Z","shell.execute_reply":"2023-12-22T03:45:01.742730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(0.01),loss='categorical_crossentropy',metrics=['accuracy'])\nhistory_model = model.fit(X,y,epochs=50,verbose=1,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:49:56.824009Z","iopub.execute_input":"2023-12-22T03:49:56.824376Z","iopub.status.idle":"2023-12-22T03:53:38.240827Z","shell.execute_reply.started":"2023-12-22T03:49:56.824348Z","shell.execute_reply":"2023-12-22T03:53:38.239964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-22T03:55:06.418013Z","iopub.execute_input":"2023-12-22T03:55:06.418966Z","iopub.status.idle":"2023-12-22T03:55:06.448080Z","shell.execute_reply.started":"2023-12-22T03:55:06.418907Z","shell.execute_reply":"2023-12-22T03:55:06.447225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=1234)\nX_train,X_test,y_train,y_test = np.array(X_train),np.array(X_test),np.array(y_train),np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:05:54.727746Z","iopub.execute_input":"2023-12-22T04:05:54.728466Z","iopub.status.idle":"2023-12-22T04:05:55.097975Z","shell.execute_reply.started":"2023-12-22T04:05:54.728433Z","shell.execute_reply":"2023-12-22T04:05:55.096866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_train,accuracy_train = model.evaluate(X_train,y_train)\n\nloss_test,accuracy_test = model.evaluate(X_test,y_test)\nprint(f\"Training loss:{round(loss_train,2)}, Training accuracy:{round(accuracy_train*100,2)}\")\nprint(f\"Testing loss {round(loss_test,2)},Training loss: {round(accuracy_test*100,2)}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T04:08:41.238718Z","iopub.execute_input":"2023-12-22T04:08:41.239127Z","iopub.status.idle":"2023-12-22T04:08:45.307623Z","shell.execute_reply.started":"2023-12-22T04:08:41.239094Z","shell.execute_reply":"2023-12-22T04:08:45.306666Z"},"trusted":true},"execution_count":null,"outputs":[]}]}