{ "cells": [ { "cell_type": "markdown", "id": "a41c76e0", "metadata": {}, "source": [ "--- \n", " \n", "\n", "

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "8b6fe310", "metadata": {}, "source": [ "

Lecture 4.1 (Descriptive Statistics)

" ] }, { "cell_type": "markdown", "id": "4c9589ab", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "cbd84c90", "metadata": {}, "source": [ "\n", "" ] }, { "cell_type": "markdown", "id": "55fe5d9d", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "id": "72bb153c", "metadata": {}, "outputs": [], "source": [ "# Unlike the other modules, we have been working so far, you have to download and install...\n", "# To install this library in Jupyter notebook\n", "import sys\n", "!{sys.executable} -m pip install -q --upgrade pip\n", "!{sys.executable} -m pip install -q statistics statsmodels scipy" ] }, { "cell_type": "code", "execution_count": null, "id": "4e97fd50", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "\n", "import statistics\n", "import scipy.stats as st\n", "import statsmodels as sm" ] }, { "cell_type": "markdown", "id": "7ef15071", "metadata": {}, "source": [ "## Learning agenda of this notebook\n", "1. Data and its Types\n", "2. Collection of Data (Population vs Sample)\n", "3. Overview of Statistics and its Types\n", "4. Descriptive Statistics: Measure of Central Tendency\n", " - Mean\n", " - Median\n", " - Mode\n", "5. Descriptive Statistics: Measure of Spread\n", " - Range\n", " - IQR\n", " - Variance\n", " - Standard Deviation\n", "6. Descriptive Statistics: Measure of Asymmetry\n", " - Skewness\n", " - Kurtosis\n", "7. Analysis Strategy\n", "8. Example Datasets\n", "9. Univariate Analysis and Data Visualization\n", "10. Bivariate Analysis and Data Visualization" ] }, { "cell_type": "code", "execution_count": null, "id": "0988df93", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b0f92d52", "metadata": {}, "source": [ "## 1. Data and its Types\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "9365d8ca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6268e89e", "metadata": {}, "source": [ "## 2. Collection of Data (Population vs Sample)\n", "\n", "- Data can be sourced directly or indirectly. \n", "- Direct methods of data collection involve collecting new data for a specific study. This type of data is known as primary data.\n", "- Indirect methods of data collection involve sourcing and accessing existing data that were not originally collected for the purpose of the study. This type of data is known as secondary data.\n", "- A survey involves collecting information from every unit in the population (a census), or from a subset of units (a sample) from the population. \n", "- **`Population:`** It is an entire pool of data from where a statistical sample is extracted. It can be visualized as a complete data set of items that are similar in nature.\n", "\n", "- **`Sample:`** It is a subset of the population, i.e. it is an integral part of the population that has been collected for analysis. \n", "\n", "- **`Variable:`** A value whose characteristics such as quantity can be measured, it can also be addressed as a data point, or a data item. \n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "63e84ffe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e0e88212", "metadata": {}, "source": [ "- **`Types of Sampling:`**\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "fd366be6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "02cd9e39", "metadata": {}, "source": [ "## 3. Overview of Statistics and its Types\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "a5825ac6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "dfcd770f", "metadata": {}, "source": [ "### Descriptive vs Inferential Statistics\n", "\n", "" ] }, { "cell_type": "markdown", "id": "acd92f41", "metadata": {}, "source": [ ">- **Descriptive statistics is used to describe, summarize and present different datasets through numerical calculations, tables and graphs.**\n", "\n", ">- **Inferential statistics is used to make inference and predictions for an entire population, based on sample data of that population.**" ] }, { "cell_type": "markdown", "id": "8a9061a8", "metadata": {}, "source": [ "| Descriptive Statistics | Inferential Statistics |\n", "| --- | --- |\n", "|Describe the features of populations and/or samples | Use samples to make generalizations about larger populations |\n", "|Draw conclusions based on known data | Draw conclusions that go beyond the available data |\n", "|It helps in organizing, analyzing and to present data in a meaningful manner | It allows us to compare data, make hypothesis and predictions |\n", "|Present final results visually, using tables, charts, or graphs| Present final results in the form of probabilities |\n", "|Use measures like central tendency, distribution, and variance| Use techniques like hypothesis testing, confidence intervals, and regression and correlation analysis |" ] }, { "cell_type": "code", "execution_count": null, "id": "aecc9821", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3e91f531", "metadata": {}, "source": [ "## 4. Descriptive Statistics: Measures of Center\n", "" ] }, { "cell_type": "markdown", "id": "976e5a0d", "metadata": {}, "source": [ "### a. Mean\n", "- `Arithmetic Mean` is the sum of the value of each observation in a dataset divided by the number of observations. The mean cannot be calculated for categorical data, as the values cannot be summed. Moreover, as mean includes every value in the distribution it is influenced by outliers and skewed distributions.\n", "```\n", "Arithmetic Mean = (x1 + x2 + … + xN) / N\n", "``` \n", "- `Geometric Mean` is calculated as the N-th root of the product of all values, where N is the number of values. The geometric mean is used if the data is comprised of different units of measure, e.g. some measure are height, some are dollars, some are miles, etc. In ML, geometric mean is used to calculate the G-Mean measure that is a model evaluation metric. The geometric mean does not accept negative or zero values.\n", "```\n", "Geometric Mean = N-root(x1 * x2 * … * xN)\n", "``` \n", "- `Harmonic Mean` is calculated as the number of values N divided by the sum of the reciprocal of the values. The harmonic mean is used if the data is comprised of rates, i.e., a ratio between two quantities with different measures, e.g. speed, acceleration, frequency, etc. In ML, harmonic mean is used to calculate the F1 measure that is a model evaluation metric. The harmonic mean does not accept negative or zero value.\n", "```\n", "Harmonic Mean = N / (1/x1 + 1/x2 + … + 1/xN)\n", "``` \n", "- Note:\n", " - If values have the same units: Use the arithmetic mean.\n", " - If values have differing units: Use the geometric mean.\n", " - If values are rates: Use the harmonic mean.\n", " \n", "### b. Median\n", "- `Median` Median is a statistical measure that determines the middle value of a dataset listed in ascending order.\n", "- It splits the data in half, so it is also called 50th percentile. \n", "- To compute median:\n", " - Arrange the data in ascending order (from the lowest to the largest value).\n", " - If the dataset contains an odd number of values, the median is a central value that will split the dataset into halves.\n", " - If the dataset contains an even number of values, find the two central values that split the dataset into halves. Then, calculate the mean of the two central values, which is the median of the dataset.\n", "- Median is used where strong outliers may skew the representation of the data. If you have one person who earns 1 billion a year and nine other people who earn under 100,000 a year, the mean income for people in the group set be around 100 million, a gross distortion.\n", "\n", "\n", "### c. Mode\n", "- Mode is frequently occurring data or elements.\n", "- If an element occurs the highest number of times, it is the mode of that data. If no number in the data is repeated, then there is no mode for that data. There can be more than one mode in a dataset if two values have the same frequency and also the highest frequency.\n", "- Outliers don’t influence the data.\n", "- The mode can be calculated for both quantitative and qualitative data." ] }, { "cell_type": "code", "execution_count": null, "id": "bb59e0a6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f19a44d5", "metadata": {}, "source": [ "**Example 1:**" ] }, { "cell_type": "code", "execution_count": null, "id": "18657e87", "metadata": {}, "outputs": [], "source": [ "import statistics\n", "print(dir(statistics))" ] }, { "cell_type": "code", "execution_count": null, "id": "7df88b98", "metadata": {}, "outputs": [], "source": [ "data1 = [14000, 19000, 16000, 20000, 15000, 16000, 15000, 15000, 22000]\n", "print(\"mean(data1): %.2f\" % statistics.mean(data1))\n", "print(\"geometric_mean(data1): %.2f\" % statistics.geometric_mean(data1))\n", "print(\"harmonic_mean(data1): %.2f\" % statistics.harmonic_mean(data1))\n", "print(\"median(data1): \", statistics.median(data1))\n", "print(\"mode(data1): \", statistics.mode(data1))" ] }, { "cell_type": "code", "execution_count": null, "id": "3177666f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "205ab18b", "metadata": {}, "source": [ "**Example 2:**" ] }, { "cell_type": "code", "execution_count": null, "id": "9038b97c", "metadata": {}, "outputs": [], "source": [ "data2 = [14000, 19000, 16000, 20000, 15000, 16000, 15000, 15000, 22000, 900000]\n", "print(\"mean(data2): %.2f\" % statistics.mean(data2))" ] }, { "cell_type": "code", "execution_count": null, "id": "b1ceed60", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7a5a74e5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5cbba4c6", "metadata": {}, "source": [ "## 5. Descriptive Statistics: Measures of Spread\n", " " ] }, { "cell_type": "markdown", "id": "4fd0b588", "metadata": {}, "source": [ "### a. Range\n", "- `Range` is the spread of your data from the lowest to the highest value in the distribution, \n", "- The range is calculated by subtracting the lowest value from the highest value. \n", "- A large range means high variability, a small range means low variability in a distribution.\n", "" ] }, { "cell_type": "markdown", "id": "f5cd9a00", "metadata": {}, "source": [ "**Example 1:**" ] }, { "cell_type": "code", "execution_count": null, "id": "04859d73", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "data1 = [25, 10, 9, 6, 12, 11, 15]\n", "range1 = np.max(data1) - np.min(data1)\n", "range1" ] }, { "cell_type": "markdown", "id": "3b405f67", "metadata": {}, "source": [ "**Example 2:**" ] }, { "cell_type": "code", "execution_count": null, "id": "68d5fe6e", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "data2 = [10, 9, 8, 11, 10, 9, 8, 11]\n", "range2 = max(data2) - min(data2)\n", "range2" ] }, { "cell_type": "code", "execution_count": null, "id": "ee9b168f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "80d22b89", "metadata": {}, "source": [ "### b. Inter Quartile Range\n", "\n", "\n", "\n", "- Quantiles divedes a distribution and the most common are `quartiles`, `percentiles`, and `deciles`.\n", "- The median, which divides a distribution in two at its midpoint, is the most well-known example of a quantile.\n", "- **Quartiles:**, as their name suggests, are quantiles that divide a distribution into quarters by splitting a rank-ordered dataset into four equal parts. \n", " - 1st Quartile Q1 is the same as the 25th percentile.\n", " - 2nd Quartile Q2 is the same as 50th percentile.\n", " - 3rd Quratile Q3 is same as 75th percentile\n", "- Inter Quartile Range is the difference between the third quartile(Q3) and the first Quartile (Q1)\n", "- The range gives us a measurement of how spread out the entirety of our data set is. The interquartile range, which tells us how far apart the first and third quartile are, indicates how spread out the middle 50% of our set of data is.\n", "- The primary advantage of using the interquartile range rather than the range for the measurement of the spread of a data set is that the interquartile range is not sensitive to outliers.\n", "- Due to its resistance to outliers, the interquartile range is useful in identifying when a value is an outlier.\n", "\n", "- **Percentiles** divide the distribution at any point out of one hundred. For example, if we'd like to identify the threshold for the top 5% of a distribution, we'd cut it at the 95th percentile. Or, for the top 1%, we'd cut at the 99th percentile.\n", "- **Deciles:** (from Latin *decimus*, meaning \"tenth\") divide a distribution into ten evenly-sized segments." ] }, { "cell_type": "code", "execution_count": null, "id": "49705991", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b9708ed5", "metadata": {}, "source": [ "#### Quartiles" ] }, { "cell_type": "markdown", "id": "400a194b", "metadata": {}, "source": [ "**Example 1:**" ] }, { "cell_type": "code", "execution_count": null, "id": "0a734c85", "metadata": {}, "outputs": [], "source": [ "import statistics\n", "data = [11,13,16,19,20,21,23,25,26,29,33,34,36,38,39,46,52,55,58]\n", "q1,q2,q3 = statistics.quantiles(data, n=4)\n", "print(\"Q1: \", q1)\n", "print(\"Q2: \", q2)\n", "print(\"Q3: \", q3)\n", "print(\"IQR = Q3 - Q1: \", q3-q1)" ] }, { "cell_type": "code", "execution_count": null, "id": "99b5925f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b93d6eb6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7a391339", "metadata": {}, "source": [ "**Example 2:**\n", "- We can use the IQR method of identifying outliers to set up a “fence” outside of Q1 and Q3. Any values that fall outside of this fence are considered outliers." ] }, { "cell_type": "code", "execution_count": null, "id": "9455e98f", "metadata": {}, "outputs": [], "source": [ "data = [0, 0, 2, 5, 8, 8, 8, 9, 9, 10, 10, 10, 11, 12, 12, 12, 14, 15, 20, 25]\n", "q1,q2,q3 = statistics.quantiles(data, n=4)\n", "print(\"Q1: \", q1)\n", "print(\"Q2: \", q2)\n", "print(\"Q3: \", q3)\n", "iqr = q3 - q1\n", "print(\"IQR = Q3 - Q1: \", iqr)" ] }, { "cell_type": "code", "execution_count": null, "id": "ccfcb3a8", "metadata": {}, "outputs": [], "source": [ "# Any observations that are more than 1.5 times IQR above Q3 are considered outliers.\n", "upperfence = q3 + iqr*1.5\n", "upperfence" ] }, { "cell_type": "code", "execution_count": null, "id": "0439572a", "metadata": {}, "outputs": [], "source": [ "# Any observations that are less than 1.5 times IQR below Q1 re considered outliers.\n", "lowerfence = q1 - iqr*1.5\n", "lowerfence" ] }, { "cell_type": "markdown", "id": "30b1c3a0", "metadata": {}, "source": [ ">There are 4 outliers: 0, 0, 20, and 25." ] }, { "cell_type": "code", "execution_count": null, "id": "0f5521d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d49f0688", "metadata": {}, "source": [ "### c. Variance\n", "- The `Variance` is a very simple statistic that gives you an extremely rough idea of how spread out a data set is. As a measure of spread, it’s actually pretty weak. A large variance of 22,000, for example, doesn’t tell you much about the spread of data — other than it’s big! The most important reason the variance exists is to give you a way to find the standard deviation which is the square root of variance.\n", "- The reason of dividing by `n-1` in case of sample variance is \"the formula uses sample mean in the numerator and as a consequence may underestimate the true variance of the population. Dividing by n-1 instead of n corrects for that bias.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "017a60ed", "metadata": {}, "source": [ "### d. Standard Deviation\n", "- Simply put, the `Standard Deviation` is a measure of how spread out data is around center of the distribution (the mean). It also gives you an idea of where, percentage wise, a certain value falls. For example, let’s say you took a test and it was normally distributed (shaped like a bell). You score one standard deviation above the mean. That tells you your score puts you in the top 84% of test takers.\n", "- Low standard deviation implies that most values are close to the mean. High standard deviation suggests that the values are more broadly spread out.\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "c92c92c6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d3082c7c", "metadata": {}, "source": [ "**Example 1:**" ] }, { "cell_type": "code", "execution_count": null, "id": "2e5d032c", "metadata": {}, "outputs": [], "source": [ "import statistics\n", "data = [2, 4, 6]\n", "print(\"Sample Mean: \", statistics.mean(data))\n", "print(\"Sample Variance: \", statistics.variance(data))\n", "print(\"Population Variance: \", statistics.pvariance(data))\n", "print(\"Sample Standard Deviation: \", statistics.stdev(data))\n", "print(\"Population Standard Deviation: \", statistics.pstdev(data))" ] }, { "cell_type": "code", "execution_count": null, "id": "c6f5c050", "metadata": {}, "outputs": [], "source": [ "data = [2, 4, 6]\n", "np.std(data)" ] }, { "cell_type": "code", "execution_count": null, "id": "ffda03e7", "metadata": {}, "outputs": [], "source": [ "statistics.stdev(data)" ] }, { "cell_type": "code", "execution_count": null, "id": "104897f9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a0ea7a73", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "671bf801", "metadata": {}, "source": [ "**Example 2:** `Machine Learning` teacher has compiled the result and wants to know whether most students are performing at the same level, or if there is a high standard deviation." ] }, { "cell_type": "code", "execution_count": null, "id": "66b104b3", "metadata": {}, "outputs": [], "source": [ "marks_ML = [75, 69, 80, 70, 60, 63, 64, 69, 71]\n", "\n", "print(\"Mean of ML Marks: \", statistics.mean(marks_ML))\n", "print(\"Standard Deviation of ML Marks: %.2f\" % statistics.stdev(marks_ML))" ] }, { "cell_type": "markdown", "id": "3e146e4c", "metadata": {}, "source": [ ">**Low standard deviation implies that most values are close to the mean.**" ] }, { "cell_type": "code", "execution_count": null, "id": "4fb16d16", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fec96358", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f55acd36", "metadata": {}, "source": [ "**Example 3:** `Artificial Intelligence` teacher has compiled the result and wants to know whether most students are performing at the same level, or if there is a high standard deviation." ] }, { "cell_type": "code", "execution_count": null, "id": "4d669602", "metadata": {}, "outputs": [], "source": [ "marks_AI = [44, 95, 25, 60, 76, 81, 93, 84, 71, 33, 85, 81]\n", "print(\"Mean of AI Marks: \", statistics.mean(marks_AI))\n", "print(\"Standard Deviation of AI Marks: %.2f\" % statistics.stdev(marks_AI))" ] }, { "cell_type": "markdown", "id": "4f7c1928", "metadata": {}, "source": [ ">**Note: High standard deviation implies that the values are more broadly spread out.**" ] }, { "cell_type": "code", "execution_count": null, "id": "76dc10b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "80e38751", "metadata": {}, "source": [ "**Example 4:** Suppose there are 10000 students in my Data Science class. You get 85% marks in your Data Science Exam. The mean of the overall result is 60% with a standard deviation of 10%. How many students are above you?" ] }, { "cell_type": "code", "execution_count": null, "id": "4c03588f", "metadata": {}, "outputs": [], "source": [ "# Let us first generate the random marks of ten thousand students, with a mean of 60 and standard deviation of 10\n", "mu = 60\n", "sigma = 10\n", "np.random.seed(54)\n", "x = np.random.normal(mu, sigma, 10000)\n", "x" ] }, { "cell_type": "code", "execution_count": null, "id": "8d396d72", "metadata": {}, "outputs": [], "source": [ "# Let us verify, whether the mean and std dev of above distribution `x` is 60 and 10 respectively\n", "print(\"np.mean(x): \", np.mean(x))\n", "print(\"np.std(x): \", np.std(x))" ] }, { "cell_type": "markdown", "id": "86f0ab3c", "metadata": {}, "source": [ ">- **Let us calculate the number of students above you out of ten thousand and visualize this by drawing a graph.**" ] }, { "cell_type": "code", "execution_count": null, "id": "ba986226", "metadata": {}, "outputs": [], "source": [ "a = len(np.where(x > 85)[0])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "31eb4486", "metadata": {}, "outputs": [], "source": [ "sns.displot(x, color='green')\n", "plt.axvline(mu, color='orange')\n", "for i in [-3, -2, -1, 1, 2, 3]:\n", " plt.axvline(mu+i*sigma, color='red')\n", "plt.axvline(85, color='purple')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "21be77f8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7ddc17d0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6e96a95a", "metadata": {}, "source": [ "**Example 5:** This is continuation of above example. Suppose your marks in Data Science are still 85%, but this time the mean of the overall result has increased to 90% with a standard deviation of 2. How many students are above you?" ] }, { "cell_type": "code", "execution_count": null, "id": "2550755c", "metadata": {}, "outputs": [], "source": [ "# Let us first generate the random marks of ten thousand students, with a mean of 90 and standard deviation of 2\n", "mu = 90\n", "sigma = 2\n", "np.random.seed(54)\n", "x = np.random.normal(mu, sigma, 10000)\n", "x" ] }, { "cell_type": "code", "execution_count": null, "id": "bcb992e1", "metadata": {}, "outputs": [], "source": [ "# Let us verify, whether the mean and std dev of above distribution `x` is 90 and 2 respectively\n", "print(\"np.mean(x): \", np.mean(x))\n", "print(\"np.std(x): \", np.std(x))" ] }, { "cell_type": "markdown", "id": "3322ef5a", "metadata": {}, "source": [ ">- **Let us calculate the number of students above you out of ten thousand and visualize this by drawing a graph.**" ] }, { "cell_type": "code", "execution_count": null, "id": "09525265", "metadata": {}, "outputs": [], "source": [ "a = len(np.where(x > 85)[0])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "e2f13663", "metadata": {}, "outputs": [], "source": [ "sns.displot(x, color='grey')\n", "plt.axvline(mu, color='orange')\n", "for i in [-3, -2, -1, 1, 2, 3]:\n", " plt.axvline(mu+i*sigma, color='red')\n", "plt.axvline(85, color='purple')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "a6f53447", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "18ef9fcc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c95866aa", "metadata": {}, "source": [ "## 6. Measures of Asymmetry\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "81e0c825", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "63c68d5b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9a1b42ad", "metadata": {}, "source": [ "### a. Skewness\n", " \n", " \n", "\n", "











\n", "\n", "- **`Skewness`:** is the measure of how much the probability distribution of a random variable deviates from the normal distribution. The skewness for a normal distribution is zero\n", " - **`Positive Skewness / Right-Skewed Distribution: (median < mean):`** If the given distribution is shifted to the left and with its tail on the right side, it is a positively skewed distribution. In this type, the majority of the observations are concentrated on the left, and the value of skewness is positive.\n", " - **`Negative Skewness / Left-Skewed Distribution: (median > mean):`** If the given distribution is shifted to the right and with its tail on the left side, it is a negatively skewed distribution. In this type, the majority of the observations are concentrated on the right, and the value of skewness is negative.\n", "- **`Measuring Skewness`:** Skewness can be measured using several methods; however, `Pearson mode skewness` and `Pearson median skewness` are the two frequently used methods.\n", "\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "18790464", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "122782ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c5eacaf7", "metadata": {}, "source": [ "**Example 1:**\n", "\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "id": "eb75b368", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "\n", "import statistics\n", "import scipy.stats as st\n", "import statsmodels as sm" ] }, { "cell_type": "code", "execution_count": 6, "id": "049e73ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mean: 0.7861795940638052\n", "Median: 0.66541753627437\n", "Mode: 0.870250440697566\n", "Skew: 0.9917815445925732\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAAFgCAYAAACFYaNMAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9A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": "display_data" } ], "source": [ "import scipy\n", "import statistics\n", "import seaborn as sns\n", "s = scipy.stats.skewnorm.rvs(20, size=10000)\n", "print(\"Mean: \", statistics.mean(s))\n", "print(\"Median: \", statistics.median(s))\n", "print(\"Mode: \", statistics.mode(s))\n", "print(\"Skew: \", scipy.stats.skew(s))\n", "\n", "sns.displot(s, kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "31a53f57", "metadata": {}, "outputs": [], "source": [ "from scipy import stats\n", "import statistics\n", "data1 = [30, 10, 15, 27, 15, 45, 31, 19, 54, 60, 95]\n", "\n", "print(\"Mean: \", statistics.mean(data1))\n", "print(\"Median: \", statistics.median(data1))\n", "print(\"Mode: \", statistics.mode(data1))\n", "print(\"Skew: \", stats.skew(data1))" ] }, { "cell_type": "code", "execution_count": 3, "id": "a2a3b985", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'stats' has no attribute 'skewnorm'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_61560/890888814.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mstats\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mseaborn\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0msns\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstats\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mskewnorm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrvs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Mean: \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstatistics\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Median: \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstatistics\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmedian\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: module 'stats' has no attribute 'skewnorm'" ] } ], "source": [ "import stats\n", "import seaborn as sns\n", "s = stats.skewnorm.rvs(20, size=10000)\n", "print(\"Mean: \", statistics.mean(data))\n", "print(\"Median: \", statistics.median(data))\n", "print(\"Mode: \", statistics.mode(data))\n", "print(\"Skew: \", stats.skew(data))\n", "\n", "sns.displot(s, kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "f1c103cd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "603b0d23", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ec8282ce", "metadata": {}, "source": [ "**Example 2:**\n", "\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "c46d005b", "metadata": {}, "outputs": [], "source": [ "from scipy import stats\n", "import statistics\n", "data2 = [20, 33, 88, 91, 85, 89, 91, 95]\n", "\n", "print(\"Mean: \", statistics.mean(data2))\n", "print(\"Median: \", statistics.median(data2))\n", "print(\"Mode: \", statistics.mode(data2))\n", "print(\"Skew: \", stats.skew(data2))" ] }, { "cell_type": "code", "execution_count": null, "id": "d948c588", "metadata": {}, "outputs": [], "source": [ "s = stats.skewnorm.rvs(-20, size=10000)\n", "print(\"Mean: \", statistics.mean(data))\n", "print(\"Median: \", statistics.median(data))\n", "print(\"Mode: \", statistics.mode(data))\n", "print(\"Skew: \", stats.skew(data))\n", "\n", "sns.displot(s, kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "5aa2b74e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "78dc707a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "1a854268", "metadata": {}, "source": [ "**Example 3:**\n", "\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "ca404079", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy import stats\n", "import statistics\n", "from matplotlib import pyplot as plt\n", "\n", "mean = 25\n", "stdev = 5\n", "data3 = np.random.normal(loc=mean, scale=stdev,size=1000)\n", "print(\"Mean: \", statistics.mean(data3))\n", "print(\"Median: \", statistics.median(data3))\n", "print(\"Mode: \", statistics.mode(data3))\n", "print(\"Skew: \", stats.skew(data3))\n", "plt.hist(data3, 50);" ] }, { "cell_type": "code", "execution_count": null, "id": "d8f1ee8e", "metadata": {}, "outputs": [], "source": [ "s = stats.skewnorm.rvs(0, size=10000)\n", "print(\"Mean: \", statistics.mean(data))\n", "print(\"Median: \", statistics.median(data))\n", "print(\"Mode: \", statistics.mode(data))\n", "print(\"Skew: \", stats.skew(data))\n", "\n", "sns.displot(s, kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "f59a3a28", "metadata": {}, "outputs": [], "source": [ "np.mean(data)" ] }, { "cell_type": "code", "execution_count": null, "id": "edf4417b", "metadata": {}, "outputs": [], "source": [ "from scipy import stats\n", "from matplotlib import pyplot as plt\n", "import numpy as np\n", "\n", "data = stats.skewnorm.rvs(0, size=1000) # first argument is \"skewness\"; 0 has no skew\n", "print(\"Mean: \", statistics.mean(data))\n", "print(\"Median: \", statistics.median(data))\n", "print(\"Mode: \", statistics.mode(data))\n", "print(\"Skew: \", stats.skew(data))\n", "\n", "#fig, ax = plt.subplots()\n", "#plt.hist(data, bins=50);\n", "sns.displot(s, kde=True);\n", "#plt.axvline(data = np.mean(data), color='black')\n", "#plt.axvline(data = np.median(data), color='black')\n", "plt.show();" ] }, { "cell_type": "code", "execution_count": null, "id": "78fcb2de", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c809c6da", "metadata": {}, "source": [ "### b. Kurtosis\n", "\n", "\n", "- `Kurtosis`: is a statistical measure, to determine if the data is heavy-tailed or light-tailed in a normal distribution.\n", "\n", " - **Leptokurtic (kurtosis>3)**: If a given distribution has a kurtosis greater than 3, it is said to be leptokurtic, which means it tends to produce more outliers than the normal distribution.\n", " - **Mesokurtic (kurtosis==3)**: This distribution looks similar to a normal distribution.\n", " - **Platykurtic (kurtosis<3**: If a given distribution has a kurtosis less than 3, it is said to be platykurtic, which means it tends to produce fewer and less extreme outliers than the normal distribution.\n", "\n", "- The main difference between skewness and kurtosis is that the skewness refers to the degree of symmetry, whereas the kurtosis refers to the degree of presence of outliers in the distribution." ] }, { "cell_type": "code", "execution_count": null, "id": "68c0ef64", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0aeb65c5", "metadata": {}, "source": [ "**Example 1:**" ] }, { "cell_type": "code", "execution_count": 12, "id": "fd5a4280", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kurtosis: 2.8626095865000307\n", "Skew: -0.02740018788755584\n" ] }, { "data": { "image/png": "iV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d3QngTuA3gQPAbuAp4Kaqav0AbJ2c17HyT7sC/gy47fQ6Vpck3wf8d+CP+cYa2ztYWV8dzT49S87XMaJ9muTbWflQaAcrE58DVfVvkvxdxrU/18v5a4xof66W5DrgXw1HQYxqf25kyxSwJF1otvoShCSNlgUsSU0sYElqYgFLUhMLWJKaWMCS1MQClqQm/xdLOk3rruPCKAAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "#Mesokurtic: kurtosis==3\n", "import numpy as np\n", "from scipy import stats\n", "import matplotlib.pyplot as plt\n", "mean = 25\n", "stdev = 5\n", "data = np.random.normal(loc=mean, scale=stdev,size=1000)\n", "print('Kurtosis:', stats.kurtosis(data, fisher=False))\n", "print(\"Skew: \", stats.skew(data))\n", "#plt.hist(data, 50);\n", "sns.displot(data);" ] }, { "cell_type": "code", "execution_count": null, "id": "42205d5f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "874adf19", "metadata": {}, "source": [ "**Example 2:**" ] }, { "cell_type": "code", "execution_count": 9, "id": "2361707f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kurtosis: 5.425202860794532\n", "Skew: -0.36819634158211506\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAOcUlEQVR4nO3df4xlZ13H8ffHrlR+SGjT2WVtG6cmG7QQBZ3UKonBLNiVJWwxliwJOIGSVdP6Kya6lT9qQpps/I1GSFaoLLFSNpWmGytIXUOIibSdQkNpl9oNXdttx+4gVYkmxYWvf8ypXqd3dmfuuXd+PPN+JZN7znPOmfPdk72feea55zyTqkKS1JbvWO8CJEnjZ7hLUoMMd0lqkO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ED3un3dqttEnM99iSR/B7xyyKb3VdVdyx02pK3O0d6Ec10rFoej3s/iv/f9wO8D76Hxa7IKXofhXl9VTyfZDtyT5CvrXdBmZbgvUVVvHOGw5aZbON0tL21vwkqvVZI/A/66W3VqikVehyGq6unu9UySO1kcvnomyc6qmu+GOs+sa5GbhMMy43EM2J/kwiRXsPjB6X3dr5DfSHJ1d5fMzwHL9f6b0r0Jn/c2Fj+MhmWu1VrXtwE4RccSSV6a5LufXwZ+isX/N8eA2W63WbbIe6gve+6rkORtwJ8AU8DdSR6sqmuq6uEkR4FHgLPADVX1re6wXwQ+CrwY+FT3tRX8TpLXsjjUcAr4eYDzXKstYwtO0bESO4A7u7uFtwF/WVWfTnI/cDTJ9cATwHXrWOOm4fQDktQgh2UkqUGGuyQ1yHCXpAYZ7pLUIMNdkhpkuEtSgwx3SWrQ/wDOrsYteVYa+QAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "#Leptokurtic: kurtosis > 3 (outliers)\n", "import numpy as np\n", "from scipy import stats\n", "data = np.random.laplace(1.45, 15, 1000)\n", "print('Kurtosis:', stats.kurtosis(data, fisher=False))\n", "print(\"Skew: \", stats.skew(data)) \n", "plt.hist(data, 50);" ] }, { "cell_type": "code", "execution_count": null, "id": "64d7ef58", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0ca53008", "metadata": {}, "source": [ "**Example 3:**" ] }, { "cell_type": "code", "execution_count": 10, "id": "814a5357", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kurtosis: 1.7752860010898066\n", "Skew: -0.0443904032575784\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAs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9U1XfmXNtmmmmGbaUAH3Jq/nY7fX/QeJL8DPAh4Jqq+taCapuXIWNeBj4+Du9dwLVJTlbVpxdS4ewNfW8/WVXfB76f5B7gYqBrgA8Z85uAW2t0cPjhJP8G/BTwlcWUuClmmmFb6RDKkFPz7wTeOP4k93Lgv6rq2KILnaGJY06yB7gdeEPj2djpJo65ql5cVXurai/w58BvNQ5vGPbevgP4pSRnJvkhRlf2PLrgOmdpyJgfY/Q/DpKcB7wUeGShVS7eTDNsy8zAa41T85P85vjxDzDqSLgWeBj4b0b/grc1cMy/A/wo8CfjGenJanwVt4Fj3laGjLmqjib5LHA/8AzwoapatRWtg4E/598DPpLkAUaHFt5RVa0vMZvkY8CVwK4kjwPvBM6C+WSYp9JLUlNb6RCKJGkdDHBJasoAl6SmDHBJasoAl6SmDHBJasoAl6Sm/g/j3IlZEX+NQQAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "#Platykurtic: kurtosis < 3 (less extreme outliers)\n", "import numpy as np\n", "from scipy import stats\n", "data = np.random.rand(1000)\n", "print('Kurtosis:', stats.kurtosis(data, fisher=False))\n", "print(\"Skew: \", stats.skew(data)) \n", "plt.hist(data, 50);" ] }, { "cell_type": "code", "execution_count": null, "id": "3e41d57c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fe807f11", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7e513cde", "metadata": {}, "source": [ "## 7. Analysis Strategy\n", "" ] }, { "cell_type": "markdown", "id": "c1946380", "metadata": {}, "source": [ "\n", "\n", "### a. Univariate Analysis\n", "- In a dataset, Univariate analysis is performed on each variable separately. It is possible on numerical as well as categorical variables. It takes data, summarizes that data and finds patterns in the data. \n", "- Suppose that the heights of students of a class are recorded, there is only one variable that is height and it is not dealing with any cause or relationship. \n", "\n", "### b. Bivariate Analysis\n", "\n", "\n", "\n", "- Bivariate analysis is an analysis that is performed to determine the relationship between 2 variables. The analysis is related to cause and the relationship between the two variables. One of the variables will be dependent and the other is independent. The data types of the two variables can be:\n", " - (Numerical-Numerical)\n", " - (Numerical-Categorical)\n", " - (Categorical-Categorical)\n", "\n", "### c. Visual Analysis\n", "- If you want to compare values and analyze trends use:\n", " - Line Graph\n", " - Bar Graph\n", " - Column Graph\n", "- If you want to show how one variable relates to one or more different variables use:\n", " - Scatter Plot\n", " - Bubble Plot\n", " - Line Plot\n", "- If you want to understand distribution of your data to understand outliers, normal tendency and range of information in your data use:\n", " - Histogram\n", " - Density Plot\n", " - Box Plot\n", " - Violin Plot\n", "- If you want to show composition of something use:\n", " - Pie Chart\n", " - Stacked Bar Graph\n", " - Stacked Column Graph\n", " - Area Graph" ] }, { "cell_type": "code", "execution_count": null, "id": "d17863df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4ac76981", "metadata": {}, "source": [ "## 8. Variance, Covariance and Correlation\n", "- `Variance` tells us how much a (single) quantity varies w.r.t. its mean. Its the spread of data around the mean value. You only know the magnitude here, as in how much the data is spread.\n", "- `Covariance` tells us direction in which two quantities vary with each other.\n", "- `Correlation` shows us both, the direction and magnitude of how two quantities vary with each other." ] }, { "cell_type": "code", "execution_count": null, "id": "980507b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "bd8739dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "675ef903", "metadata": {}, "source": [ "### a. Covariance\n", "- Covariance is used to measure as to how the mean values of two random variables move together. For example the height and weight of a person in a population. The formula to calculate sample covariance is:\n", "\n", "- The formula to calculate population covariance is:\n", "\n", "\n", "\n", " \n", "- Covariance measures the direction of the relationship between two variables. \n", " - `Positive covariance`: Indicates that two variables tend to move in the same direction.\n", " - `Negative covariance`: Reveals that two variables tend to move in inverse directions.\n", " - `Zero covarince`: Indicates that two variables have no relationship between each other.\n", "- **Covariance Matrix:** For multi-dimensional data, there applies a generalization of covariance in terms of a covariance matrix. The covariance matrix is also known as the variance-covariance matrix, as the diagonal values of the covariance matrix show variances and the other values are the covariances. The covariance matrix for two variables is a square matrix which can be written as follows:\n", "\n", "" ] }, { "cell_type": "markdown", "id": "32003eee", "metadata": {}, "source": [ "### b. Correlation\n", "- Correlation is a measure that tells us the direction as well as the magnitude of how two quantities vary with each other (e.g., height and weight).\n", "- The Pearson Correlation Coefficient (r) is used to quantify the strength and direction of linear relationship between two quantitative variables:\n", "\n", "\n", "- The value of correlation coefficient range from -1 to +1, shows the strength of the correlation:\n", " - 1 indicates a perfect positive correlation.\n", " - -1 indicates a perfect negative correlation.\n", " - 0 indicates that there is no relationship between the different variables.\n", "\n", "\n", "\n", "\n", "- **Correlation Matrix:** is a table showing correlation coefficients between various variables. The rows and columns contain the value of the variables, and each cell shows the correlation coefficient.\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "f1533725", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "00f7da38", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3c0c3c4d", "metadata": {}, "source": [ "### Example 1: Strong Positive Correlation\n", ">**Let us compute the variance and covariance for two variables `weight` and `height` of six persons**" ] }, { "cell_type": "code", "execution_count": null, "id": "189b871c", "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "import statistics\n", "\n", "weight = [61,62,73,74,82,86]\n", "height = [157,168,170,181,191,185]\n", "\n", "print(\"mean(weight): \", statistics.mean(weight))\n", "print(\"mean(height): \", statistics.mean(height))\n", "print(\"var(weight): \", statistics.variance(weight))\n", "print(\"var(height): \", statistics.variance(height))\n", "print(\"cov(weight, height): \", statistics.covariance(weight,height))\n", "print(\"Pearson Correlation Coefficient (r): \",statistics.correlation(weight, height))\n", "\n", "plt.scatter(x=weight, y=height);" ] }, { "cell_type": "markdown", "id": "3ebe2840", "metadata": {}, "source": [ "#### Covariance Matrix\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "c7669159", "metadata": {}, "outputs": [], "source": [ "print(\"var(weight): \", statistics.variance(weight))\n", "print(\"var(height): \", statistics.variance(height))\n", "print(\"cov(weight, height): \", statistics.covariance(weight,height))\n", "\n", "print(\"Covariance Matrix: \\n\", np.cov(weight,height))" ] }, { "cell_type": "markdown", "id": "d72815d8", "metadata": {}, "source": [ "#### Correlation Matrix\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "2b2c6228", "metadata": {}, "outputs": [], "source": [ "print(\"Pearson Correlation Coefficient 'r': \",statistics.correlation(weight, height))\n", "print(\"Correlation Matrix: \\n\", np.corrcoef(weight,height))" ] }, { "cell_type": "code", "execution_count": null, "id": "3ad1c581", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4cfc4950", "metadata": {}, "source": [ "### Example 2: Perfect Negative Correlation\n", ">**Let us compute the variance and covariance for two variables `time spent on yoga` and `stress level`**" ] }, { "cell_type": "code", "execution_count": null, "id": "9c14aa53", "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "import statistics\n", "\n", "yoga = [1,2,3,4,5,6,7,8,9]\n", "stress = [90,80,70,60,50,40,30,20,10]\n", "\n", "print(\"mean(yoga): \", statistics.mean(yoga))\n", "print(\"mean(stress): \", statistics.mean(stress))\n", "print(\"var(yoga): \", statistics.variance(yoga))\n", "print(\"var(stress): \", statistics.variance(stress))\n", "print(\"cov(yoga, stress): \", statistics.covariance(yoga,stress))\n", "print(\"Pearson Correlation Coefficient 'r': \",statistics.correlation(yoga, stress))\n", "\n", "plt.scatter(x=yoga, y=stress);" ] }, { "cell_type": "markdown", "id": "6caaf423", "metadata": {}, "source": [ "#### Covariance Matrix\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "5c147a69", "metadata": {}, "outputs": [], "source": [ "print(\"var(yoga): \", statistics.variance(yoga))\n", "print(\"var(stress): \", statistics.variance(stress))\n", "print(\"cov(yoga, stress): \", statistics.covariance(yoga,stress))\n", "\n", "print(\"Covariance Matrix: \\n\", np.cov(yoga,stress))" ] }, { "cell_type": "markdown", "id": "681206c5", "metadata": {}, "source": [ "#### Correlation Matrix\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "3f3db50d", "metadata": {}, "outputs": [], "source": [ "print(\"Pearson Correlation Coefficient 'r': \",statistics.correlation(yoga, stress))\n", "print(\"Correlation Matrix: \\n\", np.corrcoef(yoga,stress))" ] }, { "cell_type": "code", "execution_count": null, "id": "7c6622a8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "a0ec1d17", "metadata": {}, "source": [ "### Example 3: Standardized Data\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "0843f412", "metadata": {}, "outputs": [], "source": [ "# Correlation matrix of original data\n", "weight = [86,82,74,73,62,61]\n", "height = [157,168,170,181,191,185]\n", "print(\"Correlation Matrix: \\n\", np.corrcoef(weight,height))" ] }, { "cell_type": "code", "execution_count": null, "id": "32c4a671", "metadata": {}, "outputs": [], "source": [ "# Covariance matrix of standardized data\n", "weight = [1.28, 0.89, 0.10, 0, -1.08, -1.18]\n", "height = [-1.46, -0.58, -0.42, 0.45, 1.25, 0.77]\n", "print(\"Covariance Matrix: \\n\", np.cov(weight,height))" ] }, { "cell_type": "markdown", "id": "2cabe495", "metadata": {}, "source": [ ">**Covariance and Correlation matrix will be identical in this case**" ] }, { "cell_type": "code", "execution_count": null, "id": "14d84db5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e611b22b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c641f73a", "metadata": {}, "source": [ "## 9. Example Datasets" ] }, { "cell_type": "markdown", "id": "84ecbf44", "metadata": {}, "source": [ "### a. TITANIC Dataset\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "d1f18db4", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df_titanic = pd.read_csv('datasets/titanic3.csv')\n", "df_titanic" ] }, { "cell_type": "code", "execution_count": null, "id": "68cd5679", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9ec579c3", "metadata": {}, "source": [ "### b. IRIS Dataset\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "27c96471", "metadata": {}, "outputs": [], "source": [ "df_iris = pd.read_csv('datasets/iris.csv')\n", "df_iris" ] }, { "cell_type": "code", "execution_count": null, "id": "a5e704b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "29fe7cc7", "metadata": {}, "source": [ "### c. TIPS Dataset\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "f158aa62", "metadata": {}, "outputs": [], "source": [ "df_tips = pd.read_csv('datasets/tips.csv')\n", "df_tips" ] }, { "cell_type": "code", "execution_count": null, "id": "aa436a60", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6b72657a", "metadata": {}, "source": [ "## 10. Univariate Analysis and Data Visualization" ] }, { "cell_type": "code", "execution_count": null, "id": "f8ce2f15", "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "import statistics\n", "from scipy import stats\n", "import pandas as pd\n", "import numpy as np\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "id": "b3eb8276", "metadata": {}, "source": [ "**Example 1:** Check the spread of data of `sepal_width` column in `IRIS` dataset using **Scatter Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "45f2a147", "metadata": {}, "outputs": [], "source": [ "print(\"mean(sepal_width): %.2f\" % statistics.mean(df_iris.loc[:, 'sepal_width']))\n", "print(\"median(sepal_width): %.2f\" % statistics.median(df_iris.loc[:, 'sepal_width']))\n", "print(\"mode(sepal_width): %.2f\" % statistics.mode(df_iris.loc[:, 'sepal_width']))\n", "print(\"variance(sepal_width): %.2f\" % statistics.variance(df_iris.loc[:, 'sepal_width']))\n", "\n", "sns.relplot(x = df_iris.index, y = 'sepal_width', data = df_iris, kind='scatter');" ] }, { "cell_type": "code", "execution_count": null, "id": "e51d3e6c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "071ba779", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "50369f63", "metadata": {}, "source": [ "**Example 2:** Check the measures of center for the `age` column in `TITANIC` dataset using **Box Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "b055c643", "metadata": {}, "outputs": [], "source": [ "#There are 264 NaN values under age column, which may disturb our calculations, so replace them with mean\n", "df_titanic.loc[(df_titanic.age.isna()),'age'] = df_titanic.age.mean()\n", "\n", "print(\"min(age): \", df_titanic.loc[:, 'age'].min())\n", "print(\"max(age): \", df_titanic.loc[:, 'age'].max())\n", "print(\"median(age): \", df_titanic.loc[:, 'age'].median())\n", "print(\"q1, q2, q3: \", statistics.quantiles(df_titanic.loc[:, 'age'], n=4))\n", "sns.catplot(y='age', kind='box', data = df_titanic);" ] }, { "cell_type": "code", "execution_count": null, "id": "bf09d6ad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f2e0743e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5592384e", "metadata": {}, "source": [ "**Example 3:** Check the distribution of data of `sepal_width` column in `IRIS` dataset using **Histogram**" ] }, { "cell_type": "code", "execution_count": null, "id": "17f9ac58", "metadata": {}, "outputs": [], "source": [ "# sepal_width Column of IRIS Dataset\n", "print(\"skew(sepal_width): %.2f\" % stats.skew(df_iris.loc[:, 'sepal_width'])) \n", "print('Kurtosis(sepal_width): %.2f' % stats.kurtosis(df_iris.loc[:, 'sepal_width'], fisher=False))\n", "sns.displot(x= 'sepal_width', data=df_iris, kind='hist', kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "a30b20db", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e8d6318f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "507345bb", "metadata": {}, "source": [ "**Example 4:** Check the distribution of data of `age` column in `TITANIC` dataset using **Histogram**" ] }, { "cell_type": "code", "execution_count": null, "id": "3ba7d698", "metadata": {}, "outputs": [], "source": [ "# age Column of TITANIC Dataset\n", "print(\"skew(age): %.2f\" % stats.skew(df_titanic.loc[:, 'age'])) \n", "print('Kurtosis(age): %.2f' % stats.kurtosis(df_titanic.loc[:, 'age'], fisher=False))\n", "sns.displot(x= 'age', data=df_titanic, kind='hist', kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "92f03f8e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e8b4edc0", "metadata": {}, "source": [ "**Example 5:** Check the distribution of data of `tip` column in `TIPS` dataset using **Histogram**" ] }, { "cell_type": "code", "execution_count": null, "id": "369bbdc5", "metadata": {}, "outputs": [], "source": [ "# tip Column of TIPS Dataset\n", "print(\"skew(tip): %.2f\" % stats.skew(df_tips.loc[:, 'tip'])) \n", "print('Kurtosis(tip): %.2f' % stats.kurtosis(df_tips.loc[:, 'tip'], fisher=False))\n", "sns.displot(x= 'tip', data=df_tips, kind='hist', kde=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "7f108e5e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8b88056b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "fea3c16d", "metadata": {}, "source": [ "## 11. Bivariate Analysis and Data Visualization" ] }, { "cell_type": "markdown", "id": "d1755037", "metadata": {}, "source": [ "### a. Two Categorical Variables\n", "- Data concerning two categorical (i.e., nominal- or ordinal-level) variables can be displayed using a bar chart. " ] }, { "cell_type": "markdown", "id": "9de59e26", "metadata": {}, "source": [ "**Example 1:** Compare the `sex` and `survived` columns of `TITANIC` dataset using **Bar Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "b4a3b6dc", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='sex', y ='survived',kind='bar', data = df_titanic);" ] }, { "cell_type": "code", "execution_count": null, "id": "8552f2c2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b318a2fa", "metadata": {}, "source": [ "**Example 2:** Check the survival. count of passengers bbased on `sex` column of `TITANIC` dataset using **Count Plot** and adding the hue argument" ] }, { "cell_type": "code", "execution_count": null, "id": "cf9c02a6", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='sex',kind='count', data = df_titanic, hue='survived');" ] }, { "cell_type": "code", "execution_count": null, "id": "0f87ca02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5d3e9a3b", "metadata": {}, "source": [ "**Example 3:** Check the survival count of passengers based on `passenger class` column of `TITANIC` dataset using **Count Plot** and adding the col argument" ] }, { "cell_type": "code", "execution_count": null, "id": "7800dba5", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='pclass',kind='count', data = df_titanic, col='survived');" ] }, { "cell_type": "code", "execution_count": null, "id": "f7764d0d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "03bdd602", "metadata": {}, "source": [ "**Example 4:** Check the survival rate of passengers based on `passenger class` column of `TITANIC` dataset using **Bar Plot** " ] }, { "cell_type": "code", "execution_count": null, "id": "282d7658", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='pclass', y ='survived',kind='bar', data = df_titanic);" ] }, { "cell_type": "code", "execution_count": null, "id": "c3c353cf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "47698f1d", "metadata": {}, "source": [ "### b. One Quantitative and One Categorical Variable\n", "- Often times we want to compare groups in terms of a quantitative variable. For example, we may want to compare the age of males and females. In this case age is a quantitate variable while biological sex is a categorical variable. Graphs with groups can be used to compare the distributions of heights in these two groups. \n", "- This is an example of constructing side-by-side boxplots with groups. The side-by-side boxplots allow us to easily compare the median, IQR, and range of the two groups. The histograms with groups allow us to compare the shape, central tendency, and variability of the two groups." ] }, { "cell_type": "markdown", "id": "72a967c5", "metadata": {}, "source": [ "**Example 1:** Compare the `sex` and `age` columns of `TITANIC` dataset using **Box Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "fd74e3e1", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='sex', y='age', kind='box', data = df_titanic);" ] }, { "cell_type": "code", "execution_count": null, "id": "32855c87", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e0354465", "metadata": {}, "source": [ ">- **Have separate Boxplots for survived as not survived using `col` argument**" ] }, { "cell_type": "code", "execution_count": null, "id": "4418fec6", "metadata": {}, "outputs": [], "source": [ "sns.catplot(x ='sex', y='age', kind='box', data = df_titanic, col='survived');" ] }, { "cell_type": "code", "execution_count": null, "id": "bf67b0fe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "fe2a1162", "metadata": {}, "source": [ "**Example 2:** Compare the `pclass` and `age` columns of `TITANIC` dataset using **Box Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "06bf18d3", "metadata": { "scrolled": true }, "outputs": [], "source": [ "sns.catplot(x ='pclass', y='age', kind='box', data = df_titanic);" ] }, { "cell_type": "code", "execution_count": null, "id": "ab58370c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8d9f81e3", "metadata": {}, "source": [ ">- **Have separate Boxplots for survived as not survived using `col` argument**" ] }, { "cell_type": "code", "execution_count": null, "id": "2504d4cb", "metadata": { "scrolled": true }, "outputs": [], "source": [ "sns.catplot(x ='pclass', y='age', kind='box', data = df_titanic, col='survived');" ] }, { "cell_type": "code", "execution_count": null, "id": "7f2d9073", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e85950c1", "metadata": {}, "source": [ "### c. Two Quantitative Variables\n", "- This is done using scatterplots, correlation, and simple linear regression. \n", "- A scatterplot is a graph used to display data concerning two quantitative variables. \n", "- Correlation is a measure of the direction and strength of the relationship between two quantitative variables.\n", "- Simple linear regression uses one quantitative variable to predict a second quantitative variable. \n", "- **Scatter Plot:** \n", " - A graphical representation of two quantitative variables in which the explanatory variable is on the x-axis and the response variable is on the y-axis. When examining a scatterplot, we need to consider the following:\n", " - Direction (positive or negative)\n", " - Form (linear or non-linear) \n", " - Strength (weak, moderate, strong)\n", " - Bivariate outliers" ] }, { "cell_type": "markdown", "id": "5a0b24a8", "metadata": {}, "source": [ "**Example 1:** Compare the `sepal_length` and `sepal_width` columns of `IRIS` dataset using **Scatter Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "ba29d2d5", "metadata": {}, "outputs": [], "source": [ "print(\"corr(sepal_length,sepal_width): %.2f\" %\n", " statistics.correlation(df_iris['sepal_length'], df_iris['sepal_width']))\n", "sns.relplot(x='sepal_length', y='sepal_width', data=df_iris, kind='scatter');" ] }, { "cell_type": "code", "execution_count": null, "id": "cc599ba3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3caac358", "metadata": {}, "source": [ "**Example 2:** Compare the `petal_length` and `petal_width` columns of `IRIS` dataset using **Scatter Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "299d0b19", "metadata": {}, "outputs": [], "source": [ "print(\"corr(petal_length,petal_width): %.2f\" %\n", " statistics.correlation(df_iris['petal_length'], df_iris['petal_width']))\n", "sns.relplot(x='petal_length', y='petal_width', data=df_iris, kind='scatter');" ] }, { "cell_type": "code", "execution_count": null, "id": "4251f4df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "32d9d673", "metadata": {}, "source": [ "**Example 3:** Compare the `total_bill` and `tip` columns of `TIPS` dataset using **Scatter Plot**" ] }, { "cell_type": "code", "execution_count": null, "id": "a8e29cda", "metadata": {}, "outputs": [], "source": [ "print(\"Pearson Correlation Coefficient (r): %.2f\" %\n", " statistics.correlation(df_tips.loc[:, 'total_bill'],df_tips.loc[:, 'tip']))\n", "\n", "sns.relplot(x='total_bill', y='tip', data=df_tips, kind='scatter');" ] }, { "cell_type": "code", "execution_count": null, "id": "5b014e63", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d68331f4", "metadata": {}, "source": [ "**Example 4:** Check relationships using **Correlation Matrix** and **Heat Map** for the numberic columns of `TIPS` dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "bd0f6170", "metadata": {}, "outputs": [], "source": [ "df_tips.corr()" ] }, { "cell_type": "code", "execution_count": null, "id": "ec5f8020", "metadata": {}, "outputs": [], "source": [ "sns.heatmap(df_tips.corr(), annot=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "49130430", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fc52e236", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9793e710", "metadata": {}, "source": [ "**Example 5:** Check relationships using **Correlation Matrix** and **Heat Map** for the numberic columns of `IRIS` dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "d261c122", "metadata": {}, "outputs": [], "source": [ "df_iris.corr()" ] }, { "cell_type": "code", "execution_count": null, "id": "98065d3d", "metadata": {}, "outputs": [], "source": [ "sns.heatmap(df_iris.corr(), annot=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "f63e9679", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "202e22b4", "metadata": {}, "source": [ "**Example 6:** Check relationships using **Correlation Matrix** and **Heat Map** for the numeric columns of `TITANIC` dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "ad0d9951", "metadata": {}, "outputs": [], "source": [ "df_titanic.corr()" ] }, { "cell_type": "code", "execution_count": null, "id": "73705ddd", "metadata": {}, "outputs": [], "source": [ "sns.heatmap(df_titanic.corr(), annot=True);" ] }, { "cell_type": "code", "execution_count": null, "id": "d4ee3c1f", "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }