{"cells":[{"metadata":{"_cell_guid":"d0abbfc5-e04e-98d3-8c5c-37d114daa804","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e8c0e206-f504-253f-8ac6-2c50eff06b58","trusted":true},"cell_type":"code","source":"labeled_images = pd.read_csv('../input/train.csv')\nimages = labeled_images.iloc[0:5000,1:]\nlabels = labeled_images.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_type":"markdown","source":"Read image data using pandas\n\n5000 images as X and labels as y for training set\n\nsplit data using sklearn.model_selection\n\n0.8 train set (0.2 test set)\n\nrandom_state gurantee always get the same split when running this code. Used for documentation or debug purpose. Useless in most of other cases."},{"metadata":{"_cell_guid":"dc43b65b-d3d9-8208-e7a2-88c2535b506f","trusted":true},"cell_type":"code","source":"img=train_images.iloc[1].to_numpy()\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train_labels.iloc[1,0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"View first test set in gray scale (0-255). Not black-white"},{"metadata":{"_cell_guid":"b6143ae2-eb4e-a4af-aad3-37fa85b657b7","trusted":true},"cell_type":"code","source":"plt.hist(train_images.iloc[1])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"histoagram of color scale"},{"metadata":{"_cell_guid":"e35c02d7-4510-3202-a7b4-fb3accde0c5a","trusted":true},"cell_type":"code","source":"Cset = [0.33, 1, 3, 9, 27]\nbestscore = 0\nfor i in Cset:\n    clf = svm.SVC(C=i)\n    clf.fit(train_images, train_labels.values.ravel())\n    score = clf.score(test_images,test_labels)\n    if score > bestscore:\n        bestscore = score\n        bestclf = clf\n    print(i, score, bestscore)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"train data with linear SVM with a couple of different C and test with test images (cross validation)"},{"metadata":{"_cell_guid":"8fa0b0aa-1bf3-dcd3-6ef2-fae5ccda55b3","trusted":true},"cell_type":"code","source":"test_data=pd.read_csv('../input/test.csv')\nresults=bestclf.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"aa6f3fd3-8ff6-80b4-68ae-261172ebe580","trusted":true},"cell_type":"code","source":"results","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"28ec8d56-d3f6-573d-2230-bc5a410868c6","trusted":true},"cell_type":"code","source":"df = pd.DataFrame(results)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('results.csv', header=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Output to a .csv file"}],"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":4,"nbformat_minor":4}