{"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":"<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:250%;text-align:center;border-radius:10px 10px; padding:10px;\">Tabular Playground Series - Jul 2022</p>\n<p style=\"background-color:#5a90a7;color:#fff;font-family:newtimeroman;font-size:180%;text-align:center;border-radius:10px 10px; padding:10px;\">Practice your ML skills on this approachable dataset!</p>\n\n<img src=\"https://storage.googleapis.com/kaggle-competitions/kaggle/24673/logos/header.png?t=2021-01-02-00-34-25\">\n\n---\n<a id='section-0'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:120%;text-align:center;border-radius:10px 10px; padding:10px;\">Table Of Contents</p>\n<ol>\n    <li><a href=\"#section-1\" style=\"color:#5a90a7; font-size:120%;\">Before You Scroll!!</a></li>\n    <li><a href=\"#section-2\" style=\"color:#5a90a7;font-size:120%;\">Importing Necessary Libraries</a></li>\n    <li><a href=\"#section-3\" style=\"color:#5a90a7;font-size:120%;\">Data Loading and Pre-Processing</a></li>\n    <li><a href=\"#section-4\" style=\"color:#5a90a7;font-size:120%;\">Exploratory Data Analysis</a></li>\n    <li><a href=\"#section-5\" style=\"color:#5a90a7;font-size:120%;\">Normalization</a></li>\n    <li><a href=\"#section-6\" style=\"color:#5a90a7;font-size:120%;\">Modelling</a></li>\n    <li><a href=\"#section-7\" style=\"color:#5a90a7;font-size:120%;\">Saving Predictions</a></li>\n    <li><a href=\"#section-99\" style=\"color:#5a90a7;font-size:120%;\">Thank You</a></li>\n</ol>\n\n---","metadata":{}},{"cell_type":"markdown","source":"<a id='section-1'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Before You Scroll!!</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"markdown","source":"This notebook is still a work in progress.\n\nI have used BayesianGaussianMixture Clustering as of now but will shift to other clustering algorithms too later!!\n\nFor Normalizing the dataset, I have PowerTransformation.\n\nThe score of this notebook was **0.60099**.","metadata":{}},{"cell_type":"markdown","source":"---\n<a id='section-2'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Importing Necessary Libraries</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.cluster import KMeans,DBSCAN\nfrom sklearn.mixture import GaussianMixture, BayesianGaussianMixture\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler, MinMaxScaler,MaxAbsScaler, RobustScaler,PowerTransformer,QuantileTransformer\nfrom sklearn.metrics import silhouette_score\nfrom scipy.sparse import csr_matrix\nfrom tqdm import trange\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:21.468022Z","iopub.execute_input":"2022-07-11T10:23:21.468488Z","iopub.status.idle":"2022-07-11T10:23:23.305051Z","shell.execute_reply.started":"2022-07-11T10:23:21.468385Z","shell.execute_reply":"2022-07-11T10:23:23.303768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section-3'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Data Loading and Preprocessing</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:23.307328Z","iopub.execute_input":"2022-07-11T10:23:23.308049Z","iopub.status.idle":"2022-07-11T10:23:24.710443Z","shell.execute_reply.started":"2022-07-11T10:23:23.308006Z","shell.execute_reply":"2022-07-11T10:23:24.709260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:24.712017Z","iopub.execute_input":"2022-07-11T10:23:24.712693Z","iopub.status.idle":"2022-07-11T10:23:24.758162Z","shell.execute_reply.started":"2022-07-11T10:23:24.712639Z","shell.execute_reply":"2022-07-11T10:23:24.756600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:24.761328Z","iopub.execute_input":"2022-07-11T10:23:24.763524Z","iopub.status.idle":"2022-07-11T10:23:24.794636Z","shell.execute_reply.started":"2022-07-11T10:23:24.763484Z","shell.execute_reply":"2022-07-11T10:23:24.793364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All columns are integer or float type and also their is no missing data so no pre-processing is required.","metadata":{}},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:24.796273Z","iopub.execute_input":"2022-07-11T10:23:24.796721Z","iopub.status.idle":"2022-07-11T10:23:25.030304Z","shell.execute_reply.started":"2022-07-11T10:23:24.796661Z","shell.execute_reply":"2022-07-11T10:23:25.029229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:25.031870Z","iopub.execute_input":"2022-07-11T10:23:25.032905Z","iopub.status.idle":"2022-07-11T10:23:25.048551Z","shell.execute_reply.started":"2022-07-11T10:23:25.032868Z","shell.execute_reply":"2022-07-11T10:23:25.047409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section-4'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Exploratory Data Analysis</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(25,50));\nfor i,col in enumerate(df.columns):\n    plt.subplot(10,3,i+1);\n    sns.histplot(x=col,data=df);\n    fig.tight_layout()\n    plt.xlabel('');\n    plt.title(col,fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:25.050062Z","iopub.execute_input":"2022-07-11T10:23:25.050958Z","iopub.status.idle":"2022-07-11T10:23:50.685639Z","shell.execute_reply.started":"2022-07-11T10:23:25.050922Z","shell.execute_reply":"2022-07-11T10:23:50.684419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(50,50));\nsns.heatmap(df.corr(),annot=True,cmap='Blues');","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:50.687097Z","iopub.execute_input":"2022-07-11T10:23:50.687511Z","iopub.status.idle":"2022-07-11T10:23:55.143254Z","shell.execute_reply.started":"2022-07-11T10:23:50.687478Z","shell.execute_reply":"2022-07-11T10:23:55.141379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(['id'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.144755Z","iopub.execute_input":"2022-07-11T10:23:55.145491Z","iopub.status.idle":"2022-07-11T10:23:55.157821Z","shell.execute_reply.started":"2022-07-11T10:23:55.145446Z","shell.execute_reply":"2022-07-11T10:23:55.156659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.162105Z","iopub.execute_input":"2022-07-11T10:23:55.162762Z","iopub.status.idle":"2022-07-11T10:23:55.173920Z","shell.execute_reply.started":"2022-07-11T10:23:55.162726Z","shell.execute_reply":"2022-07-11T10:23:55.173049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_features =[\n'f_07','f_08', 'f_09', 'f_10',\n'f_11', 'f_12', 'f_13', 'f_22',\n'f_23', 'f_24', 'f_25','f_26',\n'f_27', 'f_28']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.175290Z","iopub.execute_input":"2022-07-11T10:23:55.175846Z","iopub.status.idle":"2022-07-11T10:23:55.181538Z","shell.execute_reply.started":"2022-07-11T10:23:55.175813Z","shell.execute_reply":"2022-07-11T10:23:55.180693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"These important features are based on the following discussion: \n[Link](https://www.kaggle.com/competitions/tabular-playground-series-jul-2022/discussion/334875)","metadata":{}},{"cell_type":"code","source":"df = df[imp_features]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.182895Z","iopub.execute_input":"2022-07-11T10:23:55.183443Z","iopub.status.idle":"2022-07-11T10:23:55.197159Z","shell.execute_reply.started":"2022-07-11T10:23:55.183410Z","shell.execute_reply":"2022-07-11T10:23:55.196238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.198695Z","iopub.execute_input":"2022-07-11T10:23:55.199264Z","iopub.status.idle":"2022-07-11T10:23:55.205003Z","shell.execute_reply.started":"2022-07-11T10:23:55.199221Z","shell.execute_reply":"2022-07-11T10:23:55.203871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section-5'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Normalization</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"cols = df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.207756Z","iopub.execute_input":"2022-07-11T10:23:55.208178Z","iopub.status.idle":"2022-07-11T10:23:55.214788Z","shell.execute_reply.started":"2022-07-11T10:23:55.208140Z","shell.execute_reply":"2022-07-11T10:23:55.213848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Standardisation of data\n# rob_scaler = RobustScaler().fit(df)\n# df = rob_scaler.fit_transform(df)\n\n# Transforming data with Yeo jhonson\npower_transformer = PowerTransformer().fit(df)\ndf = power_transformer.transform(df)\n\ndf = pd.DataFrame(df,columns = cols)\nprint(\"All features are now scaled\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:55.215813Z","iopub.execute_input":"2022-07-11T10:23:55.216700Z","iopub.status.idle":"2022-07-11T10:23:56.999162Z","shell.execute_reply.started":"2022-07-11T10:23:55.216634Z","shell.execute_reply":"2022-07-11T10:23:56.998105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:57.000352Z","iopub.execute_input":"2022-07-11T10:23:57.000652Z","iopub.status.idle":"2022-07-11T10:23:57.020389Z","shell.execute_reply.started":"2022-07-11T10:23:57.000625Z","shell.execute_reply":"2022-07-11T10:23:57.019265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n<a id='section-6'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Modelling</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"Elbow_M = KElbowVisualizer(KMeans(random_state=1), k=15)\nElbow_M.fit(df)\nElbow_M.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:23:57.021980Z","iopub.execute_input":"2022-07-11T10:23:57.022398Z","iopub.status.idle":"2022-07-11T10:25:28.503288Z","shell.execute_reply.started":"2022-07-11T10:23:57.022353Z","shell.execute_reply":"2022-07-11T10:25:28.502250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_b = BayesianGaussianMixture(n_components=7,random_state = 1,tol = 0.01,covariance_type = 'full',n_init=5).fit(df)\npred_bgm = model_b.predict(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:25:28.504682Z","iopub.execute_input":"2022-07-11T10:25:28.505763Z","iopub.status.idle":"2022-07-11T10:28:20.546494Z","shell.execute_reply.started":"2022-07-11T10:25:28.505728Z","shell.execute_reply":"2022-07-11T10:28:20.545115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section-6'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:200%;text-align:center;border-radius:10px 10px; padding:10px;\">Saving Predictions</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/tabular-playground-series-jul-2022/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:28:20.548586Z","iopub.execute_input":"2022-07-11T10:28:20.549499Z","iopub.status.idle":"2022-07-11T10:28:20.597789Z","shell.execute_reply.started":"2022-07-11T10:28:20.549447Z","shell.execute_reply":"2022-07-11T10:28:20.596469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:28:20.604310Z","iopub.execute_input":"2022-07-11T10:28:20.607705Z","iopub.status.idle":"2022-07-11T10:28:20.627378Z","shell.execute_reply.started":"2022-07-11T10:28:20.607630Z","shell.execute_reply":"2022-07-11T10:28:20.626123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Predicted'] = pred_bgm","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:28:20.633468Z","iopub.execute_input":"2022-07-11T10:28:20.636764Z","iopub.status.idle":"2022-07-11T10:28:20.642654Z","shell.execute_reply.started":"2022-07-11T10:28:20.636705Z","shell.execute_reply":"2022-07-11T10:28:20.641908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:28:20.644829Z","iopub.execute_input":"2022-07-11T10:28:20.646977Z","iopub.status.idle":"2022-07-11T10:28:20.657656Z","shell.execute_reply.started":"2022-07-11T10:28:20.646931Z","shell.execute_reply":"2022-07-11T10:28:20.656816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('Submission35.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T10:28:20.659567Z","iopub.execute_input":"2022-07-11T10:28:20.660486Z","iopub.status.idle":"2022-07-11T10:28:20.822151Z","shell.execute_reply.started":"2022-07-11T10:28:20.660430Z","shell.execute_reply":"2022-07-11T10:28:20.821015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='section-99'></a>\n<p style=\"background-color:#000066;color:#fff;font-family:newtimeroman;font-size:250%;text-align:center;border-radius:10px 10px; padding:10px;\">Thank You For Viewing</p>\n<p style=\"background-color:#5a90a7;color:#fff;font-family:newtimeroman;font-size:180%;text-align:center;border-radius:10px 10px; padding:10px;\">If you liked the notebook, do upvote it!!</p>\n\n<p style=\"text-align:center;\"><a href=\"#section-0\" style=\"background-color:#000;color:#fff;font-family:newtimeroman;text-align:center;border-radius:10px 10px; padding:10px;\">Back To Top</a></p>\n\n---","metadata":{}}]}