{"cells":[{"metadata":{},"cell_type":"markdown","source":"- T-SNE code from this kernel https://www.kaggle.com/carlolepelaars/97-on-mnist-with-a-single-decision-tree-t-sne \n\n- If you want to get better score on leaderboard, I would recommend you to study https://www.kaggle.com/c/digit-recognizer this competition first! \n- Same model can get better score on kannada-mnist dataset than digit-recognizer dataset"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom collections import Counter\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nplt.rcParams['figure.dpi'] = 300\nfrom IPython.display import Image \nfrom sklearn.manifold import TSNE\nfrom sklearn.metrics import accuracy_score, confusion_matrix\nfrom sklearn.externals.six import StringIO  \nfrom sklearn.decomposition import TruncatedSVD\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier, export_graphviz\n\n\nimport itertools\nimport seaborn as sns\nimport os\nprint(os.listdir('../input/Kannada-MNIST'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest_df = pd.read_csv('../input/Kannada-MNIST/test.csv')\ndig_df = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_df.shape)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test_df.drop(['id'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(train_df['label'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nx_train = (train_df.iloc[:,1:].values).astype('float32')\ny_train = train_df.iloc[:,0].values.astype('int32')\nx_test = test_df.values.astype('float32')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,8))\n\nx, y = 5, 4\nfor i in range(20):  \n    plt.subplot(y, x, i+1)\n    plt.title(str(y_train[i]))\n    plt.axis('off')\n    plt.imshow(x_train[i].reshape((28,28)),interpolation='nearest')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,8))\n\nx, y = 5, 4\nfor i in range(20):  \n    plt.subplot(y, x, i+1)\n    #plt.title(str(y_train[i]))\n    plt.axis('off')\n    plt.imshow(x_test[i].reshape((28,28)),interpolation='nearest')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"concat_df = pd.concat([train_df, test_df])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = [col for col in train_df.columns if col.startswith('pixel')]\nX_train, X_val, y_train, y_val = train_test_split(train_df[features], train_df['label'], test_size=0.25, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def acc(y_true, y_pred):\n    return round(accuracy_score(y_true, y_pred) * 100, 2)\n\nclf = DecisionTreeClassifier(max_depth=10, random_state=42)\nclf.fit(X_train, y_train)\n\ntrain_preds_baseline = clf.predict(X_train)\nval_preds_baseline = clf.predict(X_val)\nacc_baseline_train = acc(train_preds_baseline, y_train)\nacc_baseline_val = acc(val_preds_baseline, y_val)\n\nprint(f'Training accuracy for our baseline (using all pixel features): {acc_baseline_train}%')\nprint(f'Validation accuracy for our baseline (using all pixel features): {acc_baseline_val}%')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsvd = TruncatedSVD(n_components=50).fit_transform(concat_df[features])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsvd_cols = [f'component_{i+1}' for i in range(50)]\ntsvd_train = pd.DataFrame(tsvd[:len(train_df)], columns=[tsvd_cols])\ntsvd_test = pd.DataFrame(tsvd[len(train_df):], columns=[tsvd_cols])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(tsvd_train, \n                                                  train_df['label'], \n                                                  test_size=0.25, \n                                                  random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Train model with t-svd features\nclf = DecisionTreeClassifier(max_depth=10, random_state=42)\nclf.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_preds = clf.predict(X_train)\nval_preds = clf.predict(X_val)\nacc_tsvd_train = acc(train_preds, y_train)\nacc_tsvd_val = acc(val_preds, y_val)\nprint(f'Training accuracy with TSVD features (50 components): {acc_tsvd_train}%')\nprint(f'Validation accuracy with TSVD features (50 components): {acc_tsvd_val}%')\n# Check out how it performed compared to the baseline\nacc_diff = round(acc_tsvd_val - acc_baseline_val, 2)\nprint(f'\\nThis is a difference of {acc_diff}% in validation accuracy compared to the baseline.')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## T_SNE"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\ntsne = TSNE()\ntransformed = tsne.fit_transform(tsvd)  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_train = pd.DataFrame(transformed[:len(train_df)], columns=['component1', 'component2'])\ntsne_test = pd.DataFrame(transformed[len(train_df):], columns=['component1', 'component2'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(14, 14))\nplt.title(f\"Visualization of t-SNE results on the MNIST Dataset\\n\\\nAmount of datapoints = {len(tsne_train)}\", fontsize=20, weight='bold')\nsns.scatterplot(\"component1\", \"component2\", \n                data=tsne_train, hue=train_df['label'], \n                palette=\"Set1\", legend=\"full\", alpha=0.6)\n\nplt.xticks(fontsize=14)\nplt.yticks(fontsize=14)\nplt.xlabel(\"Component 1\", fontsize=16)\nplt.ylabel(\"Component 2\", fontsize=16)\nplt.legend(fontsize=16)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":1}