{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e099aa0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# help(np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d110d85e",
   "metadata": {},
   "outputs": [],
   "source": [
    "array = np.array([1,2,3,4])\n",
    "\n",
    "print(array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e69bfb56",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas import *\n",
    "\n",
    "df = DataFrame({\n",
    "    \"name\":[\"Ajay\",\"Harshada\",],\n",
    "    \"city\":[\"Pune\",\"Nashik\"],\n",
    "    \"age\":[23,22],\n",
    "    \"course\":[\"MSc.CA\",\"MSc.CA\"]\n",
    "})\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95daff59",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = df.set_index(\"name\")\n",
    "\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcd5bd3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.loc[\"Harshada\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3ac7d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = df2.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61cb876e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = read_csv(\"data\")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5807b2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8e4d23cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5], dtype=int32)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5],dtype=\"int32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e2db2f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(5,dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "78e467d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "faadb90e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[31, 31, 31],\n",
       "       [31, 31, 31],\n",
       "       [31, 31, 31]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((3,3),31)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cbbdc235",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "73ee0fee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,10,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4b381ba9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.41195796, 0.45673628, 0.94640469],\n",
       "       [0.95911271, 0.49161763, 0.06304586]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2e47c8b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 2],\n",
       "       [1, 1, 2],\n",
       "       [3, 2, 4]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(1,5,(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "42a64dc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.49901732, 1.13043626, 1.12776519],\n",
       "       [1.43584779, 1.48796517, 2.1434218 ],\n",
       "       [2.57269983, 0.92173613, 1.37378794]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(1,1,(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0262586f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0, 0],\n",
       "       [0, 1, 0, 0, 0],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [0, 0, 0, 1, 0],\n",
       "       [0, 0, 0, 0, 1]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(5,dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "95ffb7e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([ 9, 10])]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([i for i in range(1,11) ])\n",
    "print(arr)\n",
    "np.split(arr,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "984f0c7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3] [99 99] [3 2 1]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 99, 99, 3, 2, 1])\n",
    "p, q, r = np.split(a, [3, 5]) \n",
    "print(p,q,r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "95613472",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5488135 , 0.71518937, 0.60276338],\n",
       "       [0.54488318, 0.4236548 , 0.64589411]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(0)\n",
    "np.random.random((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "2f9e07d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "85c68237",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame([[\"Alex\", 10], [\"Bob\", 12], [\"Clarke\", 13]], columns=[\"Name\", \"Age\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "b9e57706",
   "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>Name</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bob</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Clarke</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Name  Age\n",
       "0    Alex   10\n",
       "1     Bob   12\n",
       "2  Clarke   13"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(df)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "b3a4afa1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "print(type(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "a9ac298a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name    object\n",
      "Age      int64\n",
      "dtype: object\n",
      "Index(['Name', 'Age'], dtype='object')\n",
      "[['Alex' 10]\n",
      " ['Bob' 12]\n",
      " ['Clarke' 13]]\n",
      "(3, 2)\n",
      "6\n",
      "2\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(df.dtypes)\n",
    "print(df.columns)\n",
    "print(df.values)\n",
    "print(df.shape)\n",
    "print(df.size)\n",
    "print(df.ndim)\n",
    "print(df.empty)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "e03960af",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\"Name\": [\"Tom\", \"Jack\", \"Steve\", \"Ricky\"], \"Age\": [28, 34, 29, 42]}\n",
    "df = pd.DataFrame(data, index=[\"rank1\", \"rank2\", \"rank3\", \"rank4\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "8ae6f1a2",
   "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>Name</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>rank1</th>\n",
       "      <td>Tom</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rank2</th>\n",
       "      <td>Jack</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rank3</th>\n",
       "      <td>Steve</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rank4</th>\n",
       "      <td>Ricky</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Age\n",
       "rank1    Tom   28\n",
       "rank2   Jack   34\n",
       "rank3  Steve   29\n",
       "rank4  Ricky   42"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "68d0e6a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name    4\n",
      "Age     4\n",
      "dtype: int64\n",
      "\n",
      "Name    TomJackSteveRicky\n",
      "Age                   133\n",
      "dtype: object\n",
      "\n",
      "Name    4\n",
      "Age     4\n",
      "dtype: int64\n",
      "\n",
      "33.25\n",
      "\n",
      "Name    Tom\n",
      "Age      42\n",
      "dtype: object\n",
      "\n",
      "Name    Jack\n",
      "Age       28\n",
      "dtype: object\n",
      "\n",
      "6.3966136874651625\n",
      "\n",
      "31.5\n",
      "\n",
      "0    28\n",
      "1    29\n",
      "2    34\n",
      "3    42\n",
      "Name: Age, dtype: int64\n",
      "\n",
      "1159536\n"
     ]
    }
   ],
   "source": [
    "print(df.count())\n",
    "print()\n",
    "\n",
    "print(df.sum())\n",
    "print()\n",
    "\n",
    "print(df.count())\n",
    "print()\n",
    "\n",
    "print(df['Age'].mean())\n",
    "print()\n",
    "\n",
    "print(df.max())\n",
    "print()\n",
    "\n",
    "print(df.min())\n",
    "print()\n",
    "\n",
    "print(df['Age'].std())\n",
    "print()\n",
    "\n",
    "print(df['Age'].median())\n",
    "print()\n",
    "\n",
    "print(df['Age'].mode())\n",
    "print()\n",
    "\n",
    "print(df['Age'].prod())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "0ae78112",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A    B\n",
      "0  1.0  NaN\n",
      "1  NaN  5.0\n",
      "2  3.0  6.0\n",
      "\n",
      "     A    B\n",
      "0  1.0  5.0\n",
      "1  3.0  5.0\n",
      "2  3.0  6.0\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\"A\": [1.0, np.nan, 3.0], \"B\": [np.nan, 5.0, 6.0]})\n",
    "print(df)\n",
    "print()\n",
    "# print(df.isnull().sum())          # A 1, B 1\n",
    "# print(df.fillna(0))\n",
    "print(df.bfill())                 # forward fill\n",
    "# print(df.dropna())                # only the last row survives\n",
    "# df[\"A\"] = df[\"A\"].fillna(df[\"A\"].mean())    # fill with column mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "292ebe0a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7990876f",
   "metadata": {},
   "source": [
    "# numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14b8e2f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import numpy \n",
    "# import numpy as np\n",
    "# from numpy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aea07e58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "l = np.array([1,2,3,4,6])\n",
    "\n",
    "print(l)\n",
    "print(type(l))\n",
    "print(np.__version__)\n",
    "\n",
    "\n",
    "print(arr[0])\n",
    "print(arr[2])\n",
    "\n",
    "print(arr[1]+arr[3])\n",
    "\n",
    "t = np.array((1,2,3,4))\n",
    "\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c89ce3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "l = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "\n",
    "print(l)\n",
    "\n",
    "print(l[1,0])\n",
    "print(l[0,2])\n",
    "print(l[0,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1f56a7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "l = np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]])\n",
    "\n",
    "print(l)\n",
    "\n",
    "print(l[0,0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c62ec062",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
    "\n",
    "print(arr[1:5:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "469c29be",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(arr[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e536c86c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "x = arr.copy()\n",
    "arr[0] = 42\n",
    "\n",
    "print(arr)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b2e5645",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "x = arr.view()\n",
    "arr[0] = 42\n",
    "\n",
    "print(arr)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae47608f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "x = arr.view()\n",
    "x[0] = 31\n",
    "\n",
    "print(arr)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac8e009c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b66319c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d37dd578",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c60ad46e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85435b1e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "476fd0a4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fe339cf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efc79c8d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24fdba09",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cee6d7a6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de2750e8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7526371",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ec87d9e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d60273a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2be6a23d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36194b3e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14389daf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5f8dbb8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f555a35",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38034a92",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de253b69",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bae61afc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4d209fa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23f9f7e8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e74acd74",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d40f262",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4632b23b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccf6fdc6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d75da292",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bb03e2b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77c2bb39",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ad04478",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "760e4441",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7499b1b7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1af76fa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[30 36 37 33 34 36 30]\n",
      " [40 25 41 26 37 32 38]\n",
      " [31 30 36 35 39 29 34]\n",
      " [25 38 34 34 32 26 25]\n",
      " [33 38 40 35 33 32 28]]\n",
      "Average temp of Mumbai: 33.71\n",
      "Average temp of Pune: 34.14\n",
      "Average temp of Delhi: 33.43\n",
      "Average temp of Chennai: 30.57\n",
      "Average temp of Kolkata: 34.14\n",
      "Hottest day overall: Day 3 in Pune (41 C)\n",
      "Hottest day in Mumbai: Day 3\n",
      "Hottest day in Pune: Day 3\n",
      "Hottest day in Delhi: Day 5\n",
      "Hottest day in Chennai: Day 2\n",
      "Hottest day in Kolkata: Day 3\n",
      "City with highest average: Pune\n",
      "Days when any city went above 35 C: [1 2 3 5 6 7]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(1)\n",
    "\n",
    "cities = [\"Mumbai\", \"Pune\", \"Delhi\", \"Chennai\", \"Kolkata\"]\n",
    "temps = np.random.randint(25, 42, size=(5, 7))\n",
    "print(temps)\n",
    "\n",
    "avg = temps.mean(axis=1)\n",
    "for i in range(5):\n",
    "    print(f\"Average temp of {cities[i]}: {avg[i]:.2f}\")\n",
    "\n",
    "row, col = np.unravel_index(temps.argmax(), temps.shape)\n",
    "print(f\"Hottest day overall: Day {col + 1} in {cities[row]} ({temps[row, col]} C)\")\n",
    "\n",
    "for i in range(5):\n",
    "    print(f\"Hottest day in {cities[i]}: Day {temps[i].argmax() + 1}\")\n",
    "\n",
    "print(\"City with highest average:\", cities[avg.argmax()])\n",
    "\n",
    "hot_days = np.where((temps > 35).any(axis=0))[0] + 1\n",
    "print(\"Days when any city went above 35 C:\", hot_days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a270947c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Grayscale:\n",
      " [[152.3  41.1 153.5  43.9]\n",
      " [101.  137.2 137.  190.4]\n",
      " [ 73.1  45.  208.3  91.7]\n",
      " [149.7 137.8 105.8 118.1]]\n",
      "Binary:\n",
      " [[255   0 255   0]\n",
      " [  0 255 255 255]\n",
      " [  0   0 255   0]\n",
      " [255 255   0   0]]\n"
     ]
    }
   ],
   "source": [
    "image = np.random.randint(0, 256, size=(4, 4, 3))\n",
    "gray = 0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]\n",
    "print(\"Grayscale:\\n\", gray.round(1))\n",
    "binary = np.where(gray > 128, 255, 0)\n",
    "print(\"Binary:\\n\", binary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b9768fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DNA: TAGAGACAGGTATAACATCGATGTACAAAA\n",
      "A : 14\n",
      "C : 4\n",
      "G : 6\n",
      "T : 6\n",
      "Most common base: A\n",
      "Repeated sequences longer than 2:\n",
      "AAAA\n",
      "Reversed DNA: AAAACATGTAGCTACAATATGGACAGAGAT\n"
     ]
    }
   ],
   "source": [
    "dna = np.random.choice([\"A\", \"T\", \"C\", \"G\"], size=30)\n",
    "print(\"DNA:\", \"\".join(dna))\n",
    "\n",
    "bases, counts = np.unique(dna, return_counts=True)\n",
    "for b, c in zip(bases, counts):\n",
    "    print(f\"{b} : {c}\")\n",
    "print(\"Most common base:\", bases[counts.argmax()])\n",
    "\n",
    "threshold = 2   # find runs of the same base longer than this\n",
    "print(f\"Repeated sequences longer than {threshold}:\")\n",
    "run = 1\n",
    "for i in range(1, len(dna) + 1):\n",
    "    if i < len(dna) and dna[i] == dna[i - 1]:\n",
    "        run += 1\n",
    "    else:\n",
    "        if run > threshold:\n",
    "            print(dna[i - 1] * run)\n",
    "        run = 1\n",
    "\n",
    "print(\"Reversed DNA:\", \"\".join(dna[::-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fdcf1206",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[101.63  97.92 115.69  95.1 ]\n",
      " [101.54 106.09  94.45 102.84]\n",
      " [105.51  85.05 107.61 105.91]\n",
      " [ 94.16 110.53  94.26 110.11]\n",
      " [115.25  80.47  99.92  82.95]\n",
      " [111.48  82.56  94.21 117.67]\n",
      " [ 95.19 110.52 110.86  92.05]\n",
      " [110.91  86.12 103.15  80.36]\n",
      " [108.36  98.83 110.58  98.7 ]\n",
      " [ 90.76 113.27 102.05  82.8 ]]\n",
      "Daily % change:\n",
      " [[ -0.09   8.34 -18.36   8.14]\n",
      " [  3.91 -19.83  13.93   2.99]\n",
      " [-10.76  29.96 -12.41   3.97]\n",
      " [ 22.4  -27.2    6.   -24.67]\n",
      " [ -3.27   2.6   -5.71  41.86]\n",
      " [-14.61  33.87  17.67 -21.77]\n",
      " [ 16.51 -22.08  -6.95 -12.7 ]\n",
      " [ -2.3   14.76   7.2   22.82]\n",
      " [-16.24  14.61  -7.71 -16.11]]\n",
      "Most volatile stock: Stock B\n",
      "Best performing stock: Stock B (15.68%)\n",
      "Stock C dropped 18.36% on Day 2\n",
      "Stock B dropped 19.83% on Day 3\n",
      "Stock A dropped 10.76% on Day 4\n",
      "Stock C dropped 12.41% on Day 4\n",
      "Stock B dropped 27.20% on Day 5\n",
      "Stock D dropped 24.67% on Day 5\n",
      "Stock C dropped 5.71% on Day 6\n",
      "Stock A dropped 14.61% on Day 7\n",
      "Stock D dropped 21.77% on Day 7\n",
      "Stock B dropped 22.08% on Day 8\n",
      "Stock C dropped 6.95% on Day 8\n",
      "Stock D dropped 12.70% on Day 8\n",
      "Stock A dropped 16.24% on Day 10\n",
      "Stock C dropped 7.71% on Day 10\n",
      "Stock D dropped 16.11% on Day 10\n"
     ]
    }
   ],
   "source": [
    "stocks = [\"Stock A\", \"Stock B\", \"Stock C\", \"Stock D\"]\n",
    "prices = np.random.uniform(80, 120, size=(10, 4)).round(2)   # 10 days x 4 stocks\n",
    "print(prices)\n",
    "\n",
    "change = (prices[1:] - prices[:-1]) / prices[:-1] * 100\n",
    "print(\"Daily % change:\\n\", change.round(2))\n",
    "\n",
    "variance = change.var(axis=0)\n",
    "print(\"Most volatile stock:\", stocks[variance.argmax()])\n",
    "\n",
    "total_return = (prices[-1] - prices[0]) / prices[0] * 100\n",
    "print(\"Best performing stock:\", stocks[total_return.argmax()],\n",
    "      f\"({total_return.max():.2f}%)\")\n",
    "\n",
    "days, stock_idx = np.where(change < -5)\n",
    "for d, s in zip(days, stock_idx):\n",
    "    print(f\"{stocks[s]} dropped {abs(change[d, s]):.2f}% on Day {d + 2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e6a4e0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49f25c4a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61ff168a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3726ef4b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6062e68a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2f7cf46",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86fd0448",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ce89d75",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8f3bdf4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be5555ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "raw",
   "id": "0d1879d3",
   "metadata": {},
   "source": [
    "Case Study 1: Climate Data Analysis\n",
    "Problem:\n",
    "You are working as a data scientist analyzing climate data. The dataset consists of\n",
    "temperature recordings from 5 cities over 7 days. You are tasked with:\n",
    "1. Calculating the average temperature per city.\n",
    "2. Finding the hottest day overall and in each city.\n",
    "3. Identifying the city with the highest average temperature.\n",
    "4. Identifying the days where the temperature in any city exceeded 35°C."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d615f62f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[30 36 37 33 34 36 30]\n",
      " [40 25 41 26 37 32 38]\n",
      " [31 43 30 43 36 35 39]\n",
      " [43 29 34 42 25 38 34]\n",
      " [34 32 26 25 42 33 38]]\n",
      "City : Pune\ttemp : 33.714285714285715\n",
      "City : Mumbai\ttemp : 34.142857142857146\n",
      "City : Kolkata\ttemp : 36.714285714285715\n",
      "City : Delhi\ttemp : 35.0\n",
      "City : Chennai\ttemp : 32.857142857142854\n",
      "Hottest Day over all : \n",
      "Day 2 Kolkata  43 C\n",
      "Pune\t3\n",
      "Mumbai\t3\n",
      "Kolkata\t2\n",
      "Delhi\t1\n",
      "Chennai\t5\n",
      "City with high avg temp Kolkata\n",
      "[1 2 3 4]\n",
      "Mumbai\n",
      "Kolkata\n",
      "Delhi\n",
      "Chennai\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(1)\n",
    "\n",
    "cities = [\"Pune\",\"Mumbai\",\"Kolkata\",\"Delhi\",\"Chennai\"]\n",
    "temps = np.random.randint(25,45, size=(5,7))\n",
    "print(temps)\n",
    "\n",
    "avg = temps.mean(axis=1)\n",
    "\n",
    "for i in range(5):\n",
    "    print(f\"City : {cities[i]}\\ttemp : {avg[i]}\")\n",
    "\n",
    "row, col = np.unravel_index(temps.argmax(), temps.shape)\n",
    "print(f\"Hottest Day over all : \\nDay {col+1} {cities[row]}  {temps[row,col]} C\")\n",
    "\n",
    "for i in range(5):\n",
    "    print(f\"{cities[i]}\\t{temps[i].argmax()+1}\") \n",
    "    \n",
    "print(f\"City with high avg temp {cities[avg.argmax()]}\")\n",
    "\n",
    "hot_days= np.where((temps > 40).any(axis=1))[0]\n",
    "\n",
    "print(hot_days)\n",
    "for i in hot_days:\n",
    "    print(cities[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "30a79b9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.     0.     0.     0.  ]\n",
      " [-15.54  -6.2   -1.59  -1.23]\n",
      " [-20.92  -2.43  -4.48  -4.39]\n",
      " [-24.61  31.46  12.47  -0.6 ]\n",
      " [ -7.76  20.31  16.25   0.82]\n",
      " [-22.65  -3.38  25.91 -13.87]\n",
      " [-24.81  16.47   5.28  -3.05]\n",
      " [ -0.33  -0.87  -4.38   6.33]\n",
      " [  5.26  20.31  27.75  -9.29]]\n",
      "[124.81884147 167.3604402  140.94528935  30.98610127]\n",
      "A 14.774017715194173\n",
      "A 15.538077882217536 3\n",
      "B 6.197741574002251 3\n",
      "A 20.923132183908045 4\n",
      "A 24.60616438356164 5\n",
      "A 7.761600502775738 6\n",
      "A 22.64581869290232 7\n",
      "D 13.866532528504363 7\n",
      "A 24.812158469945356 8\n",
      "D 9.288363058449594 10\n"
     ]
    }
   ],
   "source": [
    "stocks = [\"A\",\"B\",\"C\",\"D\"]\n",
    "prices = np.random.uniform(80,120,size=(10,4)).round(2)\n",
    "\n",
    "\n",
    "changes = (prices[:1] - prices[:-1])/prices[:-1]*100\n",
    "print(changes.round(2))\n",
    "\n",
    "variance = changes.var(axis=0)\n",
    "\n",
    "print(variance)\n",
    "\n",
    "total_return = (prices[-1]-prices[0])/prices[0]*100\n",
    "print(stocks[total_return.argmax()],total_return.max())\n",
    "\n",
    "\n",
    "days, stock_inx = np.where(changes < -5)\n",
    "for d,s in zip(days, stock_inx):\n",
    "    print(stocks[s],abs(changes[d,s]),d+2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "52447301",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D' 'D' 'C' 'C' 'A' 'C' 'D' 'C' 'C' 'D' 'A' 'D' 'D' 'C' 'D' 'D' 'D' 'A'\n",
      " 'A' 'C' 'D' 'C' 'B' 'D' 'A' 'C' 'D' 'B' 'C' 'D'] DDCCACDCCDADDCDDDAACDCBDACDBCD\n",
      "A 5\n",
      "B 2\n",
      "C 10\n",
      "D 13\n",
      "D\n",
      "DDD\n",
      "DCBDCADBCDCAADDDCDDADCCDCACCDD\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "dna = np.random.choice([\"A\",\"B\",\"C\",\"D\"],30)\n",
    "print(dna,\"\".join(dna))\n",
    "\n",
    "\n",
    "bases, count = np.unique(dna, return_counts=True)\n",
    "for b, c in zip(bases, count):\n",
    "    print(b,c)\n",
    "\n",
    "print(bases[count.argmax()])\n",
    "threshold =2 \n",
    "run =1\n",
    "\n",
    "for i in range(1,len(dna)+1):\n",
    "    if i < len(dna) and dna[i] == dna[i-1]:\n",
    "        run+=1\n",
    "    else:\n",
    "        if run > threshold:\n",
    "            print(dna[i-1]*run)\n",
    "        run =1\n",
    "        \n",
    "print(\"\".join(dna)[::-1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
