{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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)\nimport matplotlib.pyplot as plt #data visulization \nfrom sklearn import svm  # Support vector machine\nfrom sklearn.model_selection import train_test_split  # split the train data\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":"#loading data files\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\ntrain.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cf0ae381e900a477458e63c4e4bbabf526cbd6d","scrolled":true},"cell_type":"code","source":"#exploring data files\ntrain_image = train.iloc[0:5000,1:]\ntrain_label = train.iloc[0:5000,:1]\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"cf73eef5c5fc0189069e960c5e6ce1de5b676324"},"cell_type":"code","source":"#Since the image is currently one-dimension, we load it into a numpy array and reshape it so that it is two-dimensional (28x28 pixels)\ni=0\ntrain_img = train_image.iloc[i].as_matrix()\ntrain_img = train_img.reshape((28,28))\nplt.imshow(train_img, cmap = 'gray')\ntrain_label.iloc[i]\ntrain_img1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aaa759eb5e918f4fa0a270cde18a4a9e6dc26941"},"cell_type":"code","source":"#Split the training data into train & validation set\ntrain_images, val_images , train_labels, val_labels = train_test_split(train_image, train_label,train_size = 0.8, random_state =0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e610aeb5d79367819f359565b9c454099d72bcc"},"cell_type":"code","source":"#trying SVM \nclf = svm.SVC()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0605422e6df2ccea95dc8ca7b089334e0457ac77"},"cell_type":"code","source":"#fitting the modle\nclf.fit(train_images,train_labels.values.ravel())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98072228b05b6b31278665a3d35c873aeb0bf2d5"},"cell_type":"code","source":"#checking the accuracy for validation set\nclf.score(val_images, val_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd0cf9a65bdd6dce818b81ce13da16b1952d01a3"},"cell_type":"markdown","source":"**Pre-Porcessing **\n\n**Feature Standardization/regularization  ( It is used to centre the data around zero mean and unit variance)\n#1st: converting all the value grater than zero to one and rest all remain zero.\n#2nd: converting the value with standarization or divide the value by 255 as pixle value is varying from (0-255) to achive the regularization**[](http://)"},{"metadata":{"_kg_hide-input":false,"trusted":true,"_uuid":"c94c1c23f197a7aad95dff8aab72ac4aa6d9e307"},"cell_type":"code","source":"train_image/=255\ntest_SVM=test/255\ntrain_images/=255\nval_images/=255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97baf816c72d31f0d5352d22bac0cf08dbecc47f"},"cell_type":"code","source":"clf.fit(train_images,train_labels.values.ravel())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4874b019252dda6575b9cd70e0593f6c3c579edd"},"cell_type":"code","source":"clf.score(val_images,val_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f62d4d2fff76a72f2a4c4498003841103fbde886"},"cell_type":"code","source":"results_SVM=clf.predict(test_SVM[0:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92b61869470e328613af8bc3300c1601e000dcb0"},"cell_type":"code","source":"#convert the result into desierd file(.csv)\ndf = pd.DataFrame(results_SVM)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('../results_SVM.csv', header=True)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdd2f2f7c9a908d6a083796d27cf302b022757d4"},"cell_type":"code","source":"#Exploring orignal data files and converting them itno array\nx_train = np.array(train.iloc[:,1:])\ny_train = np.array(train.iloc[:,:1])\nx_test = np.array(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60331b3da81a0df023fd4224908cd06409f627d8"},"cell_type":"code","source":"n_features_train = x_train.shape[1]        #[1]represent col\nn_samples_train = x_train.shape[0]        #[0]represent row\nn_features_test = x_test.shape[1]\nn_samples_test = x_test.shape[0]\nprint(n_features_train, n_samples_train, n_features_test, n_samples_test)\nprint(x_train.shape, y_train.shape, x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd8c859b1184cf9a32ce879f8a803b2d6d782b4e"},"cell_type":"code","source":"# show the image\ndef show_img(X):\n    plt.figure(figsize=(8,7))\n    n_samples = X.shape[0]\n    X = X.reshape(n_samples, 28, 28)\n    for i in range(20):\n        plt.subplot(5, 4, i+1)\n        plt.imshow(X[i])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac9ae9c8de0d1bbe780d2e8d700d0189632ec37d"},"cell_type":"code","source":"show_img(x_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"258a09cd9abc67423c9868d6cbe69235893f8bd4"},"cell_type":"code","source":"show_img(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1d813a96c1bdc47f3b4c33b141ddc8d99e92cc2"},"cell_type":"code","source":"","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}