{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task 0 — Datasaurus Warm‑Up (Starter)\n",
    "\n",
    "**Goal:** Show why we must *always* visualize by comparing groups with nearly identical summary statistics but very different shapes when plotted.\n",
    "\n",
    "This starter uses `datasaurus_task0.csv` (long format) with four groups: `dino`, `star`, `circle`, `bullseye`.\n",
    "\n",
    "**What to do:**\n",
    "1. Load the CSV (as strings first).\n",
    "2. Compute basic stats per group (mean, std, correlation).\n",
    "3. Generate a SweetViz report (optional but recommended).\n",
    "4. Plot x vs y for each group (facet grid).\n",
    "5. Write *2–4 sentences* reflecting on why summary stats were misleading.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0) (Optional) Install packages in this environment\n",
    "Uncomment if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [
     "setup"
    ]
   },
   "outputs": [],
   "source": [
    "!pip install -q numpy pandas seaborn matplotlib sweetviz dtale\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1) Load the dataset\n",
    "Load as strings first (safer), then coerce numeric columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dataset</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>dino</td>\n",
       "      <td>61.05620938387066</td>\n",
       "      <td>50.000786041836115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>dino</td>\n",
       "      <td>57.91495193642296</td>\n",
       "      <td>59.204465996007286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>dino</td>\n",
       "      <td>61.47023196059987</td>\n",
       "      <td>43.113610600617946</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dino</td>\n",
       "      <td>57.80035367010236</td>\n",
       "      <td>47.24321395851151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>dino</td>\n",
       "      <td>53.58712459282577</td>\n",
       "      <td>50.05299250969186</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  dataset                  x                   y\n",
       "0    dino  61.05620938387066  50.000786041836115\n",
       "1    dino  57.91495193642296  59.204465996007286\n",
       "2    dino  61.47023196059987  43.113610600617946\n",
       "3    dino  57.80035367010236   47.24321395851151\n",
       "4    dino  53.58712459282577   50.05299250969186"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "csv_path = 'datasaurus_task0.csv'\n",
    "df_raw = pd.read_csv(csv_path, dtype=str)\n",
    "df_raw.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Coerce numeric columns and quick sanity checks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.DataFrame'>\n",
      "RangeIndex: 568 entries, 0 to 567\n",
      "Data columns (total 3 columns):\n",
      " #   Column   Non-Null Count  Dtype  \n",
      "---  ------   --------------  -----  \n",
      " 0   dataset  568 non-null    str    \n",
      " 1   x        568 non-null    float64\n",
      " 2   y        568 non-null    float64\n",
      "dtypes: float64(2), str(1)\n",
      "memory usage: 13.4 KB\n"
     ]
    }
   ],
   "source": [
    "df = df_raw.copy()\n",
    "for c in ['x','y']:\n",
    "    df[c] = pd.to_numeric(df[c], errors='coerce')\n",
    "df.info()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2) Basic summary stats by group (fill in)\n",
    "Compute mean, std for x & y by `dataset`, and the correlation within each group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(                  x          y\n",
       " dataset                       \n",
       " bullseye  54.502310  47.851315\n",
       " circle    53.569122  47.547658\n",
       " dino      53.833282  48.328789\n",
       " star      53.580462  47.571224,\n",
       "                  x         y\n",
       " dataset                     \n",
       " bullseye  3.693820  4.779339\n",
       " circle    3.595391  5.104593\n",
       " dino      4.054927  4.994526\n",
       " star      4.175326  4.905460,\n",
       " dataset\n",
       " bullseye   -0.018665\n",
       " circle      0.053956\n",
       " dino       -0.049008\n",
       " star        0.037045\n",
       " dtype: float64)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TODO: groupby summaries\n",
    "g = df.groupby('dataset')\n",
    "means = g[['x','y']].mean()\n",
    "stds  = g[['x','y']].std()\n",
    "corr  = g.apply(lambda d: d[['x','y']].corr().iloc[0,1])\n",
    "means, stds, corr\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3) (Optional) SweetViz profile\n",
    "Generate a quick report to observe that top-level stats look very similar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Projects\\43679_InteractiveVis\\VI_Lab_01_EDA\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "Done! Use 'show' commands to display/save.   |██████████| [100%]   00:00 -> (00:00 left)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Report task0_sweetviz_report.html was generated! NOTEBOOK/COLAB USERS: the web browser MAY not pop up, regardless, the report IS saved in your notebook/colab files.\n",
      "Wrote task0_sweetviz_report.html\n"
     ]
    }
   ],
   "source": [
    "import sweetviz as sv\n",
    "report = sv.analyze(df)\n",
    "report.show_html('task0_sweetviz_report.html')\n",
    "print('Wrote task0_sweetviz_report.html')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4) Visualize — scatter by group (facet)\n",
    "Create a facet grid with one subplot per dataset and compare shapes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47716 characters]",
      "text/plain": [
       "<Figure size 800x800 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "sns.set_theme(style='white', context='notebook')\n",
    "g = sns.FacetGrid(df, col='dataset', col_wrap=2, height=4, sharex=True, sharey=True)\n",
    "g.map_dataframe(sns.scatterplot, x='x', y='y', s=20, edgecolor=None)\n",
    "g.set_titles('{col_name}')\n",
    "for ax in g.axes.flatten():\n",
    "    ax.set_xlabel('x'); ax.set_ylabel('y')\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5) Reflection (write here)\n",
    "**Prompt:** If the per-group mean/variance/correlation were similar, why do the plots look different?\n",
    "- Which shapes do you observe?\n",
    "- What does this imply for relying solely on `.describe()` or correlation before plotting?\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
