{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":false,"collapsed":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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"trusted":false},"cell_type":"code","source":"import numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e6043fc99a2873e175fb9d0d087bc63392a5e8c8","_cell_guid":"d944141a-94b8-4caa-b87e-929a56f3912b","collapsed":true,"trusted":false},"cell_type":"code","source":"%matplotlib inline\nimport matplotlib\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"40f091e86d5178d20bf1230939fa787a54e2d2fc","_cell_guid":"1e3e7e93-61cb-4fae-9528-603b7131572d","collapsed":true,"trusted":false},"cell_type":"code","source":"path = '../input/train.csv'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"061318067802c8a44b810f652d0cea8a430475fc","_cell_guid":"53a41ff7-a0c5-4cd9-a62f-bce6fa77c951","collapsed":true,"trusted":false},"cell_type":"code","source":"df = pd.read_csv(path)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5ea13a6ac7b2bdb73c89ab13f8297528af0ff5fa","_cell_guid":"ef87e129-beab-486c-b239-995b27815492","collapsed":true,"trusted":false},"cell_type":"code","source":"data = df[df.columns[1:]]\ntarget = df['label']\nmnist = {'data':data.values, 'target': target.values}","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3744be0dc1962b284c72ba7af36d7062b2e251ff","_cell_guid":"a0783a92-c725-4c86-b88c-a6232e535580","collapsed":true,"trusted":false},"cell_type":"code","source":"X, y = mnist['data'], mnist['target']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c4b8349355f17530f6092534b830859117899048","_cell_guid":"0439e4c2-6bbe-47ba-8a9d-f3738ebd8bf6","collapsed":true,"trusted":false},"cell_type":"code","source":"def plot_digit(data):\n    image = data.reshape(28, 28)\n    plt.imshow(image, cmap=matplotlib.cm.binary,\n               interpolation='nearest')\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"807d4e781c2dac1d13931cab25ef0c5ceb923625","_cell_guid":"268d4762-edc9-4226-b24f-146d371a7359","trusted":false,"collapsed":true},"cell_type":"code","source":"index = 41000\nsome_digit = X[index]\nprint(y[index])\nplot_digit(some_digit)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b2af688b4c4ef01cf0beac3a68be6b6988db9fe","_cell_guid":"a8894778-3001-4717-a8f5-edc83ce8912c","collapsed":true,"trusted":false},"cell_type":"code","source":"X_train, X_test, y_train, y_test = X[:21000], X[21000:], y[:21000], y[21000:]\nshuffle_index = np.random.permutation(21000)\nX_train, y_train = X_train[shuffle_index], y_train[shuffle_index]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"38762ca165f3cbe971dd5b148911b5f0210161d3","_cell_guid":"17d3a052-8046-4ffc-9a80-c394f84c2137","collapsed":true,"trusted":false},"cell_type":"code","source":"from sklearn.svm import SVC\nclf = SVC(kernel='poly', degree=2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"abea2b5771ace2c7c9dc80f4eef6e9b6318937af","_cell_guid":"784dda4a-7038-4e6a-82b4-542d06364fa4","trusted":false,"collapsed":true},"cell_type":"code","source":"clf.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"da7db2ef1b759e3ba72543eeca6a49fceef397e7","_cell_guid":"215a6da4-41c0-4b0a-8371-cb1c8aac9047","collapsed":true,"trusted":false},"cell_type":"code","source":"pred = clf.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5a2e171aa09f9a2fa675d27cae66a3c582f9ee1d","_cell_guid":"2d2c5dbd-d4d3-4c63-99ec-1d3c51437a82","trusted":false,"collapsed":true},"cell_type":"code","source":"print(pred)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d857ed943e7b36d6ece3595b0cad25769dd1172e","_cell_guid":"e25c0794-5ba9-4a67-9521-b7c8e7cb5e35","trusted":false,"collapsed":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nacc = accuracy_score(pred, y_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1917c8948eb703cc76c7b42c622fcf20d2b81c74","_cell_guid":"19ed9ac2-ff44-4a0d-95fd-393e2befa7ae","trusted":false,"collapsed":true},"cell_type":"code","source":"print(acc)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0f118a5a4aecd64b6e923f7ce7c4c7db0175c571","_cell_guid":"0b496b49-bb7f-4c14-ba1b-7d7e5cb74cb3","trusted":false,"collapsed":true},"cell_type":"code","source":"clf.score(X_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1e2180bb50e0156e609030c2c5cc237f776e54f5","_cell_guid":"4a421d6d-3503-4bd4-baee-084d3a9bba6f","trusted":false,"collapsed":true},"cell_type":"code","source":"test_data = pd.read_csv('../input/test.csv')\nX_test = test_data.values\ny_pred_test = clf.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"adce6238a1508e7a9aca81644d613c512333decf","_cell_guid":"d3c24470-b4d0-46b3-8914-cb3e58678f5f","collapsed":true,"trusted":false},"cell_type":"code","source":"result_df = pd.DataFrame(data=y_pred_test, index=np.arange(1, 28001))\nresult_df.index.name = 'ImageId'\nresult_df.columns = ['Label']\nresult_df.to_csv(path_or_buf='output.csv')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}