{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"dataset = pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56d0c2a200ff1d1eba6f9cb3fc4362cd9e6f3483"},"cell_type":"code","source":"dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56eb3262a10bdcdf4d165dc9bcea28531687e576"},"cell_type":"code","source":"X = dataset.iloc[:, dataset.columns !=\"label\"]\ny = dataset.iloc[:, dataset.columns == \"label\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdc5798be78e3e88f962a24e01334f9f8376e545"},"cell_type":"code","source":"y.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d853fe497413219a36018123739bc08360821893"},"cell_type":"code","source":"X = X / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8b41924a7071872579e46040fd045164a9397b5"},"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, test_size = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ca314355ffbcf4ed7bf5423e720fce7aa95bf40"},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc_X = StandardScaler()\nX_train = sc_X.fit_transform(X_train)\nX_test = sc_X.fit_transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb5f177f25e21bc81c8a25bae64b7773417b0009"},"cell_type":"code","source":"from sklearn import svm\nclassifier = svm.SVC(gamma='scale')\nclassifier.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86d0173c826ab09b3acce45776f0280b633d6159"},"cell_type":"code","source":"y_pred = classifier.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0def63cc91efb1efccb65656a45327843169ae9b"},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y_test, y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b457bdb7c292710db7c3b24aeb1fc0038417b5ef"},"cell_type":"code","source":"cm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb7d13f0eee81bf7573f5ee01737c2c1ed844ec1"},"cell_type":"code","source":"classifier.score(X_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8c1c7b7285eb7f60fa902bcb484f5185888a590"},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdc970639c83be8e30fdd63871bf984d724e8c68"},"cell_type":"code","source":"array = np.random.randint(10, size=6)\nfor i in array:\n    plt.imshow(X_test[i].reshape(28,28))\n    plt.xlabel(\"Prediction : {} / Real_value : {}\".format(y_pred[i], y_test.values[i]))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fce44a9fb32009b6bcb9e75537295ca308df1134"},"cell_type":"code","source":"errors = []\nfor i in range(0, len(y_test)):\n    if y_test.values[i] - y_pred[i] != 0:\n        errors += [i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b23b416eace74ae1c886e04e6f902cfb3f2adda"},"cell_type":"code","source":"for i in errors:\n    plt.imshow(X_test[i].reshape(28,28))\n    plt.xlabel(\"Prediction : {} / Real_value : {}\".format(y_pred[i], y_test.values[i]))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fc0951ed38ba2b31dbdb3e305bcd6740d4ab7d5"},"cell_type":"code","source":"test = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c368cc28e53b979e3cc28fcf028ef8157e8c5bc0"},"cell_type":"code","source":"test = test / 255.0\ntest = sc_X.fit_transform(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"901202c789397aa4df28a27085d1c4ecf73e12d7"},"cell_type":"code","source":"prediction = classifier.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11313eebf086834ecb7036d3f98d9ae20c11daad"},"cell_type":"code","source":"ImageId = pd.Series(np.arange(0, len(prediction)), name=\"ImageId\")\nsubmission = pd.DataFrame(data = [ImageId, prediction]).transpose()\nsubmission = submission.rename(columns = {\"Unnamed 0\":\"Label\"})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67edff97bf70226029c58aae62a49c213ce28511"},"cell_type":"code","source":"submission.to_csv(\"submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c87751f2729e2e59e70903080317ae799c6b614"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}