{"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":"markdown","source":"# 0. Import Modules","metadata":{}},{"cell_type":"code","source":"# import sys\n# !cp ../input/rapids/rapids.21.06 /opt/conda/envs/rapids.tar.gz\n# !cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz > /dev/null\n# sys.path = [\"/opt/conda/envs/rapids/lib/python3.7/site-packages\"] + sys.path\n# sys.path = [\"/opt/conda/envs/rapids/lib/python3.7\"] + sys.path\n# sys.path = [\"/opt/conda/envs/rapids/lib\"] + sys.path \n# !cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:22.254070Z","iopub.execute_input":"2022-07-22T07:49:22.254495Z","iopub.status.idle":"2022-07-22T07:49:22.259894Z","shell.execute_reply.started":"2022-07-22T07:49:22.254407Z","shell.execute_reply":"2022-07-22T07:49:22.258870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !conda create -n rapids-22.06 -c rapidsai -c nvidia -c conda-forge cuml=22.06 python=3.9 cudatoolkit=11.5","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:22.310695Z","iopub.execute_input":"2022-07-22T07:49:22.311207Z","iopub.status.idle":"2022-07-22T07:49:22.316152Z","shell.execute_reply.started":"2022-07-22T07:49:22.311168Z","shell.execute_reply":"2022-07-22T07:49:22.315061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-22T07:49:22.318313Z","iopub.execute_input":"2022-07-22T07:49:22.319426Z","iopub.status.idle":"2022-07-22T07:49:22.333877Z","shell.execute_reply.started":"2022-07-22T07:49:22.319390Z","shell.execute_reply":"2022-07-22T07:49:22.332509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Import Data & EDA","metadata":{}},{"cell_type":"code","source":"data=pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv',index_col='id',low_memory=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:22.335469Z","iopub.execute_input":"2022-07-22T07:49:22.336265Z","iopub.status.idle":"2022-07-22T07:49:23.314344Z","shell.execute_reply.started":"2022-07-22T07:49:22.336230Z","shell.execute_reply":"2022-07-22T07:49:23.313362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.315714Z","iopub.execute_input":"2022-07-22T07:49:23.316067Z","iopub.status.idle":"2022-07-22T07:49:23.324580Z","shell.execute_reply.started":"2022-07-22T07:49:23.316032Z","shell.execute_reply":"2022-07-22T07:49:23.323661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.327744Z","iopub.execute_input":"2022-07-22T07:49:23.328354Z","iopub.status.idle":"2022-07-22T07:49:23.354485Z","shell.execute_reply.started":"2022-07-22T07:49:23.328320Z","shell.execute_reply":"2022-07-22T07:49:23.353690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.355548Z","iopub.execute_input":"2022-07-22T07:49:23.355792Z","iopub.status.idle":"2022-07-22T07:49:23.533073Z","shell.execute_reply.started":"2022-07-22T07:49:23.355770Z","shell.execute_reply":"2022-07-22T07:49:23.532180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.534383Z","iopub.execute_input":"2022-07-22T07:49:23.534704Z","iopub.status.idle":"2022-07-22T07:49:23.539760Z","shell.execute_reply.started":"2022-07-22T07:49:23.534679Z","shell.execute_reply":"2022-07-22T07:49:23.537967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig,ax = plt.subplots(3,10,figsize=(24,12))\n# for i, col in enumerate(data.columns):\n#     ax[i//10,i%10].boxplot(data[col])\n#     ax[i//10,i%10].set_title(col)   ","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.540988Z","iopub.execute_input":"2022-07-22T07:49:23.541317Z","iopub.status.idle":"2022-07-22T07:49:23.549040Z","shell.execute_reply.started":"2022-07-22T07:49:23.541283Z","shell.execute_reply":"2022-07-22T07:49:23.547774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig,ax = plt.subplots(6,5,figsize=(24,18))\n# for i, col in enumerate(data.columns):\n#     if data[col].dtypes=='int64':\n#         bins=data[col].max() - data[col].min() + 1\n#     else:\n#         bins=1000\n#     ax[i//5,i%5].hist(data[col],bins=bins)\n#     ax[i//5,i%5].set_title(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.550089Z","iopub.execute_input":"2022-07-22T07:49:23.550730Z","iopub.status.idle":"2022-07-22T07:49:23.556959Z","shell.execute_reply.started":"2022-07-22T07:49:23.550702Z","shell.execute_reply":"2022-07-22T07:49:23.556073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_corr = data.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.558276Z","iopub.execute_input":"2022-07-22T07:49:23.558767Z","iopub.status.idle":"2022-07-22T07:49:23.772685Z","shell.execute_reply.started":"2022-07-22T07:49:23.558725Z","shell.execute_reply":"2022-07-22T07:49:23.771711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_corr.style.background_gradient(cmap='coolwarm',axis=None)\n\nfig, ax = plt.subplots(figsize=(8,8))\nim = ax.imshow(data_corr,cmap='coolwarm')\nax.set_xticks(range(len(data_corr)))\nax.set_xticklabels(data_corr.index,rotation=90)\nax.set_yticks(range(len(data_corr)))\nax.set_yticklabels(data_corr.index)\nplt.colorbar(im);","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:23.774167Z","iopub.execute_input":"2022-07-22T07:49:23.774502Z","iopub.status.idle":"2022-07-22T07:49:24.289894Z","shell.execute_reply.started":"2022-07-22T07:49:23.774467Z","shell.execute_reply":"2022-07-22T07:49:24.288853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_cate=data.select_dtypes(include='int64')\ndata_cate","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:24.291417Z","iopub.execute_input":"2022-07-22T07:49:24.291784Z","iopub.status.idle":"2022-07-22T07:49:24.307297Z","shell.execute_reply.started":"2022-07-22T07:49:24.291739Z","shell.execute_reply":"2022-07-22T07:49:24.306353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_float=data.select_dtypes(include='float64')\ndata_float=data_float.loc[:,'f_22':'f_28']\ndata_float","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:24.312299Z","iopub.execute_input":"2022-07-22T07:49:24.312571Z","iopub.status.idle":"2022-07-22T07:49:24.336864Z","shell.execute_reply.started":"2022-07-22T07:49:24.312523Z","shell.execute_reply":"2022-07-22T07:49:24.335897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. GMM","metadata":{}},{"cell_type":"code","source":"# import cudf\n# import cuml","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:24.338281Z","iopub.execute_input":"2022-07-22T07:49:24.338702Z","iopub.status.idle":"2022-07-22T07:49:24.342959Z","shell.execute_reply.started":"2022-07-22T07:49:24.338667Z","shell.execute_reply":"2022-07-22T07:49:24.342020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.manifold import TSNE","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:24.344366Z","iopub.execute_input":"2022-07-22T07:49:24.345024Z","iopub.status.idle":"2022-07-22T07:49:24.353384Z","shell.execute_reply.started":"2022-07-22T07:49:24.344991Z","shell.execute_reply":"2022-07-22T07:49:24.352429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mds = MDS(n_components=2,n_init=1,n_jobs=1)\n# data_mds=mds.fit_transform(data_std)\ntsne = TSNE(n_components=2,n_iter=1000)\ndata_tsne=tsne.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:24.354915Z","iopub.execute_input":"2022-07-22T07:49:24.355262Z","iopub.status.idle":"2022-07-22T07:49:33.870980Z","shell.execute_reply.started":"2022-07-22T07:49:24.355219Z","shell.execute_reply":"2022-07-22T07:49:33.869935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import StandardScaler\n# from sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import PowerTransformer\nfrom sklearn.mixture import GaussianMixture","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:33.873747Z","iopub.execute_input":"2022-07-22T07:49:33.874372Z","iopub.status.idle":"2022-07-22T07:49:33.932267Z","shell.execute_reply.started":"2022-07-22T07:49:33.874333Z","shell.execute_reply":"2022-07-22T07:49:33.931421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler1 = MinMaxScaler()\nscaler2 = PowerTransformer()\ndata_std = pd.concat([pd.DataFrame(scaler1.fit_transform(data_float),columns=data_float.columns),pd.DataFrame(scaler2.fit_transform(data_cate),columns=data_cate.columns)],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:33.933695Z","iopub.execute_input":"2022-07-22T07:49:33.934043Z","iopub.status.idle":"2022-07-22T07:49:38.030744Z","shell.execute_reply.started":"2022-07-22T07:49:33.934010Z","shell.execute_reply":"2022-07-22T07:49:38.029593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_std","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:38.032501Z","iopub.execute_input":"2022-07-22T07:49:38.032950Z","iopub.status.idle":"2022-07-22T07:49:38.060459Z","shell.execute_reply.started":"2022-07-22T07:49:38.032911Z","shell.execute_reply":"2022-07-22T07:49:38.059335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(3,5,figsize=(24,12))\nfor i, col in enumerate(data_std.columns):\n    ax[i//5,i%5].boxplot(data_std[col])\n    ax[i//5,i%5].set_title(col)   ","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:38.061751Z","iopub.execute_input":"2022-07-22T07:49:38.062268Z","iopub.status.idle":"2022-07-22T07:49:39.413448Z","shell.execute_reply.started":"2022-07-22T07:49:38.062221Z","shell.execute_reply":"2022-07-22T07:49:39.411708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(3,5,figsize=(24,18))\nfor i, col in enumerate(data_std.columns):\n    if col in data_cate.columns:\n        bins=data[col].max() - data[col].min() + 1\n    else:\n        bins=1000\n    ax[i//5,i%5].hist(data_std[col],bins=bins)\n    ax[i//5,i%5].set_title(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:39.415281Z","iopub.execute_input":"2022-07-22T07:49:39.415854Z","iopub.status.idle":"2022-07-22T07:49:52.029222Z","shell.execute_reply.started":"2022-07-22T07:49:39.415820Z","shell.execute_reply":"2022-07-22T07:49:52.028389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_corr = data_std.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:52.030650Z","iopub.execute_input":"2022-07-22T07:49:52.030994Z","iopub.status.idle":"2022-07-22T07:49:52.095359Z","shell.execute_reply.started":"2022-07-22T07:49:52.030959Z","shell.execute_reply":"2022-07-22T07:49:52.094491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_corr.style.background_gradient(cmap='coolwarm',axis=None)\n\nfig, ax = plt.subplots(figsize=(8,8))\nim = ax.imshow(data_corr,cmap='coolwarm')\nax.set_xticks(range(len(data_corr)))\nax.set_xticklabels(data_corr.index,rotation=90)\nax.set_yticks(range(len(data_corr)))\nax.set_yticklabels(data_corr.index)\nplt.colorbar(im,shrink=0.82);","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:49:52.096966Z","iopub.execute_input":"2022-07-22T07:49:52.097321Z","iopub.status.idle":"2022-07-22T07:49:52.376423Z","shell.execute_reply.started":"2022-07-22T07:49:52.097284Z","shell.execute_reply":"2022-07-22T07:49:52.375449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cudf\n# import cuml","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:50:04.082992Z","iopub.execute_input":"2022-07-22T07:50:04.083351Z","iopub.status.idle":"2022-07-22T07:50:04.088067Z","shell.execute_reply.started":"2022-07-22T07:50:04.083319Z","shell.execute_reply":"2022-07-22T07:50:04.086944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gmm_bic = []\nfor n_clusters in range(1,20):\n    gmm = GaussianMixture(n_components=n_clusters)\n    gmm.fit(data_std)\n    data_pred = gmm.predict(data_std)\n    gmm_bic.append(gmm.bic(data_std))\n   \n    print(n_clusters,gmm.bic(data_std))","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:50:04.926599Z","iopub.execute_input":"2022-07-22T07:50:04.926949Z","iopub.status.idle":"2022-07-22T07:55:35.184149Z","shell.execute_reply.started":"2022-07-22T07:50:04.926920Z","shell.execute_reply":"2022-07-22T07:55:35.183001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(gmm_bic)\nplt.plot(gmm_bic)\nplt.title(label='gmm_bic');","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:55:35.186605Z","iopub.execute_input":"2022-07-22T07:55:35.187312Z","iopub.status.idle":"2022-07-22T07:55:35.369476Z","shell.execute_reply.started":"2022-07-22T07:55:35.187259Z","shell.execute_reply":"2022-07-22T07:55:35.368458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import BayesianGaussianMixture","metadata":{"execution":{"iopub.status.busy":"2022-07-28T00:52:44.053172Z","iopub.execute_input":"2022-07-28T00:52:44.053650Z","iopub.status.idle":"2022-07-28T00:52:44.848337Z","shell.execute_reply.started":"2022-07-28T00:52:44.053553Z","shell.execute_reply":"2022-07-28T00:52:44.847132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_clusters = 7\nbgm = BayesianGaussianMixture(n_components=n_clusters, weight_concentration_prior_type='dirichlet_distribution', tol=0.0001, max_iter=10000, verbose=1, verbose_interval=100, init_params='kmeans', n_init=10,)\nbgm.fit(data_std)\ndata_pred = bgm.predict(data_std)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:55:35.379793Z","iopub.execute_input":"2022-07-22T07:55:35.380128Z","iopub.status.idle":"2022-07-22T08:31:16.054024Z","shell.execute_reply.started":"2022-07-22T07:55:35.380094Z","shell.execute_reply":"2022-07-22T08:31:16.052490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# gmm.bic(data_std)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.055142Z","iopub.status.idle":"2022-07-22T08:31:16.056922Z","shell.execute_reply.started":"2022-07-22T08:31:16.056655Z","shell.execute_reply":"2022-07-22T08:31:16.056682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data_pred = pd.DataFrame(data_pred,index=data.index,columns=['Predicted'])\ndf_data_pred.value_counts().sort_index()\ndf_data_pred.to_csv('../working/submission.csv',index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.058390Z","iopub.status.idle":"2022-07-22T08:31:16.059174Z","shell.execute_reply.started":"2022-07-22T08:31:16.058925Z","shell.execute_reply":"2022-07-22T08:31:16.058948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_data_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.060577Z","iopub.status.idle":"2022-07-22T08:31:16.061327Z","shell.execute_reply.started":"2022-07-22T08:31:16.061077Z","shell.execute_reply":"2022-07-22T08:31:16.061100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"buf=pd.concat([df_data_pred,pd.DataFrame(data_tsne,columns=['col1','col2'])],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.062918Z","iopub.status.idle":"2022-07-22T08:31:16.063779Z","shell.execute_reply.started":"2022-07-22T08:31:16.063452Z","shell.execute_reply":"2022-07-22T08:31:16.063476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proba=pd.DataFrame(bgm.predict_proba(data_std))\nproba.to_csv('../working/proba.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.065319Z","iopub.status.idle":"2022-07-22T08:31:16.066086Z","shell.execute_reply.started":"2022-07-22T08:31:16.065832Z","shell.execute_reply":"2022-07-22T08:31:16.065855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.scatterplot(x=buf['col1'],y=buf['col2'],hue=buf['Predicted'],palette=['red','orange','yellow','green','navy','blue','purple'],alpha=0.3)\n# plt.scatter(x=buf['col1'],y=buf['col2'],c=buf['Predicted'],cmap=['red','orange','yellow','green','ivory','blue','purple'],alpha=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:31:16.067525Z","iopub.status.idle":"2022-07-22T08:31:16.068476Z","shell.execute_reply.started":"2022-07-22T08:31:16.068171Z","shell.execute_reply":"2022-07-22T08:31:16.068196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}