{"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":"import pandas as pd\nimport numpy as np\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.preprocessing import PowerTransformer, RobustScaler\n\nfrom sklearn.cluster import KMeans\nfrom sklearn.metrics import silhouette_score\nfrom sklearn.mixture import BayesianGaussianMixture\nfrom mpl_toolkits.mplot3d import Axes3D\n\n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:37.678913Z","iopub.execute_input":"2022-07-10T19:00:37.679888Z","iopub.status.idle":"2022-07-10T19:00:38.840778Z","shell.execute_reply.started":"2022-07-10T19:00:37.679764Z","shell.execute_reply":"2022-07-10T19:00:38.839584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check on the panda version and its dependencies\n# i run this from time to time to ensure all is up to date\npd.__version__\n#pd.show_versions()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:38.843025Z","iopub.execute_input":"2022-07-10T19:00:38.843394Z","iopub.status.idle":"2022-07-10T19:00:38.853412Z","shell.execute_reply.started":"2022-07-10T19:00:38.843356Z","shell.execute_reply":"2022-07-10T19:00:38.851461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# i use these to input the relevant file names\n# which i downloaded earlier and sit in the same directory as this\nfile_train = '../input/tabular-playground-series-jul-2022/data.csv'\n#file_test = 'test.csv'\nfile_sampleSubmission = '../input/tabular-playground-series-jul-2022/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:38.855246Z","iopub.execute_input":"2022-07-10T19:00:38.855581Z","iopub.status.idle":"2022-07-10T19:00:38.867071Z","shell.execute_reply.started":"2022-07-10T19:00:38.855553Z","shell.execute_reply":"2022-07-10T19:00:38.865938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# which makes this a standard cell and keeps the original traing and test data in memory\n# use train for the training data, test for the test data\n# and also download the sample submission for quick reference\ndf_train = pd.read_csv(file_train)\n#df_test = pd.read_csv(file_test)\ndf_sampleSubmission = pd.read_csv(file_sampleSubmission)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:38.869561Z","iopub.execute_input":"2022-07-10T19:00:38.869953Z","iopub.status.idle":"2022-07-10T19:00:40.1859Z","shell.execute_reply.started":"2022-07-10T19:00:38.869921Z","shell.execute_reply":"2022-07-10T19:00:40.184462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Exploring the Training Data","metadata":{}},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.187239Z","iopub.execute_input":"2022-07-10T19:00:40.18835Z","iopub.status.idle":"2022-07-10T19:00:40.230713Z","shell.execute_reply.started":"2022-07-10T19:00:40.188306Z","shell.execute_reply":"2022-07-10T19:00:40.229417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.234432Z","iopub.execute_input":"2022-07-10T19:00:40.235523Z","iopub.status.idle":"2022-07-10T19:00:40.464544Z","shell.execute_reply.started":"2022-07-10T19:00:40.235468Z","shell.execute_reply":"2022-07-10T19:00:40.463366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.466308Z","iopub.execute_input":"2022-07-10T19:00:40.467086Z","iopub.status.idle":"2022-07-10T19:00:40.494752Z","shell.execute_reply.started":"2022-07-10T19:00:40.467042Z","shell.execute_reply":"2022-07-10T19:00:40.49331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# missing data? False = none\ndf_train.isnull().values.any(),","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.496545Z","iopub.execute_input":"2022-07-10T19:00:40.497115Z","iopub.status.idle":"2022-07-10T19:00:40.515027Z","shell.execute_reply.started":"2022-07-10T19:00:40.497065Z","shell.execute_reply":"2022-07-10T19:00:40.513511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the id column\n\ndf_train = df_train.drop('id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.516869Z","iopub.execute_input":"2022-07-10T19:00:40.517335Z","iopub.status.idle":"2022-07-10T19:00:40.528411Z","shell.execute_reply.started":"2022-07-10T19:00:40.517288Z","shell.execute_reply":"2022-07-10T19:00:40.527382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# produce a list showing unique values per column\n# put into a dataframe called df_col_unique\n\ncol_name = []\ncol_unique = []\ncol_isnull = []\nfor i in range(len(df_train.columns)):\n    col_name.append(df_train.columns[i])\n    col_unique.append(df_train[df_train.columns[i]].nunique())\n    col_isnull.append(df_train[df_train.columns[i]].isnull().sum())\n\ndf_col_unique = pd.DataFrame(\n    {'col name': col_name,\n     'nunique': col_unique,\n     'isnull': col_isnull,\n     })","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.532431Z","iopub.execute_input":"2022-07-10T19:00:40.533537Z","iopub.status.idle":"2022-07-10T19:00:40.669895Z","shell.execute_reply.started":"2022-07-10T19:00:40.533495Z","shell.execute_reply":"2022-07-10T19:00:40.668695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_col_unique","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.671716Z","iopub.execute_input":"2022-07-10T19:00:40.672146Z","iopub.status.idle":"2022-07-10T19:00:40.686259Z","shell.execute_reply.started":"2022-07-10T19:00:40.672103Z","shell.execute_reply":"2022-07-10T19:00:40.685069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot histograms over the int columns\n\nplt.subplots(figsize=(20,20))\nfor i, column in enumerate(df_col_unique[df_col_unique['nunique']<100]['col name'].tolist()):\n    plt.subplot(4,4,i+1)\n    plt.hist(df_train[column], bins = 100)\n    plt.title(column)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:40.68806Z","iopub.execute_input":"2022-07-10T19:00:40.688797Z","iopub.status.idle":"2022-07-10T19:00:42.829943Z","shell.execute_reply.started":"2022-07-10T19:00:40.688751Z","shell.execute_reply":"2022-07-10T19:00:42.828866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# looking at the other columns\n\nplt.subplots(figsize=(35,45))\nfor i, column in enumerate(df_col_unique[df_col_unique['nunique']>100]['col name'].tolist()):\n    plt.subplot(21,4,i+1)\n    plt.hist(df_train[column], bins = 100)\n    plt.title(column)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:42.831033Z","iopub.execute_input":"2022-07-10T19:00:42.831336Z","iopub.status.idle":"2022-07-10T19:00:49.159072Z","shell.execute_reply.started":"2022-07-10T19:00:42.831309Z","shell.execute_reply":"2022-07-10T19:00:49.158202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scale all columns\n\nrob_scaler = RobustScaler().fit(df_train)\npower_transformer = PowerTransformer().fit(df_train)\n\ndata_scl_rob = rob_scaler.fit_transform(df_train)\ndata_rob_pwr = power_transformer.transform(data_scl_rob)\n\ndf_train_scaled = pd.DataFrame(data_rob_pwr,columns = df_train.columns)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:49.16053Z","iopub.execute_input":"2022-07-10T19:00:49.161334Z","iopub.status.idle":"2022-07-10T19:00:53.402485Z","shell.execute_reply.started":"2022-07-10T19:00:49.161284Z","shell.execute_reply":"2022-07-10T19:00:53.401213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_scaled.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:53.403727Z","iopub.execute_input":"2022-07-10T19:00:53.404038Z","iopub.status.idle":"2022-07-10T19:00:53.436838Z","shell.execute_reply.started":"2022-07-10T19:00:53.40401Z","shell.execute_reply":"2022-07-10T19:00:53.435679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clusters = []\n\nsearch_total = 25\n\nfor i in range(1, search_total):\n    km = KMeans(n_clusters=i).fit(df_train_scaled)\n    clusters.append(km.inertia_)\n    \nfig, ax = plt.subplots(figsize=(12, 8))\nsns.lineplot(x=list(range(1, search_total)), y=clusters, ax=ax)\nax.set_title('Searching for Elbow')\nax.set_xlabel('Clusters')\nax.set_ylabel('Inertia')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:00:53.438336Z","iopub.execute_input":"2022-07-10T19:00:53.439178Z","iopub.status.idle":"2022-07-10T19:09:23.522214Z","shell.execute_reply.started":"2022-07-10T19:00:53.439131Z","shell.execute_reply":"2022-07-10T19:09:23.520752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = []\n\nfor i in range(2, search_total):\n    km = KMeans(n_clusters=i, random_state=0)\n    km.fit_predict(df_train_scaled)\n    score.append(silhouette_score(df_train_scaled, km.labels_, metric='euclidean'))\n    \nfig, ax = plt.subplots(figsize=(12, 8))\nsns.lineplot(x=list(range(2, search_total)), y=score, ax=ax)\nax.set_title('silhouette_score')\nax.set_xlabel('Clusters')\nax.set_ylabel('Score')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:09:23.524015Z","iopub.execute_input":"2022-07-10T19:09:23.525425Z","iopub.status.idle":"2022-07-10T19:57:06.100314Z","shell.execute_reply.started":"2022-07-10T19:09:23.525387Z","shell.execute_reply":"2022-07-10T19:57:06.098711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(6,9):\n    BGM = BayesianGaussianMixture(n_components=i, covariance_type='full', random_state=1)\n    pred = BGM.fit_predict(df_train_scaled)\n    col_name = \"clusters_\" + str(i)\n    df_train_scaled[col_name] = pred\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:57:06.102791Z","iopub.execute_input":"2022-07-10T19:57:06.104327Z","iopub.status.idle":"2022-07-10T19:58:58.072688Z","shell.execute_reply.started":"2022-07-10T19:57:06.104269Z","shell.execute_reply":"2022-07-10T19:58:58.071269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_scaled.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.074385Z","iopub.execute_input":"2022-07-10T19:58:58.075151Z","iopub.status.idle":"2022-07-10T19:58:58.108402Z","shell.execute_reply.started":"2022-07-10T19:58:58.075105Z","shell.execute_reply":"2022-07-10T19:58:58.107494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sampleSubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.109897Z","iopub.execute_input":"2022-07-10T19:58:58.110202Z","iopub.status.idle":"2022-07-10T19:58:58.121029Z","shell.execute_reply.started":"2022-07-10T19:58:58.110175Z","shell.execute_reply":"2022-07-10T19:58:58.119838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sampleSubmission['Predicted'] = df_train_scaled['clusters_6']\n# df_sampleSubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.123091Z","iopub.execute_input":"2022-07-10T19:58:58.123928Z","iopub.status.idle":"2022-07-10T19:58:58.130651Z","shell.execute_reply.started":"2022-07-10T19:58:58.123878Z","shell.execute_reply":"2022-07-10T19:58:58.129649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sampleSubmission.to_csv('Submission_clusters6_20220710.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.132249Z","iopub.execute_input":"2022-07-10T19:58:58.132717Z","iopub.status.idle":"2022-07-10T19:58:58.144039Z","shell.execute_reply.started":"2022-07-10T19:58:58.132683Z","shell.execute_reply":"2022-07-10T19:58:58.142848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sampleSubmission['Predicted'] = df_train_scaled['clusters_7']\ndf_sampleSubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.145801Z","iopub.execute_input":"2022-07-10T19:58:58.146948Z","iopub.status.idle":"2022-07-10T19:58:58.169209Z","shell.execute_reply.started":"2022-07-10T19:58:58.146901Z","shell.execute_reply":"2022-07-10T19:58:58.168049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sampleSubmission.to_csv('Submission_clusters7.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.170845Z","iopub.execute_input":"2022-07-10T19:58:58.17168Z","iopub.status.idle":"2022-07-10T19:58:58.336368Z","shell.execute_reply.started":"2022-07-10T19:58:58.171647Z","shell.execute_reply":"2022-07-10T19:58:58.335092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sampleSubmission['Predicted'] = df_train_scaled['clusters_8']\n# df_sampleSubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.3381Z","iopub.execute_input":"2022-07-10T19:58:58.338553Z","iopub.status.idle":"2022-07-10T19:58:58.34592Z","shell.execute_reply.started":"2022-07-10T19:58:58.33851Z","shell.execute_reply":"2022-07-10T19:58:58.344853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sampleSubmission.to_csv('Submission_clusters8_20220710.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T19:58:58.347769Z","iopub.execute_input":"2022-07-10T19:58:58.348339Z","iopub.status.idle":"2022-07-10T19:58:58.354516Z","shell.execute_reply.started":"2022-07-10T19:58:58.348291Z","shell.execute_reply":"2022-07-10T19:58:58.353389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-10T20:13:53.879211Z","iopub.execute_input":"2022-07-10T20:13:53.879674Z","iopub.status.idle":"2022-07-10T20:13:54.043653Z","shell.execute_reply.started":"2022-07-10T20:13:53.879632Z","shell.execute_reply":"2022-07-10T20:13:54.042658Z"},"trusted":true},"execution_count":null,"outputs":[]}]}