{"cells":[{"metadata":{},"cell_type":"markdown","source":"This is a simple adoptation of the following MNIST notebook: https://www.kaggle.com/carlolepelaars/97-on-mnist-with-a-single-decision-tree-t-sne\nPlease upvote it if you find this notebook helpful."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Get the pydotplus package\n# !pip install pydotplus","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Standard Libraries\nimport os\nimport numpy as np \nimport pandas as pd \n\n# Visualization libraries\n#import pydotplus\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style({\"axes.facecolor\": \".95\"})\n\n# Modeling and Machine Learning\nfrom sklearn.manifold import TSNE\nfrom sklearn.metrics import accuracy_score\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\nfrom sklearn.linear_model import LogisticRegression\n\n\n# Specify Paths\nBASE_PATH = '../input/Kannada-MNIST/'\nTRAIN_PATH = BASE_PATH + 'train.csv'\nTEST_PATH = BASE_PATH + 'test.csv'\n\n# Seed for reproducability\nseed = 1234\nnp.random.seed(seed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# File sizes and specifications\nprint('\\n# Files and file sizes')\nfor file in os.listdir(BASE_PATH):\n    print('{}| {} MB'.format(file.ljust(30), \n                             str(round(os.path.getsize(BASE_PATH + file) / 1000000, 2))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load in training and testing data\ntrain_df = pd.read_csv(TRAIN_PATH)\ntest_df = pd.read_csv(TEST_PATH)\ntest_df.rename(columns={'id':'label'}, inplace=True)\nconcat_df = pd.concat([train_df, test_df])\nsample_sub = pd.read_csv(BASE_PATH + 'sample_submission.csv');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def acc(y_true, y_pred):\n    \"\"\"\n        Calculates the accuracy score between labels and predictions.\n        \n        :param y_true: The true labels of the data\n        :param y_pred: The predictions for the data\n        \n        :return: a float denoting the accuracy\n    \"\"\"\n    return round(accuracy_score(y_true, y_pred) * 100, 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = [col for col in train_df.columns if col.startswith('pixel')]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsvd = TruncatedSVD(n_components=50).fit_transform(concat_df[features])\ntsne = TSNE(n_components=3)\ntransformed = tsne.fit_transform(tsvd)  \n# Split up the t-SNE results in training and testing data\ntsne_train = pd.DataFrame(transformed[:len(train_df)], columns=['component1', 'component2', 'component3'])\ntsne_test = pd.DataFrame(transformed[len(train_df):], columns=['component1', 'component2', 'component3'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Perform another split for t-sne feature validation\nX_train, X_val, y_train, y_val = train_test_split(tsne_train, \n                                                  train_df['label'], \n                                                  test_size=0.25, \n                                                  random_state=seed)\n# Train model with t-sne features\nclf = DecisionTreeClassifier(max_depth=10, random_state=seed)\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_tsne_train = acc(train_preds, y_train)\nacc_tsne_val = acc(val_preds, y_val)\nprint(f'Training accuracy with t-SNE features: {acc_tsne_train}%')\nprint(f'Validation accuracy with t-SNE features: {acc_tsne_val}%')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Make predictions and save submission file\npredictions = clf.predict(tsne_test)\nsample_sub['label'] = predictions\nsample_sub.to_csv('submission.csv', index=False)","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}