Skip to content

Instantly share code, notes, and snippets.

@salouri
Created November 12, 2019 12:45
Show Gist options
  • Save salouri/834648884dbbce885c7ca035e32750ec to your computer and use it in GitHub Desktop.
Save salouri/834648884dbbce885c7ca035e32750ec to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"http://cocl.us/DA0101EN_NotbookLink_Top\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://www.bigdatauniversity.com\"><img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/CCLog.png\" width = 300, align = \"center\"></a>\n",
"\n",
"<h1 align=center><font size=5>Data Analysis with Python</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Data Wrangling</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Welcome!</h3>\n",
"\n",
"By the end of this notebook, you will have learned the basics of Data Wrangling! "
]
},
{
"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",
"Estimated Time Needed: <strong>30 min</strong>\n",
"</div>\n",
" \n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>What is the purpose of Data Wrangling?</h2>"
]
},
{
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>What is the fuel consumption (L/100k) rate for the diesel car?</h3>"
]
},
{
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Import pandas</h4> "
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"URL of the dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This dataset was hosted on IBM Cloud object click <a href=\"https://cocl.us/cognitive_class_DA0101EN_objectstorage\">HERE</a> for free storage "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"filename = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/auto.csv\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Python list <b>headers</b> containing name of headers "
]
},
{
"cell_type": "code",
"execution_count": 7,
"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\"."
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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",
" </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 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",
"\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",
"\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",
"\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",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# To see what the data set looks like, we'll use the head() method.\n",
"df.head()"
]
},
{
"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",
"<div>So, how do we identify all those missing values and deal with them?</div> \n",
"\n",
"\n",
"<b>How to work with missing data?</b>\n",
"\n",
"Steps for working with missing data:\n",
"<ol>\n",
" <li>dentify missing data</li>\n",
" <li>deal with missing data</li>\n",
" <li>correct data format</li>\n",
"</ol>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"identify_handle_missing_values\">Identify and handle missing values</h2>\n",
"\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"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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",
" </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 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",
"\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",
"\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",
"\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",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 6,
"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(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"dentify_missing_values\n",
"\n",
"<h4>Evaluating for Missing Data</h4>\n",
"\n",
"The missing values are converted to Python's default. We use Python's built-in functions to identify these missing values. There are two methods to detect missing data:\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."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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": 7,
"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."
]
},
{
"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>"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['symboling', 'normalized-losses', 'make', 'fuel-type', 'aspiration', 'num-of-doors', 'body-style', 'drive-wheels', 'engine-location', 'wheel-base', 'length', 'width', 'height', 'curb-weight', 'engine-type', 'num-of-cylinders', 'engine-size', 'fuel-system', 'bore', 'stroke', 'compression-ratio', 'horsepower', 'peak-rpm', 'city-mpg', 'highway-mpg', 'price']\n",
"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": [
"x = missing_data.columns.values.tolist()\n",
"print(x)\n",
"for column in x:\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",
"<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>"
]
},
{
"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>"
]
},
{
"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",
"<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",
"<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",
"<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 now without price data is not useful to us</li>\n",
" </ul>\n",
" </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Calculate the average of the column </h4>"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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>"
]
},
{
"cell_type": "code",
"execution_count": 12,
"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>"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 2.68\n",
"1 2.68\n",
"2 3.47\n",
"3 3.40\n",
"4 3.40\n",
" ... \n",
"200 3.15\n",
"201 3.15\n",
"202 2.87\n",
"203 3.40\n",
"204 3.15\n",
"Name: stroke, Length: 205, dtype: object\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"import numpy as np\n",
"print(df[\"stroke\"])\n",
"df[\"stroke\"].replace(\"?\",np.nan, inplace = True )\n",
"stroke_avg = df[\"stroke\"].astype(\"float\").mean(axis=0)\n",
"df[\"stroke\"].replace(np.nan,stroke_avg, 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>"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average horsepower: 104.25615763546799\n"
]
}
],
"source": [
"df['horsepower'].replace(\"?\",np.nan, inplace=True)\n",
"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>"
]
},
{
"cell_type": "code",
"execution_count": 23,
"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>"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average peak rpm: 5125.369458128079\n"
]
}
],
"source": [
"df['peak-rpm'].replace('?',np.nan, inplace=True)\n",
"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>"
]
},
{
"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:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"four 114\n",
"two 89\n",
"? 2\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:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# simply drop whole row with NaN in \"price\" column\n",
"#print(df.describe)\n",
"df.dropna(subset=[\"price\"], axis=0, inplace=True)\n",
"# print(df[\"price\"])\n",
"# reset index, because we droped two rows\n",
"df.reset_index(drop=True, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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",
" </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 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",
"\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",
"\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",
"\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",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Good!</b> Now, we obtain the dataset with no missing values."
]
},
{
"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",
"<p><b>.dtype()</b> to check the data type</p>\n",
"<p><b>.astype()</b> to change the data type</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Lets list the data types for each column</h4>"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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": 44,
"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> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Convert data types to proper format</h4>"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df.loc[:,[\"bore\", \"stroke\"]].replace('?',0, inplace=True)\n",
"df.loc[:,[\"bore\", \"stroke\"]] = df[[\"bore\", \"stroke\"]].astype(\"float\")\n",
"df.loc[:,\"normalized-losses\"].replace(np.nan,0, inplace=True)\n",
"df.loc[:,[\"normalized-losses\"]] = df[[\"normalized-losses\"]].astype(\"int\")\n",
"df.loc[:,[\"price\"]] = df[[\"price\"]].astype(\"float\")\n",
"df.loc[:,[\"peak-rpm\"]] = df[[\"peak-rpm\"]].astype(\"float\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Let us list the columns after the conversion</h4>"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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": 69,
"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."
]
},
{
"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",
"<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>"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>0</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>0</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>0</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 0 alfa-romero gas std \n",
"1 3 0 alfa-romero gas std \n",
"2 1 0 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": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>0</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>0</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>0</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 0 alfa-romero gas std \n",
"1 3 0 alfa-romero gas std \n",
"2 1 0 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": 71,
"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",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df[\"highway-mpg\"] = 234 / df[\"highway-mpg\"]\n",
"df.rename(columns={\"highway-mpg\":\"highway-L/100km\"}, inplace=True)"
]
},
{
"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",
"<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",
"<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>"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we can see, we've normalized \"length\", \"width\" and \"height\" in the range of [0,1]."
]
},
{
"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",
"<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",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example of Binning Data In Pandas</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Convert data to correct format "
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df[\"horsepower\"]=df[\"horsepower\"].replace('?',0).astype(int, copy=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets plot the histogram of horspower, to see what the distribution of horsepower looks like."
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAXPklEQVR4nO3de7SddX3n8feHAKJylxOMeIky1Ck6GjVVGUek4gXBMYwWlFWcjKWNzlKrq9o2tlNHZ2onXmprrauuOF5iveIFyYyzVCaVoK2iQSOC0QYxIBCTA6KgTlHwO3/s33nYhpzknJM8Z59z8n6ttddz38/3d55kf/bz7P38dqoKSZIADhp1AZKkucNQkCR1DAVJUsdQkCR1DAVJUsdQkCR1DAWNVJJtSZ426jrmuiSnJblhD8vfmeTPZrMmLUwHj7oASfuuql4y6hq0MHimoAUhyYJ5g7OQ2qL5x1DQXLAsyZVJfpzko0kOm1iQ5PeSXJPkh0nWJ3nA0LJK8tIkW4GtGfirJDvbc12Z5JFt3XsleUuS65PsaJdb7t2WnZbkhiR/kuTmdknrt4f2c1SS9ycZT3Jdkv+S5KC27Lokj2vj57eaTm7Tv5vkU238oCSrk3w3yS1JLkxybFu2tG13QZLrgX+Y7A+1hxrfl+TPd2nPq9rfYnuSFw2te2aSbyW5PcmNSV69LwdPC4uhoLngXOAM4KHAo4D/BJDkqcD/aMuXANcBH9ll27OBJwAnA88ATgV+DTgaeD5wS1vvjW3+MuBfAScArx16nvsDx7X5K4G1SR7elr0dOAp4GPAU4D8CEy+yG4HT2vipwLVtnYnpjW3891utTwEeANwKv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rK2zKMmbk3y1bf/iNv+0JJe15/tWkncmOagtOy+D38G4Kskb27xzk7y1jb8iybVt/MQkX2zjj0uysXWA+Nmhrgt+pc7Z/XPqQOI7Di1EJwHnVdXvJbkQeB7wR8DLq2pjkv8G/FfglW39o6vqKQBJvgk8s6punOhOgMHdyj+uqt9Ici/gH5N8ri17PIM+668DPgM8N8k/AW8EHgfcyqCX27OBy4A/bNs9GbglyQkM7rr9Quuj5+3AiqoaT/J84A3A7+xap9QXQ0EL0feqanMbv4JBb5hHV9XGNm8d8LGh9T86NP6PwPtamEx0CPcM4FFJfqtNH8UgeH4OfKWqJt7xf5jBC/wvgEurarzN/yBwalV9KsnhrT+rBwEfYvBjPU9u+3o48EjgkkGXPSxi0JXG7uqUemEoaCG6Y2j8LuDoyVZsfjoxUlUvSfIE4Cxgc5JlDLoffnlVfXZ4oySncc9uiIvdd1c84UvAixj0Q/MFBmcBpwCvAh4MXF1Vp+ytTqkvfqagA8GPgVuTPLlNvxDYuLsVk5xYVZdX1WuBmxm8o/8s8J/b5R2S/FrrtRYG/dk/tH2W8Hzgiwx+xOUpSY5Lsgg4b2h/lwGvbsOvA78J3FFVP2YQFGNJTmn7OSTJI/bfn0HaO88UdKBYCbwzyX2Aaxm8W9+dNyc5icG7/Q0Meqi9ElgKfK11xTzO3T93+CVgDfBvGLzQX1RVv0zyGuDz7Xn+T1VNdGX8BQZBc1lV3ZXk+wy6Pqaqft4uUf1NkqMY/P/8awY95Eqzwl5SpRlql49eXVXPHnUt0v7i5SNJUsczBUlSxzMFSVLHUJAkdQwFSVLHUJAkdQwFSVLn/wMTsupcD4pPjQAAAABJRU5ErkJggg==\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>"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 96., 192., 288.])"
]
},
"execution_count": 102,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"group_names = ['Low', 'Medium', 'High']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We apply the function \"cut\" the determine what each value of \"df['horsepower']\" belongs to. "
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>111</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>154</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>102</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>115</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>110</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>110</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>110</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>140</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>160</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>101</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>101</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>48</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>70</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" horsepower horsepower-binned\n",
"0 111 Medium\n",
"1 111 Medium\n",
"2 154 Medium\n",
"3 102 Medium\n",
"4 115 Medium\n",
"5 110 Medium\n",
"6 110 Medium\n",
"7 110 Medium\n",
"8 140 Medium\n",
"9 160 Medium\n",
"10 101 Medium\n",
"11 101 Medium\n",
"12 121 Medium\n",
"13 121 Medium\n",
"14 121 Medium\n",
"15 182 Medium\n",
"16 182 Medium\n",
"17 182 Medium\n",
"18 48 Low\n",
"19 70 Low"
]
},
"execution_count": 108,
"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."
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Low 106\n",
"Medium 93\n",
"High 6\n",
"Name: horsepower-binned, dtype: int64"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"horsepower-binned\"].value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets plot the distribution of each bin."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAAB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1TVvW1snX9s2/lCVZ3blr+ILmgurKp7klxPG0W1qu5ql6jek2Q3ur/bvwUu30bPgdRz7CNpG2uXj06tKn8vQfOOl48kST3PFCRJPc8UJEk9Q0GS1DMUJEk9Q0GS1DMUJEm9/w93/l1NzJpNXQAAAABJRU5ErkJggg==\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 narrow the intervals from 57 to 3!\n",
"</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Bins visualization</h3>\n",
"Normally, a histogram is used to visualize the distribution of bins we created above. "
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEWCAYAAAB1xKBvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAWGUlEQVR4nO3de7hddX3n8feHcPHCvQkYwRpgqFN0FDVVqSMyYr1Pw9iq+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ain": [
"<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",
"a = (0,1,2)\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\". "
]
},
{
"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",
"<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>"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"Index(['symboling', 'normalized-losses', 'make', 'fuel-type', 'aspiration',\n",
" 'num-of-doors', 'body-style', 'drive-wheels', 'engine-location',\n",
" 'wheel-base', 'length', 'width', 'height', 'curb-weight', 'engine-type',\n",
" 'num-of-cylinders', 'engine-size', 'fuel-system', 'bore', 'stroke',\n",
" 'compression-ratio', 'horsepower', 'peak-rpm', 'city-mpg',\n",
" 'highway-L/100km', 'price', 'horsepower-binned'],\n",
" dtype='object')"
]
},
"execution_count": 112,
"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\" "
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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": 113,
"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 "
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dummy_variable_1.rename(columns={'fuel-type-gas':'gas', 'fuel-type-diesel':'diesel'}, inplace=True)\n",
"dummy_variable_1.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now have the value 0 to represent \"gas\" and 1 to represent \"diesel\" in the column \"fuel-type\". We will now insert this column back into our original dataset. "
]
},
{
"cell_type": "code",
"execution_count": 122,
"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>city-mpg</th>\n",
" <th>highway-L/100km</th>\n",
" <th>price</th>\n",
" <th>horsepower-binned</th>\n",
" <th>diesel</th>\n",
" <th>gas</th>\n",
" <th>diesel</th>\n",
" <th>gas</th>\n",
" <th>diesel</th>\n",
" <th>gas</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>21</td>\n",
" <td>8.666667</td>\n",
" <td>13495</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>21</td>\n",
" <td>8.666667</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>19</td>\n",
" <td>9.000000</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>24</td>\n",
" <td>7.800000</td>\n",
" <td>13950</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>18</td>\n",
" <td>10.636364</td>\n",
" <td>17450</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>200</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</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>109.1</td>\n",
" <td>...</td>\n",
" <td>23</td>\n",
" <td>8.357143</td>\n",
" <td>16845</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>201</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>turbo</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>...</td>\n",
" <td>19</td>\n",
" <td>9.360000</td>\n",
" <td>19045</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>202</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</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>109.1</td>\n",
" <td>...</td>\n",
" <td>18</td>\n",
" <td>10.173913</td>\n",
" <td>21485</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>diesel</td>\n",
" <td>turbo</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>...</td>\n",
" <td>26</td>\n",
" <td>8.666667</td>\n",
" <td>22470</td>\n",
" <td>Medium</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>204</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>turbo</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>...</td>\n",
" <td>19</td>\n",
" <td>9.360000</td>\n",
" <td>22625</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>205 rows × 33 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",
".. ... ... ... ... ... \n",
"200 -1 95 volvo gas std \n",
"201 -1 95 volvo gas turbo \n",
"202 -1 95 volvo gas std \n",
"203 -1 95 volvo diesel turbo \n",
"204 -1 95 volvo gas turbo \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",
"200 four sedan rwd front 109.1 ... \n",
"201 four sedan rwd front 109.1 ... \n",
"202 four sedan rwd front 109.1 ... \n",
"203 four sedan rwd front 109.1 ... \n",
"204 four sedan rwd front 109.1 ... \n",
"\n",
" city-mpg highway-L/100km price horsepower-binned diesel gas diesel \\\n",
"0 21 8.666667 13495 Medium 0 1 0 \n",
"1 21 8.666667 16500 Medium 0 1 0 \n",
"2 19 9.000000 16500 Medium 0 1 0 \n",
"3 24 7.800000 13950 Medium 0 1 0 \n",
"4 18 10.636364 17450 Medium 0 1 0 \n",
".. ... ... ... ... ... .. ... \n",
"200 23 8.357143 16845 Medium 0 1 0 \n",
"201 19 9.360000 19045 Medium 0 1 0 \n",
"202 18 10.173913 21485 Medium 0 1 0 \n",
"203 26 8.666667 22470 Medium 1 0 1 \n",
"204 19 9.360000 22625 Medium 0 1 0 \n",
"\n",
" gas diesel gas \n",
"0 1 0 1 \n",
"1 1 0 1 \n",
"2 1 0 1 \n",
"3 1 0 1 \n",
"4 1 0 1 \n",
".. .. ... .. \n",
"200 1 0 1 \n",
"201 1 0 1 \n",
"202 1 0 1 \n",
"203 0 1 0 \n",
"204 1 0 1 \n",
"\n",
"[205 rows x 33 columns]"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# merge data frame \"df\" and \"dummy_variable_1\" \n",
"df = pd.concat([df, dummy_variable_1], axis=1)\n",
"df\n",
"# drop original column \"fuel-type\" from \"df\"\n",
"#df.drop(\"fuel-type-diesel\", axis = 1, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-L/100km</th>\n",
" <th>price</th>\n",
" <th>horsepower-binned</th>\n",
" <th>diesel</th>\n",
" <th>gas</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>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>8.666667</td>\n",
" <td>13495</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</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>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>8.666667</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</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>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000</td>\n",
" <td>19</td>\n",
" <td>9.000000</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</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.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500</td>\n",
" <td>24</td>\n",
" <td>7.800000</td>\n",
" <td>13950</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</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.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500</td>\n",
" <td>18</td>\n",
" <td>10.636364</td>\n",
" <td>17450</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 29 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",
"\n",
" body-style drive-wheels engine-location wheel-base ... stroke \\\n",
"0 convertible rwd front 88.6 ... 2.68 \n",
"1 convertible rwd front 88.6 ... 2.68 \n",
"2 hatchback rwd front 94.5 ... 3.47 \n",
"3 sedan fwd front 99.8 ... 3.40 \n",
"4 sedan 4wd front 99.4 ... 3.40 \n",
"\n",
" compression-ratio horsepower peak-rpm city-mpg highway-L/100km price \\\n",
"0 9.0 111 5000 21 8.666667 13495 \n",
"1 9.0 111 5000 21 8.666667 16500 \n",
"2 9.0 154 5000 19 9.000000 16500 \n",
"3 10.0 102 5500 24 7.800000 13950 \n",
"4 8.0 115 5500 18 10.636364 17450 \n",
"\n",
" horsepower-binned diesel gas \n",
"0 Medium 0 1 \n",
"1 Medium 0 1 \n",
"2 Medium 0 1 \n",
"3 Medium 0 1 \n",
"4 Medium 0 1 \n",
"\n",
"[5 rows x 29 columns]"
]
},
"execution_count": 116,
"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."
]
},
{
"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",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>std</th>\n",
" <th>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",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>200</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>201</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>202</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>204</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>205 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" std turbo\n",
"0 1 0\n",
"1 1 0\n",
"2 1 0\n",
"3 1 0\n",
"4 1 0\n",
".. ... ...\n",
"200 1 0\n",
"201 0 1\n",
"202 1 0\n",
"203 0 1\n",
"204 0 1\n",
"\n",
"[205 rows x 2 columns]"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"dummy_variable_2 = pd.get_dummies(df['aspiration'])\n",
"dummy_variable_2"
]
},
{
"cell_type": "code",
"execution_count": 124,
"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": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dummy_variable_2.rename(columns={'std':'aspiration-std', 'turbo': 'aspiration-turbo'}, inplace=True)\n",
"dummy_variable_2.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",
"-->"
]
},
{
"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",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df = pd.concat([df,dummy_variable_2] , axis=1)\n"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
"df.drop(\"aspiration\", axis=1, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 127,
"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>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>...</th>\n",
" <th>price</th>\n",
" <th>horsepower-binned</th>\n",
" <th>diesel</th>\n",
" <th>gas</th>\n",
" <th>diesel</th>\n",
" <th>gas</th>\n",
" <th>diesel</th>\n",
" <th>gas</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>?</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>168.8</td>\n",
" <td>...</td>\n",
" <td>13495</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</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>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>168.8</td>\n",
" <td>...</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</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>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>171.2</td>\n",
" <td>...</td>\n",
" <td>16500</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>176.6</td>\n",
" <td>...</td>\n",
" <td>13950</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</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>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>176.6</td>\n",
" <td>...</td>\n",
" <td>17450</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>200</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>188.8</td>\n",
" <td>...</td>\n",
" <td>16845</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>201</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>188.8</td>\n",
" <td>...</td>\n",
" <td>19045</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>202</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>188.8</td>\n",
" <td>...</td>\n",
" <td>21485</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>diesel</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>188.8</td>\n",
" <td>...</td>\n",
" <td>22470</td>\n",
" <td>Medium</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>204</th>\n",
" <td>-1</td>\n",
" <td>95</td>\n",
" <td>volvo</td>\n",
" <td>gas</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>109.1</td>\n",
" <td>188.8</td>\n",
" <td>...</td>\n",
" <td>22625</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>205 rows × 34 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type num-of-doors \\\n",
"0 3 ? alfa-romero gas two \n",
"1 3 ? alfa-romero gas two \n",
"2 1 ? alfa-romero gas two \n",
"3 2 164 audi gas four \n",
"4 2 164 audi gas four \n",
".. ... ... ... ... ... \n",
"200 -1 95 volvo gas four \n",
"201 -1 95 volvo gas four \n",
"202 -1 95 volvo gas four \n",
"203 -1 95 volvo diesel four \n",
"204 -1 95 volvo gas four \n",
"\n",
" body-style drive-wheels engine-location wheel-base length ... price \\\n",
"0 convertible rwd front 88.6 168.8 ... 13495 \n",
"1 convertible rwd front 88.6 168.8 ... 16500 \n",
"2 hatchback rwd front 94.5 171.2 ... 16500 \n",
"3 sedan fwd front 99.8 176.6 ... 13950 \n",
"4 sedan 4wd front 99.4 176.6 ... 17450 \n",
".. ... ... ... ... ... ... ... \n",
"200 sedan rwd front 109.1 188.8 ... 16845 \n",
"201 sedan rwd front 109.1 188.8 ... 19045 \n",
"202 sedan rwd front 109.1 188.8 ... 21485 \n",
"203 sedan rwd front 109.1 188.8 ... 22470 \n",
"204 sedan rwd front 109.1 188.8 ... 22625 \n",
"\n",
" horsepower-binned diesel gas diesel gas diesel gas aspiration-std \\\n",
"0 Medium 0 1 0 1 0 1 1 \n",
"1 Medium 0 1 0 1 0 1 1 \n",
"2 Medium 0 1 0 1 0 1 1 \n",
"3 Medium 0 1 0 1 0 1 1 \n",
"4 Medium 0 1 0 1 0 1 1 \n",
".. ... ... .. ... .. ... .. ... \n",
"200 Medium 0 1 0 1 0 1 1 \n",
"201 Medium 0 1 0 1 0 1 0 \n",
"202 Medium 0 1 0 1 0 1 1 \n",
"203 Medium 1 0 1 0 1 0 0 \n",
"204 Medium 0 1 0 1 0 1 0 \n",
"\n",
" aspiration-turbo \n",
"0 0 \n",
"1 0 \n",
"2 0 \n",
"3 0 \n",
"4 0 \n",
".. ... \n",
"200 0 \n",
"201 1 \n",
"202 0 \n",
"203 1 \n",
"204 1 \n",
"\n",
"[205 rows x 34 columns]"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"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",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"save the new csv "
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('clean_df.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Thank you for completing this notebook</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
" <p><a href=\"https://cocl.us/DA0101EN_NotbookLink_Top_bottom\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3>\n",
"\n",
"This notebook was written by <a href=\"https://www.linkedin.com/in/mahdi-noorian-58219234/\" target=\"_blank\">Mahdi Noorian PhD</a>, <a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>, Bahare Talayian, Eric Xiao, Steven Dong, Parizad, Hima Vsudevan and <a href=\"https://www.linkedin.com/in/fiorellawever/\" target=\"_blank\">Fiorella Wenver</a> and <a href=\" https://www.linkedin.com/in/yi-leng-yao-84451275/ \" target=\"_blank\" >Yi Yao</a>.\n",
"\n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment