Created
November 13, 2020 03:01
-
-
Save PBSWE/8037d1a4b3db9b67c7f6d1bd783287ed to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<center>\n", | |
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n", | |
"</center>\n", | |
"\n", | |
"# Data Wrangling\n", | |
"\n", | |
"Estimated time needed: **2 to 4** hours\n", | |
"\n", | |
"## Objectives\n", | |
"\n", | |
"After completing this lab you will be able to:\n", | |
"\n", | |
"- Handle missing values\n", | |
"- Correct data format\n", | |
"- Standardize and Normalize Data\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Table of content</h2>\n", | |
"\n", | |
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", | |
"<ul>\n", | |
" <li><a href=\"#identify_handle_missing_values\">Identify and handle missing values</a>\n", | |
" <ul>\n", | |
" <li><a href=\"#identify_missing_values\">Identify missing values</a></li>\n", | |
" <li><a href=\"#deal_missing_values\">Deal with missing values</a></li>\n", | |
" <li><a href=\"#correct_data_format\">Correct data format</a></li>\n", | |
" </ul>\n", | |
" </li>\n", | |
" <li><a href=\"#data_standardization\">Data standardization</a></li>\n", | |
" <li><a href=\"#data_normalization\">Data Normalization (centering/scaling)</a></li>\n", | |
" <li><a href=\"#binning\">Binning</a></li>\n", | |
" <li><a href=\"#indicator\">Indicator variable</a></li>\n", | |
"</ul>\n", | |
" \n", | |
"</div>\n", | |
" \n", | |
"<hr>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>What is the purpose of Data Wrangling?</h2>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Data Wrangling is the process of converting data from the initial format to a format that may be better for analysis.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>What is the fuel consumption (L/100k) rate for the diesel car?</h3>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>Import data</h3>\n", | |
"<p>\n", | |
"You can find the \"Automobile Data Set\" from the following link: <a href=\"https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data\">https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data</a>. \n", | |
"We will be using this data set throughout this course.\n", | |
"</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Import pandas</h4> \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import matplotlib.pylab as plt" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Reading the data set from the URL and adding the related headers.</h2>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"URL of the dataset\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This dataset was hosted on IBM Cloud object click <a href=\"https://cocl.us/corsera_da0101en_notebook_bottom\">HERE</a> for free storage \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"filename = \"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DA0101EN-SkillsNetwork/labs/Data%20files/auto.csv\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" Python list <b>headers</b> containing name of headers \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"headers = [\"symboling\",\"normalized-losses\",\"make\",\"fuel-type\",\"aspiration\", \"num-of-doors\",\"body-style\",\n", | |
" \"drive-wheels\",\"engine-location\",\"wheel-base\", \"length\",\"width\",\"height\",\"curb-weight\",\"engine-type\",\n", | |
" \"num-of-cylinders\", \"engine-size\",\"fuel-system\",\"bore\",\"stroke\",\"compression-ratio\",\"horsepower\",\n", | |
" \"peak-rpm\",\"city-mpg\",\"highway-mpg\",\"price\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Use the Pandas method <b>read_csv()</b> to load the data from the web address. Set the parameter \"names\" equal to the Python list \"headers\".\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.read_csv(filename, names = headers)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" Use the method <b>head()</b> to display the first five rows of the dataframe. \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>engine-size</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>152</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>109</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>2</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>15250</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>17710</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>1</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>wagon</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>18920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.3</td>\n", | |
" <td>140</td>\n", | |
" <td>5500</td>\n", | |
" <td>17</td>\n", | |
" <td>20</td>\n", | |
" <td>23875</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.5</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>7.0</td>\n", | |
" <td>160</td>\n", | |
" <td>5500</td>\n", | |
" <td>16</td>\n", | |
" <td>22</td>\n", | |
" <td>?</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>10 rows × 26 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n", | |
"0 3 ? alfa-romero gas std two \n", | |
"1 3 ? alfa-romero gas std two \n", | |
"2 1 ? alfa-romero gas std two \n", | |
"3 2 164 audi gas std four \n", | |
"4 2 164 audi gas std four \n", | |
"5 2 ? audi gas std two \n", | |
"6 1 158 audi gas std four \n", | |
"7 1 ? audi gas std four \n", | |
"8 1 158 audi gas turbo four \n", | |
"9 0 ? audi gas turbo two \n", | |
"\n", | |
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n", | |
"0 convertible rwd front 88.6 ... 130 \n", | |
"1 convertible rwd front 88.6 ... 130 \n", | |
"2 hatchback rwd front 94.5 ... 152 \n", | |
"3 sedan fwd front 99.8 ... 109 \n", | |
"4 sedan 4wd front 99.4 ... 136 \n", | |
"5 sedan fwd front 99.8 ... 136 \n", | |
"6 sedan fwd front 105.8 ... 136 \n", | |
"7 wagon fwd front 105.8 ... 136 \n", | |
"8 sedan fwd front 105.8 ... 131 \n", | |
"9 hatchback 4wd front 99.5 ... 131 \n", | |
"\n", | |
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n", | |
"0 mpfi 3.47 2.68 9.0 111 5000 21 \n", | |
"1 mpfi 3.47 2.68 9.0 111 5000 21 \n", | |
"2 mpfi 2.68 3.47 9.0 154 5000 19 \n", | |
"3 mpfi 3.19 3.40 10.0 102 5500 24 \n", | |
"4 mpfi 3.19 3.40 8.0 115 5500 18 \n", | |
"5 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"6 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"7 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"8 mpfi 3.13 3.40 8.3 140 5500 17 \n", | |
"9 mpfi 3.13 3.40 7.0 160 5500 16 \n", | |
"\n", | |
" highway-mpg price \n", | |
"0 27 13495 \n", | |
"1 27 16500 \n", | |
"2 26 16500 \n", | |
"3 30 13950 \n", | |
"4 22 17450 \n", | |
"5 25 15250 \n", | |
"6 25 17710 \n", | |
"7 25 18920 \n", | |
"8 20 23875 \n", | |
"9 22 ? \n", | |
"\n", | |
"[10 rows x 26 columns]" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# To see what the data set looks like, we'll use the head() method.\n", | |
"df.head(10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"As we can see, several question marks appeared in the dataframe; those are missing values which may hinder our further analysis. \n", | |
"\n", | |
"<div>So, how do we identify all those missing values and deal with them?</div> \n", | |
"\n", | |
"<b>How to work with missing data?</b>\n", | |
"\n", | |
"Steps for working with missing data:\n", | |
"\n", | |
"<ol>\n", | |
" <li>identify missing data</li>\n", | |
" <li>deal with missing data</li>\n", | |
" <li>correct data format</li>\n", | |
"</ol>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"identify_handle_missing_values\">Identify and handle missing values</h2>\n", | |
"\n", | |
"<h3 id=\"identify_missing_values\">Identify missing values</h3>\n", | |
"<h4>Convert \"?\" to NaN</h4>\n", | |
"In the car dataset, missing data comes with the question mark \"?\".\n", | |
"We replace \"?\" with NaN (Not a Number), which is Python's default missing value marker, for reasons of computational speed and convenience. Here we use the function: \n", | |
" <pre>.replace(A, B, inplace = True) </pre>\n", | |
"to replace A by B\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>engine-size</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>NaN</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>NaN</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>NaN</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>152</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>109</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>2</td>\n", | |
" <td>NaN</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>15250</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>17710</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>1</td>\n", | |
" <td>NaN</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>wagon</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>18920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.3</td>\n", | |
" <td>140</td>\n", | |
" <td>5500</td>\n", | |
" <td>17</td>\n", | |
" <td>20</td>\n", | |
" <td>23875</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0</td>\n", | |
" <td>NaN</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.5</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>7.0</td>\n", | |
" <td>160</td>\n", | |
" <td>5500</td>\n", | |
" <td>16</td>\n", | |
" <td>22</td>\n", | |
" <td>NaN</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>10 rows × 26 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n", | |
"0 3 NaN alfa-romero gas std two \n", | |
"1 3 NaN alfa-romero gas std two \n", | |
"2 1 NaN alfa-romero gas std two \n", | |
"3 2 164 audi gas std four \n", | |
"4 2 164 audi gas std four \n", | |
"5 2 NaN audi gas std two \n", | |
"6 1 158 audi gas std four \n", | |
"7 1 NaN audi gas std four \n", | |
"8 1 158 audi gas turbo four \n", | |
"9 0 NaN audi gas turbo two \n", | |
"\n", | |
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n", | |
"0 convertible rwd front 88.6 ... 130 \n", | |
"1 convertible rwd front 88.6 ... 130 \n", | |
"2 hatchback rwd front 94.5 ... 152 \n", | |
"3 sedan fwd front 99.8 ... 109 \n", | |
"4 sedan 4wd front 99.4 ... 136 \n", | |
"5 sedan fwd front 99.8 ... 136 \n", | |
"6 sedan fwd front 105.8 ... 136 \n", | |
"7 wagon fwd front 105.8 ... 136 \n", | |
"8 sedan fwd front 105.8 ... 131 \n", | |
"9 hatchback 4wd front 99.5 ... 131 \n", | |
"\n", | |
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n", | |
"0 mpfi 3.47 2.68 9.0 111 5000 21 \n", | |
"1 mpfi 3.47 2.68 9.0 111 5000 21 \n", | |
"2 mpfi 2.68 3.47 9.0 154 5000 19 \n", | |
"3 mpfi 3.19 3.40 10.0 102 5500 24 \n", | |
"4 mpfi 3.19 3.40 8.0 115 5500 18 \n", | |
"5 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"6 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"7 mpfi 3.19 3.40 8.5 110 5500 19 \n", | |
"8 mpfi 3.13 3.40 8.3 140 5500 17 \n", | |
"9 mpfi 3.13 3.40 7.0 160 5500 16 \n", | |
"\n", | |
" highway-mpg price \n", | |
"0 27 13495 \n", | |
"1 27 16500 \n", | |
"2 26 16500 \n", | |
"3 30 13950 \n", | |
"4 22 17450 \n", | |
"5 25 15250 \n", | |
"6 25 17710 \n", | |
"7 25 18920 \n", | |
"8 20 23875 \n", | |
"9 22 NaN \n", | |
"\n", | |
"[10 rows x 26 columns]" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"# replace \"?\" to NaN\n", | |
"df.replace(\"?\", np.nan, inplace = True)\n", | |
"df.head(10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Identify_missing_values\n", | |
"\n", | |
"<h4>Evaluating for Missing Data</h4>\n", | |
"\n", | |
"The missing values are converted to default. We use the following functions to identify these missing values. There are two methods to detect missing data:\n", | |
"\n", | |
"<ol>\n", | |
" <li><b>.isnull()</b></li>\n", | |
" <li><b>.notnull()</b></li>\n", | |
"</ol>\n", | |
"The output is a boolean value indicating whether the value that is passed into the argument is in fact missing data.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>engine-size</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>...</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>...</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>False</td>\n", | |
" <td>True</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>...</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>...</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>...</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>5 rows × 26 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n", | |
"0 False True False False False False \n", | |
"1 False True False False False False \n", | |
"2 False True False False False False \n", | |
"3 False False False False False False \n", | |
"4 False False False False False False \n", | |
"\n", | |
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n", | |
"0 False False False False ... False \n", | |
"1 False False False False ... False \n", | |
"2 False False False False ... False \n", | |
"3 False False False False ... False \n", | |
"4 False False False False ... False \n", | |
"\n", | |
" fuel-system bore stroke compression-ratio horsepower peak-rpm \\\n", | |
"0 False False False False False False \n", | |
"1 False False False False False False \n", | |
"2 False False False False False False \n", | |
"3 False False False False False False \n", | |
"4 False False False False False False \n", | |
"\n", | |
" city-mpg highway-mpg price \n", | |
"0 False False False \n", | |
"1 False False False \n", | |
"2 False False False \n", | |
"3 False False False \n", | |
"4 False False False \n", | |
"\n", | |
"[5 rows x 26 columns]" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"missing_data = df.isnull()\n", | |
"missing_data.head(5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\"True\" stands for missing value, while \"False\" stands for not missing value.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Count missing values in each column</h4>\n", | |
"<p>\n", | |
"Using a for loop in Python, we can quickly figure out the number of missing values in each column. As mentioned above, \"True\" represents a missing value, \"False\" means the value is present in the dataset. In the body of the for loop the method \".value_counts()\" counts the number of \"True\" values. \n", | |
"</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"symboling\n", | |
"False 205\n", | |
"Name: symboling, dtype: int64\n", | |
"\n", | |
"normalized-losses\n", | |
"False 164\n", | |
"True 41\n", | |
"Name: normalized-losses, dtype: int64\n", | |
"\n", | |
"make\n", | |
"False 205\n", | |
"Name: make, dtype: int64\n", | |
"\n", | |
"fuel-type\n", | |
"False 205\n", | |
"Name: fuel-type, dtype: int64\n", | |
"\n", | |
"aspiration\n", | |
"False 205\n", | |
"Name: aspiration, dtype: int64\n", | |
"\n", | |
"num-of-doors\n", | |
"False 203\n", | |
"True 2\n", | |
"Name: num-of-doors, dtype: int64\n", | |
"\n", | |
"body-style\n", | |
"False 205\n", | |
"Name: body-style, dtype: int64\n", | |
"\n", | |
"drive-wheels\n", | |
"False 205\n", | |
"Name: drive-wheels, dtype: int64\n", | |
"\n", | |
"engine-location\n", | |
"False 205\n", | |
"Name: engine-location, dtype: int64\n", | |
"\n", | |
"wheel-base\n", | |
"False 205\n", | |
"Name: wheel-base, dtype: int64\n", | |
"\n", | |
"length\n", | |
"False 205\n", | |
"Name: length, dtype: int64\n", | |
"\n", | |
"width\n", | |
"False 205\n", | |
"Name: width, dtype: int64\n", | |
"\n", | |
"height\n", | |
"False 205\n", | |
"Name: height, dtype: int64\n", | |
"\n", | |
"curb-weight\n", | |
"False 205\n", | |
"Name: curb-weight, dtype: int64\n", | |
"\n", | |
"engine-type\n", | |
"False 205\n", | |
"Name: engine-type, dtype: int64\n", | |
"\n", | |
"num-of-cylinders\n", | |
"False 205\n", | |
"Name: num-of-cylinders, dtype: int64\n", | |
"\n", | |
"engine-size\n", | |
"False 205\n", | |
"Name: engine-size, dtype: int64\n", | |
"\n", | |
"fuel-system\n", | |
"False 205\n", | |
"Name: fuel-system, dtype: int64\n", | |
"\n", | |
"bore\n", | |
"False 201\n", | |
"True 4\n", | |
"Name: bore, dtype: int64\n", | |
"\n", | |
"stroke\n", | |
"False 201\n", | |
"True 4\n", | |
"Name: stroke, dtype: int64\n", | |
"\n", | |
"compression-ratio\n", | |
"False 205\n", | |
"Name: compression-ratio, dtype: int64\n", | |
"\n", | |
"horsepower\n", | |
"False 203\n", | |
"True 2\n", | |
"Name: horsepower, dtype: int64\n", | |
"\n", | |
"peak-rpm\n", | |
"False 203\n", | |
"True 2\n", | |
"Name: peak-rpm, dtype: int64\n", | |
"\n", | |
"city-mpg\n", | |
"False 205\n", | |
"Name: city-mpg, dtype: int64\n", | |
"\n", | |
"highway-mpg\n", | |
"False 205\n", | |
"Name: highway-mpg, dtype: int64\n", | |
"\n", | |
"price\n", | |
"False 201\n", | |
"True 4\n", | |
"Name: price, dtype: int64\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"for column in missing_data.columns.values.tolist():\n", | |
" print(column)\n", | |
" print (missing_data[column].value_counts())\n", | |
" print(\"\") " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Based on the summary above, each column has 205 rows of data, seven columns containing missing data:\n", | |
"\n", | |
"<ol>\n", | |
" <li>\"normalized-losses\": 41 missing data</li>\n", | |
" <li>\"num-of-doors\": 2 missing data</li>\n", | |
" <li>\"bore\": 4 missing data</li>\n", | |
" <li>\"stroke\" : 4 missing data</li>\n", | |
" <li>\"horsepower\": 2 missing data</li>\n", | |
" <li>\"peak-rpm\": 2 missing data</li>\n", | |
" <li>\"price\": 4 missing data</li>\n", | |
"</ol>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3 id=\"deal_missing_values\">Deal with missing data</h3>\n", | |
"<b>How to deal with missing data?</b>\n", | |
"\n", | |
"<ol>\n", | |
" <li>drop data<br>\n", | |
" a. drop the whole row<br>\n", | |
" b. drop the whole column\n", | |
" </li>\n", | |
" <li>replace data<br>\n", | |
" a. replace it by mean<br>\n", | |
" b. replace it by frequency<br>\n", | |
" c. replace it based on other functions\n", | |
" </li>\n", | |
"</ol>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Whole columns should be dropped only if most entries in the column are empty. In our dataset, none of the columns are empty enough to drop entirely.\n", | |
"We have some freedom in choosing which method to replace data; however, some methods may seem more reasonable than others. We will apply each method to many different columns:\n", | |
"\n", | |
"<b>Replace by mean:</b>\n", | |
"\n", | |
"<ul>\n", | |
" <li>\"normalized-losses\": 41 missing data, replace them with mean</li>\n", | |
" <li>\"stroke\": 4 missing data, replace them with mean</li>\n", | |
" <li>\"bore\": 4 missing data, replace them with mean</li>\n", | |
" <li>\"horsepower\": 2 missing data, replace them with mean</li>\n", | |
" <li>\"peak-rpm\": 2 missing data, replace them with mean</li>\n", | |
"</ul>\n", | |
"\n", | |
"<b>Replace by frequency:</b>\n", | |
"\n", | |
"<ul>\n", | |
" <li>\"num-of-doors\": 2 missing data, replace them with \"four\". \n", | |
" <ul>\n", | |
" <li>Reason: 84% sedans is four doors. Since four doors is most frequent, it is most likely to occur</li>\n", | |
" </ul>\n", | |
" </li>\n", | |
"</ul>\n", | |
"\n", | |
"<b>Drop the whole row:</b>\n", | |
"\n", | |
"<ul>\n", | |
" <li>\"price\": 4 missing data, simply delete the whole row\n", | |
" <ul>\n", | |
" <li>Reason: price is what we want to predict. Any data entry without price data cannot be used for prediction; therefore any row without price data is completely useless </li>\n", | |
" </ul>\n", | |
" </li>\n", | |
"</ul>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Calculate the average of the column </h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Average of normalized-losses: 122.0\n" | |
] | |
} | |
], | |
"source": [ | |
"avg_norm_loss = df[\"normalized-losses\"].astype(\"float\").mean(axis=0)\n", | |
"print(\"Average of normalized-losses:\", avg_norm_loss)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Replace \"NaN\" by mean value in \"normalized-losses\" column</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df[\"normalized-losses\"].replace(np.nan, avg_norm_loss, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Calculate the mean value for 'bore' column</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Average of bore: 3.3297512437810943\n" | |
] | |
} | |
], | |
"source": [ | |
"avg_bore=df['bore'].astype('float').mean(axis=0)\n", | |
"print(\"Average of bore:\", avg_bore)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Replace NaN by mean value</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df[\"bore\"].replace(np.nan, avg_bore, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n", | |
"<h1> Question #1: </h1>\n", | |
"\n", | |
"<b>According to the example above, replace NaN in \"stroke\" column by mean.</b>\n", | |
"\n", | |
"</div>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Write your code below and press Shift+Enter to execute \n", | |
"mean_stroke = df['stroke'].astype('float').mean(axis=0)\n", | |
"df['stroke'].replace(np.nan, mean_stroke, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- The answer is below:\n", | |
"\n", | |
"# calculate the mean vaule for \"stroke\" column\n", | |
"avg_stroke = df[\"stroke\"].astype(\"float\").mean(axis = 0)\n", | |
"print(\"Average of stroke:\", avg_stroke)\n", | |
"\n", | |
"# replace NaN by mean value in \"stroke\" column\n", | |
"df[\"stroke\"].replace(np.nan, avg_stroke, inplace = True)\n", | |
"\n", | |
"-->\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Calculate the mean value for the 'horsepower' column:</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Average horsepower: 104.25615763546799\n" | |
] | |
} | |
], | |
"source": [ | |
"avg_horsepower = df['horsepower'].astype('float').mean(axis=0)\n", | |
"print(\"Average horsepower:\", avg_horsepower)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Replace \"NaN\" by mean value:</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df['horsepower'].replace(np.nan, avg_horsepower, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Calculate the mean value for 'peak-rpm' column:</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Average peak rpm: 5125.369458128079\n" | |
] | |
} | |
], | |
"source": [ | |
"avg_peakrpm=df['peak-rpm'].astype('float').mean(axis=0)\n", | |
"print(\"Average peak rpm:\", avg_peakrpm)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Replace NaN by mean value:</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df['peak-rpm'].replace(np.nan, avg_peakrpm, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"To see which values are present in a particular column, we can use the \".value_counts()\" method:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"four 114\n", | |
"two 89\n", | |
"Name: num-of-doors, dtype: int64" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df['num-of-doors'].value_counts()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that four doors are the most common type. We can also use the \".idxmax()\" method to calculate for us the most common type automatically:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'four'" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df['num-of-doors'].value_counts().idxmax()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The replacement procedure is very similar to what we have seen previously\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#replace the missing 'num-of-doors' values by the most frequent \n", | |
"df[\"num-of-doors\"].replace(np.nan, 'four', inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Finally, let's drop all rows that do not have price data:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# simply drop whole row with NaN in \"price\" column\n", | |
"df.dropna(subset=[\"price\"], axis=0, inplace=True)\n", | |
"\n", | |
"# reset index, because we droped two rows\n", | |
"df.reset_index(drop=True, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>engine-size</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>?</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>152</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>109</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>2</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>15250</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>17710</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>1</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>wagon</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.5</td>\n", | |
" <td>110</td>\n", | |
" <td>5500</td>\n", | |
" <td>19</td>\n", | |
" <td>25</td>\n", | |
" <td>18920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>1</td>\n", | |
" <td>158</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>105.8</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.3</td>\n", | |
" <td>140</td>\n", | |
" <td>5500</td>\n", | |
" <td>17</td>\n", | |
" <td>20</td>\n", | |
" <td>23875</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0</td>\n", | |
" <td>?</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>turbo</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.5</td>\n", | |
" <td>...</td>\n", | |
" <td>131</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.13</td>\n", | |
" <td>3.40</td>\n", | |
" <td>7.0</td>\n", | |
" <td>160</td>\n", | |
" <td>5500</td>\n", | |
" <td>16</td>\n", | |
" <td>22</td>\n", | |
" <td>?</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>2</td>\n", | |
" <td>192</td>\n", | |
" <td>bmw</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>sedan</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>101.2</td>\n", | |
" <td>...</td>\n", | |
" <td>108</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.50</td>\n", | |
" <td>2.80</td>\n", | |
" <td>8.8</td>\n", | |
" <td>101</td>\n", | |
" <td>5800</td>\n", | |
" <td>23</td>\n", | |
" <td>29</td>\n", | |
" <td>16430</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>11</th>\n", | |
" <td>0</td>\n", | |
" <td>192</td>\n", | |
" <td>bmw</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>101.2</td>\n", | |
" <td>...</td>\n", | |
" <td>108</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.50</td>\n", | |
" <td>2.80</td>\n", | |
" <td>8.8</td>\n", | |
" <td>101</td>\n", | |
" <td>5800</td>\n", | |
" <td>23</td>\n", | |
" <td>29</td>\n", | |
" <td>16925</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>12</th>\n", | |
" <td>0</td>\n", | |
" <td>188</td>\n", | |
" <td>bmw</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>sedan</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>101.2</td>\n", | |
" <td>...</td>\n", | |
" <td>164</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.31</td>\n", | |
" <td>3.19</td>\n", | |
" <td>9.0</td>\n", | |
" <td>121</td>\n", | |
" <td>4250</td>\n", | |
" <td>21</td>\n", | |
" <td>28</td>\n", | |
" <td>20970</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>13</th>\n", | |
" <td>0</td>\n", | |
" <td>188</td>\n", | |
" <td>bmw</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>101.2</td>\n", | |
" <td>...</td>\n", | |
" <td>164</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.31</td>\n", | |
" <td>3.19</td>\n", | |
" <td>9.0</td>\n", | |
" <td>121</td>\n", | |
" <td>4250</td>\n", | |
" <td>21</td>\n", | |
" <td>28</td>\n", | |
" <td>21105</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>14</th>\n", | |
" <td>1</td>\n", | |
" <td>?</td>\n", | |
" <td>bmw</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>103.5</td>\n", | |
" <td>...</td>\n", | |
" <td>164</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.31</td>\n", | |
" <td>3.19</td>\n", | |
" <td>9.0</td>\n", | |
" <td>121</td>\n", | |
" <td>4250</td>\n", | |
" <td>20</td>\n", | |
" <td>25</td>\n", | |
" <td>24565</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>15 rows × 26 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration \\\n", | |
"0 3 ? alfa-romero gas std \n", | |
"1 3 ? alfa-romero gas std \n", | |
"2 1 ? alfa-romero gas std \n", | |
"3 2 164 audi gas std \n", | |
"4 2 164 audi gas std \n", | |
"5 2 ? audi gas std \n", | |
"6 1 158 audi gas std \n", | |
"7 1 ? audi gas std \n", | |
"8 1 158 audi gas turbo \n", | |
"9 0 ? audi gas turbo \n", | |
"10 2 192 bmw gas std \n", | |
"11 0 192 bmw gas std \n", | |
"12 0 188 bmw gas std \n", | |
"13 0 188 bmw gas std \n", | |
"14 1 ? bmw gas std \n", | |
"\n", | |
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n", | |
"0 two convertible rwd front 88.6 ... \n", | |
"1 two convertible rwd front 88.6 ... \n", | |
"2 two hatchback rwd front 94.5 ... \n", | |
"3 four sedan fwd front 99.8 ... \n", | |
"4 four sedan 4wd front 99.4 ... \n", | |
"5 two sedan fwd front 99.8 ... \n", | |
"6 four sedan fwd front 105.8 ... \n", | |
"7 four wagon fwd front 105.8 ... \n", | |
"8 four sedan fwd front 105.8 ... \n", | |
"9 two hatchback 4wd front 99.5 ... \n", | |
"10 two sedan rwd front 101.2 ... \n", | |
"11 four sedan rwd front 101.2 ... \n", | |
"12 two sedan rwd front 101.2 ... \n", | |
"13 four sedan rwd front 101.2 ... \n", | |
"14 four sedan rwd front 103.5 ... \n", | |
"\n", | |
" engine-size fuel-system bore stroke compression-ratio horsepower \\\n", | |
"0 130 mpfi 3.47 2.68 9.0 111 \n", | |
"1 130 mpfi 3.47 2.68 9.0 111 \n", | |
"2 152 mpfi 2.68 3.47 9.0 154 \n", | |
"3 109 mpfi 3.19 3.40 10.0 102 \n", | |
"4 136 mpfi 3.19 3.40 8.0 115 \n", | |
"5 136 mpfi 3.19 3.40 8.5 110 \n", | |
"6 136 mpfi 3.19 3.40 8.5 110 \n", | |
"7 136 mpfi 3.19 3.40 8.5 110 \n", | |
"8 131 mpfi 3.13 3.40 8.3 140 \n", | |
"9 131 mpfi 3.13 3.40 7.0 160 \n", | |
"10 108 mpfi 3.50 2.80 8.8 101 \n", | |
"11 108 mpfi 3.50 2.80 8.8 101 \n", | |
"12 164 mpfi 3.31 3.19 9.0 121 \n", | |
"13 164 mpfi 3.31 3.19 9.0 121 \n", | |
"14 164 mpfi 3.31 3.19 9.0 121 \n", | |
"\n", | |
" peak-rpm city-mpg highway-mpg price \n", | |
"0 5000 21 27 13495 \n", | |
"1 5000 21 27 16500 \n", | |
"2 5000 19 26 16500 \n", | |
"3 5500 24 30 13950 \n", | |
"4 5500 18 22 17450 \n", | |
"5 5500 19 25 15250 \n", | |
"6 5500 19 25 17710 \n", | |
"7 5500 19 25 18920 \n", | |
"8 5500 17 20 23875 \n", | |
"9 5500 16 22 ? \n", | |
"10 5800 23 29 16430 \n", | |
"11 5800 23 29 16925 \n", | |
"12 4250 21 28 20970 \n", | |
"13 4250 21 28 21105 \n", | |
"14 4250 20 25 24565 \n", | |
"\n", | |
"[15 rows x 26 columns]" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head(15)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<b>Good!</b> Now, we obtain the dataset with no missing values.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3 id=\"correct_data_format\">Correct data format</h3>\n", | |
"<b>We are almost there!</b>\n", | |
"<p>The last step in data cleaning is checking and making sure that all data is in the correct format (int, float, text or other).</p>\n", | |
"\n", | |
"In Pandas, we use \n", | |
"\n", | |
"<p><b>.dtype()</b> to check the data type</p>\n", | |
"<p><b>.astype()</b> to change the data type</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Lets list the data types for each column</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"symboling int64\n", | |
"normalized-losses object\n", | |
"make object\n", | |
"fuel-type object\n", | |
"aspiration object\n", | |
"num-of-doors object\n", | |
"body-style object\n", | |
"drive-wheels object\n", | |
"engine-location object\n", | |
"wheel-base float64\n", | |
"length float64\n", | |
"width float64\n", | |
"height float64\n", | |
"curb-weight int64\n", | |
"engine-type object\n", | |
"num-of-cylinders object\n", | |
"engine-size int64\n", | |
"fuel-system object\n", | |
"bore object\n", | |
"stroke object\n", | |
"compression-ratio float64\n", | |
"horsepower object\n", | |
"peak-rpm object\n", | |
"city-mpg int64\n", | |
"highway-mpg int64\n", | |
"price object\n", | |
"dtype: object" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.dtypes" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p>As we can see above, some columns are not of the correct data type. Numerical variables should have type 'float' or 'int', and variables with strings such as categories should have type 'object'. For example, 'bore' and 'stroke' variables are numerical values that describe the engines, so we should expect them to be of the type 'float' or 'int'; however, they are shown as type 'object'. We have to convert data types into a proper format for each column using the \"astype()\" method.</p> \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Convert data types to proper format</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df[[\"bore\", \"stroke\", \"price\", \"peak-rpm\"]] = df[[\"bore\", \"stroke\", \"price\", \"peak-rpm\"]].astype(\"float\")\n", | |
"df[[\"normalized-losses\"]] = df[[\"normalized-losses\"]].astype(\"int\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Let us list the columns after the conversion</h4>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"symboling int64\n", | |
"normalized-losses int64\n", | |
"make object\n", | |
"fuel-type object\n", | |
"aspiration object\n", | |
"num-of-doors object\n", | |
"body-style object\n", | |
"drive-wheels object\n", | |
"engine-location object\n", | |
"wheel-base float64\n", | |
"length float64\n", | |
"width float64\n", | |
"height float64\n", | |
"curb-weight int64\n", | |
"engine-type object\n", | |
"num-of-cylinders object\n", | |
"engine-size int64\n", | |
"fuel-system object\n", | |
"bore float64\n", | |
"stroke float64\n", | |
"compression-ratio float64\n", | |
"horsepower object\n", | |
"peak-rpm float64\n", | |
"city-mpg int64\n", | |
"highway-mpg int64\n", | |
"price float64\n", | |
"dtype: object" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.dtypes" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<b>Wonderful!</b>\n", | |
"\n", | |
"Now, we finally obtain the cleaned dataset with no missing values and all data in its proper format.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"data_standardization\">Data Standardization</h2>\n", | |
"<p>\n", | |
"Data is usually collected from different agencies with different formats.\n", | |
"(Data Standardization is also a term for a particular type of data normalization, where we subtract the mean and divide by the standard deviation)\n", | |
"</p>\n", | |
" \n", | |
"<b>What is Standardization?</b>\n", | |
"<p>Standardization is the process of transforming data into a common format which allows the researcher to make the meaningful comparison.\n", | |
"</p>\n", | |
"\n", | |
"<b>Example</b>\n", | |
"\n", | |
"<p>Transform mpg to L/100km:</p>\n", | |
"<p>In our dataset, the fuel consumption columns \"city-mpg\" and \"highway-mpg\" are represented by mpg (miles per gallon) unit. Assume we are developing an application in a country that accept the fuel consumption with L/100km standard</p>\n", | |
"<p>We will need to apply <b>data transformation</b> to transform mpg into L/100km</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p>The formula for unit conversion is<p>\n", | |
"L/100km = 235 / mpg\n", | |
"<p>We can do many mathematical operations directly in Pandas.</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>engine-size</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>130</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>152</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>109</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>136</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450.0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>5 rows × 26 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration \\\n", | |
"0 3 122 alfa-romero gas std \n", | |
"1 3 122 alfa-romero gas std \n", | |
"2 1 122 alfa-romero gas std \n", | |
"3 2 164 audi gas std \n", | |
"4 2 164 audi gas std \n", | |
"\n", | |
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n", | |
"0 two convertible rwd front 88.6 ... \n", | |
"1 two convertible rwd front 88.6 ... \n", | |
"2 two hatchback rwd front 94.5 ... \n", | |
"3 four sedan fwd front 99.8 ... \n", | |
"4 four sedan 4wd front 99.4 ... \n", | |
"\n", | |
" engine-size fuel-system bore stroke compression-ratio horsepower \\\n", | |
"0 130 mpfi 3.47 2.68 9.0 111 \n", | |
"1 130 mpfi 3.47 2.68 9.0 111 \n", | |
"2 152 mpfi 2.68 3.47 9.0 154 \n", | |
"3 109 mpfi 3.19 3.40 10.0 102 \n", | |
"4 136 mpfi 3.19 3.40 8.0 115 \n", | |
"\n", | |
" peak-rpm city-mpg highway-mpg price \n", | |
"0 5000.0 21 27 13495.0 \n", | |
"1 5000.0 21 27 16500.0 \n", | |
"2 5000.0 19 26 16500.0 \n", | |
"3 5500.0 24 30 13950.0 \n", | |
"4 5500.0 18 22 17450.0 \n", | |
"\n", | |
"[5 rows x 26 columns]" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>fuel-system</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" <th>city-L/100km</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495.0</td>\n", | |
" <td>11.190476</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>11.190476</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>12.368421</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950.0</td>\n", | |
" <td>9.791667</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>mpfi</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450.0</td>\n", | |
" <td>13.055556</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>5 rows × 27 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration \\\n", | |
"0 3 122 alfa-romero gas std \n", | |
"1 3 122 alfa-romero gas std \n", | |
"2 1 122 alfa-romero gas std \n", | |
"3 2 164 audi gas std \n", | |
"4 2 164 audi gas std \n", | |
"\n", | |
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n", | |
"0 two convertible rwd front 88.6 ... \n", | |
"1 two convertible rwd front 88.6 ... \n", | |
"2 two hatchback rwd front 94.5 ... \n", | |
"3 four sedan fwd front 99.8 ... \n", | |
"4 four sedan 4wd front 99.4 ... \n", | |
"\n", | |
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n", | |
"0 mpfi 3.47 2.68 9.0 111 5000.0 21 \n", | |
"1 mpfi 3.47 2.68 9.0 111 5000.0 21 \n", | |
"2 mpfi 2.68 3.47 9.0 154 5000.0 19 \n", | |
"3 mpfi 3.19 3.40 10.0 102 5500.0 24 \n", | |
"4 mpfi 3.19 3.40 8.0 115 5500.0 18 \n", | |
"\n", | |
" highway-mpg price city-L/100km \n", | |
"0 27 13495.0 11.190476 \n", | |
"1 27 16500.0 11.190476 \n", | |
"2 26 16500.0 12.368421 \n", | |
"3 30 13950.0 9.791667 \n", | |
"4 22 17450.0 13.055556 \n", | |
"\n", | |
"[5 rows x 27 columns]" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Convert mpg to L/100km by mathematical operation (235 divided by mpg)\n", | |
"df['city-L/100km'] = 235/df[\"city-mpg\"]\n", | |
"\n", | |
"# check your transformed data \n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n", | |
"<h1> Question #2: </h1>\n", | |
"\n", | |
"<b>According to the example above, transform mpg to L/100km in the column of \"highway-mpg\", and change the name of column to \"highway-L/100km\".</b>\n", | |
"\n", | |
"</div>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>fuel-type</th>\n", | |
" <th>aspiration</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>...</th>\n", | |
" <th>bore</th>\n", | |
" <th>stroke</th>\n", | |
" <th>compression-ratio</th>\n", | |
" <th>horsepower</th>\n", | |
" <th>peak-rpm</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" <th>city-L/100km</th>\n", | |
" <th>highway-L-100km</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495.0</td>\n", | |
" <td>11.190476</td>\n", | |
" <td>8.703704</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>...</td>\n", | |
" <td>3.47</td>\n", | |
" <td>2.68</td>\n", | |
" <td>9.0</td>\n", | |
" <td>111</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>11.190476</td>\n", | |
" <td>8.703704</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>...</td>\n", | |
" <td>2.68</td>\n", | |
" <td>3.47</td>\n", | |
" <td>9.0</td>\n", | |
" <td>154</td>\n", | |
" <td>5000.0</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>12.368421</td>\n", | |
" <td>9.038462</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>...</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>10.0</td>\n", | |
" <td>102</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950.0</td>\n", | |
" <td>9.791667</td>\n", | |
" <td>7.833333</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>gas</td>\n", | |
" <td>std</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>...</td>\n", | |
" <td>3.19</td>\n", | |
" <td>3.40</td>\n", | |
" <td>8.0</td>\n", | |
" <td>115</td>\n", | |
" <td>5500.0</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450.0</td>\n", | |
" <td>13.055556</td>\n", | |
" <td>10.681818</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>5 rows × 28 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make fuel-type aspiration \\\n", | |
"0 3 122 alfa-romero gas std \n", | |
"1 3 122 alfa-romero gas std \n", | |
"2 1 122 alfa-romero gas std \n", | |
"3 2 164 audi gas std \n", | |
"4 2 164 audi gas std \n", | |
"\n", | |
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n", | |
"0 two convertible rwd front 88.6 ... \n", | |
"1 two convertible rwd front 88.6 ... \n", | |
"2 two hatchback rwd front 94.5 ... \n", | |
"3 four sedan fwd front 99.8 ... \n", | |
"4 four sedan 4wd front 99.4 ... \n", | |
"\n", | |
" bore stroke compression-ratio horsepower peak-rpm city-mpg highway-mpg \\\n", | |
"0 3.47 2.68 9.0 111 5000.0 21 27 \n", | |
"1 3.47 2.68 9.0 111 5000.0 21 27 \n", | |
"2 2.68 3.47 9.0 154 5000.0 19 26 \n", | |
"3 3.19 3.40 10.0 102 5500.0 24 30 \n", | |
"4 3.19 3.40 8.0 115 5500.0 18 22 \n", | |
"\n", | |
" price city-L/100km highway-L-100km \n", | |
"0 13495.0 11.190476 8.703704 \n", | |
"1 16500.0 11.190476 8.703704 \n", | |
"2 16500.0 12.368421 9.038462 \n", | |
"3 13950.0 9.791667 7.833333 \n", | |
"4 17450.0 13.055556 10.681818 \n", | |
"\n", | |
"[5 rows x 28 columns]" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Write your code below and press Shift+Enter to execute \n", | |
"\n", | |
"df['highway-L-100km'] = 235/df['highway-mpg']\n", | |
"#df.rename(columns={'highway-mpg':'highway-L/100km'}, inplace=True)\n", | |
"#Did not want to replace the highway-mpg column but instead /n\n", | |
"# add another column so I don't lose the values\n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- The answer is below:\n", | |
"\n", | |
"# transform mpg to L/100km by mathematical operation (235 divided by mpg)\n", | |
"df[\"highway-mpg\"] = 235/df[\"highway-mpg\"]\n", | |
"\n", | |
"# rename column name from \"highway-mpg\" to \"highway-L/100km\"\n", | |
"df.rename(columns={'\"highway-mpg\"':'highway-L/100km'}, inplace=True)\n", | |
"\n", | |
"# check your transformed data \n", | |
"df.head()\n", | |
"\n", | |
"-->\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"data_normalization\">Data Normalization</h2>\n", | |
"\n", | |
"<b>Why normalization?</b>\n", | |
"\n", | |
"<p>Normalization is the process of transforming values of several variables into a similar range. Typical normalizations include scaling the variable so the variable average is 0, scaling the variable so the variance is 1, or scaling variable so the variable values range from 0 to 1\n", | |
"</p>\n", | |
"\n", | |
"<b>Example</b>\n", | |
"\n", | |
"<p>To demonstrate normalization, let's say we want to scale the columns \"length\", \"width\" and \"height\" </p>\n", | |
"<p><b>Target:</b>would like to Normalize those variables so their value ranges from 0 to 1.</p>\n", | |
"<p><b>Approach:</b> replace original value by (original value)/(maximum value)</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# replace (original value) by (original value)/(maximum value)\n", | |
"df['length'] = df['length']/df['length'].max()\n", | |
"df['width'] = df['width']/df['width'].max()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n", | |
"<h1> Questiont #3: </h1>\n", | |
"\n", | |
"<b>According to the example above, normalize the column \"height\".</b>\n", | |
"\n", | |
"</div>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Write your code below and press Shift+Enter to execute \n", | |
"df['height'] = df['height']/df['height'].max()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- The answer is below:\n", | |
"\n", | |
"df['height'] = df['height']/df['height'].max() \n", | |
"# show the scaled columns\n", | |
"df[[\"length\",\"width\",\"height\"]].head()\n", | |
"\n", | |
"-->\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here we can see, we've normalized \"length\", \"width\" and \"height\" in the range of [0,1].\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"binning\">Binning</h2>\n", | |
"<b>Why binning?</b>\n", | |
"<p>\n", | |
" Binning is a process of transforming continuous numerical variables into discrete categorical 'bins', for grouped analysis.\n", | |
"</p>\n", | |
"\n", | |
"<b>Example: </b>\n", | |
"\n", | |
"<p>In our dataset, \"horsepower\" is a real valued variable ranging from 48 to 288, it has 57 unique values. What if we only care about the price difference between cars with high horsepower, medium horsepower, and little horsepower (3 types)? Can we rearrange them into three ‘bins' to simplify analysis? </p>\n", | |
"\n", | |
"<p>We will use the Pandas method 'cut' to segment the 'horsepower' column into 3 bins </p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>Example of Binning Data In Pandas</h3>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" Convert data to correct format \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Fixed the error by replacing 'int' by 'float' on astype method\n", | |
"df[\"horsepower\"]=df[\"horsepower\"].astype(float, copy=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets plot the histogram of horspower, to see what the distribution of horsepower looks like.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Text(0.5, 1.0, 'horsepower bins')" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEWCAYAAABhffzLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIW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0dWdk+D2Ia5Kc25a9JMnb2/TrktzYpo9McnmbfkaSjW1wwU9ODItwvzoXtjm11Nhz0FJ0FHBGVf16kguBXwJ+C3htVW1M8h+B/wC8vm1/QFU9ByDJV4DnV9V3khzQ1p8FfL+qfj7JI4G/TfKptu5YhnHWbwI+Abwoyd8B5wLPAO5gGCX2dOAy4DfbfscDtyU5jOEboJ9t4768E1hdVduSvBR4C/CrO9YpPRwGv5aib1TVVW36SoaREg+oqo1t2Trgrya2/8jE9N8CF7Q3jO0Dkj0PeGqSX27z+zO8ufwI+EJVbe+5f4ghxH8MXFpV29ryDwAnVNXHkuzTxpF6PPBBhh+BOb4d60nAU4BLhmFgWMYwlMRMdUoPmcGvpejuiel7gQMeYPsfbp+oqlcleSZwGnBVkmMYhtN9bVV9cnKnJCey87C6xczD7273OeAVDGO1fJahN/9s4A3ATwPXVtWzH6hO6eHwHL968H3gjiTHt/mXAxtn2jDJkVV1RVX9PnArQ8/8k8C/badiSPIzbdRXgGOTHNHO7b8UuJzhxziek+TgJMuAMyaOdxnwxnb/98C/AO6uqu8zvBksT/Lsdpy9kjx51zWDNLDHr16sAc5L8mjgRoZe90z+OMlRDL32DQwjvF4NrAS+1Ibi3cZ9PzX4OeAc4J8yhPlFVfWTJG8CPtMe539W1fZhiz/L8GZyWVXdm+RbwNcAqupH7XTSnyfZn+H/558xjDAr7TKOzik9RO1Uzxur6oVTLkV6UDzVI0mdsccvSZ2xxy9JnTH4JakzBr8kdcbgl6TOGPyS1Jn/B2EXAS5SRAWHAAAAAElFTkSuQmCC\n", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%matplotlib inline\n", | |
"import matplotlib as plt\n", | |
"from matplotlib import pyplot\n", | |
"plt.pyplot.hist(df[\"horsepower\"])\n", | |
"\n", | |
"# set x/y labels and plot title\n", | |
"plt.pyplot.xlabel(\"horsepower\")\n", | |
"plt.pyplot.ylabel(\"count\")\n", | |
"plt.pyplot.title(\"horsepower bins\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p>We would like 3 bins of equal size bandwidth so we use numpy's <code>linspace(start_value, end_value, numbers_generated)</code> function.</p>\n", | |
"<p>Since we want to include the minimum value of horsepower we want to set start_value=min(df[\"horsepower\"]).</p>\n", | |
"<p>Since we want to include the maximum value of horsepower we want to set end_value=max(df[\"horsepower\"]).</p>\n", | |
"<p>Since we are building 3 bins of equal length, there should be 4 dividers, so numbers_generated=4.</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We build a bin array, with a minimum value to a maximum value, with bandwidth calculated above. The bins will be values used to determine when one bin ends and another begins.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 48. , 119.33333333, 190.66666667, 262. ])" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bins = np.linspace(min(df[\"horsepower\"]), max(df[\"horsepower\"]), 4)\n", | |
"bins" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" We set group names:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"group_names = ['Low', 'Medium', 'High']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" We apply the function \"cut\" then determine what each value of \"df['horsepower']\" belongs to. \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>horsepower</th>\n", | |
" <th>horsepower-binned</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>111.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>111.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>154.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>102.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>115.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>110.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>110.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>110.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>140.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>101.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>101.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>11</th>\n", | |
" <td>121.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>12</th>\n", | |
" <td>121.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>13</th>\n", | |
" <td>121.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>14</th>\n", | |
" <td>182.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>15</th>\n", | |
" <td>182.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>16</th>\n", | |
" <td>182.0</td>\n", | |
" <td>Medium</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>17</th>\n", | |
" <td>48.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>18</th>\n", | |
" <td>70.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>19</th>\n", | |
" <td>70.0</td>\n", | |
" <td>Low</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" horsepower horsepower-binned\n", | |
"0 111.0 Low\n", | |
"1 111.0 Low\n", | |
"2 154.0 Medium\n", | |
"3 102.0 Low\n", | |
"4 115.0 Low\n", | |
"5 110.0 Low\n", | |
"6 110.0 Low\n", | |
"7 110.0 Low\n", | |
"8 140.0 Medium\n", | |
"9 101.0 Low\n", | |
"10 101.0 Low\n", | |
"11 121.0 Medium\n", | |
"12 121.0 Medium\n", | |
"13 121.0 Medium\n", | |
"14 182.0 Medium\n", | |
"15 182.0 Medium\n", | |
"16 182.0 Medium\n", | |
"17 48.0 Low\n", | |
"18 70.0 Low\n", | |
"19 70.0 Low" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df['horsepower-binned'] = pd.cut(df['horsepower'], bins, labels=group_names, include_lowest=True )\n", | |
"df[['horsepower','horsepower-binned']].head(20)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets see the number of vehicles in each bin.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Low 153\n", | |
"Medium 43\n", | |
"High 5\n", | |
"Name: horsepower-binned, dtype: int64" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[\"horsepower-binned\"].value_counts()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets plot the distribution of each bin.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Text(0.5, 1.0, 'horsepower bins')" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAZA0lEQVR4nO3dfbRddX3n8feHoOATD5qLIgGCDDoNDEWNqHVQKlWpOoalVcMSGxUbcVHUGVFDnUrHioMPo1WrZcWKYFUgPiCZ0frQWIiPYFAEASmpBAgguSAKouXxO3/sne3hepNcLjnn3Nzzfq1119n799v77O+9Jzmfs/c++7dTVUiSBLDdsAuQJM0choIkqWMoSJI6hoIkqWMoSJI6hoIkqWMoaEZKsi7Jnwy7jpkuyaFJ1m+m/5Qkfz3ImrRt237YBUjqn6o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705yHvDGJC9N8pMkP06yul1mTjua5g/a9V/Xth/ajnZ6dpLLkpySZLu278gkl7TP9Z627WVJPtBOv7Ed64gk+yb5djv95CTnJbkwydeS7D5ZnYP9c2o28pOFRsl+wJFV9RdJVgAvAd4KHFdV57VXG58IvKldfpeqehZAkkuA51XVdfndDXOOBn5VVU9ph634TpKvt30HAwtovu/+VeDFSb4LvIdmBNVbgK8nOYLm6vS3tOsdAtycZA/gvwLfSvIg4CPAoqoaT/JympFYXzOxTumBMhQ0Sq6qqova6QuBfdn0aLQAZ/VMfwc4rQ2TL7ZtzwUOTPJn7fzONMFzJ3BBVW38xH8GzRv8XcC5VTXetn8GeGZVfSnJw5M8gmY01M/SjLp6SLutJ9AMof2NdoiTOcANm6hTekAMBY2SiSNd7rKF5W/fOFFVxyR5Ks3Ndy5KchDNyJrHVdXXeldKcii/P2JqsfmROL9HM77VFTQj4b4GeDrwZpqhuS9tx/nfbJ3SA+U5BY2yzY1Gex9J9q2q86vqHcBNNJ/ovwa8vj28Q5LHtzfcATg4yT7tuYSX0wxceD7wrDR3YZtDc7+G3tFvj28ff0RzA5872hFzrwDGkjy93c6Dkuy/9f4M0u+4p6BRN+lotJN4X5L9aD7trwJ+DFwMzAd+2I6YOg4c0S7/PeBkmjt1rQbOrqp727F1/rV9nq9U1Tnt8t+iCZrVVXVPkmtpR1GtqjvbQ1Qfbm/AtD3NSK2XbpW/gNTDsY+kraw9fHR8VXm/BG1zPHwkSeq4pyBJ6rinIEnqGAqSpI6hIEnqGAqSpI6hIEnq/H92ln9qkR9/MQAAAABJRU5ErkJggg==\n", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%matplotlib inline\n", | |
"import matplotlib as plt\n", | |
"from matplotlib import pyplot\n", | |
"pyplot.bar(group_names, df[\"horsepower-binned\"].value_counts())\n", | |
"\n", | |
"# set x/y labels and plot title\n", | |
"plt.pyplot.xlabel(\"horsepower\")\n", | |
"plt.pyplot.ylabel(\"count\")\n", | |
"plt.pyplot.title(\"horsepower bins\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p>\n", | |
" Check the dataframe above carefully, you will find the last column provides the bins for \"horsepower\" with 3 categories (\"Low\",\"Medium\" and \"High\"). \n", | |
"</p>\n", | |
"<p>\n", | |
" We successfully narrowed* the intervals from 57 to 3! (Who wrote this?)\n", | |
"</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>Bins visualization</h3>\n", | |
"Normally, a histogram is used to visualize the distribution of bins we created above. \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Text(0.5, 1.0, 'horsepower bins')" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAYdElEQVR4nO3dfbRddX3n8feHoPgA8tBcEAkYZKLT4DiIKUodlUoVfBjD2KphFSeD2GgXos6IGupU7Ezp4EO11tG6YkFipWB8QDLLGYVGSbQVMCjyKJJCgEAk4UGh1kbA7/yxdzaH673JJeScc5Pzfq111tn7t/c++3t/OTmfs/c5+3dSVUiSBLDLsAuQJE0fhoIkqWMoSJI6hoIkqWMoSJI6hoIkqWMoaFpKsjbJ7w67jukuyVFJ1m1h+aeT/Mkga9KObddhFyCpf6rqrcOuQTsWjxS0U0uy07zx2Zn+Fk1fhoKms8OSXJXkZ0m+kOQJmxck+cMka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12HJ1PAPOramOSNwBnAG8aX6f0WBkKGiU3V9WV7fQVNCNW7lVVK9u2pcAXe9b/Qs/0PwDntGGyefC3lwPPSfL77fyeNMHzS+Dyqtr8jv88mhf4B4BLqmpj234u8OKq+mqS3dsxuQ4E/o7mB35e1O7rWcCzgYubYXWYQTP8xkR1So+JoaBRsqln+iFgr62s//PNE1X11iTPB14FXJnkMJohj0+pqm/0bpTkKH596ONi4iGSN/sucCLN2DffpjkKOBJ4F3AQcG1VHbm1OqXHys8UNMp+Btyb5EXt/BuBlROtmOSQqrqsqt4P3EXzjv4bwB+1p3dI8sx25F2AI5Ic3H6W8AbgOzQ/tPKSJDOTzACO79nfKuDU9v4HwO8Am6rqZzRBMZbkyHY/j0ty6PbrBulhHilo1C0EPp3kScBNNO/WJ/LhJHNo3u2voBll9ypgNvD9drjkjTz885PfBc4E/h3NC/0FVfWrJKcB32of5/9W1eahpb9NEzSrquqhJLcBPwKoql+2p6j+KsmeNP9v/5JmlF9pu3KUVGk7a08fnVpVrx5yKdKj5ukjSVLHIwVJUscjBUlSx1CQJHUMBUlSx1CQJHUMBUlS5/8D72GK8enPX1MAAAAASUVORK5CYII=\n", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%matplotlib inline\n", | |
"import matplotlib as plt\n", | |
"from matplotlib import pyplot\n", | |
"\n", | |
"\n", | |
"# draw historgram of attribute \"horsepower\" with bins = 3\n", | |
"plt.pyplot.hist(df[\"horsepower\"], bins = 3)\n", | |
"\n", | |
"# set x/y labels and plot title\n", | |
"plt.pyplot.xlabel(\"horsepower\")\n", | |
"plt.pyplot.ylabel(\"count\")\n", | |
"plt.pyplot.title(\"horsepower bins\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The plot above shows the binning result for attribute \"horsepower\". \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"indicator\">Indicator variable (or dummy variable)</h2>\n", | |
"<b>What is an indicator variable?</b>\n", | |
"<p>\n", | |
" An indicator variable (or dummy variable) is a numerical variable used to label categories. They are called 'dummies' because the numbers themselves don't have inherent meaning. \n", | |
"</p>\n", | |
"\n", | |
"<b>Why we use indicator variables?</b>\n", | |
"\n", | |
"<p>\n", | |
" So we can use categorical variables for regression analysis in the later modules.\n", | |
"</p>\n", | |
"<b>Example</b>\n", | |
"<p>\n", | |
" We see the column \"fuel-type\" has two unique values, \"gas\" or \"diesel\". Regression doesn't understand words, only numbers. To use this attribute in regression analysis, we convert \"fuel-type\" into indicator variables.\n", | |
"</p>\n", | |
"\n", | |
"<p>\n", | |
" We will use the panda's method 'get_dummies' to assign numerical values to different categories of fuel type. \n", | |
"</p>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Index(['symboling', 'normalized-losses', 'make', 'num-of-doors', 'body-style',\n", | |
" 'drive-wheels', 'engine-location', 'wheel-base', 'length', 'width',\n", | |
" 'height', 'curb-weight', 'engine-type', 'num-of-cylinders',\n", | |
" 'engine-size', 'fuel-system', 'bore', 'stroke', 'compression-ratio',\n", | |
" 'horsepower', 'peak-rpm', 'city-mpg', 'highway-mpg', 'price',\n", | |
" 'city-L/100km', 'highway-L-100km', 'horsepower-binned',\n", | |
" 'aspiration-std', 'aspiration-turbo', 'aspiration-std',\n", | |
" 'aspiration-turbo', 'aspiration-std', 'aspiration-turbo',\n", | |
" 'aspiration-std', 'aspiration-turbo', 'aspiration-std',\n", | |
" 'aspiration-turbo'],\n", | |
" dtype='object')" | |
] | |
}, | |
"execution_count": 82, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.columns" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"get indicator variables and assign it to data frame \"dummy_variable_1\" \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>diesel</th>\n", | |
" <th>gas</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" diesel gas\n", | |
"0 0 1\n", | |
"1 0 1\n", | |
"2 0 1\n", | |
"3 0 1\n", | |
"4 0 1" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dummy_variable_1 = pd.get_dummies(df[\"fuel-type\"])\n", | |
"dummy_variable_1.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"change column names for clarity \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>fuel-type-diesel</th>\n", | |
" <th>fuel-type-gas</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" fuel-type-diesel fuel-type-gas\n", | |
"0 0 1\n", | |
"1 0 1\n", | |
"2 0 1\n", | |
"3 0 1\n", | |
"4 0 1" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dummy_variable_1.rename(columns={'gas':'fuel-type-gas', 'diesel':'fuel-type-diesel'}, inplace=True)\n", | |
"dummy_variable_1.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If in the dataframe, column fuel-type has a value 'gas' in the first row gas will be '1' and diesel value will be taken as '0'.\n", | |
"\n", | |
"Similarly for row having diesel it will be 1 for diesel and 0 for gas. \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# merge data frame \"df\" and \"dummy_variable_1\" \n", | |
"df = pd.concat([df, dummy_variable_1], axis=1)\n", | |
"\n", | |
"# drop original column \"fuel-type\" from \"df\"\n", | |
"df.drop(\"fuel-type-diesel\", axis = 1, inplace=True)\n", | |
"df.drop(\"fuel-type-gas\", axis = 1, inplace=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>symboling</th>\n", | |
" <th>normalized-losses</th>\n", | |
" <th>make</th>\n", | |
" <th>num-of-doors</th>\n", | |
" <th>body-style</th>\n", | |
" <th>drive-wheels</th>\n", | |
" <th>engine-location</th>\n", | |
" <th>wheel-base</th>\n", | |
" <th>length</th>\n", | |
" <th>width</th>\n", | |
" <th>...</th>\n", | |
" <th>city-mpg</th>\n", | |
" <th>highway-mpg</th>\n", | |
" <th>price</th>\n", | |
" <th>city-L/100km</th>\n", | |
" <th>highway-L-100km</th>\n", | |
" <th>horsepower-binned</th>\n", | |
" <th>aspiration-std</th>\n", | |
" <th>aspiration-turbo</th>\n", | |
" <th>aspiration-std</th>\n", | |
" <th>aspiration-turbo</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>0.811148</td>\n", | |
" <td>0.890278</td>\n", | |
" <td>...</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>13495.0</td>\n", | |
" <td>11.190476</td>\n", | |
" <td>8.703704</td>\n", | |
" <td>Low</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>two</td>\n", | |
" <td>convertible</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>88.6</td>\n", | |
" <td>0.811148</td>\n", | |
" <td>0.890278</td>\n", | |
" <td>...</td>\n", | |
" <td>21</td>\n", | |
" <td>27</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>11.190476</td>\n", | |
" <td>8.703704</td>\n", | |
" <td>Low</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>122</td>\n", | |
" <td>alfa-romero</td>\n", | |
" <td>two</td>\n", | |
" <td>hatchback</td>\n", | |
" <td>rwd</td>\n", | |
" <td>front</td>\n", | |
" <td>94.5</td>\n", | |
" <td>0.822681</td>\n", | |
" <td>0.909722</td>\n", | |
" <td>...</td>\n", | |
" <td>19</td>\n", | |
" <td>26</td>\n", | |
" <td>16500.0</td>\n", | |
" <td>12.368421</td>\n", | |
" <td>9.038462</td>\n", | |
" <td>Medium</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>fwd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.8</td>\n", | |
" <td>0.848630</td>\n", | |
" <td>0.919444</td>\n", | |
" <td>...</td>\n", | |
" <td>24</td>\n", | |
" <td>30</td>\n", | |
" <td>13950.0</td>\n", | |
" <td>9.791667</td>\n", | |
" <td>7.833333</td>\n", | |
" <td>Low</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2</td>\n", | |
" <td>164</td>\n", | |
" <td>audi</td>\n", | |
" <td>four</td>\n", | |
" <td>sedan</td>\n", | |
" <td>4wd</td>\n", | |
" <td>front</td>\n", | |
" <td>99.4</td>\n", | |
" <td>0.848630</td>\n", | |
" <td>0.922222</td>\n", | |
" <td>...</td>\n", | |
" <td>18</td>\n", | |
" <td>22</td>\n", | |
" <td>17450.0</td>\n", | |
" <td>13.055556</td>\n", | |
" <td>10.681818</td>\n", | |
" <td>Low</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>5 rows × 31 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" symboling normalized-losses make num-of-doors body-style \\\n", | |
"0 3 122 alfa-romero two convertible \n", | |
"1 3 122 alfa-romero two convertible \n", | |
"2 1 122 alfa-romero two hatchback \n", | |
"3 2 164 audi four sedan \n", | |
"4 2 164 audi four sedan \n", | |
"\n", | |
" drive-wheels engine-location wheel-base length width ... city-mpg \\\n", | |
"0 rwd front 88.6 0.811148 0.890278 ... 21 \n", | |
"1 rwd front 88.6 0.811148 0.890278 ... 21 \n", | |
"2 rwd front 94.5 0.822681 0.909722 ... 19 \n", | |
"3 fwd front 99.8 0.848630 0.919444 ... 24 \n", | |
"4 4wd front 99.4 0.848630 0.922222 ... 18 \n", | |
"\n", | |
" highway-mpg price city-L/100km highway-L-100km horsepower-binned \\\n", | |
"0 27 13495.0 11.190476 8.703704 Low \n", | |
"1 27 16500.0 11.190476 8.703704 Low \n", | |
"2 26 16500.0 12.368421 9.038462 Medium \n", | |
"3 30 13950.0 9.791667 7.833333 Low \n", | |
"4 22 17450.0 13.055556 10.681818 Low \n", | |
"\n", | |
" aspiration-std aspiration-turbo aspiration-std aspiration-turbo \n", | |
"0 1 0 1 0 \n", | |
"1 1 0 1 0 \n", | |
"2 1 0 1 0 \n", | |
"3 1 0 1 0 \n", | |
"4 1 0 1 0 \n", | |
"\n", | |
"[5 rows x 31 columns]" | |
] | |
}, | |
"execution_count": 78, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The last two columns are now the indicator variable representation of the fuel-type variable. It's all 0s and 1s now.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n", | |
"<h1> Question #4: </h1>\n", | |
"\n", | |
"<b>As above, create indicator variable to the column of \"aspiration\": \"std\" to 0, while \"turbo\" to 1.</b>\n", | |
"\n", | |
"</div>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>aspiration-std</th>\n", | |
" <th>aspiration-turbo</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" aspiration-std aspiration-turbo\n", | |
"0 1 0\n", | |
"1 1 0\n", | |
"2 1 0\n", | |
"3 1 0\n", | |
"4 1 0" | |
] | |
}, | |
"execution_count": 85, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Write your code below and press Shift+Enter to execute \n", | |
"\n", | |
"dummy_var2 = pd.get_dummies(df['aspiration'])\n", | |
"\n", | |
"dummy_var2.rename(columns={'std': 'aspiration-std', 'turbo': 'aspiration-turbo'}, inplace=True)\n", | |
"\n", | |
"dummy_var2.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- The answer is below:\n", | |
"\n", | |
"# get indicator variables of aspiration and assign it to data frame \"dummy_variable_2\"\n", | |
"dummy_variable_2 = pd.get_dummies(df['aspiration'])\n", | |
"\n", | |
"# change column names for clarity\n", | |
"dummy_variable_2.rename(columns={'std':'aspiration-std', 'turbo': 'aspiration-turbo'}, inplace=True)\n", | |
"\n", | |
"# show first 5 instances of data frame \"dummy_variable_1\"\n", | |
"dummy_variable_2.head()\n", | |
"\n", | |
"-->\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n", | |
"<h1> Question #5: </h1>\n", | |
"\n", | |
"<b>Merge the new dataframe to the original dataframe then drop the column 'aspiration'</b>\n", | |
"\n", | |
"</div>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Write your code below and press Shift+Enter to execute \n", | |
"\n", | |
"df = pd.concat([df, dummy_var2], axis=1)\n", | |
"df.drop('aspiration', axis = 1, inplace=True)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- The answer is below:\n", | |
"\n", | |
"#merge the new dataframe to the original datafram\n", | |
"df = pd.concat([df, dummy_variable_2], axis=1)\n", | |
"\n", | |
"# drop original column \"aspiration\" from \"df\"\n", | |
"df.drop('aspiration', axis = 1, inplace=True)\n", | |
"\n", | |
"-->\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"save the new csv \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df.to_csv('clean_df.csv')\n", | |
"print(df['clean'])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Thank you for completing this lab!\n", | |
"\n", | |
"## Author\n", | |
"\n", | |
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>\n", | |
"\n", | |
"### Other Contributors\n", | |
"\n", | |
"<a href=\"https://www.linkedin.com/in/mahdi-noorian-58219234/\" target=\"_blank\">Mahdi Noorian PhD</a>\n", | |
"\n", | |
"Bahare Talayian\n", | |
"\n", | |
"Eric Xiao\n", | |
"\n", | |
"Steven Dong\n", | |
"\n", | |
"Parizad\n", | |
"\n", | |
"Hima Vasudevan\n", | |
"\n", | |
"<a href=\"https://www.linkedin.com/in/fiorellawever/\" target=\"_blank\">Fiorella Wenver</a>\n", | |
"\n", | |
"<a href=\" https://www.linkedin.com/in/yi-leng-yao-84451275/ \" target=\"_blank\" >Yi Yao</a>.\n", | |
"\n", | |
"## Change Log\n", | |
"\n", | |
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n", | |
"| ----------------- | ------- | ---------- | ----------------------------------- |\n", | |
"| 2020-10-30 | 2.2 | Lakshmi | Changed URL of csv |\n", | |
"| 2020-09-09 | 2.1 | Lakshmi | Updated Indicator Variables section |\n", | |
"| 2020-08-27 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n", | |
"\n", | |
"<hr>\n", | |
"\n", | |
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python", | |
"language": "python", | |
"name": "conda-env-python-py" | |
}, | |
"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.6.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment