<jupyter_start><jupyter_text>![GMIT LOGO](https://www.logolynx.com/images/logolynx/36/36c5ba49dd8ef0a12c11681df3d77548.jpeg) Fundamentals of Data Analysis 2018by <NAME> G00263352 Project - Boxplot**Tasks:*** Summarise the history of the box plot and situations in which it used.* Explain any relevant terminology such as the terms quartile and percentile* Demonstrate the use of the box plot using data of your choosing.* Compare the box plot to alternatives.  1. About Boxplots**A boxplot** is a graph that gives a good indication of how the values in the data are spread out. <NAME> was an American mathematician who introduced the box plot in his 1977 book, "Exploratory Data Analysis".It's a summary of five components which include “minimum”, first quartile (Q1), median, third quartile (Q3), and “maximum”. The box is drawn from first quartile to the third quartile. A vertical line that goes through the box is the median and the whiskers go from each quartile to the minimum or maximum.Tukey developed many important tools of modern statistics and introduced concepts that were central to the creation of today's telecommunications technologies. Tukey was known for his penchant for coining terms that reflected new ideas and techniques in the sciences and is credited with introducing the computer science terms "bit".Some important points to note:* The data sets must be measuring the same value, with the same units.* Box plots should not be constructed for very small data sets. A data set with only three points in it, for example, shouldn’t logically be divided into four quartiles. In these cases, graph the data as a scatterplot, without any boxes.* Usually, different trials should contain the same total number of data points. If one set is much larger than another, for example, it has a larger chance of containing random outliers.  1.1 Where is it used?Boxplots are useful for indicating whether a distribution is skewed and whether there are potential unusual observations (outliers) in the data set. It's main function is to check for key values, such as: the average, median 25th percentile ett. - the centre, spread and overall range are immediately apparent.These plots are also very useful when large numbers of observations are involved and when two or more data sets are being compared from different categories for easier, more effective decision making. They have the advantage of taking up less space. You can tell if data is symmetrical and how tighly it is grouped. ![Data comparison](https://datavizcatalogue.com/methods/images/top_images/box_plot.png)It is often used in explanatory data analysis. Examples such as data from before and after a process change or data from duplicate machines manufacturing the same products. (3)* Multiple trials of the same experiment:In many Scientific fields, when the same experiment is run many times, the results are tracked in a box plot to show consistencies or inconsistencies across trials.* Multiple trials of different experiments:Many similar experiments might be run, with some condition or parameter changing each time. A box plot might help find trends or patterns.* Data broken up by category:A box plot can serve a similar function as a bar graph, but with many values in each category.(6)  1.2 Different TypesAfter Tukey introduced the Box Plot, there have been a number variations that have been developed:**Variable Width Box Plots** use the width of the box to represent the size of the data within each group. So a group with a larger total in the data will have a larger width. **Notched Box Plots** have a narrowing of the box around the median. This is useful way to compare the differences between median values as the “notches” act as a visual guide. **Violin Plots** are a pair of joined kernel density plots and **Vase Plots and Bean Plots** are another couple variations of a Box Plot. (5)Variable width box plots and notched box plots are the most commonly used box plots.![Types of Boxplots](http://visualoop.com/media/2015/04/box_plot_variations.png)  2. Terminology**Median**The median marks the mid-point of the data and is shown by the line that divides the box into two parts. Half the scores are greater than or equal to this value and half are less.**Inter-quartile range**The range of scores from lower to upper quartile is referred to as the inter-quartile range. **Q3 Quartile**Seventy-five percent of the scores fall below the upper (Q3) quartile.**Q1 Quartile**Twenty-five percent of scores fall below the lower (Q1) quartile.**Whiskers**The upper and lower whiskers represent scores outside the middle 50%. Whiskers often (but not always) stretch over a wider range of scores than the middle quartile groups. (2)**Extreme Values**The smallest and largest values in a data set.**Outlier/Single Point**Outliers are values that are much bigger or smaller than the rest of the data. These are represented by a dot at either end of the plot.In order to be an outlier, the data value must be:Larger than Q3 by at least 1.5 times the interquartile range (IQR), orsmaller than Q1 by at least 1.5 times the IQR. ![Boxplot](http://visualoop.com/media/2015/04/box_plot_anatomy.png)  3. Boxplot Examples The data set I decided to use to demonstrate Box Plots is the Iris Data Set.<jupyter_code># Import all libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Below I want to demonstrate different types of box plots and their use when analysing data. As I mentioned above my chosen data is the Iris Data set. I will be using Pandas dataframe as an input and use Seaborn Boxplot function to create these boxplots.<jupyter_code># Import Iris data set
iris = pd.read_csv('irisdata.csv')

# Construct iris plot
sns.boxplot(x="species", y="sepal_length", data=iris) # Choose variables
plt.title("Normal Boxplot")
plt.ylabel("Sepal Length") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show plot
 
# Change line width
sns.boxplot(x="species", y="sepal_length", linewidth=3, data=iris) # change line with to 3
plt.title("Changed Line Width")
plt.ylabel("Sepal Length") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show Plot

# Add jitter with the swarmplot function.
sns.swarmplot(x='species', y='sepal_length', data=iris, color="grey") # Use swarmplot function
plt.title("Jitter Boxplot")
plt.ylabel("Sepal Length") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show Plot

# Add notch
sns.boxplot(x='species', y='sepal_length', data=iris, notch=True) 
plt.title("Notched Boxplot")
plt.ylabel("Sepal Length") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show Plot

# Construct iris plot
sns.boxplot(x="species", y="sepal_width", data=iris) # Choose variables
plt.title("Boxplot of Sepal Width")
plt.ylabel("Sepal Width") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show plot

# Construct iris plot
sns.boxplot(x="species", y="sepal_length", data=iris) # Choose variables
plt.title("Boxplot of Sepal Length")
plt.ylabel("Sepal Length") # Label y axis
plt.xlabel("Species") # Label x axis
plt.show() # Show plot<jupyter_output><empty_output><jupyter_text>Example above shows how I used a boxplot to compare species' sepal width with sepal length. I can now investigate their shape, variability, and the median. I will also look for skewed data or for any outliers. From boxplots above it is clear that there is no skewed data only a few outliers from Virginica Species on both sepal width and legth. I can see that setosa species have the widest sepals as their maximum value reaches 4.5 compared to other 2 species only reaching max 3.5 values but they are shortest in length with median value value of 5 cm compared to 6/6.5 of other 2 species.   4. How a Boxplot compare to a HistogramIn this section I will compare a boxplot to a histogram and find out when these graphs work better.**A histogram** is a graphical representation of a frequency distribution for numeric data. It is a bar chart that is often used as the first step to determine the probability distribution of a data set or a sample. It allows to visually and quickly assess the shape of the distribution, the central tendency, the amount of variation in the data, and the presence of gaps, outliers or unusual data points. (4)Both charts effectively represent different data sets; however, in certain situations, one chart may be superior to the other in achieving the goal of identifying variances among data. The type of chart aid chosen depends on the type of data collected, rough analysis of data trends, and project goals.<jupyter_code># Create Histrogram
sns.distplot(a=iris["sepal_width"], hist=True)
plt.title("Setosa") # Add title

sns.distplot(a=iris["sepal_length"], hist=True)
plt.xlabel("Centimetres") # Label x axis
plt.show() # Merge both & show plot 

# Cut the window in 2 parts
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})
 
# Add a graph in each part
sns.boxplot(iris["sepal_length"], ax=ax_box)
sns.distplot(iris["sepal_length"], ax=ax_hist)
 
# Remove x axis name for the boxplot
ax_box.set(xlabel='')
# plot all four variables
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot( iris["sepal_length"] , color="skyblue", ax=axes[0, 0]) # plot sepal length
sns.distplot( iris["sepal_width"] , color="olive", ax=axes[0, 1]) # plot sepal width
sns.distplot( iris["petal_length"] , color="gold", ax=axes[1, 0]) # plot petal length
sns.distplot( iris["petal_width"] , color="teal", ax=axes[1, 1]) # plot petal width
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07388351512656925, 'fraction_numerical': 0.011216299650133772, 'mean_word_length': 5.044154228855722, 'pattern_counts': {'":': 1, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '163771', 'license': 'Apache-2.0', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2589, 'n_words': 1372, 'path': 'BoxPlots.ipynb', 'repo_name': 'simonava5/FundamentalsProject', 'size': '9737'}