{"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\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 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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#import Libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nd0=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprint(d0.head())  # print first five rows of d0.\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 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)","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=(7,7))\nidx=500\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])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"#  2D Visualization using PCA ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"labels=l.copy()\ndata=d.copy()\n\nprint(\"The shape of the data\",data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#find the co-variance matrix which is : A^T * A\nsample_data=standardized_data\n\n# Matrix multiplication using numpy\ncovar_matrix=np.matmul(sample_data.T,sample_data)\n\nprint(\"The shape of the variance matrix = \",covar_matrix.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# finding the top two eigen-values and corresponding eigen-vectors \n# for projecting onto a 2-Dim space.\n\nfrom scipy.linalg import eigh\n\n# the parameter 'eigvals' is defined (low value to high value) \n# eigh function will return the eigen values in asending order\n# this code generates only the top 2 (782 and 783) eigenvalues.\nvalues,vectors=eigh(covar_matrix,eigvals=(782,783))\n\nprint(\"Shape of eigen vectors = \",vectors.shape)\n\n# converting the eigen vectors into (2,d) shape for easyness of further computations\nvectors=vectors.T\nprint(\"updated shape of eigen vectors =\",vectors.shape)\n\n# here the vectors[1] represent the eigen vector corresponding 1st principal eigen vector\n# here the vectors[0] represent the eigen vector corresponding 2nd principal eigen vector\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# projecting the original data sample on the plane \n#formed by two principal eigen vectors by vector-vector multiplication.\nimport matplotlib.pyplot as plt\nnew_coordinates=np.matmul(vectors,sample_data.T)\n\nprint (\" resultant new data points' shape \", vectors.shape, \"X\", sample_data.T.shape,\" = \", new_coordinates.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\n\n# appending label to the 2d projected data\nnew_coordinates=np.vstack((new_coordinates,labels)).T\n\n\n# creating a new data frame for ploting the labeled points.\ndataframe=pd.DataFrame(new_coordinates,columns=(\"1st_principle\",\"2nd_principle\",\"label\"))\nprint(dataframe.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ploting the 2d data points with seaborn\nimport seaborn as sns\nsns.FacetGrid(dataframe,hue=\"label\",size=8).map(plt.scatter,'1st_principle','2nd_principle').add_legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# PCA using Scikit-Learn","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Initializing the pca\nfrom sklearn import decomposition\npca=decomposition.PCA()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# configuring the parameteres\n# the number of components = 2\npca.n_components=2\npca_data=pca.fit_transform(sample_data)\n\n# pca_reduced will contain the 2-d projects of simple data\nprint(\"shape of pca_reduced data= \",pca_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# attaching the label for each 2-d data point \npca_data = np.vstack((pca_data.T, labels)).T\n\n# creating a new data fram which help us in ploting the result data\npca_df = pd.DataFrame(data=pca_data, columns=(\"1st_principal\", \"2nd_principal\", \"label\"))\nsns.FacetGrid(pca_df, hue=\"label\", size=6).map(plt.scatter, '1st_principal', '2nd_principal').add_legend()\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":4}