{"cells":[{"metadata":{"_uuid":"77cfd4acd453631e27fe1858e518b20cb0ef9e78"},"cell_type":"markdown","source":"### Just a kernel to learn how to submit data\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\n\nfrom sklearn import svm\nfrom sklearn.model_selection import train_test_split\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":"#importing the data to pandas dataframes\ntrain_digits = pd.read_csv('../input/train.csv')\ntest_images = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"6a8edf3565dcf9a08e2aa716459001e626ab2e0e"},"cell_type":"code","source":"#splitting the data (keeping the data to 2000 observations to save time)\ntrain_labels = train_digits.iloc[0:2000,0]\ntrain_images = train_digits.iloc[0:2000,1:]\ntrain_X, test_X, train_y, test_y = train_test_split(train_images, train_labels,\n                                                    test_size=0.33, random_state=42)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5af23b9eccb002d0547c0842ba904bc18e921c8"},"cell_type":"code","source":"#I'm going to take a quick look at one of the rows of the dataframe. \n#Each row of the matrix is an image that is 28x28 pixels, so we should have 784 columns. \n#if we reshape the an observation, one row of data, into a 28x28 matrix we can see image.\n#here is a quick function to print the image correcty\ndef display(image):\n    single_image = image.reshape((28,28))\n    plt.imshow(single_image, cmap=cm.Pastel1_r)\n    \ndisplay(train_images.iloc[10].values)\n\n#Note that these images aren't actually black and white (0,1). They are gray-scale (0-255). You can \n#see this clearly with the funky pastels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"566c512b793d1ea5990da11946ab536bf0b9c487"},"cell_type":"code","source":"#Let's standardize the greyscale between 1 and 0. \ntrain_X = train_X.apply(lambda x : x*(1/255))\ntest_X = test_X.apply(lambda x : x*(1/255))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f952e96bc6964a7f14c9d7b7f22902b008c8449"},"cell_type":"code","source":"#running just a simple support vector machine (SVM) classifier on the data. \n\nclf = svm.SVC()\nclf.fit(train_X, train_y.values.ravel())\nclf.score(test_X,test_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa3da02631a394baa360a70d4d2e040d97d49806"},"cell_type":"code","source":"test_images = test_images.apply(lambda x : x*(1/255))\nresults = clf.predict(test_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59f910a1fdaa5e1ded2231b8a96520c88041f9e0"},"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":{"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}