{"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 matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:38:34.331182Z","iopub.execute_input":"2022-07-29T08:38:34.331648Z","iopub.status.idle":"2022-07-29T08:38:35.013881Z","shell.execute_reply.started":"2022-07-29T08:38:34.331548Z","shell.execute_reply":"2022-07-29T08:38:35.012690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:31.118686Z","iopub.execute_input":"2022-07-29T08:39:31.119225Z","iopub.status.idle":"2022-07-29T08:39:32.544080Z","shell.execute_reply.started":"2022-07-29T08:39:31.119180Z","shell.execute_reply":"2022-07-29T08:39:32.542681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-29T08:39:34.476956Z","iopub.execute_input":"2022-07-29T08:39:34.477363Z","iopub.status.idle":"2022-07-29T08:39:34.487198Z","shell.execute_reply.started":"2022-07-29T08:39:34.477329Z","shell.execute_reply":"2022-07-29T08:39:34.486145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:36.119064Z","iopub.execute_input":"2022-07-29T08:39:36.119851Z","iopub.status.idle":"2022-07-29T08:39:36.156841Z","shell.execute_reply.started":"2022-07-29T08:39:36.119810Z","shell.execute_reply":"2022-07-29T08:39:36.155985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_id = train['id']","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:37.204733Z","iopub.execute_input":"2022-07-29T08:39:37.205171Z","iopub.status.idle":"2022-07-29T08:39:37.213510Z","shell.execute_reply.started":"2022-07-29T08:39:37.205134Z","shell.execute_reply":"2022-07-29T08:39:37.212173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop('id',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:38.858478Z","iopub.execute_input":"2022-07-29T08:39:38.859338Z","iopub.status.idle":"2022-07-29T08:39:38.872742Z","shell.execute_reply.started":"2022-07-29T08:39:38.859290Z","shell.execute_reply":"2022-07-29T08:39:38.871725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:39.849618Z","iopub.execute_input":"2022-07-29T08:39:39.850234Z","iopub.status.idle":"2022-07-29T08:39:39.856601Z","shell.execute_reply.started":"2022-07-29T08:39:39.850197Z","shell.execute_reply":"2022-07-29T08:39:39.855408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **EDA**","metadata":{}},{"cell_type":"markdown","source":"#### Numerical feature","metadata":{}},{"cell_type":"code","source":"num_feature = [feature for feature in train.columns if train[feature].dtype != 'o']","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:42.036629Z","iopub.execute_input":"2022-07-29T08:39:42.037769Z","iopub.status.idle":"2022-07-29T08:39:42.045231Z","shell.execute_reply.started":"2022-07-29T08:39:42.037724Z","shell.execute_reply":"2022-07-29T08:39:42.043824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of Numerical features:' ,len(num_feature))","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:42.532038Z","iopub.execute_input":"2022-07-29T08:39:42.532495Z","iopub.status.idle":"2022-07-29T08:39:42.538382Z","shell.execute_reply.started":"2022-07-29T08:39:42.532458Z","shell.execute_reply":"2022-07-29T08:39:42.537154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Discrete numerical features\ndisc_num_feature = [feature for feature in num_feature if len(train[feature].unique())<100]\n\nprint('Number of Discrete features:',len(disc_num_feature))","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:43.962893Z","iopub.execute_input":"2022-07-29T08:39:43.963952Z","iopub.status.idle":"2022-07-29T08:39:44.073928Z","shell.execute_reply.started":"2022-07-29T08:39:43.963908Z","shell.execute_reply":"2022-07-29T08:39:44.072698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature in disc_num_feature:\n    data = train.copy()\n    data[feature].hist()\n    plt.title(feature)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:44.851788Z","iopub.execute_input":"2022-07-29T08:39:44.852228Z","iopub.status.idle":"2022-07-29T08:39:46.358547Z","shell.execute_reply.started":"2022-07-29T08:39:44.852192Z","shell.execute_reply":"2022-07-29T08:39:46.357343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#continuous numerical features\n\ncont_num_feature = [feature for feature in num_feature if feature not in disc_num_feature]\n\nprint(\"Number of Continuous features: \",len(cont_num_feature))","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:46.360598Z","iopub.execute_input":"2022-07-29T08:39:46.360985Z","iopub.status.idle":"2022-07-29T08:39:46.367549Z","shell.execute_reply.started":"2022-07-29T08:39:46.360950Z","shell.execute_reply":"2022-07-29T08:39:46.366165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature in cont_num_feature:\n    data = train.copy()\n    data[feature].hist()\n    plt.title(feature)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:39:57.136268Z","iopub.execute_input":"2022-07-29T08:39:57.136679Z","iopub.status.idle":"2022-07-29T08:40:01.845293Z","shell.execute_reply.started":"2022-07-29T08:39:57.136648Z","shell.execute_reply":"2022-07-29T08:40:01.843947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature engineering","metadata":{}},{"cell_type":"code","source":"sum(train.isna().sum())","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-29T08:40:01.846993Z","iopub.execute_input":"2022-07-29T08:40:01.847506Z","iopub.status.idle":"2022-07-29T08:40:01.861430Z","shell.execute_reply.started":"2022-07-29T08:40:01.847464Z","shell.execute_reply":"2022-07-29T08:40:01.860140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create test variable as copy of train because we are going to remove outlier","metadata":{}},{"cell_type":"code","source":"test = train.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:01.895262Z","iopub.execute_input":"2022-07-29T08:40:01.896146Z","iopub.status.idle":"2022-07-29T08:40:01.906338Z","shell.execute_reply.started":"2022-07-29T08:40:01.896088Z","shell.execute_reply":"2022-07-29T08:40:01.904916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Outlier Removing","metadata":{}},{"cell_type":"markdown","source":"#### Handling Rare values in discrete numerical features","metadata":{}},{"cell_type":"code","source":"for feature in disc_num_feature:\n    temp = (train[feature].value_counts())*100/len(train)\n    temp.sort_values(ascending=False)\n    temp_df = temp[temp<0.75].index\n    train.loc[train[feature].isin(temp_df),feature] = temp_df[0]\n    #print(temp_df[0])\n    \n    \n    #temp_df = temp[temp>0.75].index\n    #train = train[(train[feature].isin(temp_df))]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:05.449484Z","iopub.execute_input":"2022-07-29T08:40:05.450241Z","iopub.status.idle":"2022-07-29T08:40:05.493918Z","shell.execute_reply.started":"2022-07-29T08:40:05.450202Z","shell.execute_reply":"2022-07-29T08:40:05.492568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:08.087077Z","iopub.execute_input":"2022-07-29T08:40:08.087496Z","iopub.status.idle":"2022-07-29T08:40:08.142990Z","shell.execute_reply.started":"2022-07-29T08:40:08.087460Z","shell.execute_reply":"2022-07-29T08:40:08.141662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['f_07'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:09.379768Z","iopub.execute_input":"2022-07-29T08:40:09.380226Z","iopub.status.idle":"2022-07-29T08:40:09.391307Z","shell.execute_reply.started":"2022-07-29T08:40:09.380188Z","shell.execute_reply":"2022-07-29T08:40:09.389791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Handling Outlier in continuous numerical features","metadata":{}},{"cell_type":"code","source":"cont_num_feature","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:11.465353Z","iopub.execute_input":"2022-07-29T08:40:11.465813Z","iopub.status.idle":"2022-07-29T08:40:11.473601Z","shell.execute_reply.started":"2022-07-29T08:40:11.465774Z","shell.execute_reply":"2022-07-29T08:40:11.472718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using Power transform on data to make distribution as Gausion like\nfrom sklearn.preprocessing import PowerTransformer","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:12.216525Z","iopub.execute_input":"2022-07-29T08:40:12.217381Z","iopub.status.idle":"2022-07-29T08:40:12.283813Z","shell.execute_reply.started":"2022-07-29T08:40:12.217331Z","shell.execute_reply":"2022-07-29T08:40:12.282480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pow_scaler = PowerTransformer()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:12.830226Z","iopub.execute_input":"2022-07-29T08:40:12.830613Z","iopub.status.idle":"2022-07-29T08:40:12.835881Z","shell.execute_reply.started":"2022-07-29T08:40:12.830582Z","shell.execute_reply":"2022-07-29T08:40:12.834561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[cont_num_feature] = pow_scaler.fit_transform(train[cont_num_feature])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:14.502499Z","iopub.execute_input":"2022-07-29T08:40:14.503748Z","iopub.status.idle":"2022-07-29T08:40:17.559165Z","shell.execute_reply.started":"2022-07-29T08:40:14.503697Z","shell.execute_reply":"2022-07-29T08:40:17.557977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:17.561552Z","iopub.execute_input":"2022-07-29T08:40:17.561919Z","iopub.status.idle":"2022-07-29T08:40:17.587853Z","shell.execute_reply.started":"2022-07-29T08:40:17.561884Z","shell.execute_reply":"2022-07-29T08:40:17.587005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create test variable as copy of train because we are going to remove outlier","metadata":{}},{"cell_type":"code","source":"#test = train.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:17.589214Z","iopub.execute_input":"2022-07-29T08:40:17.590110Z","iopub.status.idle":"2022-07-29T08:40:17.594551Z","shell.execute_reply.started":"2022-07-29T08:40:17.590073Z","shell.execute_reply":"2022-07-29T08:40:17.592928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### remove outlier in continuous numerical feature","metadata":{}},{"cell_type":"code","source":"def removing_outlier_thresholds_iqr(dataframe, col_name, th1=0.25, th3=0.75):\n    for col in col_name:\n        quartile1 = dataframe[col].quantile(th1)\n        quartile3 = dataframe[col].quantile(th3)\n        iqr = quartile3 - quartile1\n        upper_limit = quartile3 + 1.5 * iqr\n        lower_limit = quartile1 - 1.5 * iqr\n        \n        dataframe = dataframe[(dataframe[col]>lower_limit) & (dataframe[col]<upper_limit)]\n        \n        return dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:17.596993Z","iopub.execute_input":"2022-07-29T08:40:17.597645Z","iopub.status.idle":"2022-07-29T08:40:17.606640Z","shell.execute_reply.started":"2022-07-29T08:40:17.597604Z","shell.execute_reply":"2022-07-29T08:40:17.605811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = removing_outlier_thresholds_iqr(train,cont_num_feature)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:18.234536Z","iopub.execute_input":"2022-07-29T08:40:18.235384Z","iopub.status.idle":"2022-07-29T08:40:18.315759Z","shell.execute_reply.started":"2022-07-29T08:40:18.235331Z","shell.execute_reply":"2022-07-29T08:40:18.314431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scalling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:20.107207Z","iopub.execute_input":"2022-07-29T08:40:20.107590Z","iopub.status.idle":"2022-07-29T08:40:20.112915Z","shell.execute_reply.started":"2022-07-29T08:40:20.107559Z","shell.execute_reply":"2022-07-29T08:40:20.112062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"std_scaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:20.692711Z","iopub.execute_input":"2022-07-29T08:40:20.693402Z","iopub.status.idle":"2022-07-29T08:40:20.698740Z","shell.execute_reply.started":"2022-07-29T08:40:20.693347Z","shell.execute_reply":"2022-07-29T08:40:20.697547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:21.457888Z","iopub.execute_input":"2022-07-29T08:40:21.458515Z","iopub.status.idle":"2022-07-29T08:40:21.463549Z","shell.execute_reply.started":"2022-07-29T08:40:21.458466Z","shell.execute_reply":"2022-07-29T08:40:21.462601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[col] = std_scaler.fit_transform(train[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:22.101262Z","iopub.execute_input":"2022-07-29T08:40:22.102109Z","iopub.status.idle":"2022-07-29T08:40:22.199640Z","shell.execute_reply.started":"2022-07-29T08:40:22.102057Z","shell.execute_reply":"2022-07-29T08:40:22.198091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:22.921575Z","iopub.execute_input":"2022-07-29T08:40:22.921975Z","iopub.status.idle":"2022-07-29T08:40:22.950466Z","shell.execute_reply.started":"2022-07-29T08:40:22.921938Z","shell.execute_reply":"2022-07-29T08:40:22.949246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature selection","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25,25))\nsns.heatmap(train.corr(),annot=True,cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:26.836941Z","iopub.execute_input":"2022-07-29T08:40:26.838063Z","iopub.status.idle":"2022-07-29T08:40:30.883838Z","shell.execute_reply.started":"2022-07-29T08:40:26.837998Z","shell.execute_reply":"2022-07-29T08:40:30.882931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel_feature = ['f_07','f_08','f_09','f_10','f_11','f_12','f_13','f_22','f_23','f_24','f_25','f_26','f_27','f_28']","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:30.885311Z","iopub.execute_input":"2022-07-29T08:40:30.886234Z","iopub.status.idle":"2022-07-29T08:40:30.891255Z","shell.execute_reply.started":"2022-07-29T08:40:30.886197Z","shell.execute_reply":"2022-07-29T08:40:30.890188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply feature engineering and feature selection on test data","metadata":{}},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:32.422216Z","iopub.execute_input":"2022-07-29T08:40:32.422880Z","iopub.status.idle":"2022-07-29T08:40:32.448894Z","shell.execute_reply.started":"2022-07-29T08:40:32.422845Z","shell.execute_reply":"2022-07-29T08:40:32.448087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[cont_num_feature] = pow_scaler.transform(test[cont_num_feature])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:39.713700Z","iopub.execute_input":"2022-07-29T08:40:39.714157Z","iopub.status.idle":"2022-07-29T08:40:39.897326Z","shell.execute_reply.started":"2022-07-29T08:40:39.714117Z","shell.execute_reply":"2022-07-29T08:40:39.896045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:40.571333Z","iopub.execute_input":"2022-07-29T08:40:40.571756Z","iopub.status.idle":"2022-07-29T08:40:40.599663Z","shell.execute_reply.started":"2022-07-29T08:40:40.571721Z","shell.execute_reply":"2022-07-29T08:40:40.598741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[col] = std_scaler.transform(test[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:41.365900Z","iopub.execute_input":"2022-07-29T08:40:41.367106Z","iopub.status.idle":"2022-07-29T08:40:41.422295Z","shell.execute_reply.started":"2022-07-29T08:40:41.367059Z","shell.execute_reply":"2022-07-29T08:40:41.421319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.copy()\nsel_feature = ['f_07','f_08','f_09','f_10','f_11','f_12','f_13','f_22','f_23','f_24','f_25','f_26','f_27','f_28']\ntest = test[sel_feature]\nX_train = X_train[sel_feature]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:51.108659Z","iopub.execute_input":"2022-07-29T08:40:51.109088Z","iopub.status.idle":"2022-07-29T08:40:51.149821Z","shell.execute_reply.started":"2022-07-29T08:40:51.109053Z","shell.execute_reply":"2022-07-29T08:40:51.148991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-29T08:40:58.343861Z","iopub.execute_input":"2022-07-29T08:40:58.344276Z","iopub.status.idle":"2022-07-29T08:40:58.366671Z","shell.execute_reply.started":"2022-07-29T08:40:58.344242Z","shell.execute_reply":"2022-07-29T08:40:58.365771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA Transform on Test data\n#test = pca.transform(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:40:58.929135Z","iopub.execute_input":"2022-07-29T08:40:58.930188Z","iopub.status.idle":"2022-07-29T08:40:58.934436Z","shell.execute_reply.started":"2022-07-29T08:40:58.930141Z","shell.execute_reply":"2022-07-29T08:40:58.933373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:41:00.066094Z","iopub.execute_input":"2022-07-29T08:41:00.066773Z","iopub.status.idle":"2022-07-29T08:41:00.073340Z","shell.execute_reply.started":"2022-07-29T08:41:00.066727Z","shell.execute_reply":"2022-07-29T08:41:00.072371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Apply Clustering Technique","metadata":{}},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:41:03.549386Z","iopub.execute_input":"2022-07-29T08:41:03.550598Z","iopub.status.idle":"2022-07-29T08:41:04.032528Z","shell.execute_reply.started":"2022-07-29T08:41:03.550551Z","shell.execute_reply":"2022-07-29T08:41:04.031548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:41:04.325720Z","iopub.execute_input":"2022-07-29T08:41:04.326707Z","iopub.status.idle":"2022-07-29T08:41:04.377997Z","shell.execute_reply.started":"2022-07-29T08:41:04.326662Z","shell.execute_reply":"2022-07-29T08:41:04.376720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KMeans()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:41:04.962490Z","iopub.execute_input":"2022-07-29T08:41:04.962967Z","iopub.status.idle":"2022-07-29T08:41:04.969551Z","shell.execute_reply.started":"2022-07-29T08:41:04.962930Z","shell.execute_reply":"2022-07-29T08:41:04.968132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualizer = KElbowVisualizer(model,k=(2,20),timings=True)\nvisualizer.fit(X_train)\nvisualizer.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:41:05.974085Z","iopub.execute_input":"2022-07-29T08:41:05.974695Z","iopub.status.idle":"2022-07-29T08:43:51.954948Z","shell.execute_reply.started":"2022-07-29T08:41:05.974660Z","shell.execute_reply":"2022-07-29T08:43:51.953808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BGMM(Bayesian Gaussian Mixture Models Clustering Algorithm)","metadata":{}},{"cell_type":"code","source":"from sklearn.mixture import BayesianGaussianMixture","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:43:51.957041Z","iopub.execute_input":"2022-07-29T08:43:51.957500Z","iopub.status.idle":"2022-07-29T08:43:51.969096Z","shell.execute_reply.started":"2022-07-29T08:43:51.957465Z","shell.execute_reply":"2022-07-29T08:43:51.967881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bayesian_gmm = BayesianGaussianMixture(n_components=7,n_init=3,random_state=1,max_iter=200)\nbayesian_gmm.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:43:51.970691Z","iopub.execute_input":"2022-07-29T08:43:51.971234Z","iopub.status.idle":"2022-07-29T08:46:34.332429Z","shell.execute_reply.started":"2022-07-29T08:43:51.971182Z","shell.execute_reply":"2022-07-29T08:46:34.331082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=bayesian_gmm.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.336101Z","iopub.execute_input":"2022-07-29T08:46:34.337610Z","iopub.status.idle":"2022-07-29T08:46:34.528946Z","shell.execute_reply.started":"2022-07-29T08:46:34.337556Z","shell.execute_reply":"2022-07-29T08:46:34.527548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.530879Z","iopub.execute_input":"2022-07-29T08:46:34.531739Z","iopub.status.idle":"2022-07-29T08:46:34.542279Z","shell.execute_reply.started":"2022-07-29T08:46:34.531679Z","shell.execute_reply":"2022-07-29T08:46:34.540731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_id.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.544996Z","iopub.execute_input":"2022-07-29T08:46:34.546240Z","iopub.status.idle":"2022-07-29T08:46:34.563997Z","shell.execute_reply.started":"2022-07-29T08:46:34.546193Z","shell.execute_reply":"2022-07-29T08:46:34.562596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.DataFrame(pred,columns=['Predicted'])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.566957Z","iopub.execute_input":"2022-07-29T08:46:34.568056Z","iopub.status.idle":"2022-07-29T08:46:34.576476Z","shell.execute_reply.started":"2022-07-29T08:46:34.567975Z","shell.execute_reply":"2022-07-29T08:46:34.575118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([train_id,pred],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.579137Z","iopub.execute_input":"2022-07-29T08:46:34.580619Z","iopub.status.idle":"2022-07-29T08:46:34.592346Z","shell.execute_reply.started":"2022-07-29T08:46:34.580537Z","shell.execute_reply":"2022-07-29T08:46:34.590743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:34.595528Z","iopub.execute_input":"2022-07-29T08:46:34.597151Z","iopub.status.idle":"2022-07-29T08:46:34.617843Z","shell.execute_reply.started":"2022-07-29T08:46:34.597082Z","shell.execute_reply":"2022-07-29T08:46:34.616449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('./submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:56:14.154161Z","iopub.execute_input":"2022-07-29T08:56:14.154539Z","iopub.status.idle":"2022-07-29T08:56:14.318902Z","shell.execute_reply.started":"2022-07-29T08:56:14.154509Z","shell.execute_reply":"2022-07-29T08:56:14.317724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,9))\nsns.countplot(x='Predicted',data=pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:46:35.206624Z","iopub.execute_input":"2022-07-29T08:46:35.206999Z","iopub.status.idle":"2022-07-29T08:46:35.455203Z","shell.execute_reply.started":"2022-07-29T08:46:35.206965Z","shell.execute_reply":"2022-07-29T08:46:35.454075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}