{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "43e3cce0",
   "metadata": {},
   "source": [
    "# Application Session 2\n",
    "## Descriptive Statistics with NumPy & Pandas (Google Colab)\n",
    "\n",
    "**Session goals:**\n",
    "- Compute descriptive statistics for numerical data (location and spread)\n",
    "- Use **NumPy** functions alongside Pandas\n",
    "\n",
    "**Alignment note:** Application Session 2 implements the descriptive statistics of data (location and spread) introduced in Unit 1, using Python (NumPy and Pandas libraries).\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63027af7",
   "metadata": {},
   "source": [
    "### Step 1: Import required libraries\n",
    "\n",
    "- **Pandas (`pandas`)**: for loading and organizing tabular data (covered in session 1)\n",
    "- **NumPy (`numpy`)**: a widely used Python library that helps work with numbers and perform statistical and mathematical calculations. It is commonly used in many applications such as data analysis, engineering, and scientific computing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1e5ec9a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9bf1f26",
   "metadata": {},
   "source": [
    "### Step 2: Load the dataset\n",
    "\n",
    "We use the same dataset of **11 students** introduced in Session 1.\n",
    "The dataset contains study hours and assessment results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b34e8525",
   "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>student_id</th>\n",
       "      <th>gender</th>\n",
       "      <th>hours_studied</th>\n",
       "      <th>quiz1</th>\n",
       "      <th>quiz2</th>\n",
       "      <th>score</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>S01</td>\n",
       "      <td>M</td>\n",
       "      <td>0.5</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>49</td>\n",
       "      <td>2026-01-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>S02</td>\n",
       "      <td>M</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6</td>\n",
       "      <td>7.0</td>\n",
       "      <td>65</td>\n",
       "      <td>2026-01-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>S03</td>\n",
       "      <td>F</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7</td>\n",
       "      <td>7.0</td>\n",
       "      <td>70</td>\n",
       "      <td>2026-01-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>S04</td>\n",
       "      <td>M</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7</td>\n",
       "      <td>8.0</td>\n",
       "      <td>72</td>\n",
       "      <td>2026-01-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>S05</td>\n",
       "      <td>F</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "      <td>75</td>\n",
       "      <td>2026-01-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>S06</td>\n",
       "      <td>M</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8</td>\n",
       "      <td>9.0</td>\n",
       "      <td>78</td>\n",
       "      <td>2026-01-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>S07</td>\n",
       "      <td>F</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9</td>\n",
       "      <td>9.0</td>\n",
       "      <td>80</td>\n",
       "      <td>2026-01-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>S08</td>\n",
       "      <td>M</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>2026-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>S09</td>\n",
       "      <td>F</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10</td>\n",
       "      <td>10.0</td>\n",
       "      <td>85</td>\n",
       "      <td>2026-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>S10</td>\n",
       "      <td>M</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10</td>\n",
       "      <td>10.0</td>\n",
       "      <td>88</td>\n",
       "      <td>2026-01-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>S11</td>\n",
       "      <td>F</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10</td>\n",
       "      <td>10.0</td>\n",
       "      <td>90</td>\n",
       "      <td>2026-01-22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   student_id gender  hours_studied  quiz1  quiz2  score        date\n",
       "0         S01      M            0.5      3    2.0     49  2026-01-15\n",
       "1         S02      M            2.0      6    7.0     65  2026-01-18\n",
       "2         S03      F            3.0      7    7.0     70  2026-01-18\n",
       "3         S04      M            3.0      7    8.0     72  2026-01-19\n",
       "4         S05      F            4.0      8    NaN     75  2026-01-19\n",
       "5         S06      M            4.0      8    9.0     78  2026-01-20\n",
       "6         S07      F            5.0      9    9.0     80  2026-01-20\n",
       "7         S08      M            5.0      9   10.0     82  2026-01-21\n",
       "8         S09      F            6.0     10   10.0     85  2026-01-21\n",
       "9         S10      M            7.0     10   10.0     88  2026-01-22\n",
       "10        S11      F            8.0     10   10.0     90  2026-01-22"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "csv_text = \"\"\"student_id,gender,hours_studied,quiz1,quiz2,score,date\n",
    "S01,M,0.5,3,2,49,2026-01-15\n",
    "S02,M,2,6,7,65,2026-01-18\n",
    "S03,F,3,7,7,70,2026-01-18\n",
    "S04,M,3,7,8,72,2026-01-19\n",
    "S05,F,4,8,,75,2026-01-19\n",
    "S06,M,4,8,9,78,2026-01-20\n",
    "S07,F,5,9,9,80,2026-01-20\n",
    "S08,M,5,9,10,82,2026-01-21\n",
    "S09,F,6,10,10,85,2026-01-21\n",
    "S10,M,7,10,10,88,2026-01-22\n",
    "S11,F,8,10,10,90,2026-01-22\n",
    "\"\"\"\n",
    "with open(\"students.csv\", \"w\") as f:\n",
    "    f.write(csv_text)\n",
    "\n",
    "df = pd.read_csv(\"students.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bcd5e0c",
   "metadata": {},
   "source": [
    "### Step 3: Descriptive statistics using Pandas\n",
    "\n",
    "Generate descriptive statistics using Pandas methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f49e89f5",
   "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>hours_studied</th>\n",
       "      <th>quiz1</th>\n",
       "      <th>quiz2</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>11.000000</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>11.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>4.318182</td>\n",
       "      <td>7.909091</td>\n",
       "      <td>8.200000</td>\n",
       "      <td>75.818182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.194000</td>\n",
       "      <td>2.119177</td>\n",
       "      <td>2.485514</td>\n",
       "      <td>11.745792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>49.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>7.250000</td>\n",
       "      <td>71.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>78.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>5.500000</td>\n",
       "      <td>9.500000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>83.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>8.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>90.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       hours_studied      quiz1      quiz2      score\n",
       "count      11.000000  11.000000  10.000000  11.000000\n",
       "mean        4.318182   7.909091   8.200000  75.818182\n",
       "std         2.194000   2.119177   2.485514  11.745792\n",
       "min         0.500000   3.000000   2.000000  49.000000\n",
       "25%         3.000000   7.000000   7.250000  71.000000\n",
       "50%         4.000000   8.000000   9.000000  78.000000\n",
       "75%         5.500000   9.500000  10.000000  83.500000\n",
       "max         8.000000  10.000000  10.000000  90.000000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Descriptive statistics for numerical variables (inspection only)\n",
    "df[['hours_studied', 'quiz1', 'quiz2', 'score']].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f6e9f7e",
   "metadata": {},
   "source": [
    "### Step 4: Descriptive statistics using NumPy\n",
    "\n",
    "Now we compute the same statistics using NumPy to show how numerical libraries work together. NumPy works with arrays of numbers and is used to perform fast numerical and statistical calculations.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "246c3b05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert Pandas Series to NumPy arrays\n",
    "scores = df['score'].to_numpy()\n",
    "hours = df['hours_studied'].to_numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba42e926",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75.81818181818181 78.0\n"
     ]
    }
   ],
   "source": [
    "# Central tendency using NumPy\n",
    "x_par= np.mean(scores)\n",
    "m= np.median(scores)\n",
    "\n",
    "print(x_par,m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "270395fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11.74579228335136\n"
     ]
    }
   ],
   "source": [
    "# Spread using NumPy (ddof = 1 → sample standard deviation , ddof = 0 → population standard deviation)\n",
    "std=np.std(scores, ddof=1)\n",
    "print(std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0621d88e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49 90\n"
     ]
    }
   ],
   "source": [
    "# Minimum and maximum values\n",
    "min= np.min(scores)\n",
    "max= np.max(scores)\n",
    "\n",
    "print(min,max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4620c124",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "71.0 78.0 83.5\n"
     ]
    }
   ],
   "source": [
    "# Compute the first, second (median), and third quartiles (25th, 50th, and 75th percentiles)\n",
    "q1=np.percentile(scores, 25)\n",
    "q2=np.percentile(scores, 50)\n",
    "q3=np.percentile(scores, 75)\n",
    "print(q1,q2,q3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbfd32f9",
   "metadata": {},
   "source": [
    "### Step 5: Additional Descriptive Measures (Unit 1 completion)\n",
    "\n",
    "\n",
    "Compute additional descriptive measures to extend the analysis. The following measures are included to cover the numerical summaries introduced in Unit 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d093418a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "41\n"
     ]
    }
   ],
   "source": [
    "# Range of scores\n",
    "score_range = np.max(scores) - np.min(scores)\n",
    "print(score_range)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8ca474c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    3.0\n",
       "1    4.0\n",
       "2    5.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Mode (most frequent value), NumPy does not provide a direct mode function.\n",
    "df['hours_studied'].mode()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "25aeae6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "137.96363636363634\n"
     ]
    }
   ],
   "source": [
    "# Variance (ddof = 1 → sample variance , ddof = 0 → population variance)\n",
    "var= np.var(scores, ddof=1)\n",
    "print(var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b7b2bea8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.5\n"
     ]
    }
   ],
   "source": [
    "# Interquartile Range (IQR = Q3 - Q1)\n",
    "iqr = q3 - q1\n",
    "print(iqr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ded929",
   "metadata": {},
   "source": [
    "---\n",
    "### End of Session 2\n",
    "**Note:**\n",
    "- This session implemented main numerical summary measures (location and spread) from Unit 1 using Pandas and NumPy.\n",
    "- Measures covered include mean, median, mode, variance, standard deviation, range, and IQR.\n",
    "- In Session 3, we will visualize the data using graphs.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
