{"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":"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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-29T09:10:54.851213Z","iopub.execute_input":"2022-07-29T09:10:54.851649Z","iopub.status.idle":"2022-07-29T09:10:54.860702Z","shell.execute_reply.started":"2022-07-29T09:10:54.851616Z","shell.execute_reply":"2022-07-29T09:10:54.859445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabular_data=pd.read_csv(\"/kaggle/input/tabular-playground-series-jul-2022/data.csv\")\ntabular_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:54.904900Z","iopub.execute_input":"2022-07-29T09:10:54.905312Z","iopub.status.idle":"2022-07-29T09:10:55.688808Z","shell.execute_reply.started":"2022-07-29T09:10:54.905258Z","shell.execute_reply":"2022-07-29T09:10:55.687633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabular_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.690688Z","iopub.execute_input":"2022-07-29T09:10:55.691033Z","iopub.status.idle":"2022-07-29T09:10:55.698193Z","shell.execute_reply.started":"2022-07-29T09:10:55.691002Z","shell.execute_reply":"2022-07-29T09:10:55.697044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabular_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.700094Z","iopub.execute_input":"2022-07-29T09:10:55.700557Z","iopub.status.idle":"2022-07-29T09:10:55.728016Z","shell.execute_reply.started":"2022-07-29T09:10:55.700512Z","shell.execute_reply":"2022-07-29T09:10:55.726695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabular_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.731472Z","iopub.execute_input":"2022-07-29T09:10:55.732057Z","iopub.status.idle":"2022-07-29T09:10:55.954076Z","shell.execute_reply.started":"2022-07-29T09:10:55.732019Z","shell.execute_reply":"2022-07-29T09:10:55.952875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabular_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.955924Z","iopub.execute_input":"2022-07-29T09:10:55.956278Z","iopub.status.idle":"2022-07-29T09:10:55.971308Z","shell.execute_reply.started":"2022-07-29T09:10:55.956247Z","shell.execute_reply":"2022-07-29T09:10:55.970218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['id'].unique())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.972859Z","iopub.execute_input":"2022-07-29T09:10:55.973701Z","iopub.status.idle":"2022-07-29T09:10:55.982728Z","shell.execute_reply.started":"2022-07-29T09:10:55.973646Z","shell.execute_reply":"2022-07-29T09:10:55.981892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_01'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.983887Z","iopub.execute_input":"2022-07-29T09:10:55.984829Z","iopub.status.idle":"2022-07-29T09:10:55.997934Z","shell.execute_reply.started":"2022-07-29T09:10:55.984795Z","shell.execute_reply":"2022-07-29T09:10:55.996879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_00'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:55.999158Z","iopub.execute_input":"2022-07-29T09:10:55.999756Z","iopub.status.idle":"2022-07-29T09:10:56.010740Z","shell.execute_reply.started":"2022-07-29T09:10:55.999720Z","shell.execute_reply":"2022-07-29T09:10:56.009539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_02'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.012496Z","iopub.execute_input":"2022-07-29T09:10:56.013365Z","iopub.status.idle":"2022-07-29T09:10:56.026963Z","shell.execute_reply.started":"2022-07-29T09:10:56.013321Z","shell.execute_reply":"2022-07-29T09:10:56.025775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_03'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.031201Z","iopub.execute_input":"2022-07-29T09:10:56.032277Z","iopub.status.idle":"2022-07-29T09:10:56.044355Z","shell.execute_reply.started":"2022-07-29T09:10:56.032229Z","shell.execute_reply":"2022-07-29T09:10:56.043543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_04'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.045874Z","iopub.execute_input":"2022-07-29T09:10:56.046378Z","iopub.status.idle":"2022-07-29T09:10:56.058297Z","shell.execute_reply.started":"2022-07-29T09:10:56.046336Z","shell.execute_reply":"2022-07-29T09:10:56.056806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_05'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.060385Z","iopub.execute_input":"2022-07-29T09:10:56.061097Z","iopub.status.idle":"2022-07-29T09:10:56.073111Z","shell.execute_reply.started":"2022-07-29T09:10:56.061055Z","shell.execute_reply":"2022-07-29T09:10:56.071959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_06'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.074479Z","iopub.execute_input":"2022-07-29T09:10:56.074823Z","iopub.status.idle":"2022-07-29T09:10:56.091386Z","shell.execute_reply.started":"2022-07-29T09:10:56.074794Z","shell.execute_reply":"2022-07-29T09:10:56.090084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_07'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.092966Z","iopub.execute_input":"2022-07-29T09:10:56.093316Z","iopub.status.idle":"2022-07-29T09:10:56.103453Z","shell.execute_reply.started":"2022-07-29T09:10:56.093285Z","shell.execute_reply":"2022-07-29T09:10:56.102571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_08'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.104537Z","iopub.execute_input":"2022-07-29T09:10:56.105175Z","iopub.status.idle":"2022-07-29T09:10:56.119025Z","shell.execute_reply.started":"2022-07-29T09:10:56.105138Z","shell.execute_reply":"2022-07-29T09:10:56.118069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_09'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.120169Z","iopub.execute_input":"2022-07-29T09:10:56.121091Z","iopub.status.idle":"2022-07-29T09:10:56.129216Z","shell.execute_reply.started":"2022-07-29T09:10:56.121055Z","shell.execute_reply":"2022-07-29T09:10:56.128148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_10'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.130524Z","iopub.execute_input":"2022-07-29T09:10:56.131201Z","iopub.status.idle":"2022-07-29T09:10:56.141419Z","shell.execute_reply.started":"2022-07-29T09:10:56.131165Z","shell.execute_reply":"2022-07-29T09:10:56.140638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_11'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.142541Z","iopub.execute_input":"2022-07-29T09:10:56.142879Z","iopub.status.idle":"2022-07-29T09:10:56.152846Z","shell.execute_reply.started":"2022-07-29T09:10:56.142850Z","shell.execute_reply":"2022-07-29T09:10:56.151587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_12'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.155072Z","iopub.execute_input":"2022-07-29T09:10:56.155647Z","iopub.status.idle":"2022-07-29T09:10:56.166094Z","shell.execute_reply.started":"2022-07-29T09:10:56.155600Z","shell.execute_reply":"2022-07-29T09:10:56.164717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_13'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.167763Z","iopub.execute_input":"2022-07-29T09:10:56.169066Z","iopub.status.idle":"2022-07-29T09:10:56.178512Z","shell.execute_reply.started":"2022-07-29T09:10:56.169012Z","shell.execute_reply":"2022-07-29T09:10:56.177463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_14'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.180310Z","iopub.execute_input":"2022-07-29T09:10:56.181425Z","iopub.status.idle":"2022-07-29T09:10:56.193957Z","shell.execute_reply.started":"2022-07-29T09:10:56.181388Z","shell.execute_reply":"2022-07-29T09:10:56.193069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_15'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.195319Z","iopub.execute_input":"2022-07-29T09:10:56.196071Z","iopub.status.idle":"2022-07-29T09:10:56.210322Z","shell.execute_reply.started":"2022-07-29T09:10:56.196022Z","shell.execute_reply":"2022-07-29T09:10:56.209185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_16'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.211812Z","iopub.execute_input":"2022-07-29T09:10:56.212164Z","iopub.status.idle":"2022-07-29T09:10:56.226186Z","shell.execute_reply.started":"2022-07-29T09:10:56.212133Z","shell.execute_reply":"2022-07-29T09:10:56.224845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_17'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.227601Z","iopub.execute_input":"2022-07-29T09:10:56.228140Z","iopub.status.idle":"2022-07-29T09:10:56.240720Z","shell.execute_reply.started":"2022-07-29T09:10:56.228108Z","shell.execute_reply":"2022-07-29T09:10:56.239533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_18'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.242411Z","iopub.execute_input":"2022-07-29T09:10:56.243144Z","iopub.status.idle":"2022-07-29T09:10:56.256918Z","shell.execute_reply.started":"2022-07-29T09:10:56.243100Z","shell.execute_reply":"2022-07-29T09:10:56.255751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_19'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.258993Z","iopub.execute_input":"2022-07-29T09:10:56.259727Z","iopub.status.idle":"2022-07-29T09:10:56.274052Z","shell.execute_reply.started":"2022-07-29T09:10:56.259662Z","shell.execute_reply":"2022-07-29T09:10:56.272902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_20'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.275451Z","iopub.execute_input":"2022-07-29T09:10:56.276028Z","iopub.status.idle":"2022-07-29T09:10:56.287706Z","shell.execute_reply.started":"2022-07-29T09:10:56.275994Z","shell.execute_reply":"2022-07-29T09:10:56.286844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_21'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.294809Z","iopub.execute_input":"2022-07-29T09:10:56.295423Z","iopub.status.idle":"2022-07-29T09:10:56.307073Z","shell.execute_reply.started":"2022-07-29T09:10:56.295387Z","shell.execute_reply":"2022-07-29T09:10:56.305944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_22'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.308628Z","iopub.execute_input":"2022-07-29T09:10:56.309267Z","iopub.status.idle":"2022-07-29T09:10:56.319785Z","shell.execute_reply.started":"2022-07-29T09:10:56.309234Z","shell.execute_reply":"2022-07-29T09:10:56.318916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_23'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.321278Z","iopub.execute_input":"2022-07-29T09:10:56.321833Z","iopub.status.idle":"2022-07-29T09:10:56.333161Z","shell.execute_reply.started":"2022-07-29T09:10:56.321800Z","shell.execute_reply":"2022-07-29T09:10:56.332031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_24'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.334203Z","iopub.execute_input":"2022-07-29T09:10:56.334912Z","iopub.status.idle":"2022-07-29T09:10:56.345813Z","shell.execute_reply.started":"2022-07-29T09:10:56.334881Z","shell.execute_reply":"2022-07-29T09:10:56.344741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_25'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.347255Z","iopub.execute_input":"2022-07-29T09:10:56.348118Z","iopub.status.idle":"2022-07-29T09:10:56.362408Z","shell.execute_reply.started":"2022-07-29T09:10:56.348071Z","shell.execute_reply":"2022-07-29T09:10:56.361269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_26'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.363955Z","iopub.execute_input":"2022-07-29T09:10:56.364923Z","iopub.status.idle":"2022-07-29T09:10:56.376543Z","shell.execute_reply.started":"2022-07-29T09:10:56.364891Z","shell.execute_reply":"2022-07-29T09:10:56.375737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_27'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.377743Z","iopub.execute_input":"2022-07-29T09:10:56.378423Z","iopub.status.idle":"2022-07-29T09:10:56.389882Z","shell.execute_reply.started":"2022-07-29T09:10:56.378392Z","shell.execute_reply":"2022-07-29T09:10:56.388867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tabular_data['f_28'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.391135Z","iopub.execute_input":"2022-07-29T09:10:56.391932Z","iopub.status.idle":"2022-07-29T09:10:56.404193Z","shell.execute_reply.started":"2022-07-29T09:10:56.391898Z","shell.execute_reply":"2022-07-29T09:10:56.403160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr=tabular_data.corr()\ncorr","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.405599Z","iopub.execute_input":"2022-07-29T09:10:56.406356Z","iopub.status.idle":"2022-07-29T09:10:56.703982Z","shell.execute_reply.started":"2022-07-29T09:10:56.406323Z","shell.execute_reply":"2022-07-29T09:10:56.702850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.heatmap(corr)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:56.705396Z","iopub.execute_input":"2022-07-29T09:10:56.705737Z","iopub.status.idle":"2022-07-29T09:10:57.067191Z","shell.execute_reply.started":"2022-07-29T09:10:56.705706Z","shell.execute_reply":"2022-07-29T09:10:57.066076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:57.069653Z","iopub.execute_input":"2022-07-29T09:10:57.070431Z","iopub.status.idle":"2022-07-29T09:10:57.074988Z","shell.execute_reply.started":"2022-07-29T09:10:57.070384Z","shell.execute_reply":"2022-07-29T09:10:57.074259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#General Visualization\n#Let's take a look at the general spread of the data in each column of our dataset.\nT1 = tabular_data.drop(columns='id')\nplt.figure(dpi=100, figsize=(15, 5))\nsns.boxplot(data=T1)\nplt.grid(True)\nplt.title(\"Distribution of each column\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:57.076313Z","iopub.execute_input":"2022-07-29T09:10:57.076587Z","iopub.status.idle":"2022-07-29T09:10:57.961921Z","shell.execute_reply.started":"2022-07-29T09:10:57.076561Z","shell.execute_reply":"2022-07-29T09:10:57.960641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"T1.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:57.963494Z","iopub.execute_input":"2022-07-29T09:10:57.963870Z","iopub.status.idle":"2022-07-29T09:10:57.981803Z","shell.execute_reply.started":"2022-07-29T09:10:57.963836Z","shell.execute_reply":"2022-07-29T09:10:57.980921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 3 main groupings here:\n\n00-06, float data\n\n07-14. int data,\n\n15-21, float data :- Similar to 00-06.\n    \n22-28, float data :- Spread is wider than the other float data categories","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PCA - Principle Component Analysis\n\nThe Yeo-Johnson tranformation  improves the normality of data, so I will go with that.","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import PowerTransformer\npt = PowerTransformer(method='yeo-johnson')\nT_tran = pt.fit_transform(T1)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:10:57.983308Z","iopub.execute_input":"2022-07-29T09:10:57.983864Z","iopub.status.idle":"2022-07-29T09:11:01.802037Z","shell.execute_reply.started":"2022-07-29T09:10:57.983830Z","shell.execute_reply":"2022-07-29T09:11:01.801112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize the tranformed data\nplt.figure(dpi=100, figsize=(15, 5))\nsns.boxplot(data=T_tran)\nplt.grid(True)\nplt.title(\"Distribution of each column, Transformed\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:01.803449Z","iopub.execute_input":"2022-07-29T09:11:01.804138Z","iopub.status.idle":"2022-07-29T09:11:03.248615Z","shell.execute_reply.started":"2022-07-29T09:11:01.804095Z","shell.execute_reply":"2022-07-29T09:11:03.247400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verify that the standard deviation of the tranformed data is consistent.\nstd = np.std(T_tran, axis=0)\nfig, ax = plt.subplots(figsize=[15,5])\nax.plot(std)\nax.set_title(\"Standard Deviation\")\nax.set_xlabel(\"Feature Number\")\nax.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:03.250530Z","iopub.execute_input":"2022-07-29T09:11:03.251011Z","iopub.status.idle":"2022-07-29T09:11:03.447802Z","shell.execute_reply.started":"2022-07-29T09:11:03.250965Z","shell.execute_reply":"2022-07-29T09:11:03.446708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA()\npca.fit(T_tran)\nvar_ratio = pca.explained_variance_ratio_\nexp_var = pca.explained_variance_\nsing_vals = pca.singular_values_\ncomponents = pca.components_","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:03.449312Z","iopub.execute_input":"2022-07-29T09:11:03.449648Z","iopub.status.idle":"2022-07-29T09:11:03.561261Z","shell.execute_reply.started":"2022-07-29T09:11:03.449617Z","shell.execute_reply":"2022-07-29T09:11:03.559921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculate singular values\n#The singular values show the relative 'strength' of each component\nfig, ax = plt.subplots(figsize=[15,5])\nax.plot(sing_vals)\nax.set_title(\"Singular Values\")\nax.set_xlabel(\"Component Number\")\nax.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:03.563661Z","iopub.execute_input":"2022-07-29T09:11:03.564589Z","iopub.status.idle":"2022-07-29T09:11:03.792597Z","shell.execute_reply.started":"2022-07-29T09:11:03.564537Z","shell.execute_reply":"2022-07-29T09:11:03.791443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The first 6 components are the most informative.","metadata":{}},{"cell_type":"code","source":"# Cumulative proportion of variance (from PC1 to PC6)   \npercent_explained = np.cumsum(var_ratio)\n\nfig, ax = plt.subplots(figsize=[15,5])\nax.plot(percent_explained)\nax.set_title(\"Percent Explained\")\nax.set_xlabel(\"Component Number\")\nax.set_xlim([0,28])\nax.set_ylim([0,1])\nax.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:03.794069Z","iopub.execute_input":"2022-07-29T09:11:03.794396Z","iopub.status.idle":"2022-07-29T09:11:03.987022Z","shell.execute_reply.started":"2022-07-29T09:11:03.794366Z","shell.execute_reply":"2022-07-29T09:11:03.985864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA on each group\n# The group labels\ng1 = [f\"f_{x:02d}\" for x in range(7)]\ng2 = [f\"f_{x:02d}\" for x in range(7, 14)]\ng3 = [f\"f_{x:02d}\" for x in range(14, 22)]\ng4 = [f\"f_{x:02d}\" for x in range(22, 29)]\ng_label_list = [g1, g2, g3, g4]\n\n# The group data\ntabular_data_g1 = T1[g1].copy()\ntabular_data_g2 = T1[g2].copy()\ntabular_data_g3 = tabular_data[g3].copy()\ntabular_data_g4 = T1[g4].copy()\ng_data = [tabular_data_g1, tabular_data_g2, tabular_data_g3, tabular_data_g4]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:03.988521Z","iopub.execute_input":"2022-07-29T09:11:03.989347Z","iopub.status.idle":"2022-07-29T09:11:04.010527Z","shell.execute_reply.started":"2022-07-29T09:11:03.989314Z","shell.execute_reply":"2022-07-29T09:11:04.009231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize Storage\nvar_ratios = []\nexp_vars = []\nsing_val_list = []\ncomp_list = []\nprojections = []\n\n# Loop through groups\nfor data in g_data:\n    pca = PCA()\n    projection = pca.fit_transform(data)\n    projections.append(projection)\n    var_ratios.append(pca.explained_variance_ratio_)\n    exp_vars.append(pca.explained_variance_)\n    sing_val_list.append(pca.singular_values_)\n    comp_list.append(pca.components_)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:04.012118Z","iopub.execute_input":"2022-07-29T09:11:04.012698Z","iopub.status.idle":"2022-07-29T09:11:04.128413Z","shell.execute_reply.started":"2022-07-29T09:11:04.012640Z","shell.execute_reply":"2022-07-29T09:11:04.126948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Singular Value Plots\nfig, ax = plt.subplots(nrows=len(g_label_list), ncols=2, figsize=[15,15])\ngroup_names = ['g1', 'g2', 'g3', 'g4']\n\nfor idx, label in enumerate(group_names):\n    ax[idx,0].plot(sing_val_list[idx])\n    ax[idx,0].set_title(f\"Singular Values {label}\")\n    ax[idx,0].grid(True)\n    \n    percent_explained = np.cumsum(var_ratios[idx])\n    ax[idx,1].plot(percent_explained)\n    ax[idx,1].set_title(f\"Percent Explained {label}\")\n    ax[idx,1].set_ylim([0,1])\n    ax[idx,1].grid(True)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:04.130647Z","iopub.execute_input":"2022-07-29T09:11:04.131851Z","iopub.status.idle":"2022-07-29T09:11:05.134248Z","shell.execute_reply.started":"2022-07-29T09:11:04.131792Z","shell.execute_reply":"2022-07-29T09:11:05.133091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"t-SNE : t-Distributed Stochastic Neighbor Embedding","metadata":{}},{"cell_type":"code","source":"from sklearn.manifold import TSNE\nT2 = T1.sample(frac=.10, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:05.136067Z","iopub.execute_input":"2022-07-29T09:11:05.136591Z","iopub.status.idle":"2022-07-29T09:11:05.152184Z","shell.execute_reply.started":"2022-07-29T09:11:05.136544Z","shell.execute_reply":"2022-07-29T09:11:05.150892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perplexities = [5.0, 10.0, 20.0, 30.0, 40.0, 50.0]\nembeddings = []\nfor perplexity in perplexities:\n    tsne = TSNE(n_components=2, learning_rate='auto', perplexity=perplexity, verbose=1)\n    X_embedded = tsne.fit_transform(T2)\n    embeddings.append(X_embedded)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:11:05.154029Z","iopub.execute_input":"2022-07-29T09:11:05.154501Z","iopub.status.idle":"2022-07-29T09:17:40.038552Z","shell.execute_reply.started":"2022-07-29T09:11:05.154453Z","shell.execute_reply":"2022-07-29T09:17:40.037624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" t-sne doesn't look like it's giving great results","metadata":{}},{"cell_type":"markdown","source":"UMAP","metadata":{}},{"cell_type":"code","source":"import umap","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:17:40.040294Z","iopub.execute_input":"2022-07-29T09:17:40.041001Z","iopub.status.idle":"2022-07-29T09:17:40.045971Z","shell.execute_reply.started":"2022-07-29T09:17:40.040960Z","shell.execute_reply":"2022-07-29T09:17:40.045048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"neighbor_size = [5.0, 50.0, 100.0, 500.0]\numap_embeds = []\nfor n_neighbors in neighbor_size:\n     reducer = umap.UMAP(random_state=0, n_neighbors=n_neighbors, n_components=2, verbose=True)\n    \n# Fit the umap enbedding with the PowerTransformer normalized data\n     X_embed = reducer.fit_transform(T_tran)\n     umap_embeds.append(X_embed)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:17:40.047433Z","iopub.execute_input":"2022-07-29T09:17:40.048040Z","iopub.status.idle":"2022-07-29T09:39:47.551050Z","shell.execute_reply.started":"2022-07-29T09:17:40.048003Z","shell.execute_reply":"2022-07-29T09:39:47.549709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(ncols=1, nrows=len(neighbor_size), figsize=[15,45])    \nfor idx, n_neighbors in enumerate(neighbor_size):\n            x = umap_embeds[idx][:,0]\n            y = umap_embeds[idx][:,1]\n            ax[idx].scatter(x,y)\n            ax[idx].set_ylabel(f\"Num Neighbors: {n_neighbors}\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:48:12.937937Z","iopub.execute_input":"2022-07-29T09:48:12.938954Z","iopub.status.idle":"2022-07-29T09:48:14.302143Z","shell.execute_reply.started":"2022-07-29T09:48:12.938907Z","shell.execute_reply":"2022-07-29T09:48:14.300952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Clustering - Gaussian Mixture Model","metadata":{}},{"cell_type":"code","source":"# Set number of components\nnum_comps = 6\n\n# Grab components from PCA\npca = PCA(n_components=num_comps)\npca_array = pca.fit_transform(T_tran)\npca_df = pd.DataFrame(pca_array)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:52:43.368713Z","iopub.execute_input":"2022-07-29T09:52:43.369112Z","iopub.status.idle":"2022-07-29T09:52:43.897488Z","shell.execute_reply.started":"2022-07-29T09:52:43.369081Z","shell.execute_reply":"2022-07-29T09:52:43.896111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import GaussianMixture\n\n# # Intialize the GMM model\ngmm = GaussianMixture(n_components = num_comps, random_state=0)\n\n# Fit the GMM model for the dataset\ngmm.fit(pca_df)\n\n# # Assign a label to each sample\nlabels = gmm.predict(pca_df)\npca_df['Clusters']= labels","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:54:34.312994Z","iopub.execute_input":"2022-07-29T09:54:34.313434Z","iopub.status.idle":"2022-07-29T09:54:37.070278Z","shell.execute_reply.started":"2022-07-29T09:54:34.313397Z","shell.execute_reply":"2022-07-29T09:54:37.068907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Visualize the clusters and their distributions\ng = sns.PairGrid(pca_df, vars=list(range(num_comps)), hue=\"Clusters\", palette=\"tab10\")\ng.map_diag(sns.histplot)\ng.map_offdiag(sns.scatterplot)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:55:40.235342Z","iopub.execute_input":"2022-07-29T09:55:40.235849Z","iopub.status.idle":"2022-07-29T09:56:57.426462Z","shell.execute_reply.started":"2022-07-29T09:55:40.235811Z","shell.execute_reply":"2022-07-29T09:56:57.423944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prep for submission\nsubmission = pd.DataFrame()\nsubmission['id'] = pca_df.index\nsubmission['Predicted'] = pca_df.loc[:,['Clusters']]\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:57:26.628914Z","iopub.execute_input":"2022-07-29T09:57:26.629333Z","iopub.status.idle":"2022-07-29T09:57:26.656709Z","shell.execute_reply.started":"2022-07-29T09:57:26.629299Z","shell.execute_reply":"2022-07-29T09:57:26.655735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save for submitting\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:57:35.568789Z","iopub.execute_input":"2022-07-29T09:57:35.569420Z","iopub.status.idle":"2022-07-29T09:57:35.733950Z","shell.execute_reply.started":"2022-07-29T09:57:35.569387Z","shell.execute_reply":"2022-07-29T09:57:35.732892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.563305Z","iopub.status.idle":"2022-07-29T09:39:47.563712Z","shell.execute_reply.started":"2022-07-29T09:39:47.563495Z","shell.execute_reply":"2022-07-29T09:39:47.563512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.565761Z","iopub.status.idle":"2022-07-29T09:39:47.566209Z","shell.execute_reply.started":"2022-07-29T09:39:47.565974Z","shell.execute_reply":"2022-07-29T09:39:47.565991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.567401Z","iopub.status.idle":"2022-07-29T09:39:47.567847Z","shell.execute_reply.started":"2022-07-29T09:39:47.567618Z","shell.execute_reply":"2022-07-29T09:39:47.567636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.571995Z","iopub.status.idle":"2022-07-29T09:39:47.572562Z","shell.execute_reply.started":"2022-07-29T09:39:47.572276Z","shell.execute_reply":"2022-07-29T09:39:47.572302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.574003Z","iopub.status.idle":"2022-07-29T09:39:47.574568Z","shell.execute_reply.started":"2022-07-29T09:39:47.574259Z","shell.execute_reply":"2022-07-29T09:39:47.574284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:39:47.575947Z","iopub.status.idle":"2022-07-29T09:39:47.576492Z","shell.execute_reply.started":"2022-07-29T09:39:47.576204Z","shell.execute_reply":"2022-07-29T09:39:47.576230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}