{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import necessary libraries\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "# Load test module for sanity check\n",
    "from test_utils import test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Data Generation\n",
    "==="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_samples, num_features = 10, 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(10)\n",
    "data = np.random.rand(num_samples, num_features)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution\n",
    "==="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701]\n",
      " [0.22479665 0.19806286 0.76053071 0.16911084 0.08833981]\n",
      " [0.68535982 0.95339335 0.00394827 0.51219226 0.81262096]\n",
      " [0.61252607 0.72175532 0.29187607 0.91777412 0.71457578]\n",
      " [0.54254437 0.14217005 0.37334076 0.67413362 0.44183317]\n",
      " [0.43401399 0.61776698 0.51313824 0.65039718 0.60103895]\n",
      " [0.8052232  0.52164715 0.90864888 0.31923609 0.09045935]\n",
      " [0.30070006 0.11398436 0.82868133 0.04689632 0.62628715]\n",
      " [0.54758616 0.819287   0.19894754 0.8568503  0.35165264]\n",
      " [0.75464769 0.29596171 0.88393648 0.32551164 0.1650159 ]]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ Your `standardize` passed 1 tests.\n"
     ]
    }
   ],
   "source": [
    "def standardize(x):\n",
    "    \"\"\"Standardize the input data x\n",
    "\n",
    "    Args:\n",
    "        x: numpy array of shape=(num_samples, num_features)\n",
    "\n",
    "    Returns:\n",
    "        standardized data, shape=(num_samples, num_features)\n",
    "\n",
    "    >>> standardize(np.array([[1, 2], [3, 4], [5, 6]]))\n",
    "    array([[-1.22474487, -1.22474487],\n",
    "           [ 0.        ,  0.        ],\n",
    "           [ 1.22474487,  1.22474487]])\n",
    "    \"\"\"\n",
    "    ### SOLUTION\n",
    "    centered_data = x - np.mean(x, axis=0)\n",
    "    std_data = centered_data / np.std(centered_data, axis=0)\n",
    "\n",
    "    return std_data\n",
    "\n",
    "    ### TEMPLATE\n",
    "    # # ***************************************************\n",
    "    # # INSERT YOUR CODE HERE\n",
    "    # # TODO: standardize input data x\n",
    "    # # ***************************************************\n",
    "    # raise NotImplementedError\n",
    "    # return std_data\n",
    "    ### END SOLUTION\n",
    "\n",
    "\n",
    "test(standardize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.0775774  -1.34411605  0.31525355  0.80665878  0.24128662]\n",
      " [-1.81711634 -0.77630186  0.74088404 -1.25592235 -1.42276759]\n",
      " [ 0.62228127  1.64254169 -1.797091   -0.03521894  1.51565143]\n",
      " [ 0.23651339  0.90075228 -0.83122987  1.40786459  1.11788073]\n",
      " [-0.13414844 -0.95529104 -0.55795449  0.54097769  0.01136005]\n",
      " [-0.70898541  0.56774371 -0.08900028  0.45652209  0.65726018]\n",
      " [ 1.2571441   0.25993298  1.23775021 -0.72176808 -1.4141686 ]\n",
      " [-1.41508984 -1.04555188  0.96949701 -1.69076861  0.75969247]\n",
      " [-0.10744434  1.21308427 -1.14296098  1.19109415 -0.35450368]\n",
      " [ 0.98926822 -0.46279408  1.15485183 -0.69943932 -1.11169162]] \n",
      "\n",
      " [-1.88737914e-16  5.55111512e-17 -2.22044605e-17  1.11022302e-17\n",
      "  2.44249065e-16] \n",
      "\n",
      " [1. 1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "std_data = standardize(data)\n",
    "print(std_data, \"\\n\\n\", np.mean(std_data, axis=0), \"\\n\\n\", np.std(std_data, axis=0))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
