{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a58e116-5086-4c94-8108-85679e1a9312",
   "metadata": {},
   "source": [
    "# Probleme\n",
    "\n",
    "- Resourcen für die verwendten Bibliotheken einführen\n",
    "    - Matplotlib, OpenCV, Pandas\n",
    "- Notebook verschönern\n",
    "\n",
    "- Notebook aufspalten in \"Image Processing\" & \"Data Processing\"pip install jupyterlab-vim\n",
    "\n",
    "\n",
    "Erklären:\n",
    "- Array \n",
    "- Convolution\n",
    "- CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81f850a1-0650-4ade-a5db-04f49f204dc0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting jupyterlab-vim\n",
      "  Downloading jupyterlab_vim-0.16.0-py3-none-any.whl (26 kB)\n",
      "Installing collected packages: jupyterlab-vim\n",
      "Successfully installed jupyterlab-vim-0.16.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install jupyterlab-vim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e010dba-b4aa-4c67-809b-cdf5b86b57d4",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0803e1c93de27028",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Image and Data Processing with Python\n",
    "\n",
    "This exercise aims at introducing processing of images and data with python via OpenCV and pandas libraries, respectively."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b54e5077-1cac-4b2d-91fb-961d61185c3f",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-765af1c564b972d1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Image Processing - OpenCV\n",
    "\n",
    "The task of processing images, which can be achieved through different techniques, is referred to as image processing. The output after processing the image can be in the form of an image or a feature (corners, edges etc.) of the image that be used for further analysis or decision making, e.g. detecting a dog or a cat in the image.\n",
    "\n",
    "An image can be represented as a 2D function F(x,y), where x and y are the spatial coordinates. The amplitude of the function F at a specific value of x and y represents the intensity of the image (also referred to as pixel value) at those coordinates. Therefore, an image can be defined as an array of pixels arranged in columns and rows, where, the pixel provides information about the intensity and color. An image with x,y and z coordinates represents a 3D image, which is commonly referred to as an RGB image containing red, blue and green channels.\n",
    "\n",
    "OpenCV-Open Source Computer Vision- is a library that consist of 2000+ optimized algorithms for computer vision and machine learning that can be used in several ways for processing images, such as, \n",
    "* Converting images from one color space to another, i.e., between RGB and HSV (Hue Saturation Value) or RGB and gray.\n",
    "* Smoothing of images, like applying filters to images or blurring og images. \n",
    "* Extracting foreground and background information from the images (via GrabCut algorithm)\n",
    "* Image segmentation via watershed algorithm\n",
    "\n",
    "In this exercise, we will cover some basic image processing tasks.  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "067e7a56-f944-43c3-98fa-b52d682a2ee7",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-148ec1fab164a528",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "Firstly, as you might have come across, required packages should be imported. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b80964d-8159-4646-9247-75144150cef7",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5400665f5815eb17",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: opencv-python in /home/sid/.local/lib/python3.10/site-packages (4.6.0.66)\n",
      "Requirement already satisfied: numpy>=1.17.3 in /home/sid/.local/lib/python3.10/site-packages (from opencv-python) (1.23.5)\n"
     ]
    }
   ],
   "source": [
    "!pip install opencv-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a237f0b7-f18c-420a-bcec-0af9bf0166e5",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e1bc4221601b473f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'cv2'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mcv2\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mcv\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pyplot \u001b[38;5;28;01mas\u001b[39;00m plt\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'cv2'"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a072752d-b3b9-4a54-a288-a58134d74fe6",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3f2d4fc147a89f56",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "To process an image, the image must first be read from the local directory. OpenCV provides a function imread() that is used to load the image. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.\n",
    "\n",
    "**Syntax**: cv2.imread(path, flag)\\\n",
    "**Parameters:** \\\n",
    "**path:** A string representing the path of the image to be read.\\\n",
    "**flag:** It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR\\\n",
    "**Return Value:** This method returns an image that is loaded from the specified file.\n",
    "\n",
    "The three types of flags are as follows:\n",
    "\n",
    "**cv2.IMREAD_COLOR:** It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.\\\n",
    "**cv2.IMREAD_GRAYSCALE:** It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.\\\n",
    "**cv2.IMREAD_UNCHANGED:** It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c5bde46-472f-4290-9dc8-6c4ce646a40c",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8588e60318980b90",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Exercise 1\n",
    "\n",
    "Use the imread function to read an image in color and grayscale. The image is provided in folder with the name \"Fox.png\", taken from the source below. \\\n",
    "[Image source: https://unsplash.com/photos/xUUZcpQlqpM] \\\n",
    "Note: This image is used throughout the notebook for image processing tasks. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cca7f730-9c65-4220-a695-6ab14f80a622",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c5ce8a7486a8ae87",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "file_name = 'Fox.png'\n",
    "img_color = cv.imread(file_name, 1)\n",
    "img_gray = cv.imread(file_name, 0)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7fb3711-c6b3-48c5-81ee-c770fa097364",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9cf299757db8b1f4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "Similarly, use the imshow function to display the image in color and grayscale. \n",
    "\n",
    "**Syntax:** cv2.imshow(window_name, image)\\\n",
    "**Parameters:** \\\n",
    "**window_name:** A string representing the name of the window in which image to be displayed. \\\n",
    "**image:** It is the image that is to be displayed. \\\n",
    "**Return Value:** It doesn’t returns anything. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5ea56229-4cc6-4ebf-9ee3-61db5f91650b",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5619fc21fff3d88b",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 222580 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 122448 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "plt.imshow(cv.cvtColor(img_color, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_gray, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c68b2a4-db47-41e6-9c69-f6632decc517",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-fe00931e2364ad4d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "Next, we aim at learning the following:\n",
    "\n",
    "* Access pixel values and modify them - pixel values can be accessed via the rows and column coordinates. For RGB image, an array of Red, Blue and Gree are returned, whereas, for grayscale image only the corresponding intensity is returned\n",
    "* Access image properties - shape, size,  and data type \n",
    "* Set a Region of Interest (ROI)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "227cbd20-2598-46ce-9fa0-1962cf8acc9e",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3965c065698f1c03",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 2\n",
    "\n",
    "Perform the following tasks:\n",
    "* Access only the blue pixels\n",
    "* Access only the red pixels\n",
    "* Access olny the green pixels\n",
    "* Modify the above selected pixel colors \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3252309c-8e95-49b2-8442-0b3b3d84771d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f0a051e9b4f746a6",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 71 123 160]\n",
      "71\n",
      "123\n",
      "160\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 222020 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Accessing pixels in image\n",
    "img_color = cv.imread(file_name, 1)\n",
    "px = img_color[200, 200]\n",
    "print(px)\n",
    "\n",
    "blue = img_color[200, 200, 0]\n",
    "print(blue)\n",
    "\n",
    "green = img_color[200, 200, 1]\n",
    "print(green)\n",
    "\n",
    "red = img_color[200, 200, 2]\n",
    "print(red)\n",
    "\n",
    "img_color[0:100, 0:100] = [0, 0, 0]\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_color, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e5ea468-2ca4-4c2d-a81b-ed8fb686555f",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f5ada7ce3c854693",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 3\n",
    "\n",
    "Accessing image properties:\n",
    "* Print the shape, size, and data type of the image "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6ab1b9a5-46c0-4af9-a497-3c334c4869a3",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-92bdb9c1098eb969",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(666, 1000, 3)\n",
      "(666, 1000)\n",
      "1998000\n",
      "666000\n",
      "uint8\n",
      "uint8\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# image shape\n",
    "print(img_color.shape)\n",
    "print(img_gray.shape)\n",
    "\n",
    "# total number of pixels\n",
    "print(img_color.size)\n",
    "print(img_gray.size)\n",
    "\n",
    "# data type\n",
    "print(img_color.dtype)\n",
    "print(img_gray.dtype)\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "587f2993-b5d6-4177-8337-78e18e86e08f",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-54e3c13c8ffcd289",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 4\n",
    "\n",
    "It might be required in some task to select and modify a specific region of the image. For example, in eye detection tasks, first face detection is performed over the entire image and then eyes are searched for in this specific region, instead of the entire image. Thefore, the task here is to select only the face of the Fox and display it.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "43cdabd6-a75c-4918-9fcd-8f7a5773c10b",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-48fe936535ea0e96",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 216260 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# ROI can be accessed using numpy indexing\n",
    "fox_face = img_color[90:300, 170:340]\n",
    "plt.imshow(cv.cvtColor(fox_face, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "376e3c0f-3478-4022-99ee-6c05601a9b03",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-b94f847d591245f4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Image Transformation\n",
    "## Image Rescaling\n",
    "\n",
    "Next we learn how to resize an image, which refers to the scaling of images. Scaling images is useful in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and that has several advantages: \n",
    "\n",
    "* It can reduce the time of training of a neural network that reduces the number of pixels, thus, reducing the size and complexity of the image. \n",
    "* It also helps in zooming in on images. \n",
    "\n",
    "Many times we need to resize the image i.e. either shrink it or scale it up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image.\n",
    "\n",
    "**Syntax:** cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]))\\\n",
    "**Parameters:** \\\n",
    "**src:** [required] source/input image. \\\n",
    "**dsize:** [required] desired size for the output image. \\\n",
    "**fx:** [optional] scale factor along the horizontal axis. \\\n",
    "**fy:** [optional] scale factor along the vertical axis. \\\n",
    "**interpolation:** could be one of the following values \n",
    "1. INTER_NEAREST - a nearest-neighbor interpolation \n",
    "2. INTER_LINEAR - a bilinear interpolation (used by default)  \n",
    "3. INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives more-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.\n",
    "4. INTER_CUBIC - a bicubic interpolation over 4×4 pixel neighborhood \n",
    "5. INTER_LANCZOS4 -  a Lanczos interpolation over 8×8 pixel neighborhood"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1e470ab-8ee3-4724-8a5f-69f393986e0c",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-51b9173689788391",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 5\n",
    "\n",
    "* Define a variable scale_percent that will hold the percentage by which the image should be down scaled.  \n",
    "* Use this variable to change the width and height of the image to downscale the image. You are free to use any interpolation technique. It is recommened to try all to understand their effects. \n",
    "* Print the original and downscaled dimensions of the image and display the images using imshow method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "44402f44-af93-4ece-883e-850d8310dbef",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-cb3c972bc4970a07",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original image dimensions: (666, 1000, 3); Scaled Down image dimensions: (133, 200, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 222020 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 188544 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "scale_percent = 0.2\n",
    "scaled_down_img = cv.resize(img_color, None, fx= scale_percent, fy= scale_percent, interpolation= cv.INTER_AREA)\n",
    "print(f'Original image dimensions: {img_color.shape}; Scaled Down image dimensions: {scaled_down_img.shape}')\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_color, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    " \n",
    "plt.imshow(cv.cvtColor(scaled_down_img, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8f19a89-4862-4a6e-8e43-ef19b7ec74eb",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-eebd0c890bf179a2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 6\n",
    "\n",
    "Similarly, \n",
    "* Define a variable \"scale_percent\" that will hold the percentage by which the image should be upscaled.  \n",
    "* Use this variable to change the width and height of the image to upscale the image. You are free to use any interpolation technique. It is recommened to try all to understand their effects. \n",
    "* Print the original and upscaled dimensions of the image and display the images using imshow method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "410c49df-1034-411d-8617-6fb990c97fec",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-710777ab9179613f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original image dimensions: (133, 200, 3); Scaled Down image dimensions: (266, 400, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 184644 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "scale_percent = 2.0\n",
    "scaled_up_img = cv.resize(scaled_down_img, None, fx= scale_percent, fy= scale_percent, interpolation= cv.INTER_LINEAR)\n",
    "print(f'Original image dimensions: {scaled_down_img.shape}; Scaled Down image dimensions: {scaled_up_img.shape}')\n",
    "\n",
    "plt.imshow(cv.cvtColor(scaled_up_img, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "606901ca-5f3d-4b19-a99f-95aab2424b75",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d76f038a390ce80b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Image Translation and Rotation\n",
    "\n",
    "In addition to rescaling, the location of an object can be shifted or the image can be rotated. The documentation for the translation or rotation of the image can be found at: \n",
    "https://docs.opencv.org/4.x/da/d6e/tutorial_py_geometric_transformations.html "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c28e95b1-1d4b-4962-98b9-cfef4fbb36b1",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-78e5903549ae7f5a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 7\n",
    "\n",
    "Following the documentation provided above, translate and roate the image and use the imgshow function to diplay these effects. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "03f84aca-56df-44c0-af2a-eb6a708c0dd2",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f046a6dd0d760f54",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 175332 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 219204 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "rows, cols, colors = img_color.shape\n",
    "\n",
    "# The warpAffine() function applies an affine transformation to the image. \n",
    "# After applying affine transformation, all the parallel lines in the original image will remain parallel in the output image as well.\n",
    "translation_m = np.float32([[1,0,250],[0,1,50]])\n",
    "translated_img = cv.warpAffine(img_color,translation_m,(cols,rows))\n",
    "plt.imshow(cv.cvtColor(translated_img, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "# You need the center of the image to roate it\n",
    "rotation_m = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),45,1)\n",
    "rotated_img = cv.warpAffine(img_color,rotation_m,(cols,rows))\n",
    "plt.imshow(cv.cvtColor(rotated_img, cv.COLOR_BGR2RGB))\n",
    "plt.show()\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd0e2f9d-1461-48d5-a8ca-611826588e01",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a11ec638f35fe058",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Image Filtering\n",
    "\n",
    "Changing the appearance of an image by altering the color of the pixels is known as image filtering. This enables to emphasize certain features or remove other features. For example, a low pass filter (LPF) helps in removing noise from the image, blurring the image, etc, whereas, a high pass filter (HPF) helps in finding edges in the image. OpenCV provides a function cv.filter2D to convolve a kernel with an image, where a kernel is a 2D matrix. \n",
    "\n",
    "If you're not familiar what convolution is, you can watch the video under the following link: \\\n",
    "https://www.youtube.com/watch?v=KuXjwB4LzSA&ab_channel=3Blue1Brown\n",
    "\n",
    "**Syntax:** filter2D (src, dst, ddepth, kernel) \\\n",
    "**Parameters:** \\\n",
    "**Src** – The source image to apply the filter on \\\n",
    "**Dst** – Name of the output image after applying the filter \\\n",
    "**Ddepth** – Depth of the output image [ -1 will give the output image depth as same as the input image] \\\n",
    "**Kernel** – The 2d matrix we want the image to convolve with "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d796561-1bf8-483a-8400-ee8816fb440b",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bc806435dbd4c922",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 8\n",
    "\n",
    "To apply a filter to an image, the kernel should be first defined. Perform the following:\n",
    "1. Define multiple kernel - identity, edge detection, box blur and Gaussian blur\n",
    "2. Apply these kernels to the image and display the images\n",
    "\n",
    "**Idendity Kernel -** \n",
    "$$\\begin{bmatrix} 0 & 0 & 0 \\\\ 0 & 1 & 0 \\\\ 0 & 0 & 0 \\end{bmatrix}$$\n",
    "\n",
    "**Edge detection Kernel -** \n",
    "$$\\begin{bmatrix} -1 & -1 & -1 \\\\ -1 & 8 & -1 \\\\ -1 & -1 & -1 \\end{bmatrix}$$\n",
    "\n",
    "**Box Blur Kernel -** \n",
    "$$\\frac{1}{9}\\begin{bmatrix} 1 & 1 & 1 \\\\ 1 & 1 & 1 \\\\ 1 & 1 & 1 \\end{bmatrix}$$\n",
    "\n",
    "**Gaussian Blur Kernel -** \n",
    "$$\\frac{1}{256}\\begin{bmatrix} 1 & 4 & 6 & 4 & 1 \\\\ 4 & 16 & 24 & 16 & 4 \\\\ 6 & 24 & 36 & 24 & 6 \\\\ 4 & 16 & 24 & 16 & 4 \\\\ 1 & 4 & 6 & 4 & 1 \\end{bmatrix}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "50d6574f-f75c-42d5-9700-80e231b0569e",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-61a1634d1981db99",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 224208 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 225716 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 266804 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 219004 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 216612 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "\n",
    "# will have no effect on the image\n",
    "idendity_kernel = np.array([[0, 0, 0],\n",
    "                    [0, 1, 0],\n",
    "                    [0, 0, 0]])\n",
    "\n",
    "# detects the edges in the image\n",
    "edge_dect_kernel = np.array([[-1, -1, -1],\n",
    "                    [-1, 8, -1],\n",
    "                    [-1, -1, -1]])\n",
    "\n",
    "box_blur_kernel = np.ones((3, 3), np.float32)/9\n",
    "\n",
    "gaussian_blur_kernel = (1/256) * np.array([\n",
    "                    [1, 4, 6, 4, 1],\n",
    "                    [4, 16, 24, 16, 4],\n",
    "                    [6, 24, 36, 24, 6], \n",
    "                    [4, 16, 24, 16, 4], \n",
    "                    [1, 4, 6, 4, 1]])\n",
    "\n",
    "img_1 = cv.filter2D(src=img_color, ddepth=-1, kernel=idendity_kernel)\n",
    "img_2 = cv.filter2D(src=img_color, ddepth=-1, kernel=edge_dect_kernel)\n",
    "\n",
    "# Box blur and Gaussian Blur have similar effects\n",
    "img_3 = cv.filter2D(src=img_color, ddepth=-1, kernel=box_blur_kernel)\n",
    "img_4 = cv.filter2D(src=img_color, ddepth=-1, kernel=gaussian_blur_kernel)\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_color, cv.COLOR_BGR2RGB)),plt.title('Original')\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_1, cv.COLOR_BGR2RGB)),plt.title('Idendity Kernel')\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_2, cv.COLOR_BGR2RGB)),plt.title('Edge Detection Kernel')\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_3, cv.COLOR_BGR2RGB)),plt.title('Box Blur Kernel')\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(cv.cvtColor(img_4, cv.COLOR_BGR2RGB)),plt.title('Gaussian Blur Kernel')\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88629f82-dd58-4c18-81e4-2629e12ff5b8",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-1f5e6ff25f2d107f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 9\n",
    "\n",
    "In addition to the filter2D function, where a kernel needs to be explicitly defined, OpenCV also provides functions to filter the images. Read the documentation provided by OpenCV on the following functions:\n",
    "* blur\n",
    "* GaussianBlur, \n",
    "\n",
    "apply these filters to the image and display the images.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0037c3b9-36f1-499d-aa32-adc0d52b9b32",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a5251d944ce8d9c1",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 206436 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 215332 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "blur = cv.blur(img_color,(5,5))\n",
    "plt.imshow(cv.cvtColor(blur, cv.COLOR_BGR2RGB)),plt.title('Blur')\n",
    "plt.show()\n",
    "\n",
    "gaussian_blur = cv.GaussianBlur(img_color,(5,5),0)\n",
    "plt.imshow(cv.cvtColor(gaussian_blur, cv.COLOR_BGR2RGB)),plt.title('Gaussian Blur')\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1731eddd-9ffc-48af-8429-e869fef5d705",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-de15c985fedd5592",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Data Processing - pandas\n",
    "\n",
    "Pandas is a python library commonly used for processing large data sets. It provides functions for analyzing, cleaning, exploring, and manipulating data and derive conclusions based on statistical theories. Furthermore, it enables cleaning up of messy data sets, making them more readable and relevant. Through the next few exercises, we will learn the basic functionalities of pandas.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c87ada25-0a9c-4758-88c4-3872adde9d3a",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2e3eb8ad4b829277",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: pandas in /home/sid/.local/lib/python3.10/site-packages (1.5.2)\n",
      "Requirement already satisfied: numpy>=1.21.0 in /home/sid/.local/lib/python3.10/site-packages (from pandas) (1.23.5)\n",
      "Requirement already satisfied: pytz>=2020.1 in /usr/lib/python3/dist-packages (from pandas) (2022.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /home/sid/.local/lib/python3.10/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f1b1513b-3857-4989-ab04-b4ebdab10c13",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a2f64dd9099eb95c",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a581b23-b1c1-4cac-851b-53700353f218",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d72bf798024e6eab",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 10\n",
    "\n",
    "The first step is to learn to load the data. Here we focus on reading data in CSV format. Therefore, the first task is to read the provided CSV file (taken from the following link:  https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/problem12.html) via read_csv function. \n",
    "\n",
    "**Syntax:** pd.read_csv(filepath_or_buffer, sep=’ ,’ , header=’infer’,  index_col=None, usecols=None, engine=None, skiprows=None, nrows=None) \\\n",
    "**Parameters:** \\\n",
    "**filepath_or_buffer:** It is the location of the file which is to be retrieved using this function. It accepts any string path or URL of the file. \\\n",
    "**sep:** It stands for separator, default is ‘, ‘ as in CSV(comma separated values). \\\n",
    "**header:** It accepts int, a list of int, row numbers to use as the column names, and the start of the data. If no names are passed, i.e., header=None, then,  it will display the first column as 0, the second as 1, and so on. \\\n",
    "**usecols:** It is used to retrieve only selected columns from the CSV file. \\\n",
    "**nrows:** It means a number of rows to be displayed from the dataset. \\\n",
    "**index_col:** If None, there are no index numbers displayed along with records. \\  \n",
    "**skiprows:** Skips passed rows in the new data frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "22e3b329-e0ac-45ab-b2fc-4f6759cb1ea7",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ae9adc24b8a42cca",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "data = pd.read_csv('titanic.csv')\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a54185-c6f8-4489-868e-1e12888d31ce",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-44333c9f0b61e36f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 11\n",
    "\n",
    "In the next task we want to explore the read data. Perform the following tasks:\n",
    "* Use the head() function to display the first 5 rows of the data\n",
    "* Use the tail() function to display the last 5 rows of the data\n",
    "* Use the columns property to display the columns in the data\n",
    "* Create a list with any six column names and create a new data frame (from the original) with only these columns\n",
    "* Print the data type of each column\n",
    "* Create a dictionary with new column names (simply capitalize all names) and change the name of the columns via rename() function \n",
    "* Use the describe() function and print the count, mean, standard deviation, minimum, maximum, and quantile values.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "723046de-01e1-4fee-bcec-44be8c689725",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4d863ed6b2d93169",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Survived  Pclass                                               Name  \\\n",
      "0         0       3                             Mr. Owen Harris Braund   \n",
      "1         1       1  Mrs. John Bradley (Florence Briggs Thayer) Cum...   \n",
      "2         1       3                              Miss. Laina Heikkinen   \n",
      "3         1       1        Mrs. Jacques Heath (Lily May Peel) Futrelle   \n",
      "4         0       3                            Mr. William Henry Allen   \n",
      "\n",
      "      Sex   Age  Siblings/Spouses Aboard  Parents/Children Aboard     Fare  \n",
      "0    male  22.0                        1                        0   7.2500  \n",
      "1  female  38.0                        1                        0  71.2833  \n",
      "2  female  26.0                        0                        0   7.9250  \n",
      "3  female  35.0                        1                        0  53.1000  \n",
      "4    male  35.0                        0                        0   8.0500  \n",
      "     Survived  Pclass                            Name     Sex   Age  \\\n",
      "882         0       2            Rev. Juozas Montvila    male  27.0   \n",
      "883         1       1     Miss. Margaret Edith Graham  female  19.0   \n",
      "884         0       3  Miss. Catherine Helen Johnston  female   7.0   \n",
      "885         1       1            Mr. Karl Howell Behr    male  26.0   \n",
      "886         0       3              Mr. Patrick Dooley    male  32.0   \n",
      "\n",
      "     Siblings/Spouses Aboard  Parents/Children Aboard   Fare  \n",
      "882                        0                        0  13.00  \n",
      "883                        0                        0  30.00  \n",
      "884                        1                        2  23.45  \n",
      "885                        0                        0  30.00  \n",
      "886                        0                        0   7.75  \n",
      "Index(['Survived', 'Pclass', 'Name', 'Sex', 'Age', 'Siblings/Spouses Aboard',\n",
      "       'Parents/Children Aboard', 'Fare'],\n",
      "      dtype='object')\n",
      "     Survived  Pclass                                               Name  \\\n",
      "0           0       3                             Mr. Owen Harris Braund   \n",
      "1           1       1  Mrs. John Bradley (Florence Briggs Thayer) Cum...   \n",
      "2           1       3                              Miss. Laina Heikkinen   \n",
      "3           1       1        Mrs. Jacques Heath (Lily May Peel) Futrelle   \n",
      "4           0       3                            Mr. William Henry Allen   \n",
      "..        ...     ...                                                ...   \n",
      "882         0       2                               Rev. Juozas Montvila   \n",
      "883         1       1                        Miss. Margaret Edith Graham   \n",
      "884         0       3                     Miss. Catherine Helen Johnston   \n",
      "885         1       1                               Mr. Karl Howell Behr   \n",
      "886         0       3                                 Mr. Patrick Dooley   \n",
      "\n",
      "        Sex   Age     Fare  \n",
      "0      male  22.0   7.2500  \n",
      "1    female  38.0  71.2833  \n",
      "2    female  26.0   7.9250  \n",
      "3    female  35.0  53.1000  \n",
      "4      male  35.0   8.0500  \n",
      "..      ...   ...      ...  \n",
      "882    male  27.0  13.0000  \n",
      "883  female  19.0  30.0000  \n",
      "884  female   7.0  23.4500  \n",
      "885    male  26.0  30.0000  \n",
      "886    male  32.0   7.7500  \n",
      "\n",
      "[887 rows x 6 columns]\n",
      "Survived      int64\n",
      "Pclass        int64\n",
      "Name         object\n",
      "Sex          object\n",
      "Age         float64\n",
      "Fare        float64\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "print(data.head(5))\n",
    "\n",
    "print(data.tail(5))\n",
    "\n",
    "print(data.columns)\n",
    "\n",
    "cols = ['Survived', 'Pclass', 'Name', 'Sex', 'Age', 'Fare' ]\n",
    "data_with_cols = data[cols]\n",
    "print(data_with_cols)\n",
    "\n",
    "print(data_with_cols.dtypes)\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ee5ed60b-534b-43d6-a8f9-1d5f1c6cc47d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-08260af69b8d0603",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['SURVIVED', 'PCLASS', 'NAME', 'SEX', 'AGE', 'SIBLINGS/SPOUSES ABROAD',\n",
      "       'PARENTS/CHILDREN ABROAD', 'FARE'],\n",
      "      dtype='object')\n",
      "         SURVIVED      PCLASS         AGE  SIBLINGS/SPOUSES ABROAD  \\\n",
      "count  887.000000  887.000000  887.000000               887.000000   \n",
      "mean     0.385569    2.305524   29.471443                 0.525366   \n",
      "std      0.487004    0.836662   14.121908                 1.104669   \n",
      "min      0.000000    1.000000    0.420000                 0.000000   \n",
      "25%      0.000000    2.000000   20.250000                 0.000000   \n",
      "50%      0.000000    3.000000   28.000000                 0.000000   \n",
      "75%      1.000000    3.000000   38.000000                 1.000000   \n",
      "max      1.000000    3.000000   80.000000                 8.000000   \n",
      "\n",
      "       PARENTS/CHILDREN ABROAD       FARE  \n",
      "count               887.000000  887.00000  \n",
      "mean                  0.383315   32.30542  \n",
      "std                   0.807466   49.78204  \n",
      "min                   0.000000    0.00000  \n",
      "25%                   0.000000    7.92500  \n",
      "50%                   0.000000   14.45420  \n",
      "75%                   0.000000   31.13750  \n",
      "max                   6.000000  512.32920  \n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTON\n",
    "new_col_names = {'Survived': 'SURVIVED', 'Pclass': 'PCLASS', 'Name': 'NAME', 'Sex': 'SEX', 'Age': 'AGE', \n",
    "                'Siblings/Spouses Aboard': 'SIBLINGS/SPOUSES ABROAD', 'Parents/Children Aboard': 'PARENTS/CHILDREN ABROAD', 'Fare': 'FARE' }\n",
    "data = data.rename(columns=new_col_names)\n",
    "print(data.columns)\n",
    "# print(new_data.columns)\n",
    "\n",
    "print(data.describe())\n",
    "\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ce1edb1-c5c1-44d2-9e0d-eee07cc1cbdd",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-62091a587f819a86",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Exercise 12\n",
    "\n",
    "Pandas provide a groupby() function to split the data into groups based on criteria, which provides a mapping of labels to group names. \n",
    "* Group the data according to PCLASS and display the mean value\n",
    "* Plot a bar graph of the returned dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "974c2a5f-d08e-4691-aefd-a9f96d0bf017",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5cc6c7dd8afe3d16",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        SURVIVED        AGE  SIBLINGS/SPOUSES ABROAD  PARENTS/CHILDREN ABROAD  \\\n",
      "PCLASS                                                                          \n",
      "1       0.629630  38.788981                 0.416667                 0.356481   \n",
      "2       0.472826  29.868641                 0.402174                 0.380435   \n",
      "3       0.244353  25.188747                 0.620123                 0.396304   \n",
      "\n",
      "             FARE  \n",
      "PCLASS             \n",
      "1       84.154687  \n",
      "2       20.662183  \n",
      "3       13.707707  \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot: xlabel='PCLASS'>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25620 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "\n",
    "print(data.groupby('PCLASS').mean(numeric_only=True))\n",
    "data.groupby('PCLASS').mean(numeric_only=True).plot(kind='bar')\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adee91c8-743e-4315-b324-283d6a22e165",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
