{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Handwritten digit recognition using SVM¶\n"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# import all necessary libraries\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\n\nfrom random import randint\nfrom sklearn.preprocessing import scale\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.metrics import confusion_matrix\n\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Train data "},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(\"Train data shape : \",train_data.shape)\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data understanding and exploration\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Train data shape:\",train_data.shape)\nprint(\"Test data shape:\",test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Checking unique label"},{"metadata":{"trusted":true},"cell_type":"code","source":"np.unique(train_data.label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data['label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing"},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(train_data['label'],palette='icefire')\nplt.title('Number of digit in data')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"three = train_data.iloc[9, 1:].values.reshape(28,28)\nplt.imshow(three, cmap='plasma')\nplt.title('Digit 3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one = train_data.iloc[15, 1:].values.reshape(28,28)\nplt.imshow(one, cmap='plasma')\nplt.title('Digit 1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"zero = train_data.iloc[4, 1:].values.reshape(28,28)\nplt.imshow(zero, cmap='plasma')\nplt.title('Digit 0')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Heat map"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,15))\nsns.heatmap(data=train_data.corr(),annot=False,cmap='rainbow')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Almost all nearby pixel values are correlated\n"},{"metadata":{},"cell_type":"markdown","source":"## Examine few pixels¶\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(5,5))\nsns.distplot(train_data['pixel578'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.barplot(x='label', y='pixel578', data=train_data)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data creating"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = train_data.drop(\"label\",axis = 1)\ny = train_data[\"label\"]\nx","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Normalization\n\nx = x/255.0\ntest_data = test_data/255.0\n\nprint(\"x:\", x.shape)\nprint(\"test_data:\", test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"****train test split****"},{"metadata":{"trusted":true},"cell_type":"code","source":"x_scaled = scale(x)\n\n# train test split\nx_train, x_test, y_train, y_test = train_test_split(x_scaled, y, train_size = 0.8, random_state = 100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('X_train shape:',x_train.shape)\nprint('y_train shape:',y_train.shape)\nprint('X_test shape:',x_test.shape)\nprint('y_test shape:',y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model Building\n> 1. **first try - Linear model:**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn import svm\n# linear model\nmodel_linear = svm.SVC(kernel='linear')\nmodel_linear.fit(x_train, y_train)\n\n# predict\ny_pred = model_linear.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# confusion matrix and accuracy, precision, recall"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# accuracy\nprint(\"accuracy:\", metrics.accuracy_score(y_true=y_test, y_pred=y_pred), \"\\n\")\n\n# cm\nprint(metrics.confusion_matrix(y_true=y_test, y_pred=y_pred))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#precision, recall and f1-score\nscores=metrics.classification_report(y_test, y_pred, labels=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\nprint(scores)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**This linear model gives accuracy: 91.82 %**\nLet's check Non-lenear models"},{"metadata":{},"cell_type":"markdown","source":"## Non-Linear model"},{"metadata":{},"cell_type":"markdown","source":"2. Non-linear Poly kernal"},{"metadata":{"trusted":true},"cell_type":"code","source":"#non linear\nnon_linear_model_poly = svm.SVC(kernel='poly')\nnon_linear_model_poly.fit(x_train,y_train)\n# predict\ny_pred = non_linear_model_poly.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# accuracy\nprint(\"accuracy:\", metrics.accuracy_score(y_true=y_test, y_pred=y_pred), \"\\n\")\n\n# cm\nprint(metrics.confusion_matrix(y_true=y_test, y_pred=y_pred))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"3. Non-linear rbf kernal"},{"metadata":{"trusted":true},"cell_type":"code","source":"#non linear\nnon_linear_model_rbf = svm.SVC(kernel='rbf')\nnon_linear_model_rbf.fit(x_train,y_train)\n# predict\ny_pred = non_linear_model_rbf.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# accuracy\nprint(\"accuracy:\", metrics.accuracy_score(y_true=y_test, y_pred=y_pred), \"\\n\")\n\n# cm\nprint(metrics.confusion_matrix(y_true=y_test, y_pred=y_pred))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So the best test score is 0.9597619047619048 corresponding to Non-linear rbf kernal"},{"metadata":{},"cell_type":"markdown","source":"## Visualizing unseen dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = np.random.randint(1,y_pred.shape[0]+1,5)\n\nplt.figure(figsize=(16,4))\nfor i,j in enumerate(df):\n    plt.subplot(150+i+1)\n    d = x_test[j].reshape(28,28)\n    plt.title(f'Predicted Label: {y_pred[j]}')\n    plt.imshow(d)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Final model on test data(/kaggle/input/digit-recognizer/test.csv)"},{"metadata":{"trusted":true},"cell_type":"code","source":"#shape\nprint(\"Trian data shape: \",train_data.shape)\nprint(\"Test data shape: \",test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#scaling test data\nscaled_test = scale(test_data)\nscaled_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model predict using rbf \nimport time\nstart_time = time.time()\n\ntest_predict = non_linear_model_rbf.predict(scaled_test)\n\nscoretime = time.time() - start_time\nprint(\"Time consumed to predict model: \",time.strftime(\"%H:%M:%S\", time.gmtime(scoretime)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# visualize few of predicted test numbers"},{"metadata":{"trusted":true},"cell_type":"code","source":"# visualize few of predicted test numbers\n\ndf = np.random.randint(1,test_predict.shape[0]+1,5)\n\nplt.figure(figsize=(16,4))\nfor i,j in enumerate(df):\n    plt.subplot(150+i+1)\n    d = scaled_test[j].reshape(28,28)\n    plt.title(f'Predicted Label: {test_predict[j]}')\n    plt.imshow(d)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Exporting the predicted values "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Exporting the predicted values \ndata_to_submit = pd.DataFrame({\n    'ImageId':test_data.index.values+1,\n    'Label':test_predict\n})\ndata_to_submit.to_csv('my_submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","execution_count":null,"outputs":[]}],"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}