{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Exploratory data analysis\n",
    "\n",
    "Data source: https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data\n",
    "\n",
    "Important attributes description:\n",
    "* SalePrice: The property's sale price in dollars. This is the target variable that you're trying to predict.\n",
    "* MSSubClass: The building class\n",
    "* BldgType: Type of dwelling\n",
    "* HouseStyle: Style of dwelling\n",
    "* OverallQual: Overall material and finish quality\n",
    "* OverallCond: Overall condition rating\n",
    "* YearBuilt: Original construction date\n",
    "* Heating: Type of heating\n",
    "* CentralAir: Central air conditioning\n",
    "* GrLivArea: Above grade (ground) living area square feet\n",
    "* BedroomAbvGr: Number of bedrooms above basement level"
   ],
   "metadata": {
    "id": "l9hHDMhFjshW",
    "tags": []
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Import used packages"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import pandas as pd # dataframes\r\n",
    "import numpy as np # matrices and linear algebra\r\n",
    "import matplotlib.pyplot as plt # plotting\r\n",
    "import seaborn as sns # another matplotlib interface - styled and easier to use"
   ],
   "outputs": [],
   "metadata": {
    "id": "FVFwXLhL75FZ"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Load the data into the Pandas DataFrame - in our case it is a csv file"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "data = pd.read_csv('./data/train.csv', sep=',')\r\n",
    "data.head()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Price is the most interisting attribute in our dataset, let's take a look on its distribution"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Vizualize house prices in form of histogram\n",
    "- What does it tell us about the prices?"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Modify bins\n",
    "- Perhaps we want to visualize the histogram with more dense or coarse bins\n",
    "- *bins* attribute"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Add probability density curve (Kernel density estimation)\n",
    "\n",
    "We can use parameter *kde* if we want to show estimation of probability density function.\n",
    "\n",
    "Modify colors of plot, use parameters:\n",
    "- *color* \n",
    "- *edgecolor*"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Use quartiles (Q1, Q3) for highlighting most common price range in histogram\n",
    "\n",
    "Check functions *describe* and *quantile* over price column"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Use function *axvline* from Matplotlib to draw vertical lines at the quartile positions"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure(figsize=(9, 6))\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Let's add more complexity to histogram vizualizations. Does price change for different values of GaragaFinish attribute?\n",
    "\n",
    "#### GarageFinish: Interior finish of the garage\n",
    "- Fin\tFinished\n",
    "- RFn\tRough Finished\t\n",
    "- Unf\tUnfinished\n",
    "- NA\tNo Garage"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### We can see that houses with no garage are somehow missing\n",
    "- The no garage houses have a *nan* value in the feature so they are not plotted"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Sometimes the histograms distinguished by colors are not easily redeable\n",
    "- We can use *col* attribute to automatically plot every histogram on its own canvas"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Cumulative distribution plot usage\n",
    "Task: I want to buy a house with live area of 150 m^2 maximum (1614 ft^2), how much choice do I have?\n",
    "- How to solve the task visually? We can make use of *ecdfplot*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Moreover we may be interested in types of building so we can distinguish the houses by *BldgType* attribute\n",
    "\n",
    "#### BldgType: Type of dwelling\n",
    "- 1Fam\tSingle-family Detached\t\n",
    "- 2FmCon\tTwo-family Conversion; originally built as one-family dwelling\n",
    "- Duplx\tDuplex\n",
    "- TwnhsE\tTownhouse End Unit\n",
    "- TwnhsI\tTownhouse Inside Unit\n",
    "\n",
    "https://housing.com/news/what-is-a-duplex-house/"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Zoom in using the *xlim* function"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Another very useful tecnique for the numerical features distributions are boxplots\n",
    "- Did you see any of these before?\n",
    "- How should we read them?\n",
    "    - Quartiles\n",
    "    - Median\n",
    "    - Box size\n",
    "    - Outliers and IQR\n",
    "    - Min/max - with or without outliers"
   ],
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Plot only the *SalePrice* using boxplot"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Let's take a look at the *SalePrice* for different *BldgType* values \n",
    "- What can we say about the prices?"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure(figsize=(16, 9))\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## We can do the same for *GrLivArea*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Let's take a look at the *GrLivArea* for different *BldgType* values \n",
    "- What can we say about the GrLivArea?"
   ],
   "metadata": {
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure(figsize=(16, 9))\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### We can use standard *scatterplot* with *BldgType* as a *hue* so we can better grasp the relationship between *GrLivArea* and *SalePrice*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure(figsize=(16, 9))\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### We can see there are some outliers in the data, let's zoom only to area without them, how can we filter the data?"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure(figsize=(16, 9))\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## We have an information about a month and a year for the sold houses.\n",
    "### Can you vizualize what was the average price of the house by quarters?\n",
    "\n",
    "* We need to create a new columns with merged Year and Quarter information in this pattern: '2010-Q1','2010-Q2', etc.\n",
    "* We can map the values using the *apply* function or use string concatenation directly\n",
    "* If you need to change data type of the columns, you can use *astypy*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Compute the average price for the *YearQuarterSold* attribute"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Vizualize the data using *lineplot*\n",
    "- In case of xlabels mixing try to tune the *rotation* and *horizontalalignment* parameters"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Add the max, min and median to the plot\n",
    "- Use *describe* and the [Pandas Melt](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html) function\n",
    "    - use YearQuarterSold as an *id_vars*\n",
    "    - use 'min', 'mean', '50%', 'max' as *value_vars*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## What can we say about the minimum and maximum values?\n",
    "- hint - std. deviation? outliers?"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# We can take a look at number of sold houses for the defined time periods as well\n",
    "- We will use standard bar plot\n",
    "- In which quarter were the most houses sold?\n",
    "- Fun facts: [https://themortgagereports.com/44135/whats-the-best-time-of-year-to-sell-a-home](https://themortgagereports.com/44135/whats-the-best-time-of-year-to-sell-a-home)"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Tasks\n",
    "## Task 1 - Outlier detection (1p)\n",
    "- We need to somehow mark the outliers in the data according to the *SalePrice* and *GrLivArea*\n",
    "    - One possibility is to compute [IQR](https://www.statisticshowto.com/probability-and-statistics/interquartile-range/) for both columns and mark outliers using lower and upper bounds\n",
    "    - Lower bound: Q1 - 1.5*IQR\n",
    "    - Upper bound: Q3 + 1.5*IQR\n",
    "- If the house has *SalePrice* **OR** *GrLivArea* value outside of the bounds - it is an outlier\n",
    "- Vizualize the data using scatter plot and distinguish the outlier and non-outlier data using different colors (*hue*)\n",
    "\n",
    "## Task 2 - Describe what you see in the data (1p)\n",
    "- Try to vizualize the relationship between *SalePrice* and *OverallQual*\n",
    "    - You can use BoxPlots, Scatter plots, etc.\n",
    "- Describe the insight you got from the plots with a few sentences\n",
    "- Do the same for *SalePrice* and *OverallCond*; i.e. vizualize and describe insight"
   ],
   "metadata": {
    "tags": []
   }
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "ZSU_cv1_introduction_jupyter_pandas_visualization.ipynb",
   "provenance": []
  },
  "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}