{"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-01-11T18:47:17.729818Z","iopub.execute_input":"2022-01-11T18:47:17.730071Z","iopub.status.idle":"2022-01-11T18:47:17.741331Z","shell.execute_reply.started":"2022-01-11T18:47:17.730040Z","shell.execute_reply":"2022-01-11T18:47:17.740260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing import image\nimport tensorflow_datasets.public_api as tfds\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:17.743717Z","iopub.execute_input":"2022-01-11T18:47:17.744093Z","iopub.status.idle":"2022-01-11T18:47:23.610299Z","shell.execute_reply.started":"2022-01-11T18:47:17.743936Z","shell.execute_reply":"2022-01-11T18:47:23.609579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the data from TensorFlow","metadata":{}},{"cell_type":"code","source":"data = tf.keras.datasets.mnist\n(train_images, train_labels), (test_images, test_labels) = data.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:23.611760Z","iopub.execute_input":"2022-01-11T18:47:23.612022Z","iopub.status.idle":"2022-01-11T18:47:24.101467Z","shell.execute_reply.started":"2022-01-11T18:47:23.611992Z","shell.execute_reply":"2022-01-11T18:47:24.100726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the shape\ntrain_images.shape, train_labels.shape, test_images.shape, test_labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:24.105486Z","iopub.execute_input":"2022-01-11T18:47:24.106044Z","iopub.status.idle":"2022-01-11T18:47:24.113307Z","shell.execute_reply.started":"2022-01-11T18:47:24.106011Z","shell.execute_reply":"2022-01-11T18:47:24.112585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['0', '1', '2','3','4','5','6','7','8','9']","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:24.114593Z","iopub.execute_input":"2022-01-11T18:47:24.115210Z","iopub.status.idle":"2022-01-11T18:47:24.121465Z","shell.execute_reply.started":"2022-01-11T18:47:24.115172Z","shell.execute_reply":"2022-01-11T18:47:24.120672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data\n\ntrain_images = train_images / 255.\ntest_images = test_images / 255.","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:24.122886Z","iopub.execute_input":"2022-01-11T18:47:24.123198Z","iopub.status.idle":"2022-01-11T18:47:24.266949Z","shell.execute_reply.started":"2022-01-11T18:47:24.123164Z","shell.execute_reply":"2022-01-11T18:47:24.266180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building a CNN","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(16, (3,3), activation='relu', input_shape=(28,28,1)),\n          MaxPooling2D((3,3)),\n          Flatten(),\n          Dense(10, activation='softmax')         \n]);","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:23:00.265050Z","iopub.execute_input":"2022-01-11T19:23:00.265580Z","iopub.status.idle":"2022-01-11T19:23:00.299886Z","shell.execute_reply.started":"2022-01-11T19:23:00.265520Z","shell.execute_reply":"2022-01-11T19:23:00.299245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary the model\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:26.749826Z","iopub.execute_input":"2022-01-11T18:47:26.750355Z","iopub.status.idle":"2022-01-11T18:47:26.759138Z","shell.execute_reply.started":"2022-01-11T18:47:26.750317Z","shell.execute_reply":"2022-01-11T18:47:26.758258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling the CNN\n\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.005),\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=tf.keras.metrics.SparseCategoricalAccuracy())","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:26.760450Z","iopub.execute_input":"2022-01-11T18:47:26.761074Z","iopub.status.idle":"2022-01-11T18:47:26.777640Z","shell.execute_reply.started":"2022-01-11T18:47:26.761021Z","shell.execute_reply":"2022-01-11T18:47:26.776907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\n\nhistory = model.fit(train_images[...,np.newaxis], train_labels[...,np.newaxis], epochs=5, batch_size=32, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:26.781720Z","iopub.execute_input":"2022-01-11T18:47:26.782227Z","iopub.status.idle":"2022-01-11T18:47:53.208358Z","shell.execute_reply.started":"2022-01-11T18:47:26.782186Z","shell.execute_reply":"2022-01-11T18:47:53.207703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the history into a pandas DataFrame\n\ndf = pd.DataFrame(history.history)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:53.209790Z","iopub.execute_input":"2022-01-11T18:47:53.210021Z","iopub.status.idle":"2022-01-11T18:47:53.228329Z","shell.execute_reply.started":"2022-01-11T18:47:53.209991Z","shell.execute_reply":"2022-01-11T18:47:53.227573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make the plotting for the loss\n\nloss_plot = df.plot(y='loss', title=\"Loss vs. Epochs\", legend=False)\nloss_plot.set(xlabel='Epochs', ylabel='Loss');","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:53.229529Z","iopub.execute_input":"2022-01-11T18:47:53.230200Z","iopub.status.idle":"2022-01-11T18:47:53.479133Z","shell.execute_reply.started":"2022-01-11T18:47:53.230139Z","shell.execute_reply":"2022-01-11T18:47:53.478351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make the plotting for the accuracy\n\nacc_plot = df.plot(y='sparse_categorical_accuracy', title=\"Accuracy vs. Epochs\", legend=False)\nacc_plot.set(xlabel='Epochs', ylabel='Accuracy');","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:53.480462Z","iopub.execute_input":"2022-01-11T18:47:53.480772Z","iopub.status.idle":"2022-01-11T18:47:53.696180Z","shell.execute_reply.started":"2022-01-11T18:47:53.480736Z","shell.execute_reply":"2022-01-11T18:47:53.695552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, acc = model.evaluate(test_images[...,np.newaxis], test_labels[...,np.newaxis])","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:53.697400Z","iopub.execute_input":"2022-01-11T18:47:53.697686Z","iopub.status.idle":"2022-01-11T18:47:54.528947Z","shell.execute_reply.started":"2022-01-11T18:47:53.697652Z","shell.execute_reply":"2022-01-11T18:47:54.528279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:54.530380Z","iopub.execute_input":"2022-01-11T18:47:54.530678Z","iopub.status.idle":"2022-01-11T18:47:54.535592Z","shell.execute_reply.started":"2022-01-11T18:47:54.530644Z","shell.execute_reply":"2022-01-11T18:47:54.534882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using a SVM","metadata":{}},{"cell_type":"code","source":"from sklearn import svm","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:54.536875Z","iopub.execute_input":"2022-01-11T18:47:54.537315Z","iopub.status.idle":"2022-01-11T18:47:55.362230Z","shell.execute_reply.started":"2022-01-11T18:47:54.537278Z","shell.execute_reply":"2022-01-11T18:47:55.361519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting the data","metadata":{}},{"cell_type":"code","source":"dt_train= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndt_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:47:55.363738Z","iopub.execute_input":"2022-01-11T18:47:55.363999Z","iopub.status.idle":"2022-01-11T18:48:02.045673Z","shell.execute_reply.started":"2022-01-11T18:47:55.363963Z","shell.execute_reply":"2022-01-11T18:48:02.043742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = round(len(dt_train.drop('label',axis=1)) * .7)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.047637Z","iopub.execute_input":"2022-01-11T18:48:02.047886Z","iopub.status.idle":"2022-01-11T18:48:02.142643Z","shell.execute_reply.started":"2022-01-11T18:48:02.047852Z","shell.execute_reply":"2022-01-11T18:48:02.141866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Splitting to SVM","metadata":{}},{"cell_type":"code","source":"train = dt_train[:split]\ntest = dt_train[split:]","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.144086Z","iopub.execute_input":"2022-01-11T18:48:02.144341Z","iopub.status.idle":"2022-01-11T18:48:02.149234Z","shell.execute_reply.started":"2022-01-11T18:48:02.144307Z","shell.execute_reply":"2022-01-11T18:48:02.148385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop('label',axis=1)\ny_train = train['label']\nX_test = test.drop('label',axis=1)\ny_test = test['label']","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.150604Z","iopub.execute_input":"2022-01-11T18:48:02.151070Z","iopub.status.idle":"2022-01-11T18:48:02.238136Z","shell.execute_reply.started":"2022-01-11T18:48:02.151006Z","shell.execute_reply":"2022-01-11T18:48:02.237202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_test.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.240750Z","iopub.execute_input":"2022-01-11T18:48:02.241275Z","iopub.status.idle":"2022-01-11T18:48:02.251719Z","shell.execute_reply.started":"2022-01-11T18:48:02.241235Z","shell.execute_reply":"2022-01-11T18:48:02.250925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compiling, Fitting and Score","metadata":{}},{"cell_type":"code","source":"clf = svm.SVC(decision_function_shape='ovr')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.254892Z","iopub.execute_input":"2022-01-11T18:48:02.255189Z","iopub.status.idle":"2022-01-11T18:48:02.260997Z","shell.execute_reply.started":"2022-01-11T18:48:02.255164Z","shell.execute_reply":"2022-01-11T18:48:02.260279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T18:48:02.262315Z","iopub.execute_input":"2022-01-11T18:48:02.263069Z","iopub.status.idle":"2022-01-11T18:50:34.244081Z","shell.execute_reply.started":"2022-01-11T18:48:02.263041Z","shell.execute_reply":"2022-01-11T18:50:34.243343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_score = clf.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:10:59.129642Z","iopub.execute_input":"2022-01-11T19:10:59.129904Z","iopub.status.idle":"2022-01-11T19:12:48.846117Z","shell.execute_reply.started":"2022-01-11T19:10:59.129869Z","shell.execute_reply":"2022-01-11T19:12:48.845231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"SVM score: {}\".format(clf_score))","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:40:58.940919Z","iopub.execute_input":"2022-01-11T19:40:58.941183Z","iopub.status.idle":"2022-01-11T19:40:58.945587Z","shell.execute_reply.started":"2022-01-11T19:40:58.941152Z","shell.execute_reply":"2022-01-11T19:40:58.944889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using a KNN","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:00:13.383018Z","iopub.execute_input":"2022-01-11T19:00:13.383579Z","iopub.status.idle":"2022-01-11T19:00:13.439565Z","shell.execute_reply.started":"2022-01-11T19:00:13.383516Z","shell.execute_reply":"2022-01-11T19:00:13.438882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:00:24.854987Z","iopub.execute_input":"2022-01-11T19:00:24.855256Z","iopub.status.idle":"2022-01-11T19:00:24.860125Z","shell.execute_reply.started":"2022-01-11T19:00:24.855225Z","shell.execute_reply":"2022-01-11T19:00:24.859251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compiling, Fitting and Score","metadata":{}},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:00:37.454204Z","iopub.execute_input":"2022-01-11T19:00:37.454457Z","iopub.status.idle":"2022-01-11T19:00:47.707250Z","shell.execute_reply.started":"2022-01-11T19:00:37.454427Z","shell.execute_reply":"2022-01-11T19:00:47.706479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_score = knn.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:12:48.857640Z","iopub.execute_input":"2022-01-11T19:12:48.857961Z","iopub.status.idle":"2022-01-11T19:23:00.255843Z","shell.execute_reply.started":"2022-01-11T19:12:48.857925Z","shell.execute_reply":"2022-01-11T19:23:00.255109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Knn scoer: {}\".format(knn_score))","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:42:16.978935Z","iopub.execute_input":"2022-01-11T19:42:16.979200Z","iopub.status.idle":"2022-01-11T19:42:16.985702Z","shell.execute_reply.started":"2022-01-11T19:42:16.979170Z","shell.execute_reply":"2022-01-11T19:42:16.984873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Summarizing all algorithms in a frame","metadata":{}},{"cell_type":"code","source":"scores = np.array([acc, clf_score, knn_score])\nscores = pd.DataFrame(scores)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:27:35.943650Z","iopub.execute_input":"2022-01-11T19:27:35.944404Z","iopub.status.idle":"2022-01-11T19:27:35.949431Z","shell.execute_reply.started":"2022-01-11T19:27:35.944364Z","shell.execute_reply":"2022-01-11T19:27:35.948776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:32:51.855114Z","iopub.execute_input":"2022-01-11T19:32:51.855959Z","iopub.status.idle":"2022-01-11T19:32:51.863967Z","shell.execute_reply.started":"2022-01-11T19:32:51.855912Z","shell.execute_reply":"2022-01-11T19:32:51.863200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"machines = np.array(['CNN', 'SVM', 'KNN'])\nmachines = pd.DataFrame(machines)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:33:13.283258Z","iopub.execute_input":"2022-01-11T19:33:13.283512Z","iopub.status.idle":"2022-01-11T19:33:13.288136Z","shell.execute_reply.started":"2022-01-11T19:33:13.283484Z","shell.execute_reply":"2022-01-11T19:33:13.287205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = np.concatenate((scores, machines), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:33:14.902258Z","iopub.execute_input":"2022-01-11T19:33:14.902869Z","iopub.status.idle":"2022-01-11T19:33:14.907655Z","shell.execute_reply.started":"2022-01-11T19:33:14.902827Z","shell.execute_reply":"2022-01-11T19:33:14.906386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(final, columns={'Score': 0, 'Algorithm':1})","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:36:20.532729Z","iopub.execute_input":"2022-01-11T19:36:20.533387Z","iopub.status.idle":"2022-01-11T19:36:20.539747Z","shell.execute_reply.started":"2022-01-11T19:36:20.533334Z","shell.execute_reply":"2022-01-11T19:36:20.538806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = result.set_index('Algorithm')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:37:58.714266Z","iopub.execute_input":"2022-01-11T19:37:58.714544Z","iopub.status.idle":"2022-01-11T19:37:58.720592Z","shell.execute_reply.started":"2022-01-11T19:37:58.714492Z","shell.execute_reply":"2022-01-11T19:37:58.719748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Final Result","metadata":{}},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2022-01-11T19:38:01.106778Z","iopub.execute_input":"2022-01-11T19:38:01.107105Z","iopub.status.idle":"2022-01-11T19:38:01.122844Z","shell.execute_reply.started":"2022-01-11T19:38:01.107068Z","shell.execute_reply":"2022-01-11T19:38:01.121669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}