{"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_minor":4,"nbformat":4,"cells":[{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.colors as colors\nimport seaborn as sns\nfrom sklearn.utils import resample # This is used for downsample the dataset\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import GridSearchCV # This is used for hyperparameter tuning (Cross Validation)\nfrom sklearn.svm import SVC\nfrom sklearn.preprocessing import scale # This is used for scaling and centering the data\nfrom sklearn.metrics import confusion_matrix, classification_report, accuracy_score, plot_confusion_matrix, plot_precision_recall_curve, plot_roc_curve\nfrom sklearn.decomposition import PCA # This is used for dimensionality reduction\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_bar_chart(data, title, labels):\n    plt.figure(figsize=(10, 6))\n    plt.bar(np.arange(10), data, color=plt.cm.tab10(np.arange(10)))\n    plt.xlabel('Digits')\n    plt.ylabel('Count')\n    plt.xticks(np.arange(10), labels)\n    plt.title(title)\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar_chart(train['label'].value_counts().sort_index(), 'Distribution of the data', train['label'].value_counts().sort_index().index)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'].value_counts().sort_index(ascending=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Downsampling the data to 1000 samples in each class\ndef downsample_data(data, n_samples):\n    data_downsampled = pd.DataFrame()\n    for i in range(10):\n        data_downsampled = pd.concat([data_downsampled, data[data['label'] == i].sample(n_samples)], axis=0)\n    return data_downsampled\n\ntrain_downsampled = downsample_data(train, 1000)\nplot_bar_chart(train_downsampled['label'].value_counts().sort_index(), 'Distribution of the data after downsampling', train_downsampled['label'].value_counts().sort_index().index)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_downsampled['label'].value_counts().sort_index()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_downsampled.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_downsampled = train_downsampled.sample(frac=1).reset_index(drop=True)\ntrain_downsampled.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(data, labels, title):\n    plt.figure(figsize=(10, 6))\n    for i in range(10):\n        plt.subplot(2, 5, i+1)\n        plt.imshow(data[i].reshape(28, 28), cmap='gray')\n        plt.title(labels[i])\n        plt.axis('on')\n    plt.suptitle(title)\n    plt.show()\n\nplot_images(train_downsampled.drop('label', axis=1).values, train_downsampled['label'].values, 'First 10 images in the training data set')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_downsampled.drop('label', axis=1), train_downsampled['label'], test_size=0.2, random_state=42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = scale(X_train)\nX_val = scale(X_val)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Best Parameters:  {'C': 10, 'gamma': 0.001, 'kernel': 'rbf'} Used GridSearchCV","metadata":{}},{"cell_type":"code","source":"clfr = SVC(C=10, gamma=0.001, kernel='rbf', random_state=42)\nclfr.fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clfr.predict(X_val)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(data, title, labels):\n    plt.figure(figsize=(10, 6))\n    sns.heatmap(data, annot=True, cmap=plt.cm.magma, fmt='d', xticklabels=labels, yticklabels=labels)\n    plt.xlabel('Predicted')\n    plt.ylabel('Actual')\n    plt.title(title)\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(confusion_matrix(y_val, y_pred), 'Confusion Matrix', np.arange(10))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, y_pred))\nprint('New Accuracy Score: ', accuracy_score(y_val, y_pred))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = scale(test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clfr.predict(test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test[15].reshape(28, 28), cmap='gray')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[15]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(test, y_pred, 'First 10 images in the test data set')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, len(y_pred)+1), 'Label': y_pred})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}