{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0362a1eb-1e46-462a-b22c-f972c398741d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e3006ef8308b2f34",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# 7. Programmierübung: Datenanalyse\n",
    "\n",
    "<div style=\"display:flex;\">\n",
    "    <div style=\"text-align: left\">\n",
    "        Willkommen zur siebten Programmierübung Einführung in Python 3.\n",
    "    </div>\n",
    "    <img style=\"float: right; margin: 0px 15px 15px 0px\" src=\"https://www.python.org/static/img/python-logo-large.c36dccadd999.png?1576869008\" width=\"100\" />\n",
    "</div>\n",
    "\n",
    "Wenn Sie Fragen oder Verbesserungsvorschläge zum Inhalt oder Struktur der Notebooks haben, dann können sie eine E-Mail an Phil Keier ([p.keier@hbk-bs.de](mailto:p.keier@hbk-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) oder Martin Le ([martin.le@tu-bs.de](mailto:martin.le@tu-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) schreiben.\n",
    "\n",
    "Link zu einem Python Spickzettel: [hier](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/PythonForDataScience.pdf)\n",
    "\n",
    "Der Großteil des Python-Tutorials stammt aus der Veranstaltung _Deep Learning Lab_ und von [www.python-kurs.eu](https://www.python-kurs.eu/python3_kurs.php) und wurde für _Signale und Systeme_, sowie _Einführung in die Programmierung für Nicht Informatiker_ angepasst.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a97a824-bc2f-4f9d-a730-42ff05828cc8",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a5071e148ad2227c",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Data Analysis\n",
    "\n",
    "This exercise serves as preparation for the exam.\n",
    "\n",
    "This notebook includes the dataset `survey.csv`, which contains the data from the survey you completed.\n",
    "\n",
    "The data was generated from the following this [Survey](https://forms.gle/JVKq6FrSUE8kN7Jq6).\n",
    "\n",
    "Your task is to read in the data set `survey.csv` and check the following 4 hypotheses.\n",
    "\n",
    "One calculation is not enough! Write a paragraph after each calculation in which you briefly explain your calculation. (How did you proceed?, Which data were relevant to confirm/disprove the hypothesis?) Present your results in a suitable way. You can use plotting tools, maps or text. The easier & more obvious the result is to interpret, the better.\n",
    "\n",
    "Also explain how you interpret your result (In German or English). Numbers don't mean anything, their interpretation does!\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "602be29c-d586-41d2-8c25-660faa8a619d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-954c00c5bbcf2e7f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Setup\n",
    "\n",
    "In the next cell, import all the libraries & functions you use."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0731fc85-90eb-477e-8dd5-d2bcc3eeb16c",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-36fdb907d2bc33b3",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd1c8fdc-a349-4a70-9335-c08bc00a676a",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8e13f4b8f08d4cbb",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Next read in the dataset `survey.csv`. For testing puposes use the Variable Name `df`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9e276245-83dc-460a-b81b-ddd7ad8e5978",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d4b0d9ce1f988100",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "df = pd.read_csv(\"survey.csv\")\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fba07368-225f-42bf-82fa-85070d59eba0",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a7ce1cba9e2fde7a",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert isinstance(df, pd.DataFrame)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e81b8e60-b946-4822-8acb-6a63aefd51df",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d27e9df7f4255151",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "These next Cells you can use to analyse the dataset. Feel free to alter it to your needs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50e52cf6-ae36-4f80-b9a1-73a089939a69",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-928097dcba5a7f09",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d64a606c-68bb-4a46-8169-af34a07228f9",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-afd1d2c0d85ce40e",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ad9d17d9-dd59-4766-bb46-de4df822ce0f",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3da446eb91bdd35d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Hypothesis 1\n",
    "\n",
    "__The average age in the course is 25.34 years, with a surplus of females.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8e24289e-a738-4ae4-a6d4-a936263120fc",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a6a23c7c132e44d9",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean Age: 22.64\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14268 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "print(\"Mean Age:\", df[\"Age\"].mean())\n",
    "c = df[\"Sex\"].value_counts()\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.title(\"Gender Distribution\")\n",
    "plt.bar(c.keys(), c, color=[\"black\", \"white\"])\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0058bf53-8169-4098-ad28-ad7036bcdaf7",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f01beca6d16a140e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80cf1c27-644a-4538-abbc-7d5f524251d7",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-9cbb6c488a06eb18",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "171340b9-0302-4c7c-ac85-f81d1e57a9da",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3fc34b3f943dfa4b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Hypothesis 2\n",
    "\n",
    "__The most used voice assistant is Alexa.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "98fb0abc-0d50-4f4c-aeb0-16b056aeeabf",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b28aca63b8048f70",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17944 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "c = df[\"Voice Assistent\"].value_counts()\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.title(\"Voice Assistent Distribution\")\n",
    "plt.bar(c.keys(), c, color=[\"red\", \"green\", \"blue\"])\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a75600e-27e1-439e-820b-49a5340b22a3",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5db1eeca82b4c33b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ee59b36-a147-4940-be03-d923c7131bfa",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-016f2b30651bea18",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3b77bda1-5851-4394-81c7-fd649cb980e5",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-fae767f8aab85e92",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Hypothesis 3\n",
    "\n",
    "## Hypothesis 3.1\n",
    "\n",
    "__The least used smartphone operating system is iOS.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0d74bd0d-e83e-4211-bc58-b9615e36b848",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-34a9cdeebcf2d7b9",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13628 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "os_dict = {\n",
    "    \"iOS\": 0,\n",
    "    \"Android\": 0\n",
    "}\n",
    "\n",
    "for data in df[\"Which Smartphone\"]:\n",
    "    if data == \"Apple\":\n",
    "        os_dict[\"iOS\"] += 1\n",
    "    else:\n",
    "        os_dict[\"Android\"] += 1\n",
    "\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.title(\"Mobile OS Distribution\")\n",
    "plt.bar(os_dict.keys(), os_dict.values(), color=[\"silver\", \"green\"])\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66c12b57-0ae2-48eb-8c73-589949a2952d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2545dc88c22ab5c1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9bb8c23-e161-492c-b1ee-9cc4c21c2325",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-81264718d58ae0f6",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4481b1f2-a8af-4731-9890-17a4047e2f80",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8005ff19d1e616f6",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 3.2\n",
    "\n",
    "__The most used desktop operating system is Windows 10.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "99f4c2ad-c162-461e-9632-fdc2cbc541fc",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b706b4487a333142",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15688 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "os_dict = df[\"Which OS\"].value_counts()\n",
    "\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.title(\"Desktop OS Distribution\")\n",
    "plt.bar(os_dict.keys(), os_dict, color=[\"grey\", \"silver\", \"cyan\", \"yellow\"])\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea7ce7b6-5ba9-4185-b33b-c6e5f9e349f6",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-66d7fa91478051d4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93f04e29-58f3-4ea5-8af2-369061308915",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-4bf7225f7d1963f7",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3fa617bb-77dd-4c43-b30b-b422eac770ef",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d532947366781d90",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Hypothesis 4\n",
    "\n",
    "## Hypothesis 4.1\n",
    "\n",
    "__The youngest people use an iPhone.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "18467418-586b-4153-9066-0de730994e8e",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-5284bedc957436b3",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22232 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "age_mean = df[\"Age\"].mean()\n",
    "youngest = {\n",
    "    \"ios\": 0,\n",
    "    \"Android\": 0,\n",
    "    \"n\": 0,\n",
    "    \"ratio iOS\": 0,\n",
    "    \"ratio Android\": 0\n",
    "}\n",
    "\n",
    "oldest = {\n",
    "    \"ios\": 0,\n",
    "    \"Android\": 0,\n",
    "    \"n\": 0,\n",
    "    \"ratio iOS\": 0,\n",
    "    \"ratio Android\": 0\n",
    "}\n",
    "\n",
    "\n",
    "for index, row in df[[\"Age\", \"Which Smartphone\"]].iterrows():\n",
    "    if age_mean > row[\"Age\"]:\n",
    "        youngest[\"n\"] += 1\n",
    "        if row[\"Which Smartphone\"] == \"Apple\":\n",
    "            youngest[\"ios\"] += 1\n",
    "        else:\n",
    "            youngest[\"Android\"] += 1\n",
    "\n",
    "    if age_mean < row[\"Age\"]:\n",
    "        oldest[\"n\"] += 1\n",
    "        if row[\"Which Smartphone\"] != \"Apple\":\n",
    "            oldest[\"Android\"] += 1\n",
    "        else:\n",
    "            oldest[\"ios\"] += 1\n",
    "\n",
    "\n",
    "youngest[\"ratio iOS\"] = youngest[\"ios\"] / youngest[\"n\"] * 100\n",
    "youngest[\"ratio Android\"] = youngest[\"Android\"] / youngest[\"n\"] * 100\n",
    "\n",
    "oldest[\"ratio iOS\"] = oldest[\"ios\"] / oldest[\"n\"] * 100\n",
    "oldest[\"ratio Android\"] = oldest[\"Android\"] / oldest[\"n\"] * 100\n",
    "\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle')\n",
    "plt.title(\"iOS Ratio between mean Age\")\n",
    "plt.bar([\"Age under Mean iOS\", \"Age under Mean Android\", \"Age over mean iOS\", \"Age over mean Android\"], \n",
    "        [youngest[\"ratio iOS\"], youngest[\"ratio Android\"], oldest[\"ratio iOS\"], oldest[\"ratio Android\"]], color=[\"gold\", \"silver\"])\n",
    "plt.show()\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b853c11-405a-470a-806f-da7851d86d12",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-492ea1226901c0e4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f5dace2-5a13-4806-946c-0d0e4c9bc307",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-90f0108fe83268c7",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c6fdcfa2-5507-4c85-b262-c83e6b1074ee",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6ace0ffb9a32d3b1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 4.2\n",
    "\n",
    "__Older people use an Android-based smartphone.__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a76e273-a649-4bdf-ba8d-bc1351d0ec08",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-18c2e79244ca1beb",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "06313381-3ba3-4a0b-af75-2dfaf3d7acf2",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3a88641157327c7d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Use the next cell to write your Interpretation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bd2c8fa-801a-4c0a-8754-61bdc2f8127b",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a421f0cc1a31c697",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "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.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
