{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-12-03T11:04:08.073146Z","iopub.execute_input":"2022-12-03T11:04:08.073593Z","iopub.status.idle":"2022-12-03T11:04:08.102475Z","shell.execute_reply.started":"2022-12-03T11:04:08.073515Z","shell.execute_reply":"2022-12-03T11:04:08.101473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport keras\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau, CSVLogger\nfrom sklearn import datasets\nfrom keras.datasets import mnist","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:08.104334Z","iopub.execute_input":"2022-12-03T11:04:08.104987Z","iopub.status.idle":"2022-12-03T11:04:14.534805Z","shell.execute_reply.started":"2022-12-03T11:04:08.104950Z","shell.execute_reply":"2022-12-03T11:04:14.533868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model 1 using given dataset CNN","metadata":{}},{"cell_type":"code","source":"training_set=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_set=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(training_set.shape)\nprint(test_set.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:14.536112Z","iopub.execute_input":"2022-12-03T11:04:14.537193Z","iopub.status.idle":"2022-12-03T11:04:19.070695Z","shell.execute_reply.started":"2022-12-03T11:04:14.537151Z","shell.execute_reply":"2022-12-03T11:04:19.068811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=training_set['label']\ny","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.073866Z","iopub.execute_input":"2022-12-03T11:04:19.074257Z","iopub.status.idle":"2022-12-03T11:04:19.092344Z","shell.execute_reply.started":"2022-12-03T11:04:19.074218Z","shell.execute_reply":"2022-12-03T11:04:19.091273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=training_set.drop(labels=['label'],axis=1)\nX.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.093919Z","iopub.execute_input":"2022-12-03T11:04:19.094264Z","iopub.status.idle":"2022-12-03T11:04:19.181964Z","shell.execute_reply.started":"2022-12-03T11:04:19.094228Z","shell.execute_reply":"2022-12-03T11:04:19.180905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X.values.reshape(-1,28,28,1)\n#test_set.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.183543Z","iopub.execute_input":"2022-12-03T11:04:19.184039Z","iopub.status.idle":"2022-12-03T11:04:19.190698Z","shell.execute_reply.started":"2022-12-03T11:04:19.183999Z","shell.execute_reply":"2022-12-03T11:04:19.189608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y=to_categorical(y,num_classes=10)\ny","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.192371Z","iopub.execute_input":"2022-12-03T11:04:19.192790Z","iopub.status.idle":"2022-12-03T11:04:19.204615Z","shell.execute_reply.started":"2022-12-03T11:04:19.192754Z","shell.execute_reply":"2022-12-03T11:04:19.203319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model 1","metadata":{}},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.1,random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.206059Z","iopub.execute_input":"2022-12-03T11:04:19.206544Z","iopub.status.idle":"2022-12-03T11:04:19.444078Z","shell.execute_reply.started":"2022-12-03T11:04:19.206510Z","shell.execute_reply":"2022-12-03T11:04:19.443079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Build the model object\nmodel = tf.keras.models.Sequential()\n# Add the Flatten Layer\nmodel.add(tf.keras.layers.Dense(256, input_shape=(X_train.shape[1],), activation='sigmoid'))\nmodel.add(tf.keras.layers.Flatten())\n# Build the input and the hidden layers\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\n# Build the output layer\nmodel.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))\n\n# Compile the model\nmodel.compile(optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"])\nmodel.fit(x=X_train,y=y_train,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:19.445752Z","iopub.execute_input":"2022-12-03T11:04:19.446485Z","iopub.status.idle":"2022-12-03T11:04:44.209074Z","shell.execute_reply.started":"2022-12-03T11:04:19.446445Z","shell.execute_reply":"2022-12-03T11:04:44.208035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model performance\ntest_loss, test_acc = model.evaluate(x=X_test, y=y_test)\n# Print out the model accuracy \nprint('\\nTest accuracy:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:44.213459Z","iopub.execute_input":"2022-12-03T11:04:44.213760Z","iopub.status.idle":"2022-12-03T11:04:44.678298Z","shell.execute_reply.started":"2022-12-03T11:04:44.213728Z","shell.execute_reply":"2022-12-03T11:04:44.676893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test) # Make prediction\npredictions.shape\n#predictions[:,1].shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:44.680642Z","iopub.execute_input":"2022-12-03T11:04:44.681464Z","iopub.status.idle":"2022-12-03T11:04:44.975986Z","shell.execute_reply.started":"2022-12-03T11:04:44.681421Z","shell.execute_reply":"2022-12-03T11:04:44.975062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'Real Values':y_test, 'Predicted Values':[np.argmax(predictions[i]) for i in range(4200)]})\ndf","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:44.977500Z","iopub.execute_input":"2022-12-03T11:04:44.977886Z","iopub.status.idle":"2022-12-03T11:04:45.012980Z","shell.execute_reply.started":"2022-12-03T11:04:44.977849Z","shell.execute_reply":"2022-12-03T11:04:45.011966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model 2 using CNN","metadata":{}},{"cell_type":"code","source":"datas=datasets.load_digits()","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:45.014335Z","iopub.execute_input":"2022-12-03T11:04:45.014758Z","iopub.status.idle":"2022-12-03T11:04:45.101083Z","shell.execute_reply.started":"2022-12-03T11:04:45.014722Z","shell.execute_reply":"2022-12-03T11:04:45.100228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(datas.feature_names)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:45.102423Z","iopub.execute_input":"2022-12-03T11:04:45.103137Z","iopub.status.idle":"2022-12-03T11:04:45.110494Z","shell.execute_reply.started":"2022-12-03T11:04:45.103084Z","shell.execute_reply":"2022-12-03T11:04:45.109219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.gray()\nplt.imshow(datas.images[6])","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:45.112059Z","iopub.execute_input":"2022-12-03T11:04:45.112730Z","iopub.status.idle":"2022-12-03T11:04:45.340516Z","shell.execute_reply.started":"2022-12-03T11:04:45.112694Z","shell.execute_reply":"2022-12-03T11:04:45.339584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dx_train,dx_test,dy_train,dy_test=train_test_split(datas.data,datas.target,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:45.341904Z","iopub.execute_input":"2022-12-03T11:04:45.342239Z","iopub.status.idle":"2022-12-03T11:04:45.348015Z","shell.execute_reply.started":"2022-12-03T11:04:45.342205Z","shell.execute_reply":"2022-12-03T11:04:45.346870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2=Sequential([\n    tf.keras.layers.Dense(256,input_shape=([dx_train.shape[1]]),activation='sigmoid'),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128,activation='relu'),\n    tf.keras.layers.Dense(128,activation='relu'),\n    tf.keras.layers.Dense(10,activation='softmax')\n])\nmodel2.compile(optimizer='adam',metrics=['accuracy'],loss='sparse_categorical_crossentropy')\nmodel2.fit(x=datas.data,y=datas.target,epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:45.349499Z","iopub.execute_input":"2022-12-03T11:04:45.350108Z","iopub.status.idle":"2022-12-03T11:04:47.695724Z","shell.execute_reply.started":"2022-12-03T11:04:45.350073Z","shell.execute_reply":"2022-12-03T11:04:47.694833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss1, test_acc1 = model2.evaluate(x=dx_test, y=dy_test)\n# Print out the model accuracy \nprint('\\nTest accuracy:', test_acc1)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:47.697253Z","iopub.execute_input":"2022-12-03T11:04:47.697597Z","iopub.status.idle":"2022-12-03T11:04:47.852340Z","shell.execute_reply.started":"2022-12-03T11:04:47.697560Z","shell.execute_reply":"2022-12-03T11:04:47.850977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model2.predict(dx_test)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:47.853594Z","iopub.execute_input":"2022-12-03T11:04:47.854213Z","iopub.status.idle":"2022-12-03T11:04:47.947289Z","shell.execute_reply.started":"2022-12-03T11:04:47.854173Z","shell.execute_reply":"2022-12-03T11:04:47.946376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.DataFrame({'Real Values':dy_test, 'Predicted Values':[np.argmax(pred[i]) for i in range(360)]})\ndf2","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:05:07.999557Z","iopub.execute_input":"2022-12-03T11:05:07.999942Z","iopub.status.idle":"2022-12-03T11:05:08.015931Z","shell.execute_reply.started":"2022-12-03T11:05:07.999910Z","shell.execute_reply":"2022-12-03T11:05:08.014768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model 3 using SVM","metadata":{}},{"cell_type":"code","source":"(x3_train,y3_train),(x3_test,y3_test)=mnist.load_data()\nx3_train.shape\nx3_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.240234Z","iopub.status.idle":"2022-12-03T11:04:48.240711Z","shell.execute_reply.started":"2022-12-03T11:04:48.240467Z","shell.execute_reply":"2022-12-03T11:04:48.240491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.gray()\nplt.figure(figsize=(16,10))\nfor i in range(16):\n    plt.subplot(4,4,i+1)\n    plt.imshow(x3_train[i])","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.242378Z","iopub.status.idle":"2022-12-03T11:04:48.242896Z","shell.execute_reply.started":"2022-12-03T11:04:48.242604Z","shell.execute_reply":"2022-12-03T11:04:48.242644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x3_train=x3_train.reshape(-1,28*28)\nx3_test=x3_test.reshape(-1,28*28)\nx3_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.246368Z","iopub.status.idle":"2022-12-03T11:04:48.246875Z","shell.execute_reply.started":"2022-12-03T11:04:48.246603Z","shell.execute_reply":"2022-12-03T11:04:48.246627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x3_train=x3_train/255.0\nx3_train=x3_train/255.0\nprint(x3_train.shape,y3_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.248155Z","iopub.status.idle":"2022-12-03T11:04:48.248973Z","shell.execute_reply.started":"2022-12-03T11:04:48.248697Z","shell.execute_reply":"2022-12-03T11:04:48.248728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nx3_train = sc.fit_transform(x3_train)\nx3_test = sc.transform(x3_test)\nx3_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.250715Z","iopub.status.idle":"2022-12-03T11:04:48.251211Z","shell.execute_reply.started":"2022-12-03T11:04:48.250952Z","shell.execute_reply":"2022-12-03T11:04:48.250974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nclassifier = SVC(kernel='linear',gamma=0)\nclassifier.fit(x3_train,y3_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.252465Z","iopub.status.idle":"2022-12-03T11:04:48.253781Z","shell.execute_reply.started":"2022-12-03T11:04:48.253508Z","shell.execute_reply":"2022-12-03T11:04:48.253532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_3=classifier.predict(x3_test)\npred_3.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.255019Z","iopub.status.idle":"2022-12-03T11:04:48.255717Z","shell.execute_reply.started":"2022-12-03T11:04:48.255456Z","shell.execute_reply":"2022-12-03T11:04:48.255480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,confusion_matrix\nprint(accuracy_score(y3_test,pred_3))\nprint(confusion_matrix(y_test,pred_3))","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.257173Z","iopub.status.idle":"2022-12-03T11:04:48.258001Z","shell.execute_reply.started":"2022-12-03T11:04:48.257739Z","shell.execute_reply":"2022-12-03T11:04:48.257763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3=pd.DataFrame({\"Actual values\":y3_test,\"Predicted Labels\":pred_3[:,1]})","metadata":{"execution":{"iopub.status.busy":"2022-12-03T11:04:48.259552Z","iopub.status.idle":"2022-12-03T11:04:48.260385Z","shell.execute_reply.started":"2022-12-03T11:04:48.260125Z","shell.execute_reply":"2022-12-03T11:04:48.260149Z"},"trusted":true},"execution_count":null,"outputs":[]}]}