{"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":"# Introduction\n\nThe intention of this notebook is to get Pycaret Clustering working. I found getting Pycaret to run was a bit of nightmare unfortunately and there is definitely some oddities in this notebook but I was able to get Pycaret Kmeans to run. ","metadata":{}},{"cell_type":"markdown","source":"# Install Pycaret and then install specific Numpy version ","metadata":{}},{"cell_type":"code","source":"pip install pycaret --user","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:05:32.193646Z","iopub.execute_input":"2022-07-07T03:05:32.194292Z","iopub.status.idle":"2022-07-07T03:06:17.407883Z","shell.execute_reply.started":"2022-07-07T03:05:32.194167Z","shell.execute_reply":"2022-07-07T03:06:17.406487Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install numpy==1.20.0","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-07T03:06:17.410222Z","iopub.execute_input":"2022-07-07T03:06:17.410592Z","iopub.status.idle":"2022-07-07T03:06:33.293247Z","shell.execute_reply.started":"2022-07-07T03:06:17.410559Z","shell.execute_reply":"2022-07-07T03:06:33.291605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Install Numpy a second time while specifying version. I found I had to do this to actually get 1.20.0 to install. Does anyone know why this is? I'd love to increase my understanding here.","metadata":{}},{"cell_type":"code","source":"!pip install numpy==1.20.0","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:33.295358Z","iopub.execute_input":"2022-07-07T03:06:33.29576Z","iopub.status.idle":"2022-07-07T03:06:50.221584Z","shell.execute_reply.started":"2022-07-07T03:06:33.295725Z","shell.execute_reply":"2022-07-07T03:06:50.220562Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I found the following was required to get it to use the right numpy install:","metadata":{}},{"cell_type":"code","source":"try: \n    from pycaret.clustering import *\nexcept:\n    from pycaret.clustering import *","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:50.224084Z","iopub.execute_input":"2022-07-07T03:06:50.224456Z","iopub.status.idle":"2022-07-07T03:06:54.430984Z","shell.execute_reply.started":"2022-07-07T03:06:50.224421Z","shell.execute_reply":"2022-07-07T03:06:54.429636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport sklearn.preprocessing as preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:54.432911Z","iopub.execute_input":"2022-07-07T03:06:54.433297Z","iopub.status.idle":"2022-07-07T03:06:54.505399Z","shell.execute_reply.started":"2022-07-07T03:06:54.433261Z","shell.execute_reply":"2022-07-07T03:06:54.504045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and Look at Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:54.506864Z","iopub.execute_input":"2022-07-07T03:06:54.507242Z","iopub.status.idle":"2022-07-07T03:06:56.160993Z","shell.execute_reply.started":"2022-07-07T03:06:54.507209Z","shell.execute_reply":"2022-07-07T03:06:56.159363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_clu = setup(df, normalize = True, \n                   ignore_features = ['id'],\n                   session_id = 123,\n                   silent = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:56.162652Z","iopub.execute_input":"2022-07-07T03:06:56.163784Z","iopub.status.idle":"2022-07-07T03:06:58.992728Z","shell.execute_reply.started":"2022-07-07T03:06:56.163734Z","shell.execute_reply":"2022-07-07T03:06:58.991539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = create_model('kmeans', num_clusters = 7)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:06:58.994775Z","iopub.execute_input":"2022-07-07T03:06:58.995476Z","iopub.status.idle":"2022-07-07T03:10:06.097646Z","shell.execute_reply.started":"2022-07-07T03:06:58.995434Z","shell.execute_reply":"2022-07-07T03:10:06.096617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(kmeans, 'cluster')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:10:06.099164Z","iopub.execute_input":"2022-07-07T03:10:06.099735Z","iopub.status.idle":"2022-07-07T03:10:09.001557Z","shell.execute_reply.started":"2022-07-07T03:10:06.099701Z","shell.execute_reply":"2022-07-07T03:10:09.000589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(kmeans, 'elbow')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:10:09.005404Z","iopub.execute_input":"2022-07-07T03:10:09.006005Z","iopub.status.idle":"2022-07-07T03:11:19.854366Z","shell.execute_reply.started":"2022-07-07T03:10:09.005968Z","shell.execute_reply":"2022-07-07T03:11:19.853342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(kmeans, 'distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:11:19.856341Z","iopub.execute_input":"2022-07-07T03:11:19.857151Z","iopub.status.idle":"2022-07-07T03:11:23.053843Z","shell.execute_reply.started":"2022-07-07T03:11:19.857105Z","shell.execute_reply":"2022-07-07T03:11:23.052278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(kmeans, 'silhouette')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:11:23.055357Z","iopub.execute_input":"2022-07-07T03:11:23.055744Z","iopub.status.idle":"2022-07-07T03:17:18.409749Z","shell.execute_reply.started":"2022-07-07T03:11:23.05571Z","shell.execute_reply":"2022-07-07T03:17:18.407961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Submission","metadata":{}},{"cell_type":"code","source":"predictions = predict_model(kmeans, data=df)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:17:18.411542Z","iopub.execute_input":"2022-07-07T03:17:18.41191Z","iopub.status.idle":"2022-07-07T03:17:18.701245Z","shell.execute_reply.started":"2022-07-07T03:17:18.411878Z","shell.execute_reply":"2022-07-07T03:17:18.700145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:17:18.70352Z","iopub.execute_input":"2022-07-07T03:17:18.704557Z","iopub.status.idle":"2022-07-07T03:17:18.743423Z","shell.execute_reply.started":"2022-07-07T03:17:18.704487Z","shell.execute_reply":"2022-07-07T03:17:18.742383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")\nsubmission[\"Predicted\"] = predictions[\"Cluster\"]\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:29:29.695135Z","iopub.execute_input":"2022-07-07T03:29:29.695564Z","iopub.status.idle":"2022-07-07T03:29:29.919679Z","shell.execute_reply.started":"2022-07-07T03:29:29.695529Z","shell.execute_reply":"2022-07-07T03:29:29.918457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion\n\nI am glad that I was able to see this through. It was extremely difficult to get Pycaret to function in Kaggle for me and while I was going to attempt to do a little more with it, I decided to leave it early due to the cornucopia of issues I faced just getting it to run. ","metadata":{}}]}