{"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndf = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ndf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save the label column in variable l\nl = df[\"label\"]\nl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = df.drop(\"label\",axis=1)\nd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(d.shape)\nprint(l.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# display or plot a number \nplt.figure(figsize=(10,10))\nidx=3502\n\ngrid_data = d.iloc[idx].as_matrix().reshape(28,28)\nplt.imshow(grid_data,interpolation=None,cmap=\"gray\")\n\nplt.show()\n\nprint(l[idx])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = df.drop(\"label\",axis=1)\nd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l = df[\"label\"]\nl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# As first our data should be standardized \n# \n\nfrom sklearn.preprocessing import StandardScaler\nstandardized_data = StandardScaler().fit_transform(d)\nstandardized_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Building a covariance matrix\nsample_data = standardized_data\nimport numpy as np\ncovar_matrix = np.matmul(sample_data.T,sample_data)\ncovar_matrix\nprint(\"Covariance matrix shape: \",covar_matrix.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.linalg import eigh\nvalues,vectors = eigh(covar_matrix,eigvals=(782,783))\n# print(values)\n# print(vectors)\nprint(vectors.shape)\nprint(values.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Storing transpose of vectors variable \n\nvectors = vectors.T\n#The shape of Transposed vector is \nprint(\"New shape is : \",vectors.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# sample_data.shape\n\n# creating new coordinates\nnew_cordinates = np.matmul(vectors,sample_data.T)\nnew_cordinates","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_cordinates.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nnew_cordinates = np.vstack((new_cordinates,l)).T\nnew_cordinates.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating a Dataframe\ndataframe = pd.DataFrame(new_cordinates,columns=(\"ist principal\",\"2nd principal\",\"labels\"))\ndataframe","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n# Visualize the data using seaborn\nsns.FacetGrid(dataframe,hue=\"labels\",size=10)\\\n.map(plt.scatter,\"ist principal\",\"2nd principal\")\\\n.add_legend()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"# ## Steps to be carried out are:\n# Step 1: split dataset into two parts one consisting of label column and second consisting of rest of the data\n# Step 2: As we know for pca first our data should be standardized therefore we will convert our data to column standardized\n# Step 3: After that we create a covariance matrix of our standardized_data i.e standardized_data^T*standardized_data\n# Step 4: Then we find eigen values and eigen vectors (As our covariance matrix is 784*784 dimensions therefore in total we will get 784 eigen values and 784 eigen vectors).The scipy eigh funcion provides this very easily and gives eigen values and vectors in ascending order as we are only interested in last two(highest) eigen values and eigen vectors therefore we pass eigvals as 782,783\n# Step 5: Taking traspose of this eigen vector so that dimention becomes (2,784)\n# Step 6: taking eigen_vector.standardized_data^T\n# step 7: creating a vstack with eigen_vector.standardized_data^T and labels\n# step 8: creating a dataframe of vstack with corresponding column names and visualizng data using seaborn\n# Step 9: checking the graph obtained","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## To directly do PCA by\nd = df.drop(\"label\",axis=1)\nd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l = df[\"label\"]\nl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Standardizing the data\nfrom sklearn.preprocessing import StandardScaler\nstandardized_data = StandardScaler().fit_transform(d)\nstandardized_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA()\npca.n_components = 2\npca_fitdata = pca.fit_transform(standardized_data)\npca_fitdata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pca_fitdata.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# data = np.vstack((pca_fitdata,l))\n# data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = pd.DataFrame(pca_fitdata,columns=(\"1st principal\",\"2nd principal\"))\na","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b = l.to_frame(name=\"label\")\nb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1 = pd.concat([a,b],axis=1)\ndf1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualizing the dataframe made using seaborn \nsns.FacetGrid(df1,hue=\"label\",size=10).map(plt.scatter,\"1st principal\",\"2nd principal\").add_legend();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Standardized \nfrom sklearn.preprocessing import StandardScaler\nstandardized_data = StandardScaler().fit_transform(d)\nsample_data = standardized_data\n# PCA for dimentionality reduction\nfrom sklearn.decomposition import PCA\npca = PCA()\npca.n_component = 784\npca_data = pca.fit_transform(sample_data)\n\npercentage_var_explained = pca.explained_variance_/np.sum(pca.explained_variance_)\n\ncumsum_percentage_var = np.cumsum(percentage_var_explained)\n\n# Plotting \n\nplt.figure(1,figsize=(10,10))\nplt.clf()\nplt.plot(cumsum_percentage_var,linewidth=2)\nplt.axis(\"tight\")\nplt.grid()\nplt.xlabel(\"n_component\")\nplt.ylabel(\"cumsum\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print(pca.explained_variance_)\n# print(\"???????????????????????????????????????????????\")\n# print(pca.explained_variance_ratio_)\n# print(\"/////////////////////////////////////////////////////////////\")\n# print(pca.explained_variance_ratio_.cumsum())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# t-SNE(t-disb Stochastic Neighborhood Embedding):-  "},{"metadata":{"trusted":true},"cell_type":"code","source":"df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = df.drop(\"label\",axis=1)\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label = df[\"label\"]\nlabel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### As in case of PCA can only be applied to standardized data similar to that t-SNE \nfrom sklearn.preprocessing import StandardScaler\nstandardized_data = StandardScaler().fit_transform(data)\nstandardized_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install sklearn.manifold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.manifold import TSNE\n\nmodel = TSNE(random_state=0)\nmodel.n_component=2\n### Configuring the parameters\n# the number of components = 2\n# the default perplexity= 30\n# default learning rate = 200\n# default maximum number of iterations for optimization = 1000\ntsne_data = model.fit_transform(standardized_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Converting label to dataframe\na = pd.DataFrame(tsne_data,columns=(\"1st dim\",\"2nd dim\"))\na","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b = label.to_frame(name=\"label\")\nb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1 = pd.concat([a,b],axis=1)\ndf1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualizing the dataframe made using seaborn \nsns.FacetGrid(df1,hue=\"label\",size=10).map(plt.scatter,\"1st dim\",\"2nd dim\").add_legend();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Tweaking the parameter of t-SNE\nfrom sklearn.manifold import TSNE\n\nmodel = TSNE(random_state=0,perplexity=50,n_iter=5000)\nmodel.n_component=2 #n_component basically means no of column we want in our t-SNE data to have\n### Configuring the parameters\n# the number of components = 2\n# the default perplexity= 30\n# default learning rate = 200\n# default maximum number of iterations for optimization = 1000\ntsne_data = model.fit_transform(standardized_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tsne_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Converting label to dataframe\na = pd.DataFrame(tsne_data,columns=(\"1st dim\",\"2nd dim\"))\na","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b = label.to_frame(name=\"label\")\nb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1 = pd.concat([a,b],axis=1)\ndf1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualizing the dataframe made using seaborn \nsns.FacetGrid(df1,hue=\"label\",size=10).map(plt.scatter,\"1st dim\",\"2nd dim\").add_legend();","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}