{"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":"markdown","source":"# t-SNE using Scikit-Learn","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as  np\nimport matplotlib.pyplot as plt\nfrom sklearn.manifold import TSNE\n\nd0 = pd.read_csv('../input/digit-recognizer/train.csv')\n\nprint(d0.head(5)) # print first five rows of d0.\n\n# save the labels into a variable l.\nl = d0['label']\n\n# Drop the label feature and store the pixel data in d.\nd = d0.drop(\"label\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:35:55.063306Z","iopub.execute_input":"2022-04-18T09:35:55.063657Z","iopub.status.idle":"2022-04-18T09:35:58.320161Z","shell.execute_reply.started":"2022-04-18T09:35:55.063617Z","shell.execute_reply":"2022-04-18T09:35:58.318964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(d.shape)\nprint(l.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:35:58.322443Z","iopub.execute_input":"2022-04-18T09:35:58.322833Z","iopub.status.idle":"2022-04-18T09:35:58.329476Z","shell.execute_reply.started":"2022-04-18T09:35:58.322782Z","shell.execute_reply":"2022-04-18T09:35:58.328372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display or plot a number.\nplt.figure(figsize=(7,7))\nidx = 1\n\ngrid_data = d.iloc[idx].to_numpy().reshape(28,28)  # reshape from 1d to 2d pixel array\nplt.imshow(grid_data, interpolation = \"none\", cmap = \"gray\")\nplt.show()\n\nprint(l[idx])","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:35:58.330832Z","iopub.execute_input":"2022-04-18T09:35:58.331103Z","iopub.status.idle":"2022-04-18T09:35:58.507095Z","shell.execute_reply.started":"2022-04-18T09:35:58.331074Z","shell.execute_reply":"2022-04-18T09:35:58.506156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Pick first 15k data-points to work for time-efficiency\n\nlabels = l.head(15000)\ndata = d.head(15000)\n\nprint(\"The shape of sample data = \", data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:35:58.509767Z","iopub.execute_input":"2022-04-18T09:35:58.510468Z","iopub.status.idle":"2022-04-18T09:35:58.519410Z","shell.execute_reply.started":"2022-04-18T09:35:58.510420Z","shell.execute_reply":"2022-04-18T09:35:58.518340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data-preprocessing: Standardizing the data\n\nfrom sklearn.preprocessing import StandardScaler\nstandardized_data  = StandardScaler().fit_transform(data)\nprint(standardized_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:35:58.521029Z","iopub.execute_input":"2022-04-18T09:35:58.521282Z","iopub.status.idle":"2022-04-18T09:35:58.841126Z","shell.execute_reply.started":"2022-04-18T09:35:58.521251Z","shell.execute_reply":"2022-04-18T09:35:58.839946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\ndata_1000 = standardized_data[0:1000,:]\nlabels_1000 = labels[0:1000]\n\nmodel = TSNE(n_components=2, random_state=0)\n\ntsne_data = model.fit_transform(data_1000)\n\ntsne_data = np.vstack((tsne_data.T, labels_1000)).T\ntsne_df = pd.DataFrame(data = tsne_data, columns=(\"Dim_1\", \"Dim_2\", \"label\"))\n\n#Plotting the results\nsns.FacetGrid(tsne_df, hue='label', height=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:43:23.653257Z","iopub.execute_input":"2022-04-18T09:43:23.653599Z","iopub.status.idle":"2022-04-18T09:43:29.673580Z","shell.execute_reply.started":"2022-04-18T09:43:23.653562Z","shell.execute_reply":"2022-04-18T09:43:29.672723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Changing the perplexity to 50\n\nmodel = TSNE(n_components=2, random_state=0, perplexity=50)\n\ntsne_data = model.fit_transform(data_1000)\n\ntsne_data = np.vstack((tsne_data.T, labels_1000)).T\ntsne_df = pd.DataFrame(data = tsne_data, columns=(\"Dim_1\", \"Dim_2\", \"label\"))\n\n#Plotting the results\nsns.FacetGrid(tsne_df, hue='label', height=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend()\nplt.title(\"With Perplexity 50\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:43:29.675241Z","iopub.execute_input":"2022-04-18T09:43:29.675480Z","iopub.status.idle":"2022-04-18T09:43:36.293688Z","shell.execute_reply.started":"2022-04-18T09:43:29.675452Z","shell.execute_reply":"2022-04-18T09:43:36.292859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Changing the perplexity to 50\n#change Iterations to 5000\n\nmodel = TSNE(n_components=2, random_state=0, perplexity=50, n_iter=5000)\n\ntsne_data = model.fit_transform(data_1000)\n\ntsne_data = np.vstack((tsne_data.T, labels_1000)).T\ntsne_df = pd.DataFrame(data = tsne_data, columns=(\"Dim_1\", \"Dim_2\", \"label\"))\n\n#Plotting the results\nsns.FacetGrid(tsne_df, hue='label', height=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend()\nplt.title(\"With Perplexity 50\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:44:14.926140Z","iopub.execute_input":"2022-04-18T09:44:14.926487Z","iopub.status.idle":"2022-04-18T09:44:37.637466Z","shell.execute_reply.started":"2022-04-18T09:44:14.926454Z","shell.execute_reply":"2022-04-18T09:44:37.636704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Changing the perplexity to 2 which will not work well and give the random viz.\n\nmodel = TSNE(n_components=2, random_state=0, perplexity=2)\n\ntsne_data = model.fit_transform(data_1000)\n\ntsne_data = np.vstack((tsne_data.T, labels_1000)).T\ntsne_df = pd.DataFrame(data = tsne_data, columns=(\"Dim_1\", \"Dim_2\", \"label\"))\n\n#Plotting the results\nsns.FacetGrid(tsne_df, hue='label', height=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend()\nplt.title(\"With Perplexity 50\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:45:14.595789Z","iopub.execute_input":"2022-04-18T09:45:14.596173Z","iopub.status.idle":"2022-04-18T09:45:19.141150Z","shell.execute_reply.started":"2022-04-18T09:45:14.596133Z","shell.execute_reply":"2022-04-18T09:45:19.140191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#End of Notebook","metadata":{"execution":{"iopub.status.busy":"2022-04-18T09:45:43.831567Z","iopub.execute_input":"2022-04-18T09:45:43.831936Z","iopub.status.idle":"2022-04-18T09:45:43.836638Z","shell.execute_reply.started":"2022-04-18T09:45:43.831887Z","shell.execute_reply":"2022-04-18T09:45:43.835711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}