{ "cells": [ { "cell_type": "markdown", "id": "781ff07a-10bb-49a5-9daf-2b87b86774e0", "metadata": {}, "source": [ "## Basic description of data \n", "\n", "Dataset of Thai fashion and cosmetics retail sellers. Each sellers Facebook post being of a different nature (video, photos, and links). Engagement metrics consist of comments, shares, and reactions (likes, shares, comments, etc)." ] }, { "cell_type": "markdown", "id": "8aff33d6-5510-428c-a10b-db8c84cb6fd0", "metadata": {}, "source": [ "## Clustering is unsupervised learning...\n", "this means that we are given no labels for each point, not knowing what each group is. The goal of clustering is to find how close alike (or not at all) features are within the dataset and find any possible patterns." ] }, { "cell_type": "markdown", "id": "73f9dca4-0aae-4a09-8403-9199bb885996", "metadata": {}, "source": [ "## Do you expect the model to work well? If not, why?\n", "I belive that the clustering model will work well with this dataset becasue of the different variables used and its value range is large." ] }, { "cell_type": "code", "execution_count": 82, "id": "b29f6fc7-5e98-4bbb-8458-71c6d5947424", "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "import numpy as np\n", "from sklearn import datasets\n", "from sklearn.cluster import KMeans\n", "from sklearn import metrics\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import itertools\n", "import itertools as it\n", "import string\n", "from sklearn.model_selection import train_test_split\n", "import os\n", "import json\n", "from huggingface_hub import HfApi\n", "import skops.io as sio\n", "from skops import card\n", "\n", "sns.set_theme(palette='colorblind')\n", "\n", "# set global random seed so that the notes are the same each time the site builds\n", "np.random.seed(1103)\n", "np.random.seed(113)" ] }, { "cell_type": "code", "execution_count": 83, "id": "57b88ef8-aef7-4395-bc2c-d140ddb125f1", "metadata": {}, "outputs": [], "source": [ "from ucimlrepo import fetch_ucirepo\n", "import pandas as pd\n", "\n", "Thai_datase = fetch_ucirepo(id=488)\n", "\n", "thai_X = pd.DataFrame(Thai_datase.data.features) \n", "\n", "thai_df = pd.DataFrame(Thai_datase.data.features) " ] }, { "cell_type": "code", "execution_count": 84, "id": "56ec5b49-eed7-4031-b3ea-f12ec43db395", "metadata": {}, "outputs": [], "source": [ "thai_X = thai_X.drop(columns=['status_type','status_published'])" ] }, { "cell_type": "code", "execution_count": 85, "id": "ab242b36-818b-4cee-859b-abdf1d06da50", "metadata": {}, "outputs": [], "source": [ "thai_X_train, thai_X_test= train_test_split(thai_X, train_size = 0.8)" ] }, { "cell_type": "code", "execution_count": 86, "id": "a9f09724-a0b0-48de-9c73-1f84e8eed0f0", "metadata": {}, "outputs": [], "source": [ "thai_df = thai_df.drop(columns=['status_type','status_published'])" ] }, { "cell_type": "code", "execution_count": null, "id": "2f2525ec-a98b-428e-9c53-68520bdb6c0e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 87, "id": "7fc50f0b-6870-4917-9ecc-0cb9a943e22a", "metadata": {}, "outputs": [], "source": [ "km = KMeans(n_clusters=5)" ] }, { "cell_type": "code", "execution_count": 88, "id": "433f8230-b5f9-4c76-8bc7-02f68e514e77", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
KMeans(n_clusters=5)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "KMeans(n_clusters=5)" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "km.fit(thai_X_train)" ] }, { "cell_type": "markdown", "id": "08f2ef95-e316-484f-9dd6-1041fa1cdf7a", "metadata": {}, "source": [ "# These plots will be show...\n", "how users interact with each facebook post: positive engagement (likes, loves, wows, hahas), negative engagement (sads, angrys), and voluntary acitons (reactions, shares, comments)" ] }, { "cell_type": "code", "execution_count": null, "id": "e22f53ac-677a-426a-ba31-f9397bbd3ab7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "thai_df['km5'] = km.predict(thai_X).astype(str)\n", "sns.pairplot(thai_df, hue='km5')" ] }, { "cell_type": "code", "execution_count": null, "id": "b7296548-7bcb-4e15-bbec-ad030c5d55f4", "metadata": {}, "outputs": [], "source": [ "sns.scatterplot(data =thai_df,x='num_comments',y='num_loves',hue='km5')" ] }, { "cell_type": "markdown", "id": "0ffd0513-16f1-49b3-bf79-1388722fc143", "metadata": {}, "source": [ "(above) clear section groups of colors" ] }, { "cell_type": "code", "execution_count": null, "id": "39b58ab4-01e8-48d8-9493-373e750a44e1", "metadata": {}, "outputs": [], "source": [ "sns.scatterplot(data =thai_df,x='num_sads',y='num_loves',hue='km5')" ] }, { "cell_type": "markdown", "id": "f244fad1-21eb-4569-a19c-49a1a69ff3c5", "metadata": {}, "source": [ "(above) Unorganized clump of plots in the corner" ] }, { "cell_type": "code", "execution_count": 74, "id": "514c8f53-442f-44ba-b51a-4e097ee8e45b", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'km4'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", "\u001b[36mFile \u001b[39m\u001b[32m~\\miniforge3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3812\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3811\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m3812\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_engine\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3813\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:167\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:196\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7088\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7096\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[31mKeyError\u001b[39m: 'km4'", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[74]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m metrics.silhouette_score(thai_X_train, \u001b[43mthai_df\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mkm4\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m)\n", "\u001b[36mFile \u001b[39m\u001b[32m~\\miniforge3\\Lib\\site-packages\\pandas\\core\\frame.py:4107\u001b[39m, in \u001b[36mDataFrame.__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4105\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m.columns.nlevels > \u001b[32m1\u001b[39m:\n\u001b[32m 4106\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._getitem_multilevel(key)\n\u001b[32m-> \u001b[39m\u001b[32m4107\u001b[39m indexer = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4108\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[32m 4109\u001b[39m indexer = [indexer]\n", "\u001b[36mFile \u001b[39m\u001b[32m~\\miniforge3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3819\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3814\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(casted_key, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[32m 3815\u001b[39m \u001b[38;5;28misinstance\u001b[39m(casted_key, abc.Iterable)\n\u001b[32m 3816\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28many\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m casted_key)\n\u001b[32m 3817\u001b[39m ):\n\u001b[32m 3818\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key)\n\u001b[32m-> \u001b[39m\u001b[32m3819\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01merr\u001b[39;00m\n\u001b[32m 3820\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[32m 3821\u001b[39m \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[32m 3822\u001b[39m \u001b[38;5;66;03m# InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[32m 3823\u001b[39m \u001b[38;5;66;03m# the TypeError.\u001b[39;00m\n\u001b[32m 3824\u001b[39m \u001b[38;5;28mself\u001b[39m._check_indexing_error(key)\n", "\u001b[31mKeyError\u001b[39m: 'km4'" ] } ], "source": [ "metrics.silhouette_score(thai_X_train, thai_df['km4'])" ] }, { "cell_type": "code", "execution_count": null, "id": "3cee6401-5ca0-4094-8abd-72f1cf36eb6b", "metadata": {}, "outputs": [], "source": [ "metrics.adjusted_mutual_info_score(thai_df['num_reactions'],thai_df['km4'])" ] }, { "cell_type": "markdown", "id": "09fc81c0-2beb-4b13-ac9a-600413e5ab2c", "metadata": {}, "source": [ "___\n", "___\n", "## Clustering" ] }, { "cell_type": "markdown", "id": "70729a6c-a13d-449e-8daf-9654cf0cefdb", "metadata": {}, "source": [ "**Describe what question you would be asking in applying clustering to this dataset. What does it mean if clustering does not work well?**" ] }, { "cell_type": "markdown", "id": "365f8d5c-daf8-4a1f-b2ca-745bafaace1d", "metadata": {}, "source": [ "**How does this task compare to what the classification task on this dataset?**\n", "\n", "For this specific dataset, finding a correlation between people social media interations is a better fit job for clustering. Classification would not be able to recognize patterns or groupings in the dataset, only predictions about certain points." ] }, { "cell_type": "markdown", "id": "cdb29b55-9e49-4600-a5a0-569e936f4ed2", "metadata": {}, "source": [ "**Evaluate how well clustering worked on the data:**\n", "\n", "* using a true clustering metric and\n", "\n", "* using visualization and\n", "\n", "* using a clustering metric that uses the ground truth labels\n" ] }, { "cell_type": "markdown", "id": "3887e4a7-cb2d-4285-b99a-58d734f7616b", "metadata": {}, "source": [ "\n", "* Does this clustering work better or worse than expected based on the classification performance (if you didn’t complete assignment 7, also apply a classifier)\n", "*\n", "___\n", "___" ] }, { "cell_type": "markdown", "id": "023feeb4-d682-408c-be46-e2ec0f62d7c7", "metadata": {}, "source": [ "**sharing code (below)**" ] }, { "cell_type": "code", "execution_count": null, "id": "9eaed342-7d5b-4b24-9e4d-9e5f59b14f43", "metadata": {}, "outputs": [], "source": [ "api = HfApi()\n", "api.create_repo(repo_id=\"CSC310-fall25/Lineker_clustering\",)" ] }, { "cell_type": "code", "execution_count": null, "id": "4793a9da-c619-4bf7-96f4-e5bbd9e8438f", "metadata": {}, "outputs": [], "source": [ "thai_X_test.to_csv('applesauce/thai_sellers.csv')" ] }, { "cell_type": "code", "execution_count": null, "id": "550e1ef2-1eec-4440-8faa-17cf4eec41a7", "metadata": {}, "outputs": [], "source": [ "config = {\n", " \"sklearn\": {\n", " \"columns\": [\n", " \"status_type\",\n", " \"status_published\",\n", " \"num_reactions\",\n", " \"num_comments\",\n", " \"num_shares\",\n", " \"num_likes\",\n", " \"num_loves\",\n", " \"num_wows\",\n", " \"num_hahas\",\n", " \"num_sads\",\n", " \"num_angrys\"\n", " ],\n", " \"environment\": [\n", " \"scikit-learn=1.0.2\"\n", " ],\n", " \"example_input\": {\n", " \"num_reactions\": [\n", " 529,\n", " 150,\n", " 227\n", " ],\n", " \"num_shares\": [\n", " 512,\n", " 0,\n", " 236\n", " ],\n", " },\n", " \"model\": {\n", " \"file\": \"model.pkl\"\n", " },\n", " \"task\": \"clustering\"\n", " }\n", "}\n", "with open(os.path.join(local_repo,'config.json'),'w')as f:\n", " f.write(json.dumps(config))" ] }, { "cell_type": "code", "execution_count": 80, "id": "08179324-2609-43be-b288-fe7457933a17", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Card(\n", " model=KMeans(n_clusters=5),\n", " Model description=This is a Clustering model t... and cosmeticsretail sellers.,\n", " Model description/Intended uses & limitations=Plots not repr...sults may vary.,\n", " Model description/Training Procedure/Hyperparameters=TableSection(9x2),\n", " Model description/Training Procedure/Model Plot=