{"cells":[{"metadata":{"collapsed":true,"_cell_guid":"caa944fc-6883-4b58-9dff-f4ce9d709079","_uuid":"223237113a670ca88395429d8dea5d711a20417f","trusted":false},"cell_type":"code","source":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\n\nfrom sklearn import svm\n%matplotlib inline\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"41e14acb-55b5-4200-a6a2-94dcd4159259","_uuid":"a3761d4c93b5c3c41a9b03e02a4ed8ffee4eca17","trusted":false},"cell_type":"code","source":"train_df=pd.read_csv('../input/train.csv')\ntest_df=pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"66787a01-061f-4cce-897a-7e0ef500b7e8","_uuid":"5339ef72ad12f742330e06a56c81705e6057598a","trusted":false,"collapsed":true},"cell_type":"code","source":"# work only with 5000 images\nimages = train_df.iloc[0:5000,1:]\nlabels = train_df.iloc[0:5000,:1]\ntrain_images, test_images,train_labels, test_labels = train_test_split(images, labels, train_size=0.8, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b7975399-db61-4a59-b14d-aea04b1dda80","_uuid":"9b2e27bb2a402dee9b50cbe7ef058812e4cf11ee","trusted":false,"collapsed":true},"cell_type":"code","source":"i=1\nimg=train_images.iloc[2].as_matrix()\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train_labels.iloc[i,0])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b608549d-4f1a-415f-8198-58671648ae4b","_uuid":"37762b1009833d53798899df517f1cf3266d76f0","trusted":false,"collapsed":true},"cell_type":"code","source":"plt.hist(train_images.iloc[i])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3e74c634-987f-4923-8b5b-d8a89c90366e","_uuid":"6c309a7c92f98be8f548147f7ccdd1c264723550","trusted":false,"collapsed":true},"cell_type":"code","source":"clf = svm.SVC()\nclf.fit(train_images, train_labels.values.ravel())\nclf.score(test_images,test_labels)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"fda676d4-6597-43ab-b38d-dd70569dbb6e","_uuid":"f62dc2d6e18c698b2dca20782ef247e8d89b8793","trusted":false,"collapsed":true},"cell_type":"code","source":"test_images[test_images>0]=1\ntrain_images[train_images>0]=1\n\nimg=train_images.iloc[i].as_matrix().reshape((28,28))\nplt.imshow(img,cmap='binary')\nplt.title(train_labels.iloc[i])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1216b051-6c3c-4979-90bb-a31bc4a50308","_uuid":"c9f58364ef16aa7918a319bc42720dededc5a7bf","trusted":false,"collapsed":true},"cell_type":"code","source":"clf = svm.SVC()\nclf.fit(train_images, train_labels.values.ravel())\nclf.score(test_images,test_labels)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"8e7ec3a5-bde0-4e5c-820f-1e3446d90c75","_uuid":"76b5dbaa9b666221cc4f500b72b084b670717447","trusted":false},"cell_type":"code","source":"test_data=pd.read_csv('../input/test.csv')\ntest_data[test_data>0]=1\nresults=clf.predict(test_data[0:5000])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6fa4f1c7-699d-41d4-8773-9a14e550f04a","_uuid":"579383e07b2f8dbd9a2c81fa1fb5d5533ff254dd","trusted":true},"cell_type":"code","source":"df = pd.DataFrame(results)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('../input/sample_submission.csv', header=True)","execution_count":1,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"04f7fbdae75fd5bede44db7d5a86f3313d5ae610"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}