{"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 numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nfrom sklearn.preprocessing import StandardScaler\nimport warnings\nwarnings.filterwarnings(action='ignore')","metadata":{"id":"mv7smNeCs0KZ","execution":{"iopub.status.busy":"2022-07-26T19:05:30.339011Z","iopub.execute_input":"2022-07-26T19:05:30.339748Z","iopub.status.idle":"2022-07-26T19:05:32.136480Z","shell.execute_reply.started":"2022-07-26T19:05:30.339648Z","shell.execute_reply":"2022-07-26T19:05:32.135276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\nsubmission=pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")","metadata":{"id":"hNHZeWr-u8b0","execution":{"iopub.status.busy":"2022-07-26T19:05:39.331489Z","iopub.execute_input":"2022-07-26T19:05:39.332106Z","iopub.status.idle":"2022-07-26T19:05:40.739829Z","shell.execute_reply.started":"2022-07-26T19:05:39.332049Z","shell.execute_reply":"2022-07-26T19:05:40.738950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"id":"mV4uUOKQvQO-","outputId":"4e55e231-8bc0-45a8-d03c-e81b1afe7c76","execution":{"iopub.status.busy":"2022-07-26T19:05:46.256544Z","iopub.execute_input":"2022-07-26T19:05:46.256960Z","iopub.status.idle":"2022-07-26T19:05:46.297840Z","shell.execute_reply.started":"2022-07-26T19:05:46.256927Z","shell.execute_reply":"2022-07-26T19:05:46.296639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=data.drop(['id'],axis=1)","metadata":{"id":"LirUV9_2k_9R","execution":{"iopub.status.busy":"2022-07-26T19:05:52.692535Z","iopub.execute_input":"2022-07-26T19:05:52.693015Z","iopub.status.idle":"2022-07-26T19:05:52.707898Z","shell.execute_reply.started":"2022-07-26T19:05:52.692976Z","shell.execute_reply":"2022-07-26T19:05:52.706523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe().T","metadata":{"id":"pGa8xChtwFln","outputId":"95e034c6-8762-490a-90b9-d9c65ee6ed96","execution":{"iopub.status.busy":"2022-07-26T19:06:01.091249Z","iopub.execute_input":"2022-07-26T19:06:01.091713Z","iopub.status.idle":"2022-07-26T19:06:01.334746Z","shell.execute_reply.started":"2022-07-26T19:06:01.091675Z","shell.execute_reply":"2022-07-26T19:06:01.333476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"id":"rQEcfAjsnysY","outputId":"0f3a1026-39c4-4926-9381-fec81adb2e5c","execution":{"iopub.status.busy":"2022-07-26T19:06:11.521328Z","iopub.execute_input":"2022-07-26T19:06:11.521760Z","iopub.status.idle":"2022-07-26T19:06:11.545753Z","shell.execute_reply.started":"2022-07-26T19:06:11.521727Z","shell.execute_reply":"2022-07-26T19:06:11.544878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"f_07 to f_13 are different from others","metadata":{"id":"axgb_1kowznz"}},{"cell_type":"code","source":"def list_maker(my_lists,datatype):\n  cols=list(data.columns)\n  for i in cols:\n    if type(data[i][0])==datatype:\n      my_lists.append(i)\na_type=[]\nb_type=[]\n\nlist_maker(a_type,np.float64)\nlist_maker(b_type,np.int64)","metadata":{"id":"lUprVLuIxCNF","execution":{"iopub.status.busy":"2022-07-26T19:06:19.547048Z","iopub.execute_input":"2022-07-26T19:06:19.548029Z","iopub.status.idle":"2022-07-26T19:06:19.555976Z","shell.execute_reply.started":"2022-07-26T19:06:19.547981Z","shell.execute_reply":"2022-07-26T19:06:19.554446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_type,b_type","metadata":{"id":"tfyH8MBEqk55","outputId":"9780185d-599a-4415-8505-3067d86580e4","execution":{"iopub.status.busy":"2022-07-26T19:06:35.499075Z","iopub.execute_input":"2022-07-26T19:06:35.499492Z","iopub.status.idle":"2022-07-26T19:06:35.508804Z","shell.execute_reply.started":"2022-07-26T19:06:35.499457Z","shell.execute_reply":"2022-07-26T19:06:35.507437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def UVA_numeric(data, var_group):\n  \n\n  size = len(var_group)\n  plt.figure(figsize=(7*size, 3), dpi=120)\n\n  # looping over each feature\n  for j ,i in enumerate(var_group):\n\n    # calculating of descriptives of variables\n    minm = data[i].min()\n    maxm = data[i].max()\n    ran = maxm - minm\n    mean = data[i].mean()\n    median = data[i].median()\n    st_dev = data[i].std()\n    skew = data[i].skew()\n    kurt = data[i].kurtosis()\n    \n    # calculation of points of inflection\n    points = mean-st_dev, mean+st_dev\n\n    #plotting the variables with every information\n    plt.subplot(1, size, j+1)\n    sns.kdeplot(data[i], shade=True)\n    sns.scatterplot([minm, maxm], [0,0], color='blue', label='max/min')\n    sns.scatterplot([mean], [0], color='green', label='mean')\n    sns.scatterplot([median], [0], color='orange', label='median')\n    sns.scatterplot(points, [0,0], color='pink', label='points of inflection')\n    plt.xlabel(f'{i}')\n    plt.ylabel('Density')\n    plt.title(f'Point of Inflection={(round(points[0],2), round(points[1],2))}; range={round(ran,2)};\\nskewness={round(skew,2)}; kurtosis={round(kurt,2)}; \\nmean={round(mean,2)}; median={round(median,2)}')","metadata":{"id":"QNN5j1v6qxRL","execution":{"iopub.status.busy":"2022-07-26T19:06:42.304365Z","iopub.execute_input":"2022-07-26T19:06:42.304766Z","iopub.status.idle":"2022-07-26T19:06:42.317974Z","shell.execute_reply.started":"2022-07-26T19:06:42.304734Z","shell.execute_reply":"2022-07-26T19:06:42.316750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"UVA_numeric(data,a_type)","metadata":{"id":"raKhoiaxtPrZ","outputId":"8b6f5b98-42ae-4488-9e89-8995629ecdbe","execution":{"iopub.status.busy":"2022-07-26T19:06:50.964041Z","iopub.execute_input":"2022-07-26T19:06:50.964422Z","iopub.status.idle":"2022-07-26T19:07:07.543251Z","shell.execute_reply.started":"2022-07-26T19:06:50.964393Z","shell.execute_reply":"2022-07-26T19:07:07.540986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"UVA_numeric(data,b_type)","metadata":{"id":"_u4BOMjLtra1","outputId":"d178d182-a4d3-4b31-e2e1-2615617e158a","execution":{"iopub.status.busy":"2022-07-26T19:07:20.247863Z","iopub.execute_input":"2022-07-26T19:07:20.248316Z","iopub.status.idle":"2022-07-26T19:07:25.234766Z","shell.execute_reply.started":"2022-07-26T19:07:20.248273Z","shell.execute_reply":"2022-07-26T19:07:25.233484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation = data.corr()\ncorrelation","metadata":{"id":"6WBfKADNvdqI","outputId":"3195f4fa-b7ae-4dac-a690-d6a7e4617c76","execution":{"iopub.status.busy":"2022-07-26T19:07:31.550741Z","iopub.execute_input":"2022-07-26T19:07:31.551151Z","iopub.status.idle":"2022-07-26T19:07:31.839562Z","shell.execute_reply.started":"2022-07-26T19:07:31.551120Z","shell.execute_reply":"2022-07-26T19:07:31.838646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(correlation,annot=False,cmap='Blues')","metadata":{"id":"fXgfbFJ00X0W","outputId":"8c2331b1-e41b-484c-a52b-d2b6cedcf711","execution":{"iopub.status.busy":"2022-07-26T19:07:41.308239Z","iopub.execute_input":"2022-07-26T19:07:41.308717Z","iopub.status.idle":"2022-07-26T19:07:41.629272Z","shell.execute_reply.started":"2022-07-26T19:07:41.308678Z","shell.execute_reply":"2022-07-26T19:07:41.627873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n*   f_00 to f_06 and f_14 to f_21 are independent of all other features.\n*   f_07 to f_13 and f_22 to f_28 are weakly dependent on each other.\n","metadata":{"id":"klB4HXta0ewk"}},{"cell_type":"code","source":"#Shapiro-Wilk Test\nfrom scipy.stats import shapiro\nfrom termcolor import colored\n\nfor col in data.columns:\n    stat, p_value = shapiro(data[col])\n    alpha = 0.05   \n    if p_value > alpha: \n        result = colored('Accepted', 'green')\n    else:\n        result = colored('Rejected','red')        \n    print('Feature: {}\\t Hypothesis: {}'.format(col, result))","metadata":{"id":"NBdINA2uLblU","outputId":"ac7a247a-d537-4fe3-b04d-df5ff60db946","execution":{"iopub.status.busy":"2022-07-26T19:07:46.280280Z","iopub.execute_input":"2022-07-26T19:07:46.280740Z","iopub.status.idle":"2022-07-26T19:07:46.545348Z","shell.execute_reply.started":"2022-07-26T19:07:46.280695Z","shell.execute_reply":"2022-07-26T19:07:46.544100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Q-Q Plots\nfrom scipy import stats\nfigure = plt.figure(figsize = (16,12))\nfor i in range(len(data.columns)):\n    \n    \n    ax = plt.subplot(6,5, i+1)\n    stats.probplot(data.iloc[:,i], dist='norm', plot=plt)\n    \n\n    ax.get_lines()[0].set_markersize(6.0)\n    ax.get_lines()[1].set_linewidth(3.0)\n    ax.set_xticklabels([])\n    ax.set_yticklabels([])\n    plt.title(data.columns[i])\n    \nfigure.tight_layout(h_pad=1.0, w_pad=0.5)\nplt.suptitle('Normal Q-Q Charts', y=1.02, fontsize=20)\nplt.show()","metadata":{"id":"vucyKo-T1aJM","outputId":"ade3ca2d-ba99-4cd1-bdd9-bef411da7497","execution":{"iopub.status.busy":"2022-07-26T19:07:57.910926Z","iopub.execute_input":"2022-07-26T19:07:57.911326Z","iopub.status.idle":"2022-07-26T19:08:06.553407Z","shell.execute_reply.started":"2022-07-26T19:07:57.911296Z","shell.execute_reply":"2022-07-26T19:08:06.552100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Even though f_22 to f_28 failed the Shapiro-Wilk test, they still appear to be quite close to normal distribution here","metadata":{"id":"tdv2EqPe-5LT"}},{"cell_type":"code","source":"from sklearn.preprocessing import PowerTransformer\nscaled_data = pd.DataFrame(PowerTransformer().fit_transform(data))\n\nscaled_data.columns = data.columns","metadata":{"id":"_P1QS5nTUiFb","execution":{"iopub.status.busy":"2022-07-26T19:08:15.690801Z","iopub.execute_input":"2022-07-26T19:08:15.691185Z","iopub.status.idle":"2022-07-26T19:08:19.559562Z","shell.execute_reply.started":"2022-07-26T19:08:15.691155Z","shell.execute_reply":"2022-07-26T19:08:19.558398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import BayesianGaussianMixture,GaussianMixture\nBGM = BayesianGaussianMixture(n_components=7,covariance_type='full',random_state=0)\npreds = BGM.fit_predict(scaled_data)\npp=BGM.predict_proba(scaled_data)","metadata":{"id":"jAUamCkDNRPM","execution":{"iopub.status.busy":"2022-07-26T19:08:53.686646Z","iopub.execute_input":"2022-07-26T19:08:53.687065Z","iopub.status.idle":"2022-07-26T19:10:06.247031Z","shell.execute_reply.started":"2022-07-26T19:08:53.687033Z","shell.execute_reply":"2022-07-26T19:10:06.245257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('ggplot')\nplt.figure(figsize=(15,6))\nfor i in range(BGM.means_.shape[0]):\n    plt.scatter(np.arange(scaled_data.shape[1]), BGM.means_[i])\nplt.xticks(ticks=np.arange(scaled_data.shape[1]), labels=data.columns)\nplt.xlabel('Features')\nplt.show()","metadata":{"id":"KkG8fxMrP4rG","outputId":"db5a3832-3129-4315-93bf-d31dc5df0ce6","execution":{"iopub.status.busy":"2022-07-26T19:10:27.712763Z","iopub.execute_input":"2022-07-26T19:10:27.713225Z","iopub.status.idle":"2022-07-26T19:10:28.072576Z","shell.execute_reply.started":"2022-07-26T19:10:27.713191Z","shell.execute_reply":"2022-07-26T19:10:28.071248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From this plot we can see that the features f_00 to f_06 and f_14 to f_21 don't separate the clusters at all. This means we can drop these features. So the useful features are f_07 to f_13 and f_22 to f_28","metadata":{"id":"OLiwSSAmQxiz"}},{"cell_type":"code","source":"# Drop useless features\ndrop_feats = [f'f_0{i}' for i in range(7)]\ndrop_feats = drop_feats + [f'f_{i}' for i in range(14,22)]\ncrop_data = scaled_data.drop(drop_feats, axis=1)\ncrop_data.head()","metadata":{"id":"63AI70XGSvW5","outputId":"ed3e1133-eb0f-4ce8-912b-7288d0062717","execution":{"iopub.status.busy":"2022-07-26T19:10:34.882741Z","iopub.execute_input":"2022-07-26T19:10:34.883148Z","iopub.status.idle":"2022-07-26T19:10:34.911071Z","shell.execute_reply.started":"2022-07-26T19:10:34.883116Z","shell.execute_reply":"2022-07-26T19:10:34.909846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remake predictions\nmodel_BGM_crop = GaussianMixture(n_components = 7, random_state=0)\npreds_BGM_crop = model_BGM_crop.fit_predict(crop_data)\ndata['Predicted']=preds_BGM_crop","metadata":{"id":"fJxhMlW1SvIR","execution":{"iopub.status.busy":"2022-07-26T19:10:39.005656Z","iopub.execute_input":"2022-07-26T19:10:39.006075Z","iopub.status.idle":"2022-07-26T19:10:47.467315Z","shell.execute_reply.started":"2022-07-26T19:10:39.006043Z","shell.execute_reply":"2022-07-26T19:10:47.465856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl = sns.countplot(x=data[\"Predicted\"])\npl.set_title(\"Distribution of clusters\")\nplt.show()","metadata":{"id":"szqqlCKOF4RQ","outputId":"255c7d0d-01e1-4f6a-9ec9-2f10155cb078","execution":{"iopub.status.busy":"2022-07-26T19:10:53.487657Z","iopub.execute_input":"2022-07-26T19:10:53.488190Z","iopub.status.idle":"2022-07-26T19:10:53.874614Z","shell.execute_reply.started":"2022-07-26T19:10:53.488144Z","shell.execute_reply":"2022-07-26T19:10:53.873625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"Predicted\"] = data['Predicted']\nsubmission.to_csv(\"submission.csv\", index = False)","metadata":{"id":"5aE9BT1IF6dg","execution":{"iopub.status.busy":"2022-07-26T19:11:02.957200Z","iopub.execute_input":"2022-07-26T19:11:02.957616Z","iopub.status.idle":"2022-07-26T19:11:03.121329Z","shell.execute_reply.started":"2022-07-26T19:11:02.957567Z","shell.execute_reply":"2022-07-26T19:11:03.120337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"id":"0holrfrnGHY8","outputId":"49117f0a-8d27-4575-eb57-539042475ea9","execution":{"iopub.status.busy":"2022-07-26T19:11:08.177153Z","iopub.execute_input":"2022-07-26T19:11:08.177561Z","iopub.status.idle":"2022-07-26T19:11:08.190326Z","shell.execute_reply.started":"2022-07-26T19:11:08.177526Z","shell.execute_reply":"2022-07-26T19:11:08.189053Z"},"trusted":true},"execution_count":null,"outputs":[]}]}