{
  "metadata": {
    "kernelspec": {
      "name": "python",
      "display_name": "Python (Pyodide)",
      "language": "python"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "python",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8"
    }
  },
  "nbformat_minor": 5,
  "nbformat": 4,
  "cells": [
    {
      "id": "86da0179-ef95-47a5-a285-ca3628301e2b",
      "cell_type": "code",
      "source": "import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nfrom sklearn.preprocessing import StandardScaler",
      "metadata": {
        "trusted": true
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": "Matplotlib is building the font cache; this may take a moment.\n"
        }
      ],
      "execution_count": 1
    },
    {
      "id": "ec894fff-8bc6-4b0c-aabe-10ce06b5a772",
      "cell_type": "code",
      "source": "data = pd.read_csv(\"sales_data_sample.csv\", encoding='latin1')\nprint(data.head())",
      "metadata": {
        "trusted": true
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": "   ORDERNUMBER  QUANTITYORDERED  PRICEEACH  ORDERLINENUMBER    SALES  \\\n0        10107               30      95.70                2  2871.00   \n1        10121               34      81.35                5  2765.90   \n2        10134               41      94.74                2  3884.34   \n3        10145               45      83.26                6  3746.70   \n4        10159               49     100.00               14  5205.27   \n\n         ORDERDATE   STATUS  QTR_ID  MONTH_ID  YEAR_ID  ...  \\\n0   2/24/2003 0:00  Shipped       1         2     2003  ...   \n1    5/7/2003 0:00  Shipped       2         5     2003  ...   \n2    7/1/2003 0:00  Shipped       3         7     2003  ...   \n3   8/25/2003 0:00  Shipped       3         8     2003  ...   \n4  10/10/2003 0:00  Shipped       4        10     2003  ...   \n\n                    ADDRESSLINE1  ADDRESSLINE2           CITY STATE  \\\n0        897 Long Airport Avenue           NaN            NYC    NY   \n1             59 rue de l'Abbaye           NaN          Reims   NaN   \n2  27 rue du Colonel Pierre Avia           NaN          Paris   NaN   \n3             78934 Hillside Dr.           NaN       Pasadena    CA   \n4                7734 Strong St.           NaN  San Francisco    CA   \n\n  POSTALCODE COUNTRY TERRITORY CONTACTLASTNAME CONTACTFIRSTNAME DEALSIZE  \n0      10022     USA       NaN              Yu             Kwai    Small  \n1      51100  France      EMEA         Henriot             Paul    Small  \n2      75508  France      EMEA        Da Cunha           Daniel   Medium  \n3      90003     USA       NaN           Young            Julie   Medium  \n4        NaN     USA       NaN           Brown            Julie   Medium  \n\n[5 rows x 25 columns]\n"
        }
      ],
      "execution_count": 2
    },
    {
      "id": "22332424-8530-4d22-b641-5376e66e7e5c",
      "cell_type": "code",
      "source": "numeric_cols = ['QUANTITYORDERED', 'PRICEEACH', 'SALES']\ndf = data[numeric_cols].dropna()",
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "execution_count": 3
    },
    {
      "id": "cab54e6b-f2d1-4b8b-99c1-d0589d2ac625",
      "cell_type": "code",
      "source": "scaler = StandardScaler()\nscaled_data = scaler.fit_transform(df)",
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "execution_count": 4
    },
    {
      "id": "2f7e7ef6-d130-4eeb-bf08-9458a0329fd8",
      "cell_type": "code",
      "source": "wcss = []\nfor k in range(1, 11):\n    kmeans = KMeans(n_clusters=k, init='k-means++', random_state=42)\n    kmeans.fit(scaled_data)\n    wcss.append(kmeans.inertia_)",
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "execution_count": 5
    },
    {
      "id": "4465b2dc-acd6-4a4e-8129-28859d2b4b1d",
      "cell_type": "code",
      "source": "plt.figure(figsize=(8, 5))\nplt.plot(range(1, 11), wcss, marker='o')\nplt.title('Elbow Method to Determine Optimal k')\nplt.xlabel('Number of Clusters (k)')\nplt.ylabel('WCSS')\nplt.grid(True)\nplt.show()",
      "metadata": {
        "trusted": true
      },
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 800x500 with 1 Axes>",
            "image/png": "[encoded data, 43012 characters]"
          },
          "metadata": {}
        }
      ],
      "execution_count": 6
    },
    {
      "id": "a847fed0-109c-4e7b-b661-fabfa7efcf67",
      "cell_type": "code",
      "source": "optimal_k = 3\nkmeans_final = KMeans(n_clusters=optimal_k, init='k-means++', random_state=42)\ndf['Cluster'] = kmeans_final.fit_predict(scaled_data)",
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "execution_count": 7
    },
    {
      "id": "ce36d17c-594d-4c4d-a545-8117967ff928",
      "cell_type": "code",
      "source": "print(\"\\nCluster Centers (Scaled Data):\\n\", kmeans_final.cluster_centers_)",
      "metadata": {
        "trusted": true
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": "\nCluster Centers (Scaled Data):\n [[ 0.00340709 -1.31519107 -0.83846133]\n [ 0.89846585  0.64278204  1.10107381]\n [-0.79188456  0.55612886 -0.2525054 ]]\n"
        }
      ],
      "execution_count": 8
    },
    {
      "id": "c518674f-6b38-4276-929c-89b1fba8c758",
      "cell_type": "code",
      "source": "print(df.head())\n\nplt.figure(figsize=(8,5))\nplt.scatter(scaled_data[:, 0], scaled_data[:, 1], c=df['Cluster'], cmap='viridis', s=30)\nplt.title('K-Means Clustering (Sales Data)')\nplt.xlabel('Quantity Ordered (scaled)')\nplt.ylabel('Price Each (scaled)')\nplt.show()",
      "metadata": {
        "trusted": true
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": "   QUANTITYORDERED  PRICEEACH    SALES  Cluster\n0               30      95.70  2871.00        2\n1               34      81.35  2765.90        2\n2               41      94.74  3884.34        1\n3               45      83.26  3746.70        1\n4               49     100.00  5205.27        1\n"
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 800x500 with 1 Axes>",
            "image/png": "[encoded data, 129624 characters]"
          },
          "metadata": {}
        }
      ],
      "execution_count": 9
    },
    {
      "id": "d1a7c7cc-52ee-4ca3-8926-dee4c6c79ab9",
      "cell_type": "code",
      "source": "",
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "execution_count": null
    }
  ]
}