{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6f7d6ce8-709d-3897-4591-9b449611bb4e"},"outputs":[],"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)\nimport pandas as pd\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n%matplotlib inline\n\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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"51bd503f-1129-0143-a986-53333b0fc5d1"},"outputs":[],"source":"labeled_images = pd.read_csv(\"../input/train.csv\")\nlabels = labeled_images.iloc[0:5000,:1]    #iloc works on the positions in the index (so it only takes integers).\nimages = labeled_images.iloc[0:5000,1:]\n#train_test_split from scikit-learn\ntrain_images, test_images, train_labels, test_labels = train_test_split(images,labels,train_size=0.8,random_state=0)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a04700d7-187b-d2e6-7a37-b4ef0d00c159"},"outputs":[],"source":"#reshape pixels to images\ni = 1\nimg = train_images.iloc[i].as_matrix()    #pandas.dataframe.as_matrix()\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')        \n#colormapcolormap 是用于控制曲面图的颜色。\n#例如: colormap(gray) 输出一个灰色系的曲面图\nplt.title(train_labels.iloc[i,0])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"26974735-e368-eaae-f8d4-ef865bbf9c0e"},"outputs":[],"source":"plt.hist(train_images.iloc[1])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d37b3ec7-cd68-3c4e-7e09-6693278d6854"},"outputs":[],"source":"clf = svm.SVC()\nclf.fit(train_images, train_labels.values.ravel())   #numpy.ravel Return a contiguous flattened array.\nclf.score(test_images,test_labels)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d529806b-9be6-bcf5-8336-59bbc6689d42"},"outputs":[],"source":"test_images[test_images>0] = 1\ntrain_images[train_images>0] = 1\nimg = train_images.iloc[i].as_matrix().reshape((28,28))    #这里用iloc和loc结果不同！\nplt.imshow(img,cmap='binary')\nplt.title(train_labels[i])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"08bd5dfa-022c-f744-307e-e6ec5a1b185c"},"outputs":[],"source":"plt.hist(train_images.iloc[i])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8b6160fd-6b45-2de4-5693-ac27d7e8c425"},"outputs":[],"source":"clf = svm.SVC()\nclf.fit(train_images,train_labels.values.ravel())\nclf.score(test_images,test_labels)\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"391a200d-974c-443a-d91c-815f5f00ebc4"},"outputs":[],"source":"test_data=pd.read_csv('../input/test.csv')\ntest_data[test_data>0]=1\nresults=clf.predict(test_data[0:5000])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c8f5a66c-0e56-8210-9cae-9f45a6b99174"},"outputs":[],"source":"results"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"896854c0-5186-9b9c-b1ef-ddba0ea1449b"},"outputs":[],"source":"df = pd.DataFrame(results)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('results.csv',header=True)\n"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}