-
-
-
-
-
-
-
-Case Study in Human Contexts and Ethics
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Case Study in Human Contexts and Ethics
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-Learning Outcomes
-
-
-
-
-
-
-
Learn about the ethical dilemmas that data scientists face.
-
Know how critique models using contextual knowledge about data.
-
-
-
-
-
-
Disclaimer: The following chapter discusses issues of structural racism. Some of the items in the chapter may be sensitive and may or may not be the opinions, ideas, and beliefs of the students who collected the materials. The Data 100 course staff tries its best to only present information that is relevant for teaching the lessons at hand.
-
-
Note: Given the nuanced nature of some of the arguments made in the lecture, it is highly recommended that you view the lecture recording in order to fully engage and understand the material. The course notes will have the same broader structure but are by no means comprehensive.
-
Let’s immerse ourselves in the real-world story of data scientists working for an organization called the Cook County Assessor’s Office (CCAO). Their job is to estimate the values of houses in order to assign property taxes. This is because the tax burden in this area is determined by the estimated value of a house, which is different from its price. Since values change over time and there are no obvious indicators of value, they created a model to estimate the values of houses. In this chapter, we will dig deep into what problems biased the models, the consequences to human lives, and how we can learn from this example to do better.
-
-
The Problem
-
A report by the Chicago Tribune uncovered a major scandal: the team showed that the model perpetuated a highly regressive tax system that disproportionately burdened African-American and Latinx homeowners in Cook County. How did they know?
-
-
-
-
In the field of housing assessment, there are standard metrics that assessors use across the world to estimate the fairness of assessments: coefficient of dispersion and price-related differential. These metrics have been rigorously tested by experts in the field and are out of scope for our class. Calculating these metrics for the Cook County prices revealed that the pricing created by the CCAO did not fall in acceptable ranges (see figure above). This by itself is not the end of the story, but a good indicator that something fishy was going on.
-
-
-
-
This prompted them to investigate if the model itself was producing fair tax rates. Evidently, when accounting for the home owner’s income, they found that the model actually produced a regressive tax rate (see figure above). A tax rate is regressive if the percentage tax rate is higher for individuals with lower net income. A tax rate is progressive if the percentage tax rate is higher for individuals with higher net income.
-
-
-
-
Further digging suggests that not only was the system unfair to people across the axis of income, it was also unfair across the axis of race (see figure above). The likelihood of a property being under- or over-assessed was highly dependent on the owner’s race, and that did not sit well with many homeowners.
-
-
Spotlight: Appeals
-
What actually caused this to come about? A comprehensive answer goes beyond just models. At the end of the day, these are real systems that have a lot of moving parts. One of which was the appeals system. Homeowners are mailed the value their home assessed by CCAO, and the homeowner can choose to appeal to a board of elected officials to try and change the listed value of their home and thus how much they are taxed. In theory, this sounds like a very fair system: someone oversees the final pricing of houses rather than just an algorithm. However, it ended up exacerbating the problems.
-
-
“Appeals are a good thing,” Thomas Jaconetty, deputy assessor for valuation and appeals, said in an interview. “The goal here is fairness. We made the numbers. We can change them.”
-
-
-
-
-
-
Here we can borrow lessons from Critical Race Theory. On the surface, everyone having the legal right to try and appeal is undeniable. However, not everyone has an equal ability to do so. Those who have the money to hire tax lawyers to appeal for them have a drastically higher chance of trying and succeeding (see above figure). This model is part of a deeper institutional pattern rife with potential corruption.
-
-
-
-
-
Homeowners who appealed were generally under-assessed relative to homeowners who did not (see above figure). Those with higher incomes pay less in property tax, tax lawyers are able to grow their business due to their role in appeals, and politicians are commonly socially connected to the aforementioned tax lawyers and wealthy homeowners. All these stakeholders have reasons to advertise the model as an integral part of a fair system. Here lies the value in asking questions: a system that seems fair on the surface may in actuality be unfair upon taking a closer look.
-
-
-
Human Impacts
-
-
-
-
-
The impact of the housing model extends beyond the realm of home ownership and taxation. Discriminatory practices have a long history within the United States, and the model served to perpetuate this fact. To this day, Chicago is one of the most segregated cities in the United States (source). These factors are central to informing us, as data scientists, about what is at stake.
-
-
-
Spotlight: Intersection of Real Estate and Race
-
Housing has been a persistent source of racial inequality throughout US history, amongst other factors. It is one of the main areas where inequalities are created and reproduced. In the beginning, Jim Crow laws were explicit in forbidding people of color from schools, public utilities, etc.
-
-
-
-
-
Today, while advancements in Civil Rights have been made, the spirit of the laws are alive in many parts of the US. The real estate industry was “professionalized” in the 1920’s and 1930’s by aspiring to become a science guided by strict methods and principles outlined below:
-
-
Redlining: making it difficult or impossible to get a federally-backed mortgage to buy a house in specific neighborhoods coded as “risky” (red).
-
-
What made them “risky” according to the makers of these was racial composition.
-
Segregation was not only a result of federal policy, but developed by real estate professionals.
-
-
The methods centered on creating objective rating systems (information technologies) for the appraisal of property values which encoded race as a factor of valuation (see figure below),
-
-
This, in turn, influenced federal policy and practice.
-
-
-
-
-
-Source: Colin Koopman, How We Became Our Data (2019) p. 137
-
-
-
-
-
-
-
The Response: Cook County Open Data Initiative
-
The response started in politics. A new assessor, Fritz Kaegi, was elected and created a new mandate with two goals:
-
-
Distributional equity in property taxation, meaning that properties of same value treated alike during assessments.
-
Creating a new Office of Data Science.
-
-
-
-
-
-
-
Question/Problem Formulation
-
-
-
-
-
-
-Driving Questions
-
-
-
-
-
What do we want to know?
-
What problems are we trying to solve?
-
What are the hypotheses we want to test?
-
What are our metrics for success?
-
-
-
-
The new Office of Data Science started by redefining their goals.
-
-
Accurately, uniformly, and impartially assess the value of a home by
-
-
Following international standards (coefficient of dispersion)
-
Predicting value of all homes with as little total error as possible
-
-
Create a robust pipeline that accurately assesses property values at scale and is fair by
-
-
Disrupts the circuit of corruption (Board of Review appeals process)
-
Eliminates regressivity
-
Engenders trust in the system among all stakeholders
-
-
-
-
-
-
-
-
-Definitions: Fairness and Transparency
-
-
-
-
The definitions, as given by the Cook County Assessor’s Office, are given below:
-
-
Fairness: The ability of our pipeline to accurately assess property values, accounting for disparities in geography, information, etc.
-
Transparency: The ability of the data science department to share and explain pipeline results and decisions to both internal and external stakeholders
-
-
Note how the Office defines “fairness” in terms of accuracy. Thus, the problem - make the system more fair - was already framed in terms amenable to a data scientist: make the assessments more accurate. The idea here is that if the model is more accurate it will also (perhaps necessarily) become more fair, which is a big assumption. There are, in a sense, two different problems - make accurate assessments, and make a fair system.
-
-
-
The way the goals are defined lead us to ask the question: what does it actually mean to accurately assess property values, and what role does “scale” play?
-
-
What is an assessment of a home’s value?
-
What makes one assessment more accurate than another?
-
What makes one batch of assessments more accurate than another batch?
-
-
Each of the above questions leads to a slew of more questions. Considering just the first question, one answer could be that an assessment is an estimate of the value of a home. This leads to more inquiries: what is the value of a home? What determines it? How do we know? For this class, we take it to be the house’s market value.
-
-
-
Data Acquisition and Cleaning
-
-
-
-
-
-
-Driving Questions
-
-
-
-
-
What data do we have, and what data do we need?
-
How will we sample more data?
-
Is our data representative of the population we want to study?
-
-
-
-
The data scientists also critically examined their original sales data:
-
-
-
-
-
and asked the questions:
-
-
How was this data collected?
-
When was this data collected?
-
Who collected this data?
-
For what purposes was the data collected?
-
How and why were particular categories created?
-
-
For example, attributes can have different likelihoods of appearing in the data, and housing data in the floodplains geographic region of Chicago were less represented than other regions.
-
The features can even be reported at different rates. Improvements in homes, which tend to increase property value, were unlikely to be reported by the homeowners.
-
Additionally, they found that there was simply more missing data in lower income neighborhoods.
-
-
-
Exploratory Data Analysis
-
-
-
-
-
-
-Driving Questions
-
-
-
-
-
How is our data organized, and what does it contain?
-
Do we already have relevant data?
-
What are the biases, anomalies, or other issues with the data?
-
How do we transform the data to enable effective analysis?
-
-
-
-
Before the modeling step, they investigated a multitude of crucial questions:
-
-
Which attributes are most predictive of sales price?
-
Is the data uniformly distributed?
-
Do all neighborhoods have up to date data? Do all neighborhoods have the same granularity?
-
-
Do some neighborhoods have missing or outdated data?
-
-
Firstly, they found that the impact of certain features, such as bedroom number, were much more impactful in determining house value inside certain neighborhoods more than others. This informed them that different models should be used depending on the neighborhood.
-
They also noticed that low income neighborhoods had disproportionately spottier data. This informed them that they needed to develop new data collection practices - including finding new sources of data.
-
-
-
Prediction and Inference
-
-
-
-
-
-
-Driving Questions
-
-
-
-
-
What does the data say about the world?
-
Does it answer our questions or accurately solve the problem?
-
How robust are our conclusions and can we trust the predictions?
-
-
-
-
Rather than using a singular model to predict sale prices (“fair market value”) of unsold properties, the CCAO fit machine learning models that discover patterns using known sale prices and characteristics of similar and nearby properties. It uses different model weights for each township.
-
Compared to traditional mass appraisal, the CCAO’s new approach is more granular and more sensitive to neighborhood variations.
-
Here, we might ask why should any particular individual believe that the model is accurate for their property?
-
This leads us to recognize that the CCAO counts on its performance of “transparency” (putting data, models, pipeline onto GitLab) to foster public trust, which would help it equate the production of “accurate assessments” with “fairness”.
-
There’s a lot more to be said here on the relationship between accuracy, fairness, and metrics we tend to use when evaluating our models. Given the nuanced nature of the argument, it is recommended you view the corresponding lecture as the course notes are not as comprehensive for this portion of lecture.
-
-
-
Reports Decisions, and Conclusions
-
-
-
-
-
-
-Driving Questions
-
-
-
-
-
How successful is the system for each goal?
-
-
Accuracy/uniformity of the model
-
Fairness and transparency that eliminates regressivity and engenders trust
-
-
How do you know?
-
-
-
-
The model is not the end of the road. The new Office still sends homeowners their house evaluations, but now the data that they get sent back from the homeowners is taken into account. More detailed reports are being written by the Office itself to democratize the information. Town halls and other public facing outreach helps involves the whole community in the process of housing evaluations, rather than limiting participation to a select few.
-
-
-
-
Key Takeaways
-
-
Accuracy is a necessary, but not sufficient, condition of a fair system.
-
Fairness and transparency are context-dependent and sociotechnical concepts.
-
Learn to work with contexts, and consider how your data analysis will reshape them.
-
Keep in mind the power, and limits, of data analysis.
-
-
-
-
Lessons for Data Science Practice
-
-
Question/Problem Formulation
-
-
Who is responsible for framing the problem?
-
Who are the stakeholders? How are they involved in the problem framing?
-
What do you bring to the table? How does your positionality affect your understanding of the problem?
-
What are the narratives that you’re tapping into?
-
-
Data Acquisition and Cleaning
-
-
Where does the data come from?
-
Who collected it? For what purpose?
-
What kinds of collecting and recording systems and techniques were used?
-
How has this data been used in the past?
-
What restrictions are there on access to the data, and what enables you to have access?
-
-
Exploratory Data Analysis & Visualization
-
-
What kind of personal or group identities have become salient in this data?
-
Which variables became salient, and what kinds of relationship obtain between them?
-
Do any of the relationships made visible lend themselves to arguments that might be potentially harmful to a particular community?
-
-
Prediction and Inference
-
-
What does the prediction or inference do in the world?
-
Are the results useful for the intended purposes?
-
Are there benchmarks to compare the results?
-
How are your predictions and inferences dependent upon the larger system in which your model works?
-
-
Reports, Decisions, and Solutions
-
-
How do we know if we have accomplished our goals?
-
How does your work fit in the broader literature?
-
Where does your work agree or disagree with the status quo?
-
Do your conclusions make sense?
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/case_study_HCE/case_study_HCE.ipynb b/case_study_HCE/case_study_HCE.ipynb
deleted file mode 100644
index 3ade52d1..00000000
--- a/case_study_HCE/case_study_HCE.ipynb
+++ /dev/null
@@ -1,329 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "raw",
- "metadata": {},
- "source": [
- "---\n",
- "title: Case Study in Human Contexts and Ethics\n",
- "execute:\n",
- " echo: true\n",
- "format:\n",
- " html:\n",
- " code-fold: true\n",
- " code-tools: true\n",
- " toc: true\n",
- " toc-title: Case Study in Human Contexts and Ethics\n",
- " page-layout: full\n",
- " theme:\n",
- " - cosmo\n",
- " - cerulean\n",
- " callout-icon: false\n",
- "---"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "::: {.callout-note collapse=\"false\"}\n",
- "## Learning Outcomes\n",
- "* Learn about the ethical dilemmas that data scientists face.\n",
- "* Know how critique models using contextual knowledge about data. \n",
- ":::"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "> **Disclaimer**: The following chapter discusses issues of structural racism. Some of the items in the chapter may be sensitive and may or may not be the opinions, ideas, and beliefs of the students who collected the materials. The Data 100 course staff tries its best to only present information that is relevant for teaching the lessons at hand.\n",
- "\n",
- "**Note:** Given the nuanced nature of some of the arguments made in the lecture, it is highly recommended that you view the lecture recording in order to fully engage and understand the material. The course notes will have the same broader structure but are by no means comprehensive.\n",
- "\n",
- "\n",
- "Let's immerse ourselves in the real-world story of data scientists working for an organization called the Cook County Assessor's Office (CCAO). Their job is to **estimate the values of houses** in order to **assign property taxes**. This is because the tax burden in this area is determined by the estimated **value** of a house, which is different from its price. Since values change over time and there are no obvious indicators of value, they created a **model** to estimate the values of houses. In this chapter, we will dig deep into what problems biased the models, the consequences to human lives, and how we can learn from this example to do better. \n",
- "\n",
- "\n",
- "## The Problem\n",
- "\n",
- "A [report](https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/assessments.html) by the Chicago Tribune uncovered a major scandal: the team showed that the model perpetuated a highly regressive tax system that disproportionately burdened African-American and Latinx homeowners in Cook County. How did they know? \n",
- "\n",
- "
\n",
- "\n",
- "In the field of housing assessment, there are standard metrics that assessors use across the world to estimate the fairness of assessments: [coefficient of dispersion](https://www.realestateagent.com/real-estate-glossary/real-estate/coefficient-of-dispersion.html) and [price-related differential](https://leg.wa.gov/House/Committees/FIN/Documents/2009/RatioText.pdf). These metrics have been rigorously tested by experts in the field and are out of scope for our class. Calculating these metrics for the Cook County prices revealed that the pricing created by the CCAO did not fall in acceptable ranges (see figure above). This by itself is **not the end** of the story, but a good indicator that **something fishy was going on**.\n",
- "\n",
- "
\n",
- "\n",
- "This prompted them to investigate if the model itself was producing fair tax rates. Evidently, when accounting for the home owner's income, they found that the model actually produced a **regressive** tax rate (see figure above). A tax rate is **regressive** if the percentage tax rate is higher for individuals with lower net income. A tax rate is **progressive** if the percentage tax rate is higher for individuals with higher net income. \n",
- "\n",
- "
\n",
- " \n",
- "Further digging suggests that not only was the system unfair to people across the axis of income, it was also unfair across the axis of race (see figure above). The likelihood of a property being under- or over-assessed was highly dependent on the owner's race, and that did not sit well with many homeowners.\n",
- "\n",
- "\n",
- "### Spotlight: Appeals\n",
- "\n",
- "What actually caused this to come about? A comprehensive answer goes beyond just models. At the end of the day, these are real systems that have a lot of moving parts. One of which was the **appeals system**. Homeowners are mailed the value their home assessed by CCAO, and the homeowner can choose to appeal to a board of elected officials to try and change the listed value of their home and thus how much they are taxed. In theory, this sounds like a very fair system: someone oversees the final pricing of houses rather than just an algorithm. However, it ended up exacerbating the problems. \n",
- "\n",
- "> “Appeals are a good thing,” Thomas Jaconetty, deputy assessor for valuation and appeals, said in an interview. “The goal here is fairness. We made the numbers. We can change them.”\n",
- "\n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "Here we can borrow lessons from [Critical Race Theory](https://www.britannica.com/topic/critical-race-theory). On the surface, everyone having the legal right to try and appeal is undeniable. However, not everyone has an equal ability to do so. Those who have the money to hire tax lawyers to appeal for them have a drastically higher chance of trying and succeeding (see above figure). This model is part of a deeper institutional pattern rife with potential corruption.\n",
- "\n",
- "\n",
- "
\n",
- " \n",
- "\n",
- "Homeowners who appealed were generally under-assessed relative to homeowners who did not (see above figure). Those with higher incomes pay less in property tax, tax lawyers are able to grow their business due to their role in appeals, and politicians are commonly socially connected to the aforementioned tax lawyers and wealthy homeowners. All these stakeholders have reasons to advertise the model as an integral part of a fair system. Here lies the value in asking questions: a system that seems fair on the surface may in actuality be unfair upon taking a closer look. \n",
- "\n",
- "### Human Impacts\n",
- "\n",
- "
\n",
- " \n",
- "\n",
- "The impact of the housing model extends beyond the realm of home ownership and taxation. Discriminatory practices have a long history within the United States, and the model served to perpetuate this fact. To this day, Chicago is one of the most segregated cities in the United States ([source](https://fivethirtyeight.com/features/the-most-diverse-cities-are-often-the-most-segregated/)). These factors are central to informing us, as data scientists, about what is at stake.\n",
- "\n",
- "\n",
- "### Spotlight: Intersection of Real Estate and Race\n",
- "\n",
- "Housing has been a persistent source of racial inequality throughout US history, amongst other factors. It is one of the main areas where inequalities are created and reproduced. In the beginning, [Jim Crow](https://www.history.com/topics/early-20th-century-us/jim-crow-laws) laws were explicit in forbidding people of color from schools, public utilities, etc. \n",
- "\n",
- "
\n",
- " \n",
- "\n",
- "Today, while advancements in Civil Rights have been made, the spirit of the laws are alive in many parts of the US. The real estate industry was “professionalized” in the 1920’s and 1930’s by aspiring to become a science guided by strict methods and principles outlined below:\n",
- "\n",
- "- Redlining: making it difficult or impossible to get a federally-backed mortgage to buy a house in specific neighborhoods coded as “risky” (red).\n",
- " - What made them “risky” according to the makers of these was racial composition.\n",
- " - Segregation was not only a result of federal policy, but developed by real estate professionals.\n",
- "- The methods centered on creating objective rating systems (information technologies) for the appraisal of property values which encoded **race** as a factor of valuation (see figure below),\n",
- " - This, in turn, influenced federal policy and practice.\n",
- "\n",
- "
Source: Colin Koopman, How We Became Our Data (2019) p. 137
\n",
- " \n",
- "\n",
- "\n",
- "## The Response: Cook County Open Data Initiative\n",
- "\n",
- "The response started in politics. A new assessor, Fritz Kaegi, was elected and created a new mandate with two goals: \n",
- "\n",
- "1. Distributional equity in property taxation, meaning that properties of same value treated alike during assessments.\n",
- "2. Creating a new Office of Data Science.\n",
- "\n",
- "
\n",
- " \n",
- "\n",
- "### Question/Problem Formulation\n",
- "::: {.callout-note}\n",
- "## Driving Questions\n",
- "\n",
- "- What do we want to know?\n",
- "- What problems are we trying to solve?\n",
- "- What are the hypotheses we want to test?\n",
- "- What are our metrics for success?\n",
- ":::\n",
- "\n",
- "The new Office of Data Science started by redefining their goals. \n",
- "\n",
- "1. Accurately, uniformly, and impartially assess the value of a home by\n",
- " - Following international standards (coefficient of dispersion)\n",
- " - Predicting value of all homes with as little total error as possible\n",
- "\n",
- "2. Create a robust pipeline that accurately assesses property values at scale and is fair by\n",
- " - Disrupts the circuit of corruption (Board of Review appeals process)\n",
- " - Eliminates regressivity\n",
- " - Engenders trust in the system among all stakeholders \n",
- "\n",
- "\n",
- "::: {.callout-tip}\n",
- "## Definitions: Fairness and Transparency\n",
- "The definitions, as given by the Cook County Assessor's Office, are given below: \n",
- "\n",
- "* Fairness: The ability of our pipeline to accurately assess property values, accounting for disparities in geography, information, etc. \n",
- "* Transparency: The ability of the data science department to share and explain pipeline results and decisions to both internal and external stakeholders \n",
- "\n",
- "Note how the Office defines \"fairness\" in terms of accuracy. Thus, the problem - make the system more fair - was already framed in terms amenable to a data scientist: make the assessments more accurate. \n",
- "The idea here is that if the model is more accurate it will also (perhaps necessarily) become more fair, which is a big assumption. There are, in a sense, two different problems - make accurate assessments, and make a fair system. \n",
- ":::\n",
- "\n",
- "The way the goals are defined lead us to ask the question: what does it actually mean to accurately assess property values, and what role does “scale” play?\n",
- "\n",
- "1. What is an assessment of a home’s value?\n",
- "2. What makes one assessment more accurate than another?\n",
- "3. What makes one batch of assessments more accurate than another batch?\n",
- "\n",
- "Each of the above questions leads to a slew of more questions. Considering just the first question, one answer could be that an assessment is an estimate of the value of a home. This leads to more inquiries: what is the value of a home? What determines it? How do we know? For this class, we take it to be the house's market value.\n",
- "\n",
- "### Data Acquisition and Cleaning\n",
- "::: {.callout-note}\n",
- "## Driving Questions\n",
- "\n",
- "- What data do we have, and what data do we need?\n",
- "- How will we sample more data?\n",
- "- Is our data representative of the population we want to study?\n",
- ":::\n",
- "\n",
- "The data scientists also critically examined their original sales data: \n",
- "\n",
- "
\n",
- " \n",
- "\n",
- "and asked the questions:\n",
- "\n",
- "1. How was this data collected?\n",
- "2. When was this data collected? \n",
- "3. Who collected this data?\n",
- "4. For what purposes was the data collected?\n",
- "5. How and why were particular categories created? \n",
- "\n",
- "For example, attributes can have different likelihoods of appearing in the data, and housing data in the floodplains geographic region of Chicago were less represented than other regions.\n",
- "\n",
- "The features can even be reported at different rates. Improvements in homes, which tend to increase property value, were unlikely to be reported by the homeowners.\n",
- "\n",
- "Additionally, they found that there was simply more missing data in lower income neighborhoods. \n",
- "\n",
- "### Exploratory Data Analysis\n",
- "::: {.callout-note}\n",
- "## Driving Questions\n",
- "\n",
- "- How is our data organized, and what does it contain?\n",
- "- Do we already have relevant data?\n",
- "- What are the biases, anomalies, or other issues with the data?\n",
- "- How do we transform the data to enable effective analysis?\n",
- ":::\n",
- "\n",
- "Before the modeling step, they investigated a multitude of crucial questions: \n",
- "\n",
- "1. Which attributes are most predictive of sales price?\n",
- "2. Is the data uniformly distributed? \n",
- "3. Do all neighborhoods have up to date data? Do all neighborhoods have the same granularity? \n",
- "4. Do some neighborhoods have missing or outdated data? \n",
- "\n",
- "Firstly, they found that the impact of certain features, such as bedroom number, were much more impactful in determining house value inside certain neighborhoods more than others. This informed them that different models should be used depending on the neighborhood.\n",
- "\n",
- "They also noticed that low income neighborhoods had disproportionately spottier data. This informed them that they needed to develop new data collection practices - including finding new sources of data. \n",
- "\n",
- "\n",
- "\n",
- "### Prediction and Inference\n",
- "::: {.callout-note}\n",
- "## Driving Questions\n",
- "\n",
- "- What does the data say about the world?\n",
- "- Does it answer our questions or accurately solve the problem?\n",
- "- How robust are our conclusions and can we trust the predictions? \n",
- ":::\n",
- "\n",
- "Rather than using a singular model to predict sale prices (“fair market value”) of unsold properties, the CCAO fit machine learning models that discover patterns using known sale prices and characteristics of **similar and nearby properties**. It uses different model weights for each township.\n",
- "\n",
- "Compared to traditional mass appraisal, the CCAO’s new approach is more granular and more sensitive to neighborhood variations. \n",
- "\n",
- "Here, we might ask why should any particular individual believe that the model is accurate for their property?\n",
- "\n",
- "This leads us to recognize that the CCAO counts on its performance of “transparency” (putting data, models, pipeline onto GitLab) to foster public trust, which would help it equate the production of “accurate assessments” with “fairness”.\n",
- "\n",
- "There's a lot more to be said here on the relationship between accuracy, fairness, and metrics we tend to use when evaluating our models. Given the nuanced nature of the argument, it is recommended you view the corresponding lecture as the course notes are not as comprehensive for this portion of lecture.\n",
- "\n",
- "### Reports Decisions, and Conclusions\n",
- "::: {.callout-note}\n",
- "## Driving Questions\n",
- "\n",
- "- How successful is the system for each goal?\n",
- " - Accuracy/uniformity of the model\n",
- " - Fairness and transparency that eliminates regressivity and engenders trust\n",
- "- How do you know? \n",
- ":::\n",
- "\n",
- "The model is not the end of the road. The new Office still sends homeowners their house evaluations, but now the data that they get sent back from the homeowners is taken into account. More detailed reports are being written by the Office itself to democratize the information. Town halls and other public facing outreach helps involves the whole community in the process of housing evaluations, rather than limiting participation to a select few.\n",
- "\n",
- "## Key Takeaways\n",
- "\n",
- "1. Accuracy is a necessary, but not sufficient, condition of a fair system.\n",
- "\n",
- "2. Fairness and transparency are context-dependent and sociotechnical concepts.\n",
- "\n",
- "3. Learn to work with contexts, and consider how your data analysis will reshape them.\n",
- "\n",
- "4. Keep in mind the power, and limits, of data analysis.\n",
- "\n",
- "\n",
- "\n",
- "## Lessons for Data Science Practice\n",
- "\n",
- "1. Question/Problem Formulation\n",
- "\n",
- " - Who is responsible for framing the problem?\n",
- " - Who are the stakeholders? How are they involved in the problem framing?\n",
- " - What do you bring to the table? How does your positionality affect your understanding of the problem?\n",
- " - What are the narratives that you're tapping into? \n",
- "\n",
- "2. Data Acquisition and Cleaning\n",
- "\n",
- " - Where does the data come from?\n",
- " - Who collected it? For what purpose?\n",
- " - What kinds of collecting and recording systems and techniques were used? \n",
- " - How has this data been used in the past?\n",
- " - What restrictions are there on access to the data, and what enables you to have access?\n",
- "\n",
- "3. Exploratory Data Analysis & Visualization\n",
- "\n",
- " - What kind of personal or group identities have become salient in this data? \n",
- " - Which variables became salient, and what kinds of relationship obtain between them? \n",
- " - Do any of the relationships made visible lend themselves to arguments that might be potentially harmful to a particular community?\n",
- "\n",
- "4. Prediction and Inference\n",
- "\n",
- " - What does the prediction or inference do in the world?\n",
- " - Are the results useful for the intended purposes?\n",
- " - Are there benchmarks to compare the results?\n",
- " - How are your predictions and inferences dependent upon the larger system in which your model works?\n",
- "\n",
- "5. Reports, Decisions, and Solutions\n",
- "\n",
- " - How do we know if we have accomplished our goals?\n",
- " - How does your work fit in the broader literature? \n",
- " - Where does your work agree or disagree with the status quo?\n",
- " - Do your conclusions make sense?\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.9.16"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
-}
diff --git a/cv_regularization/cv_reg.ipynb b/cv_regularization/cv_reg.ipynb
deleted file mode 100644
index f9be1e28..00000000
--- a/cv_regularization/cv_reg.ipynb
+++ /dev/null
@@ -1,635 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "raw",
- "metadata": {},
- "source": [
- "---\n",
- "title: Cross Validation and Regularization\n",
- "format:\n",
- " html:\n",
- " toc: true\n",
- " toc-depth: 5\n",
- " toc-location: right\n",
- " code-fold: false\n",
- " theme:\n",
- " - cosmo\n",
- " - cerulean\n",
- " callout-icon: false\n",
- "---"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "::: {.callout-note collapse=\"false\"}\n",
- "## Learning Outcomes\n",
- "* Recognize the need for validation and test sets to preview model performance on unseen data\n",
- "* Apply cross-validation to select model hyperparameters\n",
- "* Understand the conceptual basis for L1 and L2 regularization\n",
- ":::\n",
- "\n",
- "At the end of the Feature Engineering lecture (Lecture 14), we arrived at the issue of fine-tuning model complexity. We identified that a model that's too complex can lead to overfitting, while a model that's too simple can lead to underfitting. This brings us to a natural question: how do we control model complexity to avoid under- and overfitting? \n",
- "\n",
- "To answer this question, we will need to address two things: first, we need to understand *when* our model begins to overfit by assessing its performance on unseen data. We can achieve this through **cross-validation**. Secondly, we need to introduce a technique to adjust the complexity of our models ourselves – to do so, we will apply **regularization**.\n",
- "\n",
- "## Training, Test, and Validation Sets\n",
- "\n",
- "From the last lecture, we learned that *increasing* model complexity *decreased* our model's training error but *increased* its variance. This makes intuitive sense: adding more features causes our model to fit more closely to data it encountered during training, but generalize worse to new data it hasn't seen before. For this reason, a low training error is not always representative of our model's underlying performance - we need to also assess how well it performs on unseen data to ensure that it is not overfitting.\n",
- "\n",
- "Truly, the only way to know when our model overfits is by evaluating it on unseen data. Unfortunately, that means we need to wait for more data. This may be very expensive and time-consuming.\n",
- "\n",
- "How should we proceed? In this section, we will build up a viable solution to this problem.\n",
- "\n",
- "### Test Sets\n",
- "\n",
- "The simplest approach to avoid overfitting is to keep some of our data \"secret\" from ourselves. We can set aside a random portion of our full dataset to use only for testing purposes. The datapoints in this **test set** will *not* be used in the model fitting process. Instead, we will:\n",
- "\n",
- "* Use the remaining portion of our dataset – now called the **training set** – to run ordinary least squares, gradient descent, or some other technique to fit model parameters\n",
- "* Take the fitted model and use it to make predictions on datapoints in the test set. The model's performance on the test set (expressed as the MSE, RMSE, etc.) is now indicative of how well it can make predictions on unseen data\n",
- "\n",
- "Importantly, the optimal model parameters were found by *only* considering the data in the training set. After the model has been fitted to the training data, we do not change any parameters before making predictions on the test set. Importantly, we only ever make predictions on the test set **once** after all model design has been completely finalized. We treat the test set performance as the final test of how well a model does.\n",
- "\n",
- "The process of sub-dividing our dataset into training and test sets is known as a **train-test split**. Typically, between 10% and 20% of the data is allocated to the test set.\n",
- "\n",
- "
\n",
- "\n",
- "In `sklearn`, the `train_test_split` function of the `model_selection` module allows us to automatically generate train-test splits. \n",
- "\n",
- "Throughout today's work, we will work with the `vehicles` dataset from previous lectures. As before, we will attempt to predict the `mpg` of a vehicle from transformations of its `hp`. In the cell below, we allocate 20% of the full dataset to testing, and the remaining 80% to training."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "#| code-fold: true\n",
- "import pandas as pd\n",
- "import numpy as np\n",
- "import seaborn as sns\n",
- "import warnings\n",
- "warnings.filterwarnings('ignore')\n",
- "\n",
- "# Load the dataset and construct the design matrix\n",
- "vehicles = sns.load_dataset(\"mpg\").rename(columns={\"horsepower\":\"hp\"}).dropna()\n",
- "X = vehicles[[\"hp\"]]\n",
- "X[\"hp^2\"] = vehicles[\"hp\"]**2\n",
- "X[\"hp^3\"] = vehicles[\"hp\"]**3\n",
- "X[\"hp^4\"] = vehicles[\"hp\"]**4\n",
- "\n",
- "Y = vehicles[\"mpg\"]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Size of full dataset: 392 points\n",
- "Size of training set: 313 points\n",
- "Size of test set: 79 points\n"
- ]
- }
- ],
- "source": [
- "from sklearn.model_selection import train_test_split\n",
- "\n",
- "# `test_size` specifies the proportion of the full dataset that should be allocated to testing\n",
- "# `random_state` makes our results reproducible for educational purposes\n",
- "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=220)\n",
- "\n",
- "print(f\"Size of full dataset: {X.shape[0]} points\")\n",
- "print(f\"Size of training set: {X_train.shape[0]} points\")\n",
- "print(f\"Size of test set: {X_test.shape[0]} points\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "After performing our train-test split, we fit a model to the training set and assess its performance on the test set."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "import sklearn.linear_model as lm\n",
- "from sklearn.metrics import mean_squared_error\n",
- "\n",
- "model = lm.LinearRegression()\n",
- "\n",
- "# Fit to the training set\n",
- "model.fit(X_train, Y_train)\n",
- "\n",
- "# Make predictions on the test set\n",
- "test_predictions = model.predict(X_test)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Validation Sets\n",
- "\n",
- "Now, what if we were dissatisfied with our test set performance? With our current framework, we'd be stuck. As outlined previously, assessing model performance on the test set is the *final* stage of the model design process. We can't go back and adjust our model based on the new discovery that it is overfitting – if we did, then we would be *factoring in information from the test set* to design our model. The test error would no longer be a true representation of the model's performance on unseen data! \n",
- "\n",
- "Our solution is to introduce a **validation set**. A validation set is a random portion of the *training set* that is set aside for assessing model performance while the model is *still being developed*. The process for using a validation set is:\n",
- "\n",
- "* Perform a train-test split. Set the test set aside; we will not touch it until the very end of the model design process.\n",
- "* Set aside a portion of the training set to be used for validation.\n",
- "* Fit the model parameters to the datapoints contained in the remaining portion of the training set.\n",
- "* Assess the model's performance on the validation set. Adjust the model as needed, re-fit it to the remaining portion of the training set, then re-evaluate it on the validation set. Repeat as necessary until you are satisfied.\n",
- "* After *all* model development is complete, assess the model's performance on the test set. This is the final test of how well the model performs on unseen data. No further modifications should be made to the model.\n",
- "\n",
- "The process of creating a validation set is called a **validation split**.\n",
- "\n",
- "
\n",
- "\n",
- "Note that the validation error behaves quite differently from the training error explored previously. Recall that the training error decreased monotonically with increasing model degree – as the model became more complex, it made better and better predictions on the training data. The validation error, in contrast, decreases *then increases* as we increase model complexity. This reflects the transition from under- to overfitting. At low model complexity, the model underfits because it is not complex enough to capture the main trends in the data. At high model complexity, the model overfits because it \"memorizes\" the training data too closely.\n",
- "\n",
- "We can update our understanding of the relationships between error, complexity, and model variance:\n",
- "\n",
- "
\n",
- "\n",
- "Our goal is to train a model with complexity near the orange dotted line – this is where our model achieves minimum error on the validation set. Note that this relationship is a simplification of the real-world. But for the purposes of Data 100, this is good enough.\n",
- "\n",
- "## K-Fold Cross-Validation\n",
- "\n",
- "Introducing a validation set gave us an \"extra\" chance to assess model performance on another set of unseen data. We are able to finetune the model design based on its performance on this one set of validation data.\n",
- "\n",
- "But what if, by random chance, our validation set just happened to contain many outliers? It is possible that the validation datapoints we set aside do not actually represent other unseen data that the model might encounter. Ideally, we would like to validate our model's performance on several different unseen datasets. This would give us greater confidence in our understanding of how the model behaves on new data.\n",
- "\n",
- "Let's think back to our validation framework. Earlier, we set aside x% of our training data (say, 20%) to use for validation. \n",
- "\n",
- "
\n",
- "\n",
- "In the example above, we set aside the first 20% of training datapoints for the validation set. This was an arbitrary choice. We could have set aside *any* 20% portion of the training data for validation. In fact, there are 5 non-overlapping \"chunks\" of training points that we could have designated as the validation set.\n",
- "\n",
- "
\n",
- "\n",
- "The common term for one of these chunks is a **fold**. In the example above, we had 5 folds, each containing 20% of the training data. This gives us a new perspective: we really have *5* validation sets \"hidden\" in our training set. \n",
- "\n",
- "In **cross-validation**, we perform validation splits for each fold in the training set. For a dataset with $K$ folds, we:\n",
- "\n",
- "* Pick one fold to be the validation fold\n",
- "* Fit the model to training data from every fold *other* than the validation fold\n",
- "* Compute the model's error on the validation fold and record it\n",
- "* Repeat for all $K$ folds\n",
- "\n",
- "The **cross-validation error** is then the *average* error across all $K$ validation folds. \n",
- "
\n",
- "\n",
- "### Model Selection Workflow\n",
- "At this stage, we have refined our model selection workflow. We begin by performing a train-test split to set aside a test set for the final evaluation of model performance. Then, we alternate between adjusting our design matrix and computing the cross-validation error to finetune the model's design. In the example below, we illustrate the use of 4-fold cross-validation to help inform model design.\n",
- "\n",
- "
\n",
- "\n",
- "### Hyperparameters\n",
- "An important use of cross-validation is for **hyperparameter** selection. A hyperparameter is some value in a model that is chosen *before* the model is fit to any data. This means that it is distinct from the model *parameters* $\\theta_i$ because its value is selected before the training process begins. We cannot use our usual techniques – calculus, ordinary least squares, or gradient descent – to choose its value. Instead, we must decide it ourselves. \n",
- "\n",
- "Some examples of hyperparameters in Data 100 are:\n",
- "\n",
- "* The degree of our polynomial model (recall that we selected the degree before creating our design matrix and calling `.fit`)\n",
- "* The learning rate, $\\alpha$, in gradient descent\n",
- "* The regularization penalty, $\\lambda$ (to be introduced later this lecture)\n",
- "\n",
- "To select a hyperparameter value via cross-validation, we first list out several \"guesses\" for what the best hyperparameter may be. For each guess, we then run cross-validation to compute the cross-validation error incurred by the model when using that choice of hyperparameter value. We then select the value of the hyperparameter that resulted in the lowest cross-validation error. \n",
- "\n",
- "For example, we may wish to use cross-validation to decide what value we should use for $\\alpha$, which controls the step size of each gradient descent update. To do so, we list out some possible guesses for the best $\\alpha$: 0.1, 1, and 10. For each possible value, we perform cross-validation to see what error the model has *when we use that value of $\\alpha$ to train it*.\n",
- "\n",
- "
\n",
- "\n",
- "## Regularization\n",
- "\n",
- "We've now addressed the first of our two goals for today: creating a framework to assess model performance on unseen data. Now, we'll discuss our second objective: developing a technique to adjust model complexity. This will allow us to directly tackle the issues of under- and overfitting.\n",
- "\n",
- "Earlier, we adjusted the complexity of our polynomial model by tuning a hyperparameter – the degree of the polynomial. We trialed several different polynomial degrees, computed the validation error for each, and selected the value that minimized the validation error. Tweaking the \"complexity\" was simple; it was only a matter of adjusting the polynomial degree.\n",
- "\n",
- "In most machine learning problems, complexity is defined differently from what we have seen so far. Today, we'll explore two different definitions of complexity: the *squared* and *absolute* magnitude of $\\theta_i$ coefficients.\n",
- "\n",
- "### Constraining Model Parameters\n",
- "\n",
- "Think back to our work using gradient descent to descend down a loss surface. You may find it helpful to refer back to the Gradient Descent note to refresh your memory. Our aim was to find the combination of model parameters that led to the model having minimum loss. We visualized this using a contour map by plotting possible parameter values on the horizontal and vertical axes, which allows us to take a bird's eye view above the loss surface. We want to find the model parameters corresponding to the lowest point on the loss surface.\n",
- "\n",
- "
\n",
- "\n",
- "Let's review our current modeling framework.\n",
- "\n",
- "$$\\hat{\\mathbb{Y}} = \\theta_0 + \\theta_1 \\phi_1 + \\theta_2 \\phi_2 + \\ldots + \\theta_p \\phi_p$$\n",
- "\n",
- "Recall that we represent our features with $\\phi_i$ to reflect the fact that we have performed feature engineering. \n",
- "\n",
- "Previously, we restricted model complexity by limiting the total number of features present in the model. We only included a limited number of polynomial features at a time; all other polynomials were excluded from the model.\n",
- "\n",
- "What if, instead of fully removing particular features, we kept all features and used each one only a \"little bit\"? If we put a limit on how *much* each feature can contribute to the predictions, we can still control the model's complexity without the need to manually determine how many features should be removed. \n",
- "\n",
- "What do we mean by a \"little bit\"? Consider the case where some parameter $\\theta_i$ is close to or equal to 0. Then, feature $\\phi_i$ barely impacts the prediction – the feature is weighted by such a small value that its presence doesn't significantly change the value of $\\hat{\\mathbb{Y}}$. If we restrict how large each parameter $\\theta_i$ can be, we restrict how much feature $\\phi_i$ contributes to the model. This has the effect of *reducing* model complexity.\n",
- "\n",
- "In **regularization**, we restrict model complexity by *putting a limit* on the magnitudes of the model parameters $\\theta_i$. \n",
- "\n",
- "What do these limits look like? Suppose we specify that the sum of all absolute parameter values can be no greater than some number $Q$. In other words:\n",
- "\n",
- "$$\\sum_{i=1}^p |\\theta_i| \\leq Q$$\n",
- "\n",
- "where $p$ is the total number of parameters in the model. You can think of this as us giving our model a \"budget\" for how it distributes the magnitudes of each parameter. If the model assigns a large value to some $\\theta_i$, it may have to assign a small value to some other $\\theta_j$. This has the effect of increasing feature $\\phi_i$'s influence on the predictions while decreasing the influence of feature $\\phi_j$. The model will need to be strategic about how the parameter weights are distributed – ideally, more \"important\" features will receive greater weighting. \n",
- "\n",
- "Notice that the intercept term, $\\theta_0$, is excluded from this constraint. **We typically do not regularize the intercept term**.\n",
- "\n",
- "Now, let's think back to gradient descent and visualize the loss surface as a contour map. As a refresher, a loss surface means that each point represents the model's loss for a particular combination of $\\theta_1$, $\\theta_2$. Let's say our goal is to find the combination of parameters that gives us the lowest loss. \n",
- "\n",
- "
\n",
- "\n",
- "With no constraint, the optimal $\\hat{\\theta}$ is in the center. \n",
- "\n",
- "Applying this constraint limits what combinations of model parameters are valid. We can now only consider parameter combinations with a total absolute sum less than or equal to our number $Q$. This means that we can only assign our *regularized* parameter vector $\\hat{\\theta}_{\\text{Reg}}$ to positions in the green diamond below.\n",
- "\n",
- "
\n",
- "\n",
- "We can no longer select the parameter vector that *truly* minimizes the loss surface, $\\hat{\\theta}_{\\text{No Reg}}$, because this combination of parameters does not lie within our allowed region. Instead, we select whatever allowable combination brings us *closest* to the true minimum loss.\n",
- "\n",
- "
\n",
- "\n",
- "Notice that, under regularization, our optimized $\\theta_1$ and $\\theta_2$ values are much smaller than they were without regularization (indeed, $\\theta_1$ has decreased to 0). The model has *decreased in complexity* because we have limited how much our features contribute to the model. In fact, by setting its parameter to 0, we have effectively removed the influence of feature $\\phi_1$ from the model altogether. \n",
- "\n",
- "If we change the value of $Q$, we change the region of allowed parameter combinations. The model will still choose the combination of parameters that produces the lowest loss – the closest point in the constrained region to the true minimizer, $\\hat{\\theta}_{\\text{No Reg}}$.\n",
- "\n",
- "If we make $Q$ smaller:\n",
- "
\n",
- "\n",
- "If we make $Q$ larger: \n",
- "
\n",
- "\n",
- "* When $Q$ is small, we severely restrict the size of our parameters. $\\theta_i$s are small in value, and features $\\phi_i$ only contribute a little to the model. The allowed region of model parameters contracts, and the model becomes much simpler.\n",
- "* When $Q$ is large, we do not restrict our parameter sizes by much. $\\theta_i$s are large in value, and features $\\phi_i$ contribute more to the model. The allowed region of model parameters expands, and the model becomes more complex.\n",
- "\n",
- "Consider the extreme case of when $Q$ is extremely large. In this situation, our restriction has essentially no effect, and the allowed region includes the OLS solution!\n",
- "\n",
- "
\n",
- "\n",
- "\n",
- "Now what if $Q$ were very small? Our parameters are then set to (essentially 0). If the model has no intercept term: $\\hat{\\mathbb{Y}} = (0)\\phi_1 + (0)\\phi_2 + \\ldots = 0$. And if the model has an intercept term: $\\hat{\\mathbb{Y}} = (0)\\phi_1 + (0)\\phi_2 + \\ldots = \\theta_0$. Remember that the intercept term is excluded from the constraint - this is so we avoid the situation where we always predict 0.\n",
- "\n",
- "Let's summarize what we have seen. \n",
- "\n",
- "
\n",
- "\n",
- "## L1 (LASSO) Regularization\n",
- "\n",
- "How do we actually apply our constraint $\\sum_{i=1}^p |\\theta_i| \\leq Q$? We will do so by modifying the *objective function* that we seek to minimize when fitting a model.\n",
- "\n",
- "Recall our ordinary least squares objective function: our goal was to find parameters that minimize the model's mean squared error.\n",
- "\n",
- "$$\\frac{1}{n} \\sum_{i=1}^n (y_i - \\hat{y}_i)^2 = \\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 \\phi_{i, 1} + \\theta_2 \\phi_{i, 2} + \\ldots + \\theta_p \\phi_{i, p}))^2$$\n",
- "\n",
- "To apply our constraint, we need to rephrase our minimization goal. \n",
- "\n",
- "$$\\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 \\phi_{i, 1} + \\theta_2 \\phi_{i, 2} + \\ldots + \\theta_p \\phi_{i, p}))^2\\:\\text{such that} \\sum_{i=1}^p |\\theta_i| \\leq Q$$\n",
- "\n",
- "Unfortunately, we can't directly use this formulation as our objective function – it's not easy to mathematically optimize over a constraint. Instead, we will apply the magic of the [Lagrangian Duality](https://en.wikipedia.org/wiki/Duality_(optimization)). The details of this are out of scope (take EECS 127 if you're interested in learning more), but the end result is very useful. It turns out that minimizing the following *augmented* objective function is *equivalent* to our minimization goal above.\n",
- "\n",
- "$$\\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 \\phi_{i, 1} + \\theta_2 \\phi_{i, 2} + \\ldots + \\theta_p \\phi_{i, p}))^2 + \\lambda \\sum_{i=1}^p \\vert \\theta_i \\vert = ||\\mathbb{Y} - \\mathbb{X}\\theta||_2^2 + \\lambda \\sum_{i=1}^p |\\theta_i|$$\n",
- "\n",
- "The second of these two expressions includes the MSE expressed using vector notation.\n",
- "\n",
- "Notice that we've replaced the constraint with a second term in our objective function. We're now minimizing a function with an additional regularization term that *penalizes large coefficients*. In order to minimize this new objective function, we'll end up balancing two components:\n",
- "\n",
- "* Keep the model's error on the training data low, represented by the term $\\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 x_{i, 1} + \\theta_2 x_{i, 2} + \\ldots + \\theta_p x_{i, p}))^2$\n",
- "* At the same time, keep the magnitudes of model parameters low, represented by the term $\\lambda \\sum_{i=1}^p |\\theta_i|$\n",
- "\n",
- "The $\\lambda$ factor controls the degree of regularization. Roughly speaking, $\\lambda$ is related to our $Q$ constraint from before by the rule $\\lambda \\approx \\frac{1}{Q}$. To understand why, let's consider two extreme examples:\n",
- "\n",
- "- Assume $\\lambda \\rightarrow \\infty$. Then, $\\lambda \\sum_{j=1}^{d} \\vert \\theta_j \\vert$ dominates the cost function. To minimize this term, we set $\\theta_j = 0$ for all $j \\ge 1$. This is a very constrained model that is mathematically equivalent to the constant model. Earlier, we explained the constant model also arises when the L2 norm ball radius $Q \\rightarrow 0$.\n",
- "\n",
- "- Assume $\\lambda \\rightarrow 0$. Then, $\\lambda \\sum_{j=1}^{d} \\vert \\theta_j \\vert$ is 0. Minimizing the cost function is equivalent to $\\min_{\\theta} \\frac{1}{n} || Y - X\\theta ||_2^2$, our usual MSE loss function. The act of minimizing MSE loss is just our familiar OLS, and the optimal solution is the global minimum $\\hat{\\theta} = \\hat\\theta_{No Reg.}$. We showed that the global optimum is achieved when the L2 norm ball radius $Q \\rightarrow \\infty$.\n",
- "\n",
- "We call $\\lambda$ the **regularization penalty hyperparameter** and select its value via cross-validation.\n",
- "\n",
- "The process of finding the optimal $\\hat{\\theta}$ to minimize our new objective function is called **L1 regularization**. It is also sometimes known by the acronym \"LASSO\", which stands for \"Least Absolute Shrinkage and Selection Operator.\"\n",
- "\n",
- "Unlike ordinary least squares, which can be solved via the closed-form solution $\\hat{\\theta}_{OLS} = (\\mathbb{X}^{\\top}\\mathbb{X})^{-1}\\mathbb{X}^{\\top}\\mathbb{Y}$, there is no closed-form solution for the optimal parameter vector under L1 regularization. Instead, we use the `Lasso` model class of `sklearn`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([-2.54932056e-01, -9.48597165e-04, 8.91976284e-06, -1.22872290e-08])"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "import sklearn.linear_model as lm\n",
- "\n",
- "# The alpha parameter represents our lambda term\n",
- "lasso_model = lm.Lasso(alpha=2)\n",
- "lasso_model.fit(X_train, Y_train)\n",
- "\n",
- "lasso_model.coef_"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Notice that all model coefficients are very small in magnitude. In fact, some of them are so small that they are essentially 0. An important characteristic of L1 regularization is that many model parameters are set to 0. In other words, LASSO effectively **selects only a subset** of the features. The reason for this comes back to our loss surface and allowed \"diamond\" regions from earlier – we can often get closer to the lowest loss contour at a corner of the diamond than along an edge. \n",
- "\n",
- "When a model parameter is set to 0 or close to 0, its corresponding feature is essentially removed from the model. We say that L1 regularization performs **feature selection** because, by setting the parameters of unimportant features to 0, LASSO \"selects\" which features are more useful for modeling. \n",
- "\n",
- "## Scaling Features for Regularization\n",
- "\n",
- "The regularization procedure we just performed had one subtle issue. To see what it is, let's take a look at the design matrix for our `lasso_model`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "
\n",
- "\n",
- "
\n",
- " \n",
- "
\n",
- "
\n",
- "
hp
\n",
- "
hp^2
\n",
- "
hp^3
\n",
- "
hp^4
\n",
- "
\n",
- " \n",
- " \n",
- "
\n",
- "
259
\n",
- "
85.0
\n",
- "
7225.0
\n",
- "
614125.0
\n",
- "
52200625.0
\n",
- "
\n",
- "
\n",
- "
129
\n",
- "
67.0
\n",
- "
4489.0
\n",
- "
300763.0
\n",
- "
20151121.0
\n",
- "
\n",
- "
\n",
- "
207
\n",
- "
102.0
\n",
- "
10404.0
\n",
- "
1061208.0
\n",
- "
108243216.0
\n",
- "
\n",
- "
\n",
- "
302
\n",
- "
70.0
\n",
- "
4900.0
\n",
- "
343000.0
\n",
- "
24010000.0
\n",
- "
\n",
- "
\n",
- "
71
\n",
- "
97.0
\n",
- "
9409.0
\n",
- "
912673.0
\n",
- "
88529281.0
\n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " hp hp^2 hp^3 hp^4\n",
- "259 85.0 7225.0 614125.0 52200625.0\n",
- "129 67.0 4489.0 300763.0 20151121.0\n",
- "207 102.0 10404.0 1061208.0 108243216.0\n",
- "302 70.0 4900.0 343000.0 24010000.0\n",
- "71 97.0 9409.0 912673.0 88529281.0"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "X_train.head()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Our features – `hp`, `hp^2`, `hp^3`, and `hp^4` – are on drastically different numeric scales! The values contained in `hp^4` are orders of magnitude larger than those contained in `hp`. This can be a problem because the value of `hp^4` will naturally contribute more to each predicted $\\hat{y}$ because it is so much greater than the values of the other features. For `hp` to have much of an impact at all on the prediction, it must be scaled by a large model parameter. \n",
- "\n",
- "By inspecting the fitted parameters of our model, we see that this is the case – the parameter for `hp` is much larger in magnitude than the parameter for `hp^4`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "
\n",
- "\n",
- "
\n",
- " \n",
- "
\n",
- "
\n",
- "
Feature
\n",
- "
Parameter
\n",
- "
\n",
- " \n",
- " \n",
- "
\n",
- "
0
\n",
- "
hp
\n",
- "
-2.549321e-01
\n",
- "
\n",
- "
\n",
- "
1
\n",
- "
hp^2
\n",
- "
-9.485972e-04
\n",
- "
\n",
- "
\n",
- "
2
\n",
- "
hp^3
\n",
- "
8.919763e-06
\n",
- "
\n",
- "
\n",
- "
3
\n",
- "
hp^4
\n",
- "
-1.228723e-08
\n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Feature Parameter\n",
- "0 hp -2.549321e-01\n",
- "1 hp^2 -9.485972e-04\n",
- "2 hp^3 8.919763e-06\n",
- "3 hp^4 -1.228723e-08"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pd.DataFrame({\"Feature\":X_train.columns, \"Parameter\":lasso_model.coef_})"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Recall that by applying regularization, we give our a model a \"budget\" for how it can allocate the values of model parameters. For `hp` to have much of an impact on each prediction, LASSO is forced to \"spend\" more of this budget on the parameter for `hp`.\n",
- "\n",
- "We can avoid this issue by **scaling** the data before regularizing. This is a process where we convert all features to the same numeric scale. A common way to scale data is to perform **standardization** such that all features have mean 0 and standard deviation 1; essentially, we replace everything with its Z-score.\n",
- "\n",
- "$$z_k = \\frac{x_k - \\mu_k}{\\sigma_k}$$\n",
- "\n",
- "## L2 (Ridge) Regularization\n",
- "\n",
- "In all of our work above, we considered the constraint $\\sum_{i=1}^p |\\theta_i| \\leq Q$ to limit the complexity of the model. What if we had applied a different constraint?\n",
- "\n",
- "In **L2 regularization**, also known as **ridge regression**, we constrain the model such that the sum of the *squared* parameters must be less than some number $Q$. This constraint takes the form:\n",
- "\n",
- "$$\\sum_{i=1}^p \\theta_i^2 \\leq Q$$\n",
- "\n",
- "As before, we typically do not regularize the intercept term. \n",
- "\n",
- "The allowed region of parameters for a given value of $Q$ is now shaped like a ball.\n",
- "\n",
- "
\n",
- "\n",
- "If we modify our objective function like before, we find that our new goal is to minimize the function:\n",
- "$$\\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 \\phi_{i, 1} + \\theta_2 \\phi_{i, 2} + \\ldots + \\theta_p \\phi_{i, p}))^2\\:\\text{such that} \\sum_{i=1}^p \\theta_i^2 \\leq Q$$\n",
- "\n",
- "Notice that all we have done is change the constraint on the model parameters. The first term in the expression, the MSE, has not changed.\n",
- "\n",
- "Using Lagrangian Duality, we can re-express our objective function as:\n",
- "$$\\frac{1}{n} \\sum_{i=1}^n (y_i - (\\theta_0 + \\theta_1 \\phi_{i, 1} + \\theta_2 \\phi_{i, 2} + \\ldots + \\theta_p \\phi_{i, p}))^2 + \\lambda \\sum_{i=1}^p \\theta_i^2 = ||\\mathbb{Y} - \\mathbb{X}\\theta||_2^2 + \\lambda \\sum_{i=1}^p \\theta_i^2$$\n",
- "\n",
- "When applying L2 regularization, our goal is to minimize this updated objective function.\n",
- "\n",
- "Unlike L1 regularization, L2 regularization *does* have a closed-form solution for the best parameter vector when regularization is applied:\n",
- "\n",
- "$$\\hat\\theta_{\\text{ridge}} = (\\mathbb{X}^{\\top}\\mathbb{X} + n\\lambda I)^{-1}\\mathbb{X}^{\\top}\\mathbb{Y}$$\n",
- "\n",
- "This solution exists **even if $\\mathbb{X}$ is not full column rank**. This is a major reason why L2 regularization is often used – it can produce a solution even when there is colinearity in the features. We will discuss the concept of colinearity in a future lecture. We will not derive this result in Data 100, as it involves a fair bit of matrix calculus.\n",
- "\n",
- "In `sklearn`, we perform L2 regularization using the `Ridge` class. Notice that we scale the data before regularizing."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 5.89130559e-02, -6.42445915e-03, 4.44468157e-05, -8.83981945e-08])"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "ridge_model = lm.Ridge(alpha=1) # alpha represents the hyperparameter lambda\n",
- "ridge_model.fit(X_train, Y_train)\n",
- "\n",
- "ridge_model.coef_"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Regression Summary\n",
- "\n",
- "Our regression models are summarized below. Note the objective function is what the gradient descent optimizer minimizes. \n",
- "\n",
- "| Type | Model | Loss | Regularization | Objective Function | Solution |\n",
- "|-----------------|----------------------------------------|---------------|----------------|-------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|\n",
- "| OLS | $\\hat{\\mathbb{Y}} = \\mathbb{X}\\theta$ | MSE | None | $\\frac{1}{n} \\|\\mathbb{Y}-\\mathbb{X} \\theta\\|^2_2$ | $\\hat{\\theta}_{OLS} = (\\mathbb{X}^{\\top}\\mathbb{X})^{-1}\\mathbb{X}^{\\top}\\mathbb{Y}$ if $\\mathbb{X}$ is full column rank |\n",
- "| Ridge | $\\hat{\\mathbb{Y}} = \\mathbb{X} \\theta$ | MSE | L2 | $\\frac{1}{n} \\|\\mathbb{Y}-\\mathbb{X}\\theta\\|^2_2 + \\lambda \\sum_{i=1}^p \\theta_i^2$ | $\\hat{\\theta}_{ridge} = (\\mathbb{X}^{\\top}\\mathbb{X} + n \\lambda I)^{-1}\\mathbb{X}^{\\top}\\mathbb{Y}$ |\n",
- "| LASSO | $\\hat{\\mathbb{Y}} = \\mathbb{X} \\theta$ | MSE | L1 | $\\frac{1}{n} \\|\\mathbb{Y}-\\mathbb{X}\\theta\\|^2_2 + \\lambda \\sum_{i=1}^p \\vert \\theta_i \\vert$ | No closed form | |\n"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.9.16"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
-}
diff --git a/docs/case_study_HCE/case_study_HCE.html b/docs/case_study_HCE/case_study_HCE.html
new file mode 100644
index 00000000..0d93c77a
--- /dev/null
+++ b/docs/case_study_HCE/case_study_HCE.html
@@ -0,0 +1,1214 @@
+
+
+
+
+
+
+
+
+
+Principles and Techniques of Data Science - 15 Case Study in Human Contexts and Ethics
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Learn about the ethical dilemmas that data scientists face.
+
Know how critique models using contextual knowledge about data.
+
+
+
+
+
+
Disclaimer: The following chapter discusses issues of structural racism. Some of the items in the chapter may be sensitive and may or may not be the opinions, ideas, and beliefs of the students who collected the materials. The Data 100 course staff tries its best to only present information that is relevant for teaching the lessons at hand.
+
+
Note: Given the nuanced nature of some of the arguments made in the lecture, it is highly recommended that you view the lecture recording in order to fully engage and understand the material. The course notes will have the same broader structure but are by no means comprehensive.
+
Let’s immerse ourselves in the real-world story of data scientists working for an organization called the Cook County Assessor’s Office (CCAO). Their job is to estimate the values of houses in order to assign property taxes. This is because the tax burden in this area is determined by the estimated value of a house, which is different from its price. Since values change over time and there are no obvious indicators of value, they created a model to estimate the values of houses. In this chapter, we will dig deep into what problems biased the models, the consequences to human lives, and how we can learn from this example to do better.
+
+
15.1 The Problem
+
A report by the Chicago Tribune uncovered a major scandal: the team showed that the model perpetuated a highly regressive tax system that disproportionately burdened African-American and Latinx homeowners in Cook County. How did they know?
+
+
+
+
In the field of housing assessment, there are standard metrics that assessors use across the world to estimate the fairness of assessments: coefficient of dispersion and price-related differential. These metrics have been rigorously tested by experts in the field and are out of scope for our class. Calculating these metrics for the Cook County prices revealed that the pricing created by the CCAO did not fall in acceptable ranges (see figure above). This by itself is not the end of the story, but a good indicator that something fishy was going on.
+
+
+
+
This prompted them to investigate if the model itself was producing fair tax rates. Evidently, when accounting for the home owner’s income, they found that the model actually produced a regressive tax rate (see figure above). A tax rate is regressive if the percentage tax rate is higher for individuals with lower net income. A tax rate is progressive if the percentage tax rate is higher for individuals with higher net income.
+
+
+
+
Further digging suggests that not only was the system unfair to people across the axis of income, it was also unfair across the axis of race (see figure above). The likelihood of a property being under- or over-assessed was highly dependent on the owner’s race, and that did not sit well with many homeowners.
+
+
15.1.1 Spotlight: Appeals
+
What actually caused this to come about? A comprehensive answer goes beyond just models. At the end of the day, these are real systems that have a lot of moving parts. One of which was the appeals system. Homeowners are mailed the value their home assessed by CCAO, and the homeowner can choose to appeal to a board of elected officials to try and change the listed value of their home and thus how much they are taxed. In theory, this sounds like a very fair system: someone oversees the final pricing of houses rather than just an algorithm. However, it ended up exacerbating the problems.
+
+
“Appeals are a good thing,” Thomas Jaconetty, deputy assessor for valuation and appeals, said in an interview. “The goal here is fairness. We made the numbers. We can change them.”
+
+
+
+
+
+
Here we can borrow lessons from Critical Race Theory. On the surface, everyone having the legal right to try and appeal is undeniable. However, not everyone has an equal ability to do so. Those who have the money to hire tax lawyers to appeal for them have a drastically higher chance of trying and succeeding (see above figure). This model is part of a deeper institutional pattern rife with potential corruption.
+
+
+
+
+
Homeowners who appealed were generally under-assessed relative to homeowners who did not (see above figure). Those with higher incomes pay less in property tax, tax lawyers are able to grow their business due to their role in appeals, and politicians are commonly socially connected to the aforementioned tax lawyers and wealthy homeowners. All these stakeholders have reasons to advertise the model as an integral part of a fair system. Here lies the value in asking questions: a system that seems fair on the surface may in actuality be unfair upon taking a closer look.
+
+
+
15.1.2 Human Impacts
+
+
+
+
+
The impact of the housing model extends beyond the realm of home ownership and taxation. Discriminatory practices have a long history within the United States, and the model served to perpetuate this fact. To this day, Chicago is one of the most segregated cities in the United States (source). These factors are central to informing us, as data scientists, about what is at stake.
+
+
+
15.1.3 Spotlight: Intersection of Real Estate and Race
+
Housing has been a persistent source of racial inequality throughout US history, amongst other factors. It is one of the main areas where inequalities are created and reproduced. In the beginning, Jim Crow laws were explicit in forbidding people of color from schools, public utilities, etc.
+
+
+
+
+
Today, while advancements in Civil Rights have been made, the spirit of the laws are alive in many parts of the US. The real estate industry was “professionalized” in the 1920’s and 1930’s by aspiring to become a science guided by strict methods and principles outlined below:
+
+
Redlining: making it difficult or impossible to get a federally-backed mortgage to buy a house in specific neighborhoods coded as “risky” (red).
+
+
What made them “risky” according to the makers of these was racial composition.
+
Segregation was not only a result of federal policy, but developed by real estate professionals.
+
+
The methods centered on creating objective rating systems (information technologies) for the appraisal of property values which encoded race as a factor of valuation (see figure below),
+
+
This, in turn, influenced federal policy and practice.
+
+
+
+
+
+Source: Colin Koopman, How We Became Our Data (2019) p. 137
+
+
+
+
+
+
+
15.2 The Response: Cook County Open Data Initiative
+
The response started in politics. A new assessor, Fritz Kaegi, was elected and created a new mandate with two goals:
+
+
Distributional equity in property taxation, meaning that properties of same value treated alike during assessments.
+
Creating a new Office of Data Science.
+
+
+
+
+
+
+
15.2.1 Question/Problem Formulation
+
+
+
+
+
+
+Driving Questions
+
+
+
+
+
What do we want to know?
+
What problems are we trying to solve?
+
What are the hypotheses we want to test?
+
What are our metrics for success?
+
+
+
+
The new Office of Data Science started by redefining their goals.
+
+
Accurately, uniformly, and impartially assess the value of a home by
+
+
Following international standards (coefficient of dispersion)
+
Predicting value of all homes with as little total error as possible
+
+
Create a robust pipeline that accurately assesses property values at scale and is fair by
+
+
Disrupts the circuit of corruption (Board of Review appeals process)
+
Eliminates regressivity
+
Engenders trust in the system among all stakeholders
+
+
+
+
+
+
+
+
+Definitions: Fairness and Transparency
+
+
+
+
The definitions, as given by the Cook County Assessor’s Office, are given below:
+
+
Fairness: The ability of our pipeline to accurately assess property values, accounting for disparities in geography, information, etc.
+
Transparency: The ability of the data science department to share and explain pipeline results and decisions to both internal and external stakeholders
+
+
Note how the Office defines “fairness” in terms of accuracy. Thus, the problem - make the system more fair - was already framed in terms amenable to a data scientist: make the assessments more accurate. The idea here is that if the model is more accurate it will also (perhaps necessarily) become more fair, which is a big assumption. There are, in a sense, two different problems - make accurate assessments, and make a fair system.
+
+
+
The way the goals are defined lead us to ask the question: what does it actually mean to accurately assess property values, and what role does “scale” play?
+
+
What is an assessment of a home’s value?
+
What makes one assessment more accurate than another?
+
What makes one batch of assessments more accurate than another batch?
+
+
Each of the above questions leads to a slew of more questions. Considering just the first question, one answer could be that an assessment is an estimate of the value of a home. This leads to more inquiries: what is the value of a home? What determines it? How do we know? For this class, we take it to be the house’s market value.
+
+
+
15.2.2 Data Acquisition and Cleaning
+
+
+
+
+
+
+Driving Questions
+
+
+
+
+
What data do we have, and what data do we need?
+
How will we sample more data?
+
Is our data representative of the population we want to study?
+
+
+
+
The data scientists also critically examined their original sales data:
+
+
+
+
+
and asked the questions:
+
+
How was this data collected?
+
When was this data collected?
+
Who collected this data?
+
For what purposes was the data collected?
+
How and why were particular categories created?
+
+
For example, attributes can have different likelihoods of appearing in the data, and housing data in the floodplains geographic region of Chicago were less represented than other regions.
+
The features can even be reported at different rates. Improvements in homes, which tend to increase property value, were unlikely to be reported by the homeowners.
+
Additionally, they found that there was simply more missing data in lower income neighborhoods.
+
+
+
15.2.3 Exploratory Data Analysis
+
+
+
+
+
+
+Driving Questions
+
+
+
+
+
How is our data organized, and what does it contain?
+
Do we already have relevant data?
+
What are the biases, anomalies, or other issues with the data?
+
How do we transform the data to enable effective analysis?
+
+
+
+
Before the modeling step, they investigated a multitude of crucial questions:
+
+
Which attributes are most predictive of sales price?
+
Is the data uniformly distributed?
+
Do all neighborhoods have up to date data? Do all neighborhoods have the same granularity?
+
+
Do some neighborhoods have missing or outdated data?
+
+
Firstly, they found that the impact of certain features, such as bedroom number, were much more impactful in determining house value inside certain neighborhoods more than others. This informed them that different models should be used depending on the neighborhood.
+
They also noticed that low income neighborhoods had disproportionately spottier data. This informed them that they needed to develop new data collection practices - including finding new sources of data.
+
+
+
15.2.4 Prediction and Inference
+
+
+
+
+
+
+Driving Questions
+
+
+
+
+
What does the data say about the world?
+
Does it answer our questions or accurately solve the problem?
+
How robust are our conclusions and can we trust the predictions?
+
+
+
+
Rather than using a singular model to predict sale prices (“fair market value”) of unsold properties, the CCAO fit machine learning models that discover patterns using known sale prices and characteristics of similar and nearby properties. It uses different model weights for each township.
+
Compared to traditional mass appraisal, the CCAO’s new approach is more granular and more sensitive to neighborhood variations.
+
Here, we might ask why should any particular individual believe that the model is accurate for their property?
+
This leads us to recognize that the CCAO counts on its performance of “transparency” (putting data, models, pipeline onto GitLab) to foster public trust, which would help it equate the production of “accurate assessments” with “fairness”.
+
There’s a lot more to be said here on the relationship between accuracy, fairness, and metrics we tend to use when evaluating our models. Given the nuanced nature of the argument, it is recommended you view the corresponding lecture as the course notes are not as comprehensive for this portion of lecture.
+
+
+
15.2.5 Reports Decisions, and Conclusions
+
+
+
+
+
+
+Driving Questions
+
+
+
+
+
How successful is the system for each goal?
+
+
Accuracy/uniformity of the model
+
Fairness and transparency that eliminates regressivity and engenders trust
+
+
How do you know?
+
+
+
+
The model is not the end of the road. The new Office still sends homeowners their house evaluations, but now the data that they get sent back from the homeowners is taken into account. More detailed reports are being written by the Office itself to democratize the information. Town halls and other public facing outreach helps involves the whole community in the process of housing evaluations, rather than limiting participation to a select few.
+
+
+
+
15.3 Key Takeaways
+
+
Accuracy is a necessary, but not sufficient, condition of a fair system.
+
Fairness and transparency are context-dependent and sociotechnical concepts.
+
Learn to work with contexts, and consider how your data analysis will reshape them.
+
Keep in mind the power, and limits, of data analysis.
+
+
+
+
15.4 Lessons for Data Science Practice
+
+
Question/Problem Formulation
+
+
Who is responsible for framing the problem?
+
Who are the stakeholders? How are they involved in the problem framing?
+
What do you bring to the table? How does your positionality affect your understanding of the problem?
+
What are the narratives that you’re tapping into?
+
+
Data Acquisition and Cleaning
+
+
Where does the data come from?
+
Who collected it? For what purpose?
+
What kinds of collecting and recording systems and techniques were used?
+
How has this data been used in the past?
+
What restrictions are there on access to the data, and what enables you to have access?
+
+
Exploratory Data Analysis & Visualization
+
+
What kind of personal or group identities have become salient in this data?
+
Which variables became salient, and what kinds of relationship obtain between them?
+
Do any of the relationships made visible lend themselves to arguments that might be potentially harmful to a particular community?
+
+
Prediction and Inference
+
+
What does the prediction or inference do in the world?
+
Are the results useful for the intended purposes?
+
Are there benchmarks to compare the results?
+
How are your predictions and inferences dependent upon the larger system in which your model works?
+
+
Reports, Decisions, and Solutions
+
+
How do we know if we have accomplished our goals?
+
How does your work fit in the broader literature?
+
Where does your work agree or disagree with the status quo?
---
+title: Case Study in Human Contexts and Ethics
+execute:
+ echo: true
+format:
+ html:
+ code-fold: true
+ code-tools: true
+ toc: true
+ toc-title: Case Study in Human Contexts and Ethics
+ page-layout: full
+ theme:
+ - cosmo
+ - cerulean
+ callout-icon: false
+jupyter: python3
+---
+
+::: {.callout-note collapse="false"}
+## Learning Outcomes
+* Learn about the ethical dilemmas that data scientists face.
+* Know how critique models using contextual knowledge about data.
+:::
+
+> **Disclaimer**: The following chapter discusses issues of structural racism. Some of the items in the chapter may be sensitive and may or may not be the opinions, ideas, and beliefs of the students who collected the materials. The Data 100 course staff tries its best to only present information that is relevant for teaching the lessons at hand.
+
+**Note:** Given the nuanced nature of some of the arguments made in the lecture, it is highly recommended that you view the lecture recording in order to fully engage and understand the material. The course notes will have the same broader structure but are by no means comprehensive.
+
+
+Let's immerse ourselves in the real-world story of data scientists working for an organization called the Cook County Assessor's Office (CCAO). Their job is to **estimate the values of houses** in order to **assign property taxes**. This is because the tax burden in this area is determined by the estimated **value** of a house, which is different from its price. Since values change over time and there are no obvious indicators of value, they created a **model** to estimate the values of houses. In this chapter, we will dig deep into what problems biased the models, the consequences to human lives, and how we can learn from this example to do better.
+
+
+## The Problem
+
+A [report](https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/assessments.html) by the Chicago Tribune uncovered a major scandal: the team showed that the model perpetuated a highly regressive tax system that disproportionately burdened African-American and Latinx homeowners in Cook County. How did they know?
+
+<center><ahref="https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/assessments.html">
+<imgsrc="images/vis_1.png"></img></a></center>
+
+In the field of housing assessment, there are standard metrics that assessors use across the world to estimate the fairness of assessments: [coefficient of dispersion](https://www.realestateagent.com/real-estate-glossary/real-estate/coefficient-of-dispersion.html) and [price-related differential](https://leg.wa.gov/House/Committees/FIN/Documents/2009/RatioText.pdf). These metrics have been rigorously tested by experts in the field and are out of scope for our class. Calculating these metrics for the Cook County prices revealed that the pricing created by the CCAO did not fall in acceptable ranges (see figure above). This by itself is **not the end** of the story, but a good indicator that **something fishy was going on**.
+
+<center><ahref="https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/assessments.html">
+<imgsrc="images/vis_2.png"width="300"></img></a></center>
+
+This prompted them to investigate if the model itself was producing fair tax rates. Evidently, when accounting for the home owner's income, they found that the model actually produced a **regressive** tax rate (see figure above). A tax rate is **regressive** if the percentage tax rate is higher for individuals with lower net income. A tax rate is **progressive** if the percentage tax rate is higher for individuals with higher net income.
+
+<center><ahref="https://www.clccrul.org/bpnc-v-berrios-facts?rq=berrios">
+<imgsrc="images/vis_3.jpg"width="600"></img>
+</a></center>
+<br>
+Further digging suggests that not only was the system unfair to people across the axis of income, it was also unfair across the axis of race (see figure above). The likelihood of a property being under- or over-assessed was highly dependent on the owner's race, and that did not sit well with many homeowners.
+
+
+### Spotlight: Appeals
+
+What actually caused this to come about? A comprehensive answer goes beyond just models. At the end of the day, these are real systems that have a lot of moving parts. One of which was the **appeals system**. Homeowners are mailed the value their home assessed by CCAO, and the homeowner can choose to appeal to a board of elected officials to try and change the listed value of their home and thus how much they are taxed. In theory, this sounds like a very fair system: someone oversees the final pricing of houses rather than just an algorithm. However, it ended up exacerbating the problems.
+
+> “Appeals are a good thing,” Thomas Jaconetty, deputy assessor for valuation and appeals, said in an interview. “The goal here is fairness. We made the numbers. We can change them.”
+
+<center><ahref="https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/appeals.html"><imgsrc="images/vis_4.png"></img>
+</a></center>
+
+<br/>
+
+Here we can borrow lessons from [Critical Race Theory](https://www.britannica.com/topic/critical-race-theory). On the surface, everyone having the legal right to try and appeal is undeniable. However, not everyone has an equal ability to do so. Those who have the money to hire tax lawyers to appeal for them have a drastically higher chance of trying and succeeding (see above figure). This model is part of a deeper institutional pattern rife with potential corruption.
+
+
+<center><ahref="https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/appeals.html"><imgsrc="images/vis_5.png"></img>
+</a></center>
+<br/>
+
+Homeowners who appealed were generally under-assessed relative to homeowners who did not (see above figure). Those with higher incomes pay less in property tax, tax lawyers are able to grow their business due to their role in appeals, and politicians are commonly socially connected to the aforementioned tax lawyers and wealthy homeowners. All these stakeholders have reasons to advertise the model as an integral part of a fair system. Here lies the value in asking questions: a system that seems fair on the surface may in actuality be unfair upon taking a closer look.
+
+### Human Impacts
+
+<center><ahref="https://apps.chicagotribune.com/news/watchdog/cook-county-property-tax-divide/assessments.html"><imgsrc="images/vis_6.png"></img>
+</a></center>
+<br/>
+
+The impact of the housing model extends beyond the realm of home ownership and taxation. Discriminatory practices have a long history within the United States, and the model served to perpetuate this fact. To this day, Chicago is one of the most segregated cities in the United States ([source](https://fivethirtyeight.com/features/the-most-diverse-cities-are-often-the-most-segregated/)). These factors are central to informing us, as data scientists, about what is at stake.
+
+
+### Spotlight: Intersection of Real Estate and Race
+
+Housing has been a persistent source of racial inequality throughout US history, amongst other factors. It is one of the main areas where inequalities are created and reproduced. In the beginning, [Jim Crow](https://www.history.com/topics/early-20th-century-us/jim-crow-laws) laws were explicit in forbidding people of color from schools, public utilities, etc.
+
+<center><ahref="https://dsl.richmond.edu/panorama/redlining/#loc=11/41.84/-87.674"><imgsrc="images/vis_7.png"></img></a></center>
+<br/>
+
+Today, while advancements in Civil Rights have been made, the spirit of the laws are alive in many parts of the US. The real estate industry was “professionalized” in the 1920’s and 1930’s by aspiring to become a science guided by strict methods and principles outlined below:
+
+- Redlining: making it difficult or impossible to get a federally-backed mortgage to buy a house in specific neighborhoods coded as “risky” (red).
+ - What made them “risky” according to the makers of these was racial composition.
+ - Segregation was not only a result of federal policy, but developed by real estate professionals.
+- The methods centered on creating objective rating systems (information technologies) for the appraisal of property values which encoded **race** as a factor of valuation (see figure below),
+ - This, in turn, influenced federal policy and practice.
+
+<center><imgsrc="images/vis_8.png"></img><figcaption>Source: Colin Koopman, How We Became Our Data (2019) p. 137</figcaption></center>
+<br/>
+
+
+## The Response: Cook County Open Data Initiative
+
+The response started in politics. A new assessor, Fritz Kaegi, was elected and created a new mandate with two goals:
+
+1. Distributional equity in property taxation, meaning that properties of same value treated alike during assessments.
+2. Creating a new Office of Data Science.
+
+<center><imgsrc="images/vis_9.png"width=300px></img></center>
+<br/>
+
+### Question/Problem Formulation
+::: {.callout-note}
+## Driving Questions
+
+- What do we want to know?
+- What problems are we trying to solve?
+- What are the hypotheses we want to test?
+- What are our metrics for success?
+:::
+
+The new Office of Data Science started by redefining their goals.
+
+1. Accurately, uniformly, and impartially assess the value of a home by
+ - Following international standards (coefficient of dispersion)
+ - Predicting value of all homes with as little total error as possible
+
+2. Create a robust pipeline that accurately assesses property values at scale and is fair by
+ - Disrupts the circuit of corruption (Board of Review appeals process)
+ - Eliminates regressivity
+ - Engenders trust in the system among all stakeholders
+
+
+::: {.callout-tip}
+## <b>Definitions</b>: Fairness and Transparency
+The definitions, as given by the Cook County Assessor's Office, are given below: <br>
+
+* Fairness: The ability of our pipeline to accurately assess property values, accounting for disparities in geography, information, etc. <br>
+* Transparency: The ability of the data science department to share and explain pipeline results and decisions to both internal and external stakeholders <br>
+
+Note how the Office defines "fairness" in terms of accuracy. Thus, the problem - make the system more fair - was already framed in terms amenable to a data scientist: make the assessments more accurate.<br>
+The idea here is that if the model is more accurate it will also (perhaps necessarily) become more fair, which is a big assumption. There are, in a sense, two different problems - make accurate assessments, and make a fair system.
+:::
+
+The way the goals are defined lead us to ask the question: what does it actually mean to accurately assess property values, and what role does “scale” play?
+
+1. What is an assessment of a home’s value?
+2. What makes one assessment more accurate than another?
+3. What makes one batch of assessments more accurate than another batch?
+
+Each of the above questions leads to a slew of more questions. Considering just the first question, one answer could be that an assessment is an estimate of the value of a home. This leads to more inquiries: what is the value of a home? What determines it? How do we know? For this class, we take it to be the house's market value.
+
+### Data Acquisition and Cleaning
+::: {.callout-note}
+## Driving Questions
+
+- What data do we have, and what data do we need?
+- How will we sample more data?
+- Is our data representative of the population we want to study?
+:::
+
+The data scientists also critically examined their original sales data:
+
+<center><imgsrc="images/vis_10.png"></img></center>
+<br/>
+
+and asked the questions:
+
+1. How was this data collected?
+2. When was this data collected?
+3. Who collected this data?
+4. For what purposes was the data collected?
+5. How and why were particular categories created?
+
+For example, attributes can have different likelihoods of appearing in the data, and housing data in the floodplains geographic region of Chicago were less represented than other regions.
+
+The features can even be reported at different rates. Improvements in homes, which tend to increase property value, were unlikely to be reported by the homeowners.
+
+Additionally, they found that there was simply more missing data in lower income neighborhoods.
+
+### Exploratory Data Analysis
+::: {.callout-note}
+## Driving Questions
+
+- How is our data organized, and what does it contain?
+- Do we already have relevant data?
+- What are the biases, anomalies, or other issues with the data?
+- How do we transform the data to enable effective analysis?
+:::
+
+Before the modeling step, they investigated a multitude of crucial questions:
+
+1. Which attributes are most predictive of sales price?
+2. Is the data uniformly distributed?
+3. Do all neighborhoods have up to date data? Do all neighborhoods have the same granularity?
+4. Do some neighborhoods have missing or outdated data?
+
+Firstly, they found that the impact of certain features, such as bedroom number, were much more impactful in determining house value inside certain neighborhoods more than others. This informed them that different models should be used depending on the neighborhood.
+
+They also noticed that low income neighborhoods had disproportionately spottier data. This informed them that they needed to develop new data collection practices - including finding new sources of data.
+
+
+
+### Prediction and Inference
+::: {.callout-note}
+## Driving Questions
+
+- What does the data say about the world?
+- Does it answer our questions or accurately solve the problem?
+- How robust are our conclusions and can we trust the predictions?
+:::
+
+Rather than using a singular model to predict sale prices (“fair market value”) of unsold properties, the CCAO fit machine learning models that discover patterns using known sale prices and characteristics of **similar and nearby properties**. It uses different model weights for each township.
+
+Compared to traditional mass appraisal, the CCAO’s new approach is more granular and more sensitive to neighborhood variations.
+
+Here, we might ask why should any particular individual believe that the model is accurate for their property?
+
+This leads us to recognize that the CCAO counts on its performance of “transparency” (putting data, models, pipeline onto GitLab) to foster public trust, which would help it equate the production of “accurate assessments” with “fairness”.
+
+There's a lot more to be said here on the relationship between accuracy, fairness, and metrics we tend to use when evaluating our models. Given the nuanced nature of the argument, it is recommended you view the corresponding lecture as the course notes are not as comprehensive for this portion of lecture.
+
+### Reports Decisions, and Conclusions
+::: {.callout-note}
+## Driving Questions
+
+- How successful is the system for each goal?
+ - Accuracy/uniformity of the model
+ - Fairness and transparency that eliminates regressivity and engenders trust
+- How do you know?
+:::
+
+The model is not the end of the road. The new Office still sends homeowners their house evaluations, but now the data that they get sent back from the homeowners is taken into account. More detailed reports are being written by the Office itself to democratize the information. Town halls and other public facing outreach helps involves the whole community in the process of housing evaluations, rather than limiting participation to a select few.
+
+## Key Takeaways
+
+1. Accuracy is a necessary, but not sufficient, condition of a fair system.
+
+2. Fairness and transparency are context-dependent and sociotechnical concepts.
+
+3. Learn to work with contexts, and consider how your data analysis will reshape them.
+
+4. Keep in mind the power, and limits, of data analysis.
+
+
+
+## Lessons for Data Science Practice
+
+1. Question/Problem Formulation
+
+ - Who is responsible for framing the problem?
+ - Who are the stakeholders? How are they involved in the problem framing?
+ - What do you bring to the table? How does your positionality affect your understanding of the problem?
+ - What are the narratives that you're tapping into?
+
+2. Data Acquisition and Cleaning
+
+ - Where does the data come from?
+ - Who collected it? For what purpose?
+ - What kinds of collecting and recording systems and techniques were used?
+ - How has this data been used in the past?
+ - What restrictions are there on access to the data, and what enables you to have access?
+
+3. Exploratory Data Analysis & Visualization
+
+ - What kind of personal or group identities have become salient in this data?
+ - Which variables became salient, and what kinds of relationship obtain between them?
+ - Do any of the relationships made visible lend themselves to arguments that might be potentially harmful to a particular community?
+
+4. Prediction and Inference
+
+ - What does the prediction or inference do in the world?
+ - Are the results useful for the intended purposes?
+ - Are there benchmarks to compare the results?
+ - How are your predictions and inferences dependent upon the larger system in which your model works?
+
+5. Reports, Decisions, and Solutions
+
+ - How do we know if we have accomplished our goals?
+ - How does your work fit in the broader literature?
+ - Where does your work agree or disagree with the status quo?
+ - Do your conclusions make sense?
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/docs/case_study_HCE/images/vis_1.png b/docs/case_study_HCE/images/vis_1.png
new file mode 100644
index 00000000..a9ecac7b
Binary files /dev/null and b/docs/case_study_HCE/images/vis_1.png differ
diff --git a/docs/case_study_HCE/images/vis_10.png b/docs/case_study_HCE/images/vis_10.png
new file mode 100644
index 00000000..61daefb9
Binary files /dev/null and b/docs/case_study_HCE/images/vis_10.png differ
diff --git a/docs/case_study_HCE/images/vis_2.png b/docs/case_study_HCE/images/vis_2.png
new file mode 100644
index 00000000..db39da9e
Binary files /dev/null and b/docs/case_study_HCE/images/vis_2.png differ
diff --git a/docs/case_study_HCE/images/vis_3.jpg b/docs/case_study_HCE/images/vis_3.jpg
new file mode 100644
index 00000000..72e64539
Binary files /dev/null and b/docs/case_study_HCE/images/vis_3.jpg differ
diff --git a/docs/case_study_HCE/images/vis_4.png b/docs/case_study_HCE/images/vis_4.png
new file mode 100644
index 00000000..472809df
Binary files /dev/null and b/docs/case_study_HCE/images/vis_4.png differ
diff --git a/docs/case_study_HCE/images/vis_5.png b/docs/case_study_HCE/images/vis_5.png
new file mode 100644
index 00000000..74853eb2
Binary files /dev/null and b/docs/case_study_HCE/images/vis_5.png differ
diff --git a/docs/case_study_HCE/images/vis_6.png b/docs/case_study_HCE/images/vis_6.png
new file mode 100644
index 00000000..60d63cfb
Binary files /dev/null and b/docs/case_study_HCE/images/vis_6.png differ
diff --git a/docs/case_study_HCE/images/vis_7.png b/docs/case_study_HCE/images/vis_7.png
new file mode 100644
index 00000000..ed490433
Binary files /dev/null and b/docs/case_study_HCE/images/vis_7.png differ
diff --git a/docs/case_study_HCE/images/vis_8.png b/docs/case_study_HCE/images/vis_8.png
new file mode 100644
index 00000000..e2ebc46b
Binary files /dev/null and b/docs/case_study_HCE/images/vis_8.png differ
diff --git a/docs/case_study_HCE/images/vis_9.png b/docs/case_study_HCE/images/vis_9.png
new file mode 100644
index 00000000..aab37580
Binary files /dev/null and b/docs/case_study_HCE/images/vis_9.png differ
diff --git a/docs/constant_model_loss_transformations/loss_transformations.html b/docs/constant_model_loss_transformations/loss_transformations.html
index 29371ea8..2d7f17e1 100644
--- a/docs/constant_model_loss_transformations/loss_transformations.html
+++ b/docs/constant_model_loss_transformations/loss_transformations.html
@@ -232,6 +232,18 @@
14Sklearn and Feature Engineering
+
+
Recognize the need for validation and test sets to preview model performance on unseen data
+
Apply cross-validation to select model hyperparameters
+
Understand the conceptual basis for L1 and L2 regularization
+
+
+
+
+
At the end of the Feature Engineering lecture (Lecture 14), we arrived at the issue of fine-tuning model complexity. We identified that a model that’s too complex can lead to overfitting, while a model that’s too simple can lead to underfitting. This brings us to a natural question: how do we control model complexity to avoid under- and overfitting?
+
To answer this question, we will need to address two things: first, we need to understand when our model begins to overfit by assessing its performance on unseen data. We can achieve this through cross-validation. Secondly, we need to introduce a technique to adjust the complexity of our models ourselves – to do so, we will apply regularization.
+
+
16.1 Training, Test, and Validation Sets
+
From the last lecture, we learned that increasing model complexity decreased our model’s training error but increased its variance. This makes intuitive sense: adding more features causes our model to fit more closely to data it encountered during training, but generalize worse to new data it hasn’t seen before. For this reason, a low training error is not always representative of our model’s underlying performance - we need to also assess how well it performs on unseen data to ensure that it is not overfitting.
+
Truly, the only way to know when our model overfits is by evaluating it on unseen data. Unfortunately, that means we need to wait for more data. This may be very expensive and time-consuming.
+
How should we proceed? In this section, we will build up a viable solution to this problem.
+
+
16.1.1 Test Sets
+
The simplest approach to avoid overfitting is to keep some of our data “secret” from ourselves. We can set aside a random portion of our full dataset to use only for testing purposes. The datapoints in this test set will not be used in the model fitting process. Instead, we will:
+
+
Use the remaining portion of our dataset – now called the training set – to run ordinary least squares, gradient descent, or some other technique to fit model parameters
+
Take the fitted model and use it to make predictions on datapoints in the test set. The model’s performance on the test set (expressed as the MSE, RMSE, etc.) is now indicative of how well it can make predictions on unseen data
+
+
Importantly, the optimal model parameters were found by only considering the data in the training set. After the model has been fitted to the training data, we do not change any parameters before making predictions on the test set. Importantly, we only ever make predictions on the test set once after all model design has been completely finalized. We treat the test set performance as the final test of how well a model does.
+
The process of sub-dividing our dataset into training and test sets is known as a train-test split. Typically, between 10% and 20% of the data is allocated to the test set.
+
+
+
+
In sklearn, the train_test_split function of the model_selection module allows us to automatically generate train-test splits.
+
Throughout today’s work, we will work with the vehicles dataset from previous lectures. As before, we will attempt to predict the mpg of a vehicle from transformations of its hp. In the cell below, we allocate 20% of the full dataset to testing, and the remaining 80% to training.
+
+
+Code
+
import pandas as pd
+import numpy as np
+import seaborn as sns
+import warnings
+warnings.filterwarnings('ignore')
+
+# Load the dataset and construct the design matrix
+vehicles = sns.load_dataset("mpg").rename(columns={"horsepower":"hp"}).dropna()
+X = vehicles[["hp"]]
+X["hp^2"] = vehicles["hp"]**2
+X["hp^3"] = vehicles["hp"]**3
+X["hp^4"] = vehicles["hp"]**4
+
+Y = vehicles["mpg"]
+
+
+
+
from sklearn.model_selection import train_test_split
+
+# `test_size` specifies the proportion of the full dataset that should be allocated to testing
+# `random_state` makes our results reproducible for educational purposes
+X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=220)
+
+print(f"Size of full dataset: {X.shape[0]} points")
+print(f"Size of training set: {X_train.shape[0]} points")
+print(f"Size of test set: {X_test.shape[0]} points")
+
+
Size of full dataset: 392 points
+Size of training set: 313 points
+Size of test set: 79 points
+
+
+
After performing our train-test split, we fit a model to the training set and assess its performance on the test set.
+
+
import sklearn.linear_model as lm
+from sklearn.metrics import mean_squared_error
+
+model = lm.LinearRegression()
+
+# Fit to the training set
+model.fit(X_train, Y_train)
+
+# Make predictions on the test set
+test_predictions = model.predict(X_test)
+
+
+
+
16.1.2 Validation Sets
+
Now, what if we were dissatisfied with our test set performance? With our current framework, we’d be stuck. As outlined previously, assessing model performance on the test set is the final stage of the model design process. We can’t go back and adjust our model based on the new discovery that it is overfitting – if we did, then we would be factoring in information from the test set to design our model. The test error would no longer be a true representation of the model’s performance on unseen data!
+
Our solution is to introduce a validation set. A validation set is a random portion of the training set that is set aside for assessing model performance while the model is still being developed. The process for using a validation set is:
+
+
Perform a train-test split. Set the test set aside; we will not touch it until the very end of the model design process.
+
Set aside a portion of the training set to be used for validation.
+
Fit the model parameters to the datapoints contained in the remaining portion of the training set.
+
Assess the model’s performance on the validation set. Adjust the model as needed, re-fit it to the remaining portion of the training set, then re-evaluate it on the validation set. Repeat as necessary until you are satisfied.
+
After all model development is complete, assess the model’s performance on the test set. This is the final test of how well the model performs on unseen data. No further modifications should be made to the model.
+
+
The process of creating a validation set is called a validation split.
+
+
+
+
Note that the validation error behaves quite differently from the training error explored previously. Recall that the training error decreased monotonically with increasing model degree – as the model became more complex, it made better and better predictions on the training data. The validation error, in contrast, decreases then increases as we increase model complexity. This reflects the transition from under- to overfitting. At low model complexity, the model underfits because it is not complex enough to capture the main trends in the data. At high model complexity, the model overfits because it “memorizes” the training data too closely.
+
We can update our understanding of the relationships between error, complexity, and model variance:
+
+
+
+
Our goal is to train a model with complexity near the orange dotted line – this is where our model achieves minimum error on the validation set. Note that this relationship is a simplification of the real-world. But for the purposes of Data 100, this is good enough.
+
+
+
+
16.2 K-Fold Cross-Validation
+
Introducing a validation set gave us an “extra” chance to assess model performance on another set of unseen data. We are able to finetune the model design based on its performance on this one set of validation data.
+
But what if, by random chance, our validation set just happened to contain many outliers? It is possible that the validation datapoints we set aside do not actually represent other unseen data that the model might encounter. Ideally, we would like to validate our model’s performance on several different unseen datasets. This would give us greater confidence in our understanding of how the model behaves on new data.
+
Let’s think back to our validation framework. Earlier, we set aside x% of our training data (say, 20%) to use for validation.
+
+
+
+
In the example above, we set aside the first 20% of training datapoints for the validation set. This was an arbitrary choice. We could have set aside any 20% portion of the training data for validation. In fact, there are 5 non-overlapping “chunks” of training points that we could have designated as the validation set.
+
+
+
+
The common term for one of these chunks is a fold. In the example above, we had 5 folds, each containing 20% of the training data. This gives us a new perspective: we really have 5 validation sets “hidden” in our training set.
+
In cross-validation, we perform validation splits for each fold in the training set. For a dataset with \(K\) folds, we:
+
+
Pick one fold to be the validation fold
+
Fit the model to training data from every fold other than the validation fold
+
Compute the model’s error on the validation fold and record it
+
Repeat for all \(K\) folds
+
+The cross-validation error is then the average error across all \(K\) validation folds.
+
+
+
+
+
16.2.1 Model Selection Workflow
+
At this stage, we have refined our model selection workflow. We begin by performing a train-test split to set aside a test set for the final evaluation of model performance. Then, we alternate between adjusting our design matrix and computing the cross-validation error to finetune the model’s design. In the example below, we illustrate the use of 4-fold cross-validation to help inform model design.
+
+
+
+
+
+
16.2.2 Hyperparameters
+
An important use of cross-validation is for hyperparameter selection. A hyperparameter is some value in a model that is chosen before the model is fit to any data. This means that it is distinct from the model parameters\(\theta_i\) because its value is selected before the training process begins. We cannot use our usual techniques – calculus, ordinary least squares, or gradient descent – to choose its value. Instead, we must decide it ourselves.
+
Some examples of hyperparameters in Data 100 are:
+
+
The degree of our polynomial model (recall that we selected the degree before creating our design matrix and calling .fit)
+
The learning rate, \(\alpha\), in gradient descent
+
The regularization penalty, \(\lambda\) (to be introduced later this lecture)
+
+
To select a hyperparameter value via cross-validation, we first list out several “guesses” for what the best hyperparameter may be. For each guess, we then run cross-validation to compute the cross-validation error incurred by the model when using that choice of hyperparameter value. We then select the value of the hyperparameter that resulted in the lowest cross-validation error.
+
For example, we may wish to use cross-validation to decide what value we should use for \(\alpha\), which controls the step size of each gradient descent update. To do so, we list out some possible guesses for the best \(\alpha\): 0.1, 1, and 10. For each possible value, we perform cross-validation to see what error the model has when we use that value of \(\alpha\) to train it.
+
+
+
+
+
+
+
16.3 Regularization
+
We’ve now addressed the first of our two goals for today: creating a framework to assess model performance on unseen data. Now, we’ll discuss our second objective: developing a technique to adjust model complexity. This will allow us to directly tackle the issues of under- and overfitting.
+
Earlier, we adjusted the complexity of our polynomial model by tuning a hyperparameter – the degree of the polynomial. We trialed several different polynomial degrees, computed the validation error for each, and selected the value that minimized the validation error. Tweaking the “complexity” was simple; it was only a matter of adjusting the polynomial degree.
+
In most machine learning problems, complexity is defined differently from what we have seen so far. Today, we’ll explore two different definitions of complexity: the squared and absolute magnitude of \(\theta_i\) coefficients.
+
+
16.3.1 Constraining Model Parameters
+
Think back to our work using gradient descent to descend down a loss surface. You may find it helpful to refer back to the Gradient Descent note to refresh your memory. Our aim was to find the combination of model parameters that led to the model having minimum loss. We visualized this using a contour map by plotting possible parameter values on the horizontal and vertical axes, which allows us to take a bird’s eye view above the loss surface. We want to find the model parameters corresponding to the lowest point on the loss surface.
Recall that we represent our features with \(\phi_i\) to reflect the fact that we have performed feature engineering.
+
Previously, we restricted model complexity by limiting the total number of features present in the model. We only included a limited number of polynomial features at a time; all other polynomials were excluded from the model.
+
What if, instead of fully removing particular features, we kept all features and used each one only a “little bit”? If we put a limit on how much each feature can contribute to the predictions, we can still control the model’s complexity without the need to manually determine how many features should be removed.
+
What do we mean by a “little bit”? Consider the case where some parameter \(\theta_i\) is close to or equal to 0. Then, feature \(\phi_i\) barely impacts the prediction – the feature is weighted by such a small value that its presence doesn’t significantly change the value of \(\hat{\mathbb{Y}}\). If we restrict how large each parameter \(\theta_i\) can be, we restrict how much feature \(\phi_i\) contributes to the model. This has the effect of reducing model complexity.
+
In regularization, we restrict model complexity by putting a limit on the magnitudes of the model parameters \(\theta_i\).
+
What do these limits look like? Suppose we specify that the sum of all absolute parameter values can be no greater than some number \(Q\). In other words:
+
\[\sum_{i=1}^p |\theta_i| \leq Q\]
+
where \(p\) is the total number of parameters in the model. You can think of this as us giving our model a “budget” for how it distributes the magnitudes of each parameter. If the model assigns a large value to some \(\theta_i\), it may have to assign a small value to some other \(\theta_j\). This has the effect of increasing feature \(\phi_i\)’s influence on the predictions while decreasing the influence of feature \(\phi_j\). The model will need to be strategic about how the parameter weights are distributed – ideally, more “important” features will receive greater weighting.
+
Notice that the intercept term, \(\theta_0\), is excluded from this constraint. We typically do not regularize the intercept term.
+
Now, let’s think back to gradient descent and visualize the loss surface as a contour map. As a refresher, a loss surface means that each point represents the model’s loss for a particular combination of \(\theta_1\), \(\theta_2\). Let’s say our goal is to find the combination of parameters that gives us the lowest loss.
+
+
+
+
With no constraint, the optimal \(\hat{\theta}\) is in the center.
+
Applying this constraint limits what combinations of model parameters are valid. We can now only consider parameter combinations with a total absolute sum less than or equal to our number \(Q\). This means that we can only assign our regularized parameter vector \(\hat{\theta}_{\text{Reg}}\) to positions in the green diamond below.
+
+
+
+
We can no longer select the parameter vector that truly minimizes the loss surface, \(\hat{\theta}_{\text{No Reg}}\), because this combination of parameters does not lie within our allowed region. Instead, we select whatever allowable combination brings us closest to the true minimum loss.
+
+
+
+
Notice that, under regularization, our optimized \(\theta_1\) and \(\theta_2\) values are much smaller than they were without regularization (indeed, \(\theta_1\) has decreased to 0). The model has decreased in complexity because we have limited how much our features contribute to the model. In fact, by setting its parameter to 0, we have effectively removed the influence of feature \(\phi_1\) from the model altogether.
+
If we change the value of \(Q\), we change the region of allowed parameter combinations. The model will still choose the combination of parameters that produces the lowest loss – the closest point in the constrained region to the true minimizer, \(\hat{\theta}_{\text{No Reg}}\).
+If we make \(Q\) smaller:
+
+
+
+If we make \(Q\) larger:
+
+
+
+
+
When \(Q\) is small, we severely restrict the size of our parameters. \(\theta_i\)s are small in value, and features \(\phi_i\) only contribute a little to the model. The allowed region of model parameters contracts, and the model becomes much simpler.
+
When \(Q\) is large, we do not restrict our parameter sizes by much. \(\theta_i\)s are large in value, and features \(\phi_i\) contribute more to the model. The allowed region of model parameters expands, and the model becomes more complex.
+
+
Consider the extreme case of when \(Q\) is extremely large. In this situation, our restriction has essentially no effect, and the allowed region includes the OLS solution!
+
+
+
+
+
Now what if \(Q\) were very small? Our parameters are then set to (essentially 0). If the model has no intercept term: \(\hat{\mathbb{Y}} = (0)\phi_1 + (0)\phi_2 + \ldots = 0\). And if the model has an intercept term: \(\hat{\mathbb{Y}} = (0)\phi_1 + (0)\phi_2 + \ldots = \theta_0\). Remember that the intercept term is excluded from the constraint - this is so we avoid the situation where we always predict 0.
+
Let’s summarize what we have seen.
+
+
+
+
+
+
+
16.4 L1 (LASSO) Regularization
+
How do we actually apply our constraint \(\sum_{i=1}^p |\theta_i| \leq Q\)? We will do so by modifying the objective function that we seek to minimize when fitting a model.
+
Recall our ordinary least squares objective function: our goal was to find parameters that minimize the model’s mean squared error.
Unfortunately, we can’t directly use this formulation as our objective function – it’s not easy to mathematically optimize over a constraint. Instead, we will apply the magic of the Lagrangian Duality. The details of this are out of scope (take EECS 127 if you’re interested in learning more), but the end result is very useful. It turns out that minimizing the following augmented objective function is equivalent to our minimization goal above.
The second of these two expressions includes the MSE expressed using vector notation.
+
Notice that we’ve replaced the constraint with a second term in our objective function. We’re now minimizing a function with an additional regularization term that penalizes large coefficients. In order to minimize this new objective function, we’ll end up balancing two components:
+
+
Keep the model’s error on the training data low, represented by the term \(\frac{1}{n} \sum_{i=1}^n (y_i - (\theta_0 + \theta_1 x_{i, 1} + \theta_2 x_{i, 2} + \ldots + \theta_p x_{i, p}))^2\)
+
At the same time, keep the magnitudes of model parameters low, represented by the term \(\lambda \sum_{i=1}^p |\theta_i|\)
+
+
The \(\lambda\) factor controls the degree of regularization. Roughly speaking, \(\lambda\) is related to our \(Q\) constraint from before by the rule \(\lambda \approx \frac{1}{Q}\). To understand why, let’s consider two extreme examples:
+
+
Assume \(\lambda \rightarrow \infty\). Then, \(\lambda \sum_{j=1}^{d} \vert \theta_j \vert\) dominates the cost function. To minimize this term, we set \(\theta_j = 0\) for all \(j \ge 1\). This is a very constrained model that is mathematically equivalent to the constant model. Earlier, we explained the constant model also arises when the L2 norm ball radius \(Q \rightarrow 0\).
+
Assume \(\lambda \rightarrow 0\). Then, \(\lambda \sum_{j=1}^{d} \vert \theta_j \vert\) is 0. Minimizing the cost function is equivalent to \(\min_{\theta} \frac{1}{n} || Y - X\theta ||_2^2\), our usual MSE loss function. The act of minimizing MSE loss is just our familiar OLS, and the optimal solution is the global minimum \(\hat{\theta} = \hat\theta_{No Reg.}\). We showed that the global optimum is achieved when the L2 norm ball radius \(Q \rightarrow \infty\).
+
+
We call \(\lambda\) the regularization penalty hyperparameter and select its value via cross-validation.
+
The process of finding the optimal \(\hat{\theta}\) to minimize our new objective function is called L1 regularization. It is also sometimes known by the acronym “LASSO”, which stands for “Least Absolute Shrinkage and Selection Operator.”
+
Unlike ordinary least squares, which can be solved via the closed-form solution \(\hat{\theta}_{OLS} = (\mathbb{X}^{\top}\mathbb{X})^{-1}\mathbb{X}^{\top}\mathbb{Y}\), there is no closed-form solution for the optimal parameter vector under L1 regularization. Instead, we use the Lasso model class of sklearn.
+
+
import sklearn.linear_model as lm
+
+# The alpha parameter represents our lambda term
+lasso_model = lm.Lasso(alpha=2)
+lasso_model.fit(X_train, Y_train)
+
+lasso_model.coef_
Notice that all model coefficients are very small in magnitude. In fact, some of them are so small that they are essentially 0. An important characteristic of L1 regularization is that many model parameters are set to 0. In other words, LASSO effectively selects only a subset of the features. The reason for this comes back to our loss surface and allowed “diamond” regions from earlier – we can often get closer to the lowest loss contour at a corner of the diamond than along an edge.
+
When a model parameter is set to 0 or close to 0, its corresponding feature is essentially removed from the model. We say that L1 regularization performs feature selection because, by setting the parameters of unimportant features to 0, LASSO “selects” which features are more useful for modeling.
+
+
+
16.5 Scaling Features for Regularization
+
The regularization procedure we just performed had one subtle issue. To see what it is, let’s take a look at the design matrix for our lasso_model.
+
+
X_train.head()
+
+
+
+
+
+
+
+
+
hp
+
hp^2
+
hp^3
+
hp^4
+
+
+
+
+
259
+
85.0
+
7225.0
+
614125.0
+
52200625.0
+
+
+
129
+
67.0
+
4489.0
+
300763.0
+
20151121.0
+
+
+
207
+
102.0
+
10404.0
+
1061208.0
+
108243216.0
+
+
+
302
+
70.0
+
4900.0
+
343000.0
+
24010000.0
+
+
+
71
+
97.0
+
9409.0
+
912673.0
+
88529281.0
+
+
+
+
+
+
+
+
Our features – hp, hp^2, hp^3, and hp^4 – are on drastically different numeric scales! The values contained in hp^4 are orders of magnitude larger than those contained in hp. This can be a problem because the value of hp^4 will naturally contribute more to each predicted \(\hat{y}\) because it is so much greater than the values of the other features. For hp to have much of an impact at all on the prediction, it must be scaled by a large model parameter.
+
By inspecting the fitted parameters of our model, we see that this is the case – the parameter for hp is much larger in magnitude than the parameter for hp^4.
Recall that by applying regularization, we give our a model a “budget” for how it can allocate the values of model parameters. For hp to have much of an impact on each prediction, LASSO is forced to “spend” more of this budget on the parameter for hp.
+
We can avoid this issue by scaling the data before regularizing. This is a process where we convert all features to the same numeric scale. A common way to scale data is to perform standardization such that all features have mean 0 and standard deviation 1; essentially, we replace everything with its Z-score.
+
\[z_k = \frac{x_k - \mu_k}{\sigma_k}\]
+
+
+
16.6 L2 (Ridge) Regularization
+
In all of our work above, we considered the constraint \(\sum_{i=1}^p |\theta_i| \leq Q\) to limit the complexity of the model. What if we had applied a different constraint?
+
In L2 regularization, also known as ridge regression, we constrain the model such that the sum of the squared parameters must be less than some number \(Q\). This constraint takes the form:
+
\[\sum_{i=1}^p \theta_i^2 \leq Q\]
+
As before, we typically do not regularize the intercept term.
+
The allowed region of parameters for a given value of \(Q\) is now shaped like a ball.
+
+
+
+
If we modify our objective function like before, we find that our new goal is to minimize the function: \[\frac{1}{n} \sum_{i=1}^n (y_i - (\theta_0 + \theta_1 \phi_{i, 1} + \theta_2 \phi_{i, 2} + \ldots + \theta_p \phi_{i, p}))^2\:\text{such that} \sum_{i=1}^p \theta_i^2 \leq Q\]
+
Notice that all we have done is change the constraint on the model parameters. The first term in the expression, the MSE, has not changed.
This solution exists even if \(\mathbb{X}\) is not full column rank. This is a major reason why L2 regularization is often used – it can produce a solution even when there is colinearity in the features. We will discuss the concept of colinearity in a future lecture. We will not derive this result in Data 100, as it involves a fair bit of matrix calculus.
+
In sklearn, we perform L2 regularization using the Ridge class. Notice that we scale the data before regularizing.
force=False)
covid_file # a file path wrapper object
-
Using cached version that was downloaded (UTC): Fri Aug 25 09:57:25 2023
+
Using cached version that was downloaded (UTC): Fri Aug 18 22:19:42 2023
PosixPath('data/confirmed-cases.json')
@@ -676,7 +688,7 @@
!ls -lh {covid_file}!wc -l {covid_file}
-
-rw-r--r-- 1 lillianweng staff 114K Aug 25 09:57 data/confirmed-cases.json
+
-rw-r--r-- 1 Ishani staff 114K Aug 18 22:19 data/confirmed-cases.json
1109 data/confirmed-cases.json
@@ -4089,8 +4101,14 @@
sns.displot(co2['Days']);plt.title("Distribution of days feature");# suppresses unneeded plotting output
+
+
/Users/Ishani/micromamba/lib/python3.9/site-packages/seaborn/axisgrid.py:118: UserWarning:
+
+The figure layout has changed to tight
+
+
-
+
In terms of data quality, a handful of months have averages based on measurements taken on fewer than half the days. In addition, there are nearly 200 missing values–that’s about 27% of the data!
@@ -4100,8 +4118,8 @@
Code
-
sns.scatterplot(x="Yr", y="Days", data=co2);
-plt.title("Day field by Year");# the ; suppresses output
+
sns.scatterplot(x="Yr", y="Days", data=co2);
+plt.title("Day field by Year");# the ; suppresses output
@@ -4125,17 +4143,23 @@
Code
-
# Histograms of average CO2 measurements
-sns.displot(co2['Avg']);
+
# Histograms of average CO2 measurements
+sns.displot(co2['Avg']);
+
+
/Users/Ishani/micromamba/lib/python3.9/site-packages/seaborn/axisgrid.py:118: UserWarning:
+
+The figure layout has changed to tight
+
+
-
+
The non-missing values are in the 300-400 range (a regular range of CO2 levels).
We also see that there are only a few missing Avg values (<1% of values). Let’s examine all of them:
-
co2[co2["Avg"] <0]
+
co2[co2["Avg"] <0]
@@ -4244,8 +4268,8 @@
Code
-
sns.lineplot(x='DecDate', y='Avg', data=co2)
-plt.title("CO2 Average By Month");
+
sns.lineplot(x='DecDate', y='Avg', data=co2)
+plt.title("CO2 Average By Month");
@@ -4257,9 +4281,9 @@
-
# 1. Drop missing values
-co2_drop = co2[co2['Avg'] >0]
-co2_drop.head()
+
# 1. Drop missing values
+co2_drop = co2[co2['Avg'] >0]
+co2_drop.head()
@@ -4335,9 +4359,9 @@
-
# 2. Replace NaN with -99.99
-co2_NA = co2.replace(-99.99, np.NaN)
-co2_NA.head()
+
# 2. Replace NaN with -99.99
+co2_NA = co2.replace(-99.99, np.NaN)
+co2_NA.head()
@@ -4421,10 +4445,10 @@
-
# 3. Use interpolated column which estimates missing Avg values
-co2_impute = co2.copy()
-co2_impute['Avg'] = co2['Int']
-co2_impute.head()
+
# 3. Use interpolated column which estimates missing Avg values
+co2_impute = co2.copy()
+co2_impute['Avg'] = co2['Int']
+co2_impute.head()
@@ -4504,30 +4528,30 @@
Code
-
# results of plotting data in 1958
-
-def line_and_points(data, ax, title):
-# assumes single year, hence Mo
- ax.plot('Mo', 'Avg', data=data)
- ax.scatter('Mo', 'Avg', data=data)
- ax.set_xlim(2, 13)
- ax.set_title(title)
- ax.set_xticks(np.arange(3, 13))
-
-def data_year(data, year):
-return data[data["Yr"] ==1958]
-
-# uses matplotlib subplots
-# you may see more next week; focus on output for now
-fig, axes = plt.subplots(ncols =3, figsize=(12, 4), sharey=True)
-
-year =1958
-line_and_points(data_year(co2_drop, year), axes[0], title="1. Drop Missing")
-line_and_points(data_year(co2_NA, year), axes[1], title="2. Missing Set to NaN")
-line_and_points(data_year(co2_impute, year), axes[2], title="3. Missing Interpolated")
-
-fig.suptitle(f"Monthly Averages for {year}")
-plt.tight_layout()
+
# results of plotting data in 1958
+
+def line_and_points(data, ax, title):
+# assumes single year, hence Mo
+ ax.plot('Mo', 'Avg', data=data)
+ ax.scatter('Mo', 'Avg', data=data)
+ ax.set_xlim(2, 13)
+ ax.set_title(title)
+ ax.set_xticks(np.arange(3, 13))
+
+def data_year(data, year):
+return data[data["Yr"] ==1958]
+
+# uses matplotlib subplots
+# you may see more next week; focus on output for now
+fig, axes = plt.subplots(ncols =3, figsize=(12, 4), sharey=True)
+
+year =1958
+line_and_points(data_year(co2_drop, year), axes[0], title="1. Drop Missing")
+line_and_points(data_year(co2_NA, year), axes[1], title="2. Missing Set to NaN")
+line_and_points(data_year(co2_impute, year), axes[2], title="3. Missing Interpolated")
+
+fig.suptitle(f"Monthly Averages for {year}")
+plt.tight_layout()
@@ -4544,8 +4568,8 @@
Code
-
sns.lineplot(x='DecDate', y='Avg', data=co2_impute)
-plt.title("CO2 Average By Month, Imputed");
+
sns.lineplot(x='DecDate', y='Avg', data=co2_impute)
+plt.title("CO2 Average By Month, Imputed");
@@ -4572,9 +4596,9 @@
Code
-
co2_year = co2_impute.groupby('Yr').mean()
-sns.lineplot(x='Yr', y='Avg', data=co2_year)
-plt.title("CO2 Average By Year");
+
co2_year = co2_impute.groupby('Yr').mean()
+sns.lineplot(x='Yr', y='Avg', data=co2_year)
+plt.title("CO2 Average By Year");
@@ -4915,1218 +4939,1218 @@
<
Source Code
-
---
-title: Data Cleaning and EDA
-execute:
- echo: true
-format:
- html:
- code-fold: true
- code-tools: true
- toc: true
- toc-title: Data Cleaning and EDA
- page-layout: full
- theme:
- - cosmo
- - cerulean
- callout-icon: false
-jupyter: python3
----
-
-```{python}
-#| code-fold: true
-import numpy as np
-import pandas as pd
-
-import matplotlib.pyplot as plt
-import seaborn as sns
-#%matplotlib inline
-plt.rcParams['figure.figsize'] = (12, 9)
-
-sns.set()
-sns.set_context('talk')
-np.set_printoptions(threshold=20, precision=2, suppress=True)
-pd.set_option('display.max_rows', 30)
-pd.set_option('display.max_columns', None)
-pd.set_option('display.precision', 2)
-# This option stops scientific notation for pandas
-pd.set_option('display.float_format', '{:.2f}'.format)
-
-# Silence some spurious seaborn warnings
-import warnings
-warnings.filterwarnings("ignore", category=FutureWarning)
-```
-
-::: {.callout-note collapse="false"}
-## Learning Outcomes
-* Recognize common file formats
-* Categorize data by its variable type
-* Build awareness of issues with data faithfulness and develop targeted solutions
-:::
-
-**This content is covered in lectures 4, 5, and 6.**
-
-In the past few lectures, we've learned that `pandas` is a toolkit to restructure, modify, and explore a dataset. What we haven't yet touched on is *how* to make these data transformation decisions. When we receive a new set of data from the "real world," how do we know what processing we should do to convert this data into a usable form?
-
-**Data cleaning**, also called **data wrangling**, is the process of transforming raw data to facilitate subsequent analysis. It is often used to address issues like:
-
-* Unclear structure or formatting
-* Missing or corrupted values
-* Unit conversions
-* ...and so on
-
-**Exploratory Data Analysis (EDA)** is the process of understanding a new dataset. It is an open-ended, informal analysis that involves familiarizing ourselves with the variables present in the data, discovering potential hypotheses, and identifying possible issues with the data. This last point can often motivate further data cleaning to address any problems with the dataset's format; because of this, EDA and data cleaning are often thought of as an "infinite loop," with each process driving the other.
-
-In this lecture, we will consider the key properties of data to consider when performing data cleaning and EDA. In doing so, we'll develop a "checklist" of sorts for you to consider when approaching a new dataset. Throughout this process, we'll build a deeper understanding of this early (but very important!) stage of the data science lifecycle.
-
-## Structure
-
-### File Formats
-There are many file types for storing structured data: TSV, JSON, XML, ASCII, SAS, etc. We'll only cover CSV, TSV, and JSON in lecture, but you'll likely encounter other formats as you work with different datasets. Reading documentation is your best bet for understanding how to process the multitude of different file types.
-
-#### CSV
-CSVs, which stand for **Comma-Separated Values**, are a common tabular data format.
-In the past two `pandas` lectures, we briefly touched on the idea of file format: the way data is encoded in a file for storage. Specifically, our `elections` and `babynames` datasets were stored and loaded as CSVs:
-
-```{python}
-#| code-fold: false
-pd.read_csv("data/elections.csv").head(5)
-```
-
-To better understand the properties of a CSV, let's take a look at the first few rows of the raw data file to see what it looks like before being loaded into a `DataFrame`. We'll use the `repr()` function to return the raw string with its special characters:
-
-```{python}
-#| code-fold: false
-withopen("data/elections.csv", "r") as table:
- i =0
-for row in table:
-print(repr(row))
- i +=1
-if i >3:
-break
-```
-
-Each row, or **record**, in the data is delimited by a newline `\n`. Each column, or **field**, in the data is delimited by a comma `,` (hence, comma-separated!).
-
-#### TSV
-
-Another common file type is **TSV (Tab-Separated Values)**. In a TSV, records are still delimited by a newline `\n`, while fields are delimited by `\t` tab character.
-
-Let's check out the first few rows of the raw TSV file. Again, we'll use the `repr()` function so that `print` shows the special characters.
-
-```{python}
-#| code-fold: false
-withopen("data/elections.txt", "r") as table:
- i =0
-for row in table:
-print(repr(row))
- i +=1
-if i >3:
-break
-```
-
-TSVs can be loaded into `pandas` using `pd.read_csv`. We'll need to specify the **delimiter** with parameter` sep='\t'`[(documentation)](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html).
-
-```{python}
-#| code-fold: false
-pd.read_csv("data/elections.txt", sep='\t').head(3)
-```
-
-An issue with CSVs and TSVs comes up whenever there are commas or tabs within the records. How does `pandas` differentiate between a comma delimiter vs. a comma within the field itself, for example `8,900`? To remedy this, check out the [`quotechar` parameter](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html).
-
-#### JSON
-**JSON (JavaScript Object Notation)** files behave similarly to Python dictionaries. A raw JSON is shown below.
-
-```{python}
-#| code-fold: false
-withopen("data/elections.json", "r") as table:
- i =0
-for row in table:
-print(row)
- i +=1
-if i >8:
-break
-```
-
-JSON files can be loaded into `pandas` using `pd.read_json`.
-
-```{python}
-#| code-fold: false
-pd.read_json('data/elections.json').head(3)
-```
-
-##### EDA with JSON: Berkeley COVID-19 Data
-The City of Berkeley Open Data [website](https://data.cityofberkeley.info/Health/COVID-19-Confirmed-Cases/xn6j-b766) has a dataset with COVID-19 Confirmed Cases among Berkeley residents by date. Let's download the file and save it as a JSON (note the source URL file type is also a JSON). In the interest of reproducible data science, we will download the data programatically. We have defined some helper functions in the [`ds100_utils.py`](https://ds100.org/fa23/resources/assets/lectures/lec05/lec05-eda.html) file that we can reuse these helper functions in many different notebooks.
-
-```{python}
-#| code-fold: false
-from ds100_utils import fetch_and_cache
-
-covid_file = fetch_and_cache(
-"https://data.cityofberkeley.info/api/views/xn6j-b766/rows.json?accessType=DOWNLOAD",
-"confirmed-cases.json",
- force=False)
-covid_file # a file path wrapper object
-```
-
-###### File Size
-Let's start our analysis by getting a rough estimate of the size of the dataset to inform the tools we use to view the data. For relatively small datasets, we can use a text editor or spreadsheet. For larger datasets, more programmatic exploration or distributed computing tools may be more fitting. Here we will use `Python` tools to probe the file.
-
-Since there seem to be text files, let's investigate the number of lines, which often corresponds to the number of records
-
-```{python}
-#| code-fold: false
-import os
-
-print(covid_file, "is", os.path.getsize(covid_file) /1e6, "MB")
-
-withopen(covid_file, "r") as f:
-print(covid_file, "is", sum(1for l in f), "lines.")
-```
-
-###### Unix Commands
-As part of the EDA workflow, Unix commands can come in very handy. In fact, there's an entire book called ["Data Science at the Command Line"](https://datascienceatthecommandline.com/) that explores this idea in depth!
-In Jupyter/IPython, you can prefix lines with `!` to execute arbitrary Unix commands, and within those lines, you can refer to `Python` variables and expressions with the syntax `{expr}`.
-
-Here, we use the `ls` command to list files, using the `-lh` flags, which request "long format with information in human-readable form." We also use the `wc` command for "word count," but with the `-l` flag, which asks for line counts instead of words.
-
-These two give us the same information as the code above, albeit in a slightly different form:
-
-```{python}
-#| code-fold: false
-!ls -lh {covid_file}
-!wc -l {covid_file}
-```
-
-###### File Contents
-Let's explore the data format using `Python`.
-
-```{python}
-#| code-fold: false
-withopen(covid_file, "r") as f:
-for i, row inenumerate(f):
-print(repr(row)) # print raw strings
-if i >=4: break
-```
-
-We can use the `head` Unix command (which is where `pandas`' `head` method comes from!) to see the first few lines of the file:
-
-```{python}
-#| code-fold: false
-!head -5 {covid_file}
-```
-
-In order to load the JSON file into `pandas`, Let's first do some EDA with `Python`'s `json` package to understand the particular structure of this JSON file so that we can decide what (if anything) to load into `pandas`. `Python` has relatively good support for JSON data since it closely matches the internal python object model. In the following cell we import the entire JSON datafile into a python dictionary using the `json` package.
-
-```{python}
-#| code-fold: false
-import json
-
-withopen(covid_file, "rb") as f:
- covid_json = json.load(f)
-```
-
-The `covid_json` variable is now a dictionary encoding the data in the file:
-
-```{python}
-#| code-fold: false
-type(covid_json)
-```
-
-We can examine what keys are in the top level json object by listing out the keys.
-
-```{python}
-#| code-fold: false
-covid_json.keys()
-```
-
-**Observation**: The JSON dictionary contains a `meta` key which likely refers to meta data (data about the data). Meta data often maintained with the data and can be a good source of additional information.
-
-
-We can investigate the meta data further by examining the keys associated with the metadata.
-
-```{python}
-#| code-fold: false
-covid_json['meta'].keys()
-```
-
-The `meta` key contains another dictionary called `view`. This likely refers to meta-data about a particular "view" of some underlying database. We will learn more about views when we study SQL later in the class.
-
-```{python}
-#| code-fold: false
-covid_json['meta']['view'].keys()
-```
-
-Notice that this a nested/recursive data structure. As we dig deeper we reveal more and more keys and the corresponding data:
-
-```
-meta
-|-> data
- | ... (haven't explored yet)
-|-> view
- | -> id
- | -> name
- | -> attribution
- ...
- | -> description
- ...
- | -> columns
- ...
-```
-
-
-There is a key called description in the view sub dictionary. This likely contains a description of the data:
-
-```{python}
-#| code-fold: false
-print(covid_json['meta']['view']['description'])
-```
-
-###### Examining the Data Field for Records
-
-We can look at a few entries in the `data` field. This is what we'll load into `pandas`.
-
-```{python}
-#| code-fold: false
-for i inrange(3):
-print(f"{i:03} | {covid_json['data'][i]}")
-```
-
-Observations:
-* These look like equal-length records, so maybe `data` is a table!
-* But what do each of values in the record mean? Where can we find column headers?
-
-For that, we'll need the `columns` key in the metadata dictionary. This returns a list:
-
-```{python}
-#| code-fold: false
-type(covid_json['meta']['view']['columns'])
-```
-
-###### Summary of exploring the JSON file
-
-1. The above **metadata** tells us a lot about the columns in the data including column names, potential data anomalies, and a basic statistic.
-1. Because of its non-tabular structure, JSON makes it easier (than CSV) to create **self-documenting data**, meaning that information about the data is stored in the same file as the data.
-1. Self-documenting data can be helpful since it maintains its own description and these descriptions are more likely to be updated as data changes.
-
-###### Loading COVID Data into `pandas`
-Finally, let's load the data (not the metadata) into a `pandas``DataFrame`. In the following block of code we:
-
-1. Translate the JSON records into a `DataFrame`:
-
- * fields: `covid_json['meta']['view']['columns']`
- * records: `covid_json['data']`
-
-
-1. Remove columns that have no metadata description. This would be a bad idea in general, but here we remove these columns since the above analysis suggests they are unlikely to contain useful information.
-
-1. Examine the `tail` of the table.
-
-```{python}
-#| code-fold: false
-# Load the data from JSON and assign column titles
-covid = pd.DataFrame(
- covid_json['data'],
- columns=[c['name'] for c in covid_json['meta']['view']['columns']])
-
-covid.tail()
-```
-
-### Variable Types
-
-After loading data into a file, it's a good idea to take the time to understand what pieces of information are encoded in the dataset. In particular, we want to identify what variable types are present in our data. Broadly speaking, we can categorize variables into one of two overarching types.
-
-**Quantitative variables** describe some numeric quantity or amount. We can divide quantitative data further into:
-
-* **Continuous quantitative variables**: numeric data that can be measured on a continuous scale to arbitrary precision. Continuous variables do not have a strict set of possible values – they can be recorded to any number of decimal places. For example, weights, GPA, or CO<sub>2</sub> concentrations.
-* **Discrete quantitative variables**: numeric data that can only take on a finite set of possible values. For example, someone's age or the number of siblings they have.
-
-**Qualitative variables**, also known as **categorical variables**, describe data that isn't measuring some quantity or amount. The sub-categories of categorical data are:
-
-* **Ordinal qualitative variables**: categories with ordered levels. Specifically, ordinal variables are those where the difference between levels has no consistent, quantifiable meaning. Some examples include levels of education (high school, undergrad, grad, etc.), income bracket (low, medium, high), or Yelp rating.
-* **Nominal qualitative variables**: categories with no specific order. For example, someone's political affiliation or Cal ID number.
-
-![Classification of variable types](images/variable.png)
-
-Note that many variables don't sit neatly in just one of these categories. Qualitative variables could have numeric levels, and conversely, quantitative variables could be stored as strings.
-
-### Primary and Foreign Keys
-
-Last time, we introduced `.merge` as the `pandas` method for joining multiple `DataFrame`s together. In our discussion of joins, we touched on the idea of using a "key" to determine what rows should be merged from each table. Let's take a moment to examine this idea more closely.
-
-The **primary key** is the column or set of columns in a table that *uniquely* determine the values of the remaining columns. It can be thought of as the unique identifier for each individual row in the table. For example, a table of Data 100 students might use each student's Cal ID as the primary key.
-
-```{python}
-#| echo: false
-pd.DataFrame({"Cal ID":[3034619471, 3035619472, 3025619473, 3046789372], \
-"Name":["Oski", "Ollie", "Orrie", "Ollie"], \
-"Major":["Data Science", "Computer Science", "Data Science", "Economics"]})
-```
-
-The **foreign key** is the column or set of columns in a table that reference primary keys in other tables. Knowing a dataset's foreign keys can be useful when assigning the `left_on` and `right_on` parameters of `.merge`. In the table of office hour tickets below, `"Cal ID"` is a foreign key referencing the previous table.
-
-```{python}
-#| echo: false
-pd.DataFrame({"OH Request":[1, 2, 3, 4], \
-"Cal ID":[3034619471, 3035619472, 3025619473, 3035619472], \
-"Question":["HW 2 Q1", "HW 2 Q3", "Lab 3 Q4", "HW 2 Q7"]})
-```
-
-## Granularity, Scope, and Temporality
-
-After understanding the structure of the dataset, the next task is to determine what exactly the data represents. We'll do so by considering the data's granularity, scope, and temporality.
-
-### Granularity
-The **granularity** of a dataset is what a single row represents. You can also think of it as the level of detail included in the data. To determine the data's granularity, ask: what does each row in the dataset represent? Fine-grained data contains a high level of detail, with a single row representing a small individual unit. For example, each record may represent one person. Coarse-grained data is encoded such that a single row represents a large individual unit – for example, each record may represent a group of people.
-
-### Scope
-The **scope** of a dataset is the subset of the population covered by the data. If we were investigating student performance in Data Science courses, a dataset with a narrow scope might encompass all students enrolled in Data 100 whereas a dataset with an expansive scope might encompass all students in California.
-
-### Temporality
-The **temporality** of a dataset describes the periodicity over which the data was collected as well as when the data was most recently collected or updated.
-
-Time and date fields of a dataset could represent a few things:
-
-1. when the "event" happened
-2. when the data was collected, or when it was entered into the system
-3. when the data was copied into the database
-
-To fully understand the temporality of the data, it also may be necessary to standardize time zones or inspect recurring time-based trends in the data (do patterns recur in 24-hour periods? Over the course of a month? Seasonally?). The convention for standardizing time is the Coordinated Universal Time (UTC), an international time standard measured at 0 degrees latitude that stays consistent throughout the year (no daylight savings). We can represent Berkeley's time zone, Pacific Standard Time (PST), as UTC-7 (with daylight savings).
-
-#### Temporality with `pandas`' `dt` accessors
-Let's briefly look at how we can use `pandas`' `dt` accessors to work with dates/times in a dataset using the dataset you'll see in Lab 3: the Berkeley PD Calls for Service dataset.
-
-```{python}
-#| code-fold: true
-calls = pd.read_csv("data/Berkeley_PD_-_Calls_for_Service.csv")
-calls.head()
-```
-
-Looks like there are three columns with dates/times: `EVENTDT`, `EVENTTM`, and `InDbDate`.
-
-Most likely, `EVENTDT` stands for the date when the event took place, `EVENTTM` stands for the time of day the event took place (in 24-hr format), and `InDbDate` is the date this call is recorded onto the database.
-
-If we check the data type of these columns, we will see they are stored as strings. We can convert them to `datetime` objects using pandas `to_datetime` function.
-
-```{python}
-#| code-fold: false
-calls["EVENTDT"] = pd.to_datetime(calls["EVENTDT"])
-calls.head()
-```
-
-Now, we can use the `dt` accessor on this column.
-
-We can get the month:
-
-```{python}
-#| code-fold: false
-calls["EVENTDT"].dt.month.head()
-```
-
-Which day of the week the date is on:
-
-```{python}
-#| code-fold: false
-calls["EVENTDT"].dt.dayofweek.head()
-```
-
-Check the mimimum values to see if there are any suspicious-looking, 70s dates:
-
-```{python}
-#| code-fold: false
-calls.sort_values("EVENTDT").head()
-```
-
-Doesn't look like it! We are good!
-
-
-We can also do many things with the `dt` accessor like switching time zones and converting time back to UNIX/POSIX time. Check out the documentation on [`.dt` accessor](https://pandas.pydata.org/docs/user_guide/basics.html#basics-dt-accessors) and [time series/date functionality](https://pandas.pydata.org/docs/user_guide/timeseries.html#).
-
-## Faithfulness
-
-At this stage in our data cleaning and EDA workflow, we've achieved quite a lot: we've identified how our data is structured, come to terms with what information it encodes, and gained insight as to how it was generated. Throughout this process, we should always recall the original intent of our work in Data Science – to use data to better understand and model the real world. To achieve this goal, we need to ensure that the data we use is faithful to reality; that is, that our data accurately captures the "real world."
-
-Data used in research or industry is often "messy" – there may be errors or inaccuracies that impact the faithfulness of the dataset. Signs that data may not be faithful include:
-
-* Unrealistic or "incorrect" values, such as negative counts, locations that don't exist, or dates set in the future
-* Violations of obvious dependencies, like an age that does not match a birthday
-* Clear signs that data was entered by hand, which can lead to spelling errors or fields that are incorrectly shifted
-* Signs of data falsification, such as fake email addresses or repeated use of the same names
-* Duplicated records or fields containing the same information
-* Truncated data, e.g. Microsoft Excel would limit the number of rows to 655536 and the number of columns to 255
-
-We often solve some of these more common issues in the following ways:
-
-* Spelling errors: apply corrections or drop records that aren't in a dictionary
-* Time zone inconsistencies: convert to a common time zone (e.g. UTC)
-* Duplicated records or fields: identify and eliminate duplicates (using primary keys)
-* Unspecified or inconsistent units: infer the units and check that values are in reasonable ranges in the data
-
-### Missing Values
-Another common issue encountered with real-world datasets is that of missing data. One strategy to resolve this is to simply drop any records with missing values from the dataset. This does, however, introduce the risk of inducing biases – it is possible that the missing or corrupt records may be systemically related to some feature of interest in the data. Another solution is to keep the data as `NaN` values.
-
-A third method to address missing data is to perform **imputation**: infer the missing values using other data available in the dataset. There is a wide variety of imputation techniques that can be implemented; some of the most common are listed below.
-
-* Average imputation: replace missing values with the average value for that field
-* Hot deck imputation: replace missing values with some random value
-* Regression imputation: develop a model to predict missing values
-* Multiple imputation: replace missing values with multiple random values
-
-Regardless of the strategy used to deal with missing data, we should think carefully about *why* particular records or fields may be missing – this can help inform whether or not the absence of these values is significant or meaningful.
-
-# EDA Demo 1: Tuberculosis in the United States
-
-Now, let's walk through the data-cleaning and EDA workflow to see what can we learn about the presence of Tuberculosis in the United States!
-
-We will examine the data included in the [original CDC article](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w#T1_down) published in 2021.
-
-
-## CSVs and Field Names
-Suppose Table 1 was saved as a CSV file located in `data/cdc_tuberculosis.csv`.
-
-We can then explore the CSV (which is a text file, and does not contain binary-encoded data) in many ways:
-1. Using a text editor like emacs, vim, VSCode, etc.
-2. Opening the CSV directly in DataHub (read-only), Excel, Google Sheets, etc.
-3. The `Python` file object
-4. `pandas`, using `pd.read_csv()`
-
-To try out options 1 and 2, you can view or download the Tuberculosis from the [lecture demo notebook](https://data100.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2FDS-100%2Ffa23-student&urlpath=lab%2Ftree%2Ffa23-student%2Flecture%2Flec05%2Flec04-eda.ipynb&branch=main) under the `data` folder in the left hand menu. Notice how the CSV file is a type of **rectangular data (i.e., tabular data) stored as comma-separated values**.
-
-Next, let's try out option 3 using the `Python` file object. We'll look at the first four lines:
-
-```{python}
-#| code-fold: true
-withopen("data/cdc_tuberculosis.csv", "r") as f:
- i =0
-for row in f:
-print(row)
- i +=1
-if i >3:
-break
-```
-
-Whoa, why are there blank lines interspaced between the lines of the CSV?
-
-You may recall that all line breaks in text files are encoded as the special newline character `\n`. Python's `print()` prints each string (including the newline), and an additional newline on top of that.
-
-If you're curious, we can use the `repr()` function to return the raw string with all special characters:
-
-```{python}
-#| code-fold: true
-withopen("data/cdc_tuberculosis.csv", "r") as f:
- i =0
-for row in f:
-print(repr(row)) # print raw strings
- i +=1
-if i >3:
-break
-```
-
-Finally, let's try option 4 and use the tried-and-true Data 100 approach: `pandas`.
-
-```{python}
-#| code-fold: false
-tb_df = pd.read_csv("data/cdc_tuberculosis.csv")
-tb_df.head()
-```
-
-You may notice some strange things about this table: what's up with the "Unnamed" column names and the first row?
-
-Congratulations — you're ready to wrangle your data! Because of how things are stored, we'll need to clean the data a bit to name our columns better.
-
-A reasonable first step is to identify the row with the right header. The `pd.read_csv()` function ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)) has the convenient `header` parameter that we can set to use the elements in row 1 as the appropriate columns:
-
-```{python}
-#| code-fold: false
-tb_df = pd.read_csv("data/cdc_tuberculosis.csv", header=1) # row index
-tb_df.head(5)
-```
-
-Wait...but now we can't differentiate betwen the "Number of TB cases" and "TB incidence" year columns. `pandas` has tried to make our lives easier by automatically adding ".1" to the latter columns, but this doesn't help us, as humans, understand the data.
-
-We can do this manually with `df.rename()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html?highlight=rename#pandas.DataFrame.rename)):
-
-```{python}
-#| code-fold: false
-rename_dict = {'2019': 'TB cases 2019',
-'2020': 'TB cases 2020',
-'2021': 'TB cases 2021',
-'2019.1': 'TB incidence 2019',
-'2020.1': 'TB incidence 2020',
-'2021.1': 'TB incidence 2021'}
-tb_df = tb_df.rename(columns=rename_dict)
-tb_df.head(5)
-```
-
-## Record Granularity
-
-You might already be wondering: what's up with that first record?
-
-Row 0 is what we call a **rollup record**, or summary record. It's often useful when displaying tables to humans. The **granularity** of record 0 (Totals) vs the rest of the records (States) is different.
-
-Okay, EDA step two. How was the rollup record aggregated?
-
-Let's check if Total TB cases is the sum of all state TB cases. If we sum over all rows, we should get **2x** the total cases in each of our TB cases by year (why do you think this is?).
-
-```{python}
-#| code-fold: true
-tb_df.sum(axis=0)
-```
-
-Whoa, what's going on with the TB cases in 2019, 2020, and 2021? Check out the column types:
-
-```{python}
-#| code-fold: true
-tb_df.dtypes
-```
-
-Since there are commas in the values for TB cases, the numbers are read as the `object` datatype, or **storage type** (close to the `Python` string datatype), so `pandas` is concatenating strings instead of adding integers (recall that `Python` can "sum", or concatenate, strings together: `"data" + "100"` evaluates to `"data100"`).
-
-
-Fortunately `read_csv` also has a `thousands` parameter ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)):
-
-```{python}
-#| code-fold: false
-# improve readability: chaining method calls with outer parentheses/line breaks
-tb_df = (
- pd.read_csv("data/cdc_tuberculosis.csv", header=1, thousands=',')
- .rename(columns=rename_dict)
-)
-tb_df.head(5)
-```
-
-```{python}
-#| code-fold: false
-tb_df.sum()
-```
-
-The Total TB cases look right. Phew!
-
-Let's just look at the records with **state-level granularity**:
-
-```{python}
-#| code-fold: true
-state_tb_df = tb_df[1:]
-state_tb_df.head(5)
-```
-
-## Gather Census Data
-
-U.S. Census population estimates [source](https://www.census.gov/data/tables/time-series/demo/popest/2010s-state-total.html) (2019), [source](https://www.census.gov/data/tables/time-series/demo/popest/2020s-state-total.html) (2020-2021).
-
-Running the below cells cleans the data.
-There are a few new methods here:
-* `df.convert_dtypes()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.convert_dtypes.html)) conveniently converts all float dtypes into ints and is out of scope for the class.
-* `df.drop_na()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html)) will be explained in more detail next time.
-
-```{python}
-#| code-fold: true
-# 2010s census data
-census_2010s_df = pd.read_csv("data/nst-est2019-01.csv", header=3, thousands=",")
-census_2010s_df = (
- census_2010s_df
- .reset_index()
- .drop(columns=["index", "Census", "Estimates Base"])
- .rename(columns={"Unnamed: 0": "Geographic Area"})
- .convert_dtypes() # "smart" converting of columns, use at your own risk
- .dropna() # we'll introduce this next time
-)
-census_2010s_df['Geographic Area'] = census_2010s_df['Geographic Area'].str.strip('.')
-
-# with pd.option_context('display.min_rows', 30): # shows more rows
-# display(census_2010s_df)
-
-census_2010s_df.head(5)
-```
-
-Occasionally, you will want to modify code that you have imported. To reimport those modifications you can either use `python`'s `importlib` library:
-
-```python
-from importlib importreload
-reload(utils)
-```
-
-or use `iPython` magic which will intelligently import code when files change:
-
-```python
-%load_ext autoreload
-%autoreload 2
-```
-
-```{python}
-#| code-fold: true
-# census 2020s data
-census_2020s_df = pd.read_csv("data/NST-EST2022-POP.csv", header=3, thousands=",")
-census_2020s_df = (
- census_2020s_df
- .reset_index()
- .drop(columns=["index", "Unnamed: 1"])
- .rename(columns={"Unnamed: 0": "Geographic Area"})
- .convert_dtypes() # "smart" converting of columns, use at your own risk
- .dropna() # we'll introduce this next time
-)
-census_2020s_df['Geographic Area'] = census_2020s_df['Geographic Area'].str.strip('.')
-
-census_2020s_df.head(5)
-```
-
-## Joining Data (Merging `DataFrame`s)
-
-Time to `merge`! Here we use the `DataFrame` method `df1.merge(right=df2, ...)` on `DataFrame``df1` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)). Contrast this with the function `pd.merge(left=df1, right=df2, ...)` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.merge.html?highlight=pandas%20merge#pandas.merge)). Feel free to use either.
-
-```{python}
-#| code-fold: false
-# merge TB DataFrame with two US census DataFrames
-tb_census_df = (
- tb_df
- .merge(right=census_2010s_df,
- left_on="U.S. jurisdiction", right_on="Geographic Area")
- .merge(right=census_2020s_df,
- left_on="U.S. jurisdiction", right_on="Geographic Area")
-)
-tb_census_df.head(5)
-```
-
-Having all of these columns is a little unwieldy. We could either drop the unneeded columns now, or just merge on smaller census `DataFrame`s. Let's do the latter.
-
-```{python}
-#| code-fold: false
-# try merging again, but cleaner this time
-tb_census_df = (
- tb_df
- .merge(right=census_2010s_df[["Geographic Area", "2019"]],
- left_on="U.S. jurisdiction", right_on="Geographic Area")
- .drop(columns="Geographic Area")
- .merge(right=census_2020s_df[["Geographic Area", "2020", "2021"]],
- left_on="U.S. jurisdiction", right_on="Geographic Area")
- .drop(columns="Geographic Area")
-)
-tb_census_df.head(5)
-```
-
-## Reproducing Data: Compute Incidence
-
-Let's recompute incidence to make sure we know where the original CDC numbers came from.
-
-From the [CDC report](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w#T1_down): TB incidence is computed as “Cases per 100,000 persons using mid-year population estimates from the U.S. Census Bureau.”
-
-If we define a group as 100,000 people, then we can compute the TB incidence for a given state population as
-
-$$\text{TB incidence} = \frac{\text{TB cases in population}}{\text{groups in population}} = \frac{\text{TB cases in population}}{\text{population}/100000} $$
-
-$$= \frac{\text{TB cases in population}}{\text{population}} \times 100000$$
-
-Let's try this for 2019:
-
-```{python}
-#| code-fold: false
-tb_census_df["recompute incidence 2019"] = tb_census_df["TB cases 2019"]/tb_census_df["2019"]*100000
-tb_census_df.head(5)
-```
-
-Awesome!!!
-
-Let's use a for-loop and `Python` format strings to compute TB incidence for all years. `Python` f-strings are just used for the purposes of this demo, but they're handy to know when you explore data beyond this course ([documentation](https://docs.python.org/3/tutorial/inputoutput.html)).
-
-```{python}
-#| code-fold: false
-# recompute incidence for all years
-for year in [2019, 2020, 2021]:
- tb_census_df[f"recompute incidence {year}"] = tb_census_df[f"TB cases {year}"]/tb_census_df[f"{year}"]*100000
-tb_census_df.head(5)
-```
-
-These numbers look pretty close!!! There are a few errors in the hundredths place, particularly in 2021. It may be useful to further explore reasons behind this discrepancy.
-
-```{python}
-#| code-fold: false
-tb_census_df.describe()
-```
-
-## Bonus EDA: Reproducing the Reported Statistic
-
-
-**How do we reproduce that reported statistic in the original [CDC report](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w)?**
-
-> Reported TB incidence (cases per 100,000 persons) increased **9.4%**, from **2.2** during 2020 to **2.4** during 2021 but was lower than incidence during 2019 (2.7). Increases occurred among both U.S.-born and non–U.S.-born persons.
-
-This is TB incidence computed across the entire U.S. population! How do we reproduce this?
-* We need to reproduce the "Total" TB incidences in our rolled record.
-* But our current `tb_census_df` only has 51 entries (50 states plus Washington, D.C.). There is no rolled record.
-* What happened...?
-
-Let's get exploring!
-
-Before we keep exploring, we'll set all indexes to more meaningful values, instead of just numbers that pertain to some row at some point. This will make our cleaning slightly easier.
-
-```{python}
-#| code-fold: true
-tb_df = tb_df.set_index("U.S. jurisdiction")
-tb_df.head(5)
-```
-
-```{python}
-#| code-fold: false
-census_2010s_df = census_2010s_df.set_index("Geographic Area")
-census_2010s_df.head(5)
-```
-
-```{python}
-#| code-fold: false
-census_2020s_df = census_2020s_df.set_index("Geographic Area")
-census_2020s_df.head(5)
-```
-
-It turns out that our merge above only kept state records, even though our original `tb_df` had the "Total" rolled record:
-
-```{python}
-#| code-fold: false
-tb_df.head()
-```
-
-Recall that `merge` by default does an **inner** merge by default, meaning that it only preserves keys that are present in **both** `DataFrame`s.
-
-The rolled records in our census `DataFrame` have different `Geographic Area` fields, which was the key we merged on:
-
-```{python}
-#| code-fold: false
-census_2010s_df.head(5)
-```
-
-The Census `DataFrame` has several rolled records. The aggregate record we are looking for actually has the Geographic Area named "United States".
-
-One straightforward way to get the right merge is to rename the value itself. Because we now have the Geographic Area index, we'll use `df.rename()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html)):
-
-```{python}
-#| code-fold: false
-# rename rolled record for 2010s
-census_2010s_df.rename(index={'United States':'Total'}, inplace=True)
-census_2010s_df.head(5)
-```
-
-```{python}
-#| code-fold: false
-# same, but for 2020s rename rolled record
-census_2020s_df.rename(index={'United States':'Total'}, inplace=True)
-census_2020s_df.head(5)
-```
-
-<br/>
-
-Next let's rerun our merge. Note the different chaining, because we are now merging on indexes (`df.merge()`[documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)).
-
-```{python}
-#| code-fold: false
-tb_census_df = (
- tb_df
- .merge(right=census_2010s_df[["2019"]],
- left_index=True, right_index=True)
- .merge(right=census_2020s_df[["2020", "2021"]],
- left_index=True, right_index=True)
-)
-tb_census_df.head(5)
-```
-
-<br/>
-
-Finally, let's recompute our incidences:
-
-```{python}
-#| code-fold: false
-# recompute incidence for all years
-for year in [2019, 2020, 2021]:
- tb_census_df[f"recompute incidence {year}"] = tb_census_df[f"TB cases {year}"]/tb_census_df[f"{year}"]*100000
-tb_census_df.head(5)
-```
-
-We reproduced the total U.S. incidences correctly!
-
-We're almost there. Let's revisit the quote:
-
-> Reported TB incidence (cases per 100,000 persons) increased **9.4%**, from **2.2** during 2020 to **2.4** during 2021 but was lower than incidence during 2019 (2.7). Increases occurred among both U.S.-born and non–U.S.-born persons.
-
-Recall that percent change from $A$ to $B$ is computed as
-$\text{percent change} = \frac{B - A}{A} \times 100$.
-
-```{python}
-#| code-fold: false
-#| tags: []
-incidence_2020 = tb_census_df.loc['Total', 'recompute incidence 2020']
-incidence_2020
-```
-
-```{python}
-#| code-fold: false
-#| tags: []
-incidence_2021 = tb_census_df.loc['Total', 'recompute incidence 2021']
-incidence_2021
-```
-
-```{python}
-#| code-fold: false
-#| tags: []
-difference = (incidence_2021 - incidence_2020)/incidence_2020 *100
-difference
-```
-
-# EDA Demo 2: Mauna Loa CO<sub>2</sub> Data -- A Lesson in Data Faithfulness
-
-[Mauna Loa Observatory](https://gml.noaa.gov/ccgg/trends/data.html) has been monitoring CO<sub>2</sub> concentrations since 1958
-
-```{python}
-#| code-fold: false
-co2_file ="data/co2_mm_mlo.txt"
-```
-
-Let's do some **EDA**!!
-
-## Reading this file into Pandas?
-Let's instead check out this `.txt` file. Some questions to keep in mind: Do we trust this file extension? What structure is it?
-
-Lines 71-78 (inclusive) are shown below:
-
- line number | file contents
-
- 71 | # decimal average interpolated trend #days
- 72 | # date (season corr)
- 73 | 1958 3 1958.208 315.71 315.71 314.62 -1
- 74 | 1958 4 1958.292 317.45 317.45 315.29 -1
- 75 | 1958 5 1958.375 317.50 317.50 314.71 -1
- 76 | 1958 6 1958.458 -99.99 317.10 314.85 -1
- 77 | 1958 7 1958.542 315.86 315.86 314.98 -1
- 78 | 1958 8 1958.625 314.93 314.93 315.94 -1
-
-
-Notice how:
-
-- The values are separated by white space, possibly tabs.
-- The data line up down the rows. For example, the month appears in 7th to 8th position of each line.
-- The 71st and 72nd lines in the file contain column headings split over two lines.
-
-We can use `read_csv` to read the data into a `pandas``DataFrame`, and we provide several arguments to specify that the separators are white space, there is no header (**we will set our own column names**), and to skip the first 72 rows of the file.
-
-```{python}
-#| code-fold: false
-co2 = pd.read_csv(
- co2_file, header =None, skiprows =72,
- sep =r'\s+'#delimiter for continuous whitespace (stay tuned for regex next lecture))
-)
-co2.head()
-```
-
-Congratulations! You've wrangled the data!
-
-<br/>
-
-...But our columns aren't named.
-**We need to do more EDA.**
-
-## Exploring Variable Feature Types
-
-The NOAA [webpage](https://gml.noaa.gov/ccgg/trends/) might have some useful tidbits (in this case it doesn't).
-
-Using this information, we'll rerun `pd.read_csv`, but this time with some **custom column names.**
-
-```{python}
-#| code-fold: false
-co2 = pd.read_csv(
- co2_file, header =None, skiprows =72,
- sep ='\s+', #regex for continuous whitespace (next lecture)
- names = ['Yr', 'Mo', 'DecDate', 'Avg', 'Int', 'Trend', 'Days']
-)
-co2.head()
-```
-
-## Visualizing CO<sub>2</sub>
-Scientific studies tend to have very clean data, right...? Let's jump right in and make a time series plot of CO2 monthly averages.
-
-```{python}
-#| code-fold: true
-sns.lineplot(x='DecDate', y='Avg', data=co2);
-```
-
-The code above uses the `seaborn` plotting library (abbreviated `sns`). We will cover this in the Visualization lecture, but now you don't need to worry about how it works!
-
-Yikes! Plotting the data uncovered a problem. The sharp vertical lines suggest that we have some **missing values**. What happened here?
-
-```{python}
-#| code-fold: false
-co2.head()
-```
-
-```{python}
-#| code-fold: false
-co2.tail()
-```
-
-Some data have unusual values like -1 and -99.99.
-
-Let's check the description at the top of the file again.
-
-* -1 signifies a missing value for the number of days `Days` the equipment was in operation that month.
-* -99.99 denotes a missing monthly average `Avg`
-
-How can we fix this? First, let's explore other aspects of our data. Understanding our data will help us decide what to do with the missing values.
-
-<br/>
-
-
-## Sanity Checks: Reasoning about the data
-First, we consider the shape of the data. How many rows should we have?
-
-* If chronological order, we should have one record per month.
-* Data from March 1958 to August 2019.
-* We should have $ 12 \times (2019-1957) - 2 - 4 = 738 $ records.
-
-```{python}
-#| code-fold: false
-co2.shape
-```
-
-Nice!! The number of rows (i.e. records) match our expectations.\
-
-<br/>
-
-
-Let's now check the quality of each feature.
-
-## Understanding Missing Value 1: `Days`
-`Days` is a time field, so let's analyze other time fields to see if there is an explanation for missing values of days of operation.
-
-Let's start with **months**, `Mo`.
-
-Are we missing any records? The number of months should have 62 or 61 instances (March 1957-August 2019).
-
-```{python}
-#| code-fold: false
-co2["Mo"].value_counts().sort_index()
-```
-
-As expected Jan, Feb, Sep, Oct, Nov, and Dec have 61 occurrences and the rest 62.
-
-<br/>
-
-Next let's explore **days** `Days` itself, which is the number of days that the measurement equipment worked.
-
-```{python}
-#| code-fold: true
-sns.displot(co2['Days']);
-plt.title("Distribution of days feature");# suppresses unneeded plotting output
-```
-
-In terms of data quality, a handful of months have averages based on measurements taken on fewer than half the days. In addition, there are nearly 200 missing values--**that's about 27% of the data**!
-
-<br/>
-
-Finally, let's check the last time feature, **year** `Yr`.
-
-Let's check to see if there is any connection between missing-ness and the year of the recording.
-
-```{python}
-#| code-fold: true
-sns.scatterplot(x="Yr", y="Days", data=co2);
-plt.title("Day field by Year");# the ; suppresses output
-```
-
-**Observations**:
-
-* All of the missing data are in the early years of operation.
-* It appears there may have been problems with equipment in the mid to late 80s.
-
-**Potential Next Steps**:
-
-* Confirm these explanations through documentation about the historical readings.
-* Maybe drop earliest recordings? However, we would want to delay such action until after we have examined the time trends and assess whether there are any potential problems.
-
-<br/>
-
-## Understanding Missing Value 2: `Avg`
-Next, let's return to the -99.99 values in `Avg` to analyze the overall quality of the CO2 measurements. We'll plot a histogram of the average CO<sub>2</sub> measurements
-
-```{python}
-#| code-fold: true
-# Histograms of average CO2 measurements
-sns.displot(co2['Avg']);
-```
-
-The non-missing values are in the 300-400 range (a regular range of CO2 levels).
-
-We also see that there are only a few missing `Avg` values (**<1% of values**). Let's examine all of them:
-
-```{python}
-#| code-fold: false
-co2[co2["Avg"] <0]
-```
-
-There doesn't seem to be a pattern to these values, other than that most records also were missing `Days` data.
-
-## Drop, `NaN`, or Impute Missing `Avg` Data?
-
-How should we address the invalid `Avg` data?
-
-1. Drop records
-2. Set to NaN
-3. Impute using some strategy
-
-Remember we want to fix the following plot:
-
-```{python}
-#| code-fold: true
-sns.lineplot(x='DecDate', y='Avg', data=co2)
-plt.title("CO2 Average By Month");
-```
-
-Since we are plotting `Avg` vs `DecDate`, we should just focus on dealing with missing values for `Avg`.
-
-
-Let's consider a few options:
-1. Drop those records
-2. Replace -99.99 with NaN
-3. Substitute it with a likely value for the average CO2?
-
-What do you think are the pros and cons of each possible action?
-
-<br/>
-
-
-Let's examine each of these three options.
-
-```{python}
-#| code-fold: false
-# 1. Drop missing values
-co2_drop = co2[co2['Avg'] >0]
-co2_drop.head()
-```
-
-```{python}
-#| code-fold: false
-# 2. Replace NaN with -99.99
-co2_NA = co2.replace(-99.99, np.NaN)
-co2_NA.head()
-```
-
-We'll also use a third version of the data.
-
-First, we note that the dataset already comes with a **substitute value** for the -99.99.
-
-From the file description:
-
-> The `interpolated` column includes average values from the preceding column (`average`)
-and **interpolated values** where data are missing. Interpolated values are
-computed in two steps...
-
-The `Int` feature has values that exactly match those in `Avg`, except when `Avg` is -99.99, and then a **reasonable** estimate is used instead.
-
-So, the third version of our data will use the `Int` feature instead of `Avg`.
-
-```{python}
-#| code-fold: false
-# 3. Use interpolated column which estimates missing Avg values
-co2_impute = co2.copy()
-co2_impute['Avg'] = co2['Int']
-co2_impute.head()
-```
-
-What's a **reasonable** estimate?
-
-To answer this question, let's zoom in on a short time period, say the measurements in 1958 (where we know we have two missing values).
-
-```{python}
-#| code-fold: true
-# results of plotting data in 1958
-
-def line_and_points(data, ax, title):
-# assumes single year, hence Mo
- ax.plot('Mo', 'Avg', data=data)
- ax.scatter('Mo', 'Avg', data=data)
- ax.set_xlim(2, 13)
- ax.set_title(title)
- ax.set_xticks(np.arange(3, 13))
-
-def data_year(data, year):
-return data[data["Yr"] ==1958]
-
-# uses matplotlib subplots
-# you may see more next week; focus on output for now
-fig, axes = plt.subplots(ncols =3, figsize=(12, 4), sharey=True)
-
-year =1958
-line_and_points(data_year(co2_drop, year), axes[0], title="1. Drop Missing")
-line_and_points(data_year(co2_NA, year), axes[1], title="2. Missing Set to NaN")
-line_and_points(data_year(co2_impute, year), axes[2], title="3. Missing Interpolated")
-
-fig.suptitle(f"Monthly Averages for {year}")
-plt.tight_layout()
-```
-
-In the big picture since there are only 7 `Avg` values missing (**<1%** of 738 months), any of these approaches would work.
-
-However there is some appeal to **option C: Imputing**:
-
-* Shows seasonal trends for CO2
-* We are plotting all months in our data as a line plot
-
-<br/>
-
-
-Let's replot our original figure with option 3:
-
-```{python}
-#| code-fold: true
-sns.lineplot(x='DecDate', y='Avg', data=co2_impute)
-plt.title("CO2 Average By Month, Imputed");
-```
-
-Looks pretty close to what we see on the NOAA [website](https://gml.noaa.gov/ccgg/trends/)!
-
-## Presenting the data: A Discussion on Data Granularity
-
-From the description:
-
-* monthly measurements are averages of average day measurements.
-* The NOAA GML website has datasets for daily/hourly measurements too.
-
-The data you present depends on your research question.
-
-**How do CO2 levels vary by season?**
-
-* You might want to keep average monthly data.
-
-**Are CO2 levels rising over the past 50+ years, consistent with global warming predictions?**
-
-* You might be happier with a **coarser granularity** of average year data!
-
-```{python}
-#| code-fold: true
-co2_year = co2_impute.groupby('Yr').mean()
-sns.lineplot(x='Yr', y='Avg', data=co2_year)
-plt.title("CO2 Average By Year");
-```
-
-Indeed, we see a rise by nearly 100 ppm of CO2 since Mauna Loa began recording in 1958.
-
-# Summary
-We went over a lot of content this lecture; let's summarize the most important points:
-
-## Dealing with Missing Values
-There are a few options we can take to deal with missing data:
-
-* Drop missing records
-* Keep `NaN` missing values
-* Impute using an interpolated column
-
-## EDA and Data Wrangling
-There are several ways to approach EDA and Data Wrangling:
-
-* Examine the **data and metadata**: what is the date, size, organization, and structure of the data?
-* Examine each **field/attribute/dimension** individually.
-* Examine pairs of related dimensions (e.g. breaking down grades by major).
-* Along the way, we can:
- * **Visualize** or summarize the data.
- * **Validate assumptions** about data and its collection process. Pay particular attention to when the data was collected.
- * Identify and **address anomalies**.
- * Apply data transformations and corrections (we'll cover this in the upcoming lecture).
- * **Record everything you do!** Developing in Jupyter Notebook promotes *reproducibility* of your own work!
+
---
+title: Data Cleaning and EDA
+execute:
+ echo: true
+format:
+ html:
+ code-fold: true
+ code-tools: true
+ toc: true
+ toc-title: Data Cleaning and EDA
+ page-layout: full
+ theme:
+ - cosmo
+ - cerulean
+ callout-icon: false
+jupyter: python3
+---
+
+```{python}
+#| code-fold: true
+import numpy as np
+import pandas as pd
+
+import matplotlib.pyplot as plt
+import seaborn as sns
+#%matplotlib inline
+plt.rcParams['figure.figsize'] = (12, 9)
+
+sns.set()
+sns.set_context('talk')
+np.set_printoptions(threshold=20, precision=2, suppress=True)
+pd.set_option('display.max_rows', 30)
+pd.set_option('display.max_columns', None)
+pd.set_option('display.precision', 2)
+# This option stops scientific notation for pandas
+pd.set_option('display.float_format', '{:.2f}'.format)
+
+# Silence some spurious seaborn warnings
+import warnings
+warnings.filterwarnings("ignore", category=FutureWarning)
+```
+
+::: {.callout-note collapse="false"}
+## Learning Outcomes
+* Recognize common file formats
+* Categorize data by its variable type
+* Build awareness of issues with data faithfulness and develop targeted solutions
+:::
+
+**This content is covered in lectures 4, 5, and 6.**
+
+In the past few lectures, we've learned that `pandas` is a toolkit to restructure, modify, and explore a dataset. What we haven't yet touched on is *how* to make these data transformation decisions. When we receive a new set of data from the "real world," how do we know what processing we should do to convert this data into a usable form?
+
+**Data cleaning**, also called **data wrangling**, is the process of transforming raw data to facilitate subsequent analysis. It is often used to address issues like:
+
+* Unclear structure or formatting
+* Missing or corrupted values
+* Unit conversions
+* ...and so on
+
+**Exploratory Data Analysis (EDA)** is the process of understanding a new dataset. It is an open-ended, informal analysis that involves familiarizing ourselves with the variables present in the data, discovering potential hypotheses, and identifying possible issues with the data. This last point can often motivate further data cleaning to address any problems with the dataset's format; because of this, EDA and data cleaning are often thought of as an "infinite loop," with each process driving the other.
+
+In this lecture, we will consider the key properties of data to consider when performing data cleaning and EDA. In doing so, we'll develop a "checklist" of sorts for you to consider when approaching a new dataset. Throughout this process, we'll build a deeper understanding of this early (but very important!) stage of the data science lifecycle.
+
+## Structure
+
+### File Formats
+There are many file types for storing structured data: TSV, JSON, XML, ASCII, SAS, etc. We'll only cover CSV, TSV, and JSON in lecture, but you'll likely encounter other formats as you work with different datasets. Reading documentation is your best bet for understanding how to process the multitude of different file types.
+
+#### CSV
+CSVs, which stand for **Comma-Separated Values**, are a common tabular data format.
+In the past two `pandas` lectures, we briefly touched on the idea of file format: the way data is encoded in a file for storage. Specifically, our `elections` and `babynames` datasets were stored and loaded as CSVs:
+
+```{python}
+#| code-fold: false
+pd.read_csv("data/elections.csv").head(5)
+```
+
+To better understand the properties of a CSV, let's take a look at the first few rows of the raw data file to see what it looks like before being loaded into a `DataFrame`. We'll use the `repr()` function to return the raw string with its special characters:
+
+```{python}
+#| code-fold: false
+withopen("data/elections.csv", "r") as table:
+ i =0
+for row in table:
+print(repr(row))
+ i +=1
+if i >3:
+break
+```
+
+Each row, or **record**, in the data is delimited by a newline `\n`. Each column, or **field**, in the data is delimited by a comma `,` (hence, comma-separated!).
+
+#### TSV
+
+Another common file type is **TSV (Tab-Separated Values)**. In a TSV, records are still delimited by a newline `\n`, while fields are delimited by `\t` tab character.
+
+Let's check out the first few rows of the raw TSV file. Again, we'll use the `repr()` function so that `print` shows the special characters.
+
+```{python}
+#| code-fold: false
+withopen("data/elections.txt", "r") as table:
+ i =0
+for row in table:
+print(repr(row))
+ i +=1
+if i >3:
+break
+```
+
+TSVs can be loaded into `pandas` using `pd.read_csv`. We'll need to specify the **delimiter** with parameter` sep='\t'`[(documentation)](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html).
+
+```{python}
+#| code-fold: false
+pd.read_csv("data/elections.txt", sep='\t').head(3)
+```
+
+An issue with CSVs and TSVs comes up whenever there are commas or tabs within the records. How does `pandas` differentiate between a comma delimiter vs. a comma within the field itself, for example `8,900`? To remedy this, check out the [`quotechar` parameter](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html).
+
+#### JSON
+**JSON (JavaScript Object Notation)** files behave similarly to Python dictionaries. A raw JSON is shown below.
+
+```{python}
+#| code-fold: false
+withopen("data/elections.json", "r") as table:
+ i =0
+for row in table:
+print(row)
+ i +=1
+if i >8:
+break
+```
+
+JSON files can be loaded into `pandas` using `pd.read_json`.
+
+```{python}
+#| code-fold: false
+pd.read_json('data/elections.json').head(3)
+```
+
+##### EDA with JSON: Berkeley COVID-19 Data
+The City of Berkeley Open Data [website](https://data.cityofberkeley.info/Health/COVID-19-Confirmed-Cases/xn6j-b766) has a dataset with COVID-19 Confirmed Cases among Berkeley residents by date. Let's download the file and save it as a JSON (note the source URL file type is also a JSON). In the interest of reproducible data science, we will download the data programatically. We have defined some helper functions in the [`ds100_utils.py`](https://ds100.org/fa23/resources/assets/lectures/lec05/lec05-eda.html) file that we can reuse these helper functions in many different notebooks.
+
+```{python}
+#| code-fold: false
+from ds100_utils import fetch_and_cache
+
+covid_file = fetch_and_cache(
+"https://data.cityofberkeley.info/api/views/xn6j-b766/rows.json?accessType=DOWNLOAD",
+"confirmed-cases.json",
+ force=False)
+covid_file # a file path wrapper object
+```
+
+###### File Size
+Let's start our analysis by getting a rough estimate of the size of the dataset to inform the tools we use to view the data. For relatively small datasets, we can use a text editor or spreadsheet. For larger datasets, more programmatic exploration or distributed computing tools may be more fitting. Here we will use `Python` tools to probe the file.
+
+Since there seem to be text files, let's investigate the number of lines, which often corresponds to the number of records
+
+```{python}
+#| code-fold: false
+import os
+
+print(covid_file, "is", os.path.getsize(covid_file) /1e6, "MB")
+
+withopen(covid_file, "r") as f:
+print(covid_file, "is", sum(1for l in f), "lines.")
+```
+
+###### Unix Commands
+As part of the EDA workflow, Unix commands can come in very handy. In fact, there's an entire book called ["Data Science at the Command Line"](https://datascienceatthecommandline.com/) that explores this idea in depth!
+In Jupyter/IPython, you can prefix lines with `!` to execute arbitrary Unix commands, and within those lines, you can refer to `Python` variables and expressions with the syntax `{expr}`.
+
+Here, we use the `ls` command to list files, using the `-lh` flags, which request "long format with information in human-readable form." We also use the `wc` command for "word count," but with the `-l` flag, which asks for line counts instead of words.
+
+These two give us the same information as the code above, albeit in a slightly different form:
+
+```{python}
+#| code-fold: false
+!ls -lh {covid_file}
+!wc -l {covid_file}
+```
+
+###### File Contents
+Let's explore the data format using `Python`.
+
+```{python}
+#| code-fold: false
+withopen(covid_file, "r") as f:
+for i, row inenumerate(f):
+print(repr(row)) # print raw strings
+if i >=4: break
+```
+
+We can use the `head` Unix command (which is where `pandas`' `head` method comes from!) to see the first few lines of the file:
+
+```{python}
+#| code-fold: false
+!head -5 {covid_file}
+```
+
+In order to load the JSON file into `pandas`, Let's first do some EDA with `Python`'s `json` package to understand the particular structure of this JSON file so that we can decide what (if anything) to load into `pandas`. `Python` has relatively good support for JSON data since it closely matches the internal python object model. In the following cell we import the entire JSON datafile into a python dictionary using the `json` package.
+
+```{python}
+#| code-fold: false
+import json
+
+withopen(covid_file, "rb") as f:
+ covid_json = json.load(f)
+```
+
+The `covid_json` variable is now a dictionary encoding the data in the file:
+
+```{python}
+#| code-fold: false
+type(covid_json)
+```
+
+We can examine what keys are in the top level json object by listing out the keys.
+
+```{python}
+#| code-fold: false
+covid_json.keys()
+```
+
+**Observation**: The JSON dictionary contains a `meta` key which likely refers to meta data (data about the data). Meta data often maintained with the data and can be a good source of additional information.
+
+
+We can investigate the meta data further by examining the keys associated with the metadata.
+
+```{python}
+#| code-fold: false
+covid_json['meta'].keys()
+```
+
+The `meta` key contains another dictionary called `view`. This likely refers to meta-data about a particular "view" of some underlying database. We will learn more about views when we study SQL later in the class.
+
+```{python}
+#| code-fold: false
+covid_json['meta']['view'].keys()
+```
+
+Notice that this a nested/recursive data structure. As we dig deeper we reveal more and more keys and the corresponding data:
+
+```
+meta
+|-> data
+ | ... (haven't explored yet)
+|-> view
+ | -> id
+ | -> name
+ | -> attribution
+ ...
+ | -> description
+ ...
+ | -> columns
+ ...
+```
+
+
+There is a key called description in the view sub dictionary. This likely contains a description of the data:
+
+```{python}
+#| code-fold: false
+print(covid_json['meta']['view']['description'])
+```
+
+###### Examining the Data Field for Records
+
+We can look at a few entries in the `data` field. This is what we'll load into `pandas`.
+
+```{python}
+#| code-fold: false
+for i inrange(3):
+print(f"{i:03} | {covid_json['data'][i]}")
+```
+
+Observations:
+* These look like equal-length records, so maybe `data` is a table!
+* But what do each of values in the record mean? Where can we find column headers?
+
+For that, we'll need the `columns` key in the metadata dictionary. This returns a list:
+
+```{python}
+#| code-fold: false
+type(covid_json['meta']['view']['columns'])
+```
+
+###### Summary of exploring the JSON file
+
+1. The above **metadata** tells us a lot about the columns in the data including column names, potential data anomalies, and a basic statistic.
+1. Because of its non-tabular structure, JSON makes it easier (than CSV) to create **self-documenting data**, meaning that information about the data is stored in the same file as the data.
+1. Self-documenting data can be helpful since it maintains its own description and these descriptions are more likely to be updated as data changes.
+
+###### Loading COVID Data into `pandas`
+Finally, let's load the data (not the metadata) into a `pandas``DataFrame`. In the following block of code we:
+
+1. Translate the JSON records into a `DataFrame`:
+
+ * fields: `covid_json['meta']['view']['columns']`
+ * records: `covid_json['data']`
+
+
+1. Remove columns that have no metadata description. This would be a bad idea in general, but here we remove these columns since the above analysis suggests they are unlikely to contain useful information.
+
+1. Examine the `tail` of the table.
+
+```{python}
+#| code-fold: false
+# Load the data from JSON and assign column titles
+covid = pd.DataFrame(
+ covid_json['data'],
+ columns=[c['name'] for c in covid_json['meta']['view']['columns']])
+
+covid.tail()
+```
+
+### Variable Types
+
+After loading data into a file, it's a good idea to take the time to understand what pieces of information are encoded in the dataset. In particular, we want to identify what variable types are present in our data. Broadly speaking, we can categorize variables into one of two overarching types.
+
+**Quantitative variables** describe some numeric quantity or amount. We can divide quantitative data further into:
+
+* **Continuous quantitative variables**: numeric data that can be measured on a continuous scale to arbitrary precision. Continuous variables do not have a strict set of possible values – they can be recorded to any number of decimal places. For example, weights, GPA, or CO<sub>2</sub> concentrations.
+* **Discrete quantitative variables**: numeric data that can only take on a finite set of possible values. For example, someone's age or the number of siblings they have.
+
+**Qualitative variables**, also known as **categorical variables**, describe data that isn't measuring some quantity or amount. The sub-categories of categorical data are:
+
+* **Ordinal qualitative variables**: categories with ordered levels. Specifically, ordinal variables are those where the difference between levels has no consistent, quantifiable meaning. Some examples include levels of education (high school, undergrad, grad, etc.), income bracket (low, medium, high), or Yelp rating.
+* **Nominal qualitative variables**: categories with no specific order. For example, someone's political affiliation or Cal ID number.
+
+![Classification of variable types](images/variable.png)
+
+Note that many variables don't sit neatly in just one of these categories. Qualitative variables could have numeric levels, and conversely, quantitative variables could be stored as strings.
+
+### Primary and Foreign Keys
+
+Last time, we introduced `.merge` as the `pandas` method for joining multiple `DataFrame`s together. In our discussion of joins, we touched on the idea of using a "key" to determine what rows should be merged from each table. Let's take a moment to examine this idea more closely.
+
+The **primary key** is the column or set of columns in a table that *uniquely* determine the values of the remaining columns. It can be thought of as the unique identifier for each individual row in the table. For example, a table of Data 100 students might use each student's Cal ID as the primary key.
+
+```{python}
+#| echo: false
+pd.DataFrame({"Cal ID":[3034619471, 3035619472, 3025619473, 3046789372], \
+"Name":["Oski", "Ollie", "Orrie", "Ollie"], \
+"Major":["Data Science", "Computer Science", "Data Science", "Economics"]})
+```
+
+The **foreign key** is the column or set of columns in a table that reference primary keys in other tables. Knowing a dataset's foreign keys can be useful when assigning the `left_on` and `right_on` parameters of `.merge`. In the table of office hour tickets below, `"Cal ID"` is a foreign key referencing the previous table.
+
+```{python}
+#| echo: false
+pd.DataFrame({"OH Request":[1, 2, 3, 4], \
+"Cal ID":[3034619471, 3035619472, 3025619473, 3035619472], \
+"Question":["HW 2 Q1", "HW 2 Q3", "Lab 3 Q4", "HW 2 Q7"]})
+```
+
+## Granularity, Scope, and Temporality
+
+After understanding the structure of the dataset, the next task is to determine what exactly the data represents. We'll do so by considering the data's granularity, scope, and temporality.
+
+### Granularity
+The **granularity** of a dataset is what a single row represents. You can also think of it as the level of detail included in the data. To determine the data's granularity, ask: what does each row in the dataset represent? Fine-grained data contains a high level of detail, with a single row representing a small individual unit. For example, each record may represent one person. Coarse-grained data is encoded such that a single row represents a large individual unit – for example, each record may represent a group of people.
+
+### Scope
+The **scope** of a dataset is the subset of the population covered by the data. If we were investigating student performance in Data Science courses, a dataset with a narrow scope might encompass all students enrolled in Data 100 whereas a dataset with an expansive scope might encompass all students in California.
+
+### Temporality
+The **temporality** of a dataset describes the periodicity over which the data was collected as well as when the data was most recently collected or updated.
+
+Time and date fields of a dataset could represent a few things:
+
+1. when the "event" happened
+2. when the data was collected, or when it was entered into the system
+3. when the data was copied into the database
+
+To fully understand the temporality of the data, it also may be necessary to standardize time zones or inspect recurring time-based trends in the data (do patterns recur in 24-hour periods? Over the course of a month? Seasonally?). The convention for standardizing time is the Coordinated Universal Time (UTC), an international time standard measured at 0 degrees latitude that stays consistent throughout the year (no daylight savings). We can represent Berkeley's time zone, Pacific Standard Time (PST), as UTC-7 (with daylight savings).
+
+#### Temporality with `pandas`' `dt` accessors
+Let's briefly look at how we can use `pandas`' `dt` accessors to work with dates/times in a dataset using the dataset you'll see in Lab 3: the Berkeley PD Calls for Service dataset.
+
+```{python}
+#| code-fold: true
+calls = pd.read_csv("data/Berkeley_PD_-_Calls_for_Service.csv")
+calls.head()
+```
+
+Looks like there are three columns with dates/times: `EVENTDT`, `EVENTTM`, and `InDbDate`.
+
+Most likely, `EVENTDT` stands for the date when the event took place, `EVENTTM` stands for the time of day the event took place (in 24-hr format), and `InDbDate` is the date this call is recorded onto the database.
+
+If we check the data type of these columns, we will see they are stored as strings. We can convert them to `datetime` objects using pandas `to_datetime` function.
+
+```{python}
+#| code-fold: false
+calls["EVENTDT"] = pd.to_datetime(calls["EVENTDT"])
+calls.head()
+```
+
+Now, we can use the `dt` accessor on this column.
+
+We can get the month:
+
+```{python}
+#| code-fold: false
+calls["EVENTDT"].dt.month.head()
+```
+
+Which day of the week the date is on:
+
+```{python}
+#| code-fold: false
+calls["EVENTDT"].dt.dayofweek.head()
+```
+
+Check the mimimum values to see if there are any suspicious-looking, 70s dates:
+
+```{python}
+#| code-fold: false
+calls.sort_values("EVENTDT").head()
+```
+
+Doesn't look like it! We are good!
+
+
+We can also do many things with the `dt` accessor like switching time zones and converting time back to UNIX/POSIX time. Check out the documentation on [`.dt` accessor](https://pandas.pydata.org/docs/user_guide/basics.html#basics-dt-accessors) and [time series/date functionality](https://pandas.pydata.org/docs/user_guide/timeseries.html#).
+
+## Faithfulness
+
+At this stage in our data cleaning and EDA workflow, we've achieved quite a lot: we've identified how our data is structured, come to terms with what information it encodes, and gained insight as to how it was generated. Throughout this process, we should always recall the original intent of our work in Data Science – to use data to better understand and model the real world. To achieve this goal, we need to ensure that the data we use is faithful to reality; that is, that our data accurately captures the "real world."
+
+Data used in research or industry is often "messy" – there may be errors or inaccuracies that impact the faithfulness of the dataset. Signs that data may not be faithful include:
+
+* Unrealistic or "incorrect" values, such as negative counts, locations that don't exist, or dates set in the future
+* Violations of obvious dependencies, like an age that does not match a birthday
+* Clear signs that data was entered by hand, which can lead to spelling errors or fields that are incorrectly shifted
+* Signs of data falsification, such as fake email addresses or repeated use of the same names
+* Duplicated records or fields containing the same information
+* Truncated data, e.g. Microsoft Excel would limit the number of rows to 655536 and the number of columns to 255
+
+We often solve some of these more common issues in the following ways:
+
+* Spelling errors: apply corrections or drop records that aren't in a dictionary
+* Time zone inconsistencies: convert to a common time zone (e.g. UTC)
+* Duplicated records or fields: identify and eliminate duplicates (using primary keys)
+* Unspecified or inconsistent units: infer the units and check that values are in reasonable ranges in the data
+
+### Missing Values
+Another common issue encountered with real-world datasets is that of missing data. One strategy to resolve this is to simply drop any records with missing values from the dataset. This does, however, introduce the risk of inducing biases – it is possible that the missing or corrupt records may be systemically related to some feature of interest in the data. Another solution is to keep the data as `NaN` values.
+
+A third method to address missing data is to perform **imputation**: infer the missing values using other data available in the dataset. There is a wide variety of imputation techniques that can be implemented; some of the most common are listed below.
+
+* Average imputation: replace missing values with the average value for that field
+* Hot deck imputation: replace missing values with some random value
+* Regression imputation: develop a model to predict missing values
+* Multiple imputation: replace missing values with multiple random values
+
+Regardless of the strategy used to deal with missing data, we should think carefully about *why* particular records or fields may be missing – this can help inform whether or not the absence of these values is significant or meaningful.
+
+# EDA Demo 1: Tuberculosis in the United States
+
+Now, let's walk through the data-cleaning and EDA workflow to see what can we learn about the presence of Tuberculosis in the United States!
+
+We will examine the data included in the [original CDC article](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w#T1_down) published in 2021.
+
+
+## CSVs and Field Names
+Suppose Table 1 was saved as a CSV file located in `data/cdc_tuberculosis.csv`.
+
+We can then explore the CSV (which is a text file, and does not contain binary-encoded data) in many ways:
+1. Using a text editor like emacs, vim, VSCode, etc.
+2. Opening the CSV directly in DataHub (read-only), Excel, Google Sheets, etc.
+3. The `Python` file object
+4. `pandas`, using `pd.read_csv()`
+
+To try out options 1 and 2, you can view or download the Tuberculosis from the [lecture demo notebook](https://data100.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2FDS-100%2Ffa23-student&urlpath=lab%2Ftree%2Ffa23-student%2Flecture%2Flec05%2Flec04-eda.ipynb&branch=main) under the `data` folder in the left hand menu. Notice how the CSV file is a type of **rectangular data (i.e., tabular data) stored as comma-separated values**.
+
+Next, let's try out option 3 using the `Python` file object. We'll look at the first four lines:
+
+```{python}
+#| code-fold: true
+withopen("data/cdc_tuberculosis.csv", "r") as f:
+ i =0
+for row in f:
+print(row)
+ i +=1
+if i >3:
+break
+```
+
+Whoa, why are there blank lines interspaced between the lines of the CSV?
+
+You may recall that all line breaks in text files are encoded as the special newline character `\n`. Python's `print()` prints each string (including the newline), and an additional newline on top of that.
+
+If you're curious, we can use the `repr()` function to return the raw string with all special characters:
+
+```{python}
+#| code-fold: true
+withopen("data/cdc_tuberculosis.csv", "r") as f:
+ i =0
+for row in f:
+print(repr(row)) # print raw strings
+ i +=1
+if i >3:
+break
+```
+
+Finally, let's try option 4 and use the tried-and-true Data 100 approach: `pandas`.
+
+```{python}
+#| code-fold: false
+tb_df = pd.read_csv("data/cdc_tuberculosis.csv")
+tb_df.head()
+```
+
+You may notice some strange things about this table: what's up with the "Unnamed" column names and the first row?
+
+Congratulations — you're ready to wrangle your data! Because of how things are stored, we'll need to clean the data a bit to name our columns better.
+
+A reasonable first step is to identify the row with the right header. The `pd.read_csv()` function ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)) has the convenient `header` parameter that we can set to use the elements in row 1 as the appropriate columns:
+
+```{python}
+#| code-fold: false
+tb_df = pd.read_csv("data/cdc_tuberculosis.csv", header=1) # row index
+tb_df.head(5)
+```
+
+Wait...but now we can't differentiate betwen the "Number of TB cases" and "TB incidence" year columns. `pandas` has tried to make our lives easier by automatically adding ".1" to the latter columns, but this doesn't help us, as humans, understand the data.
+
+We can do this manually with `df.rename()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html?highlight=rename#pandas.DataFrame.rename)):
+
+```{python}
+#| code-fold: false
+rename_dict = {'2019': 'TB cases 2019',
+'2020': 'TB cases 2020',
+'2021': 'TB cases 2021',
+'2019.1': 'TB incidence 2019',
+'2020.1': 'TB incidence 2020',
+'2021.1': 'TB incidence 2021'}
+tb_df = tb_df.rename(columns=rename_dict)
+tb_df.head(5)
+```
+
+## Record Granularity
+
+You might already be wondering: what's up with that first record?
+
+Row 0 is what we call a **rollup record**, or summary record. It's often useful when displaying tables to humans. The **granularity** of record 0 (Totals) vs the rest of the records (States) is different.
+
+Okay, EDA step two. How was the rollup record aggregated?
+
+Let's check if Total TB cases is the sum of all state TB cases. If we sum over all rows, we should get **2x** the total cases in each of our TB cases by year (why do you think this is?).
+
+```{python}
+#| code-fold: true
+tb_df.sum(axis=0)
+```
+
+Whoa, what's going on with the TB cases in 2019, 2020, and 2021? Check out the column types:
+
+```{python}
+#| code-fold: true
+tb_df.dtypes
+```
+
+Since there are commas in the values for TB cases, the numbers are read as the `object` datatype, or **storage type** (close to the `Python` string datatype), so `pandas` is concatenating strings instead of adding integers (recall that `Python` can "sum", or concatenate, strings together: `"data" + "100"` evaluates to `"data100"`).
+
+
+Fortunately `read_csv` also has a `thousands` parameter ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)):
+
+```{python}
+#| code-fold: false
+# improve readability: chaining method calls with outer parentheses/line breaks
+tb_df = (
+ pd.read_csv("data/cdc_tuberculosis.csv", header=1, thousands=',')
+ .rename(columns=rename_dict)
+)
+tb_df.head(5)
+```
+
+```{python}
+#| code-fold: false
+tb_df.sum()
+```
+
+The Total TB cases look right. Phew!
+
+Let's just look at the records with **state-level granularity**:
+
+```{python}
+#| code-fold: true
+state_tb_df = tb_df[1:]
+state_tb_df.head(5)
+```
+
+## Gather Census Data
+
+U.S. Census population estimates [source](https://www.census.gov/data/tables/time-series/demo/popest/2010s-state-total.html) (2019), [source](https://www.census.gov/data/tables/time-series/demo/popest/2020s-state-total.html) (2020-2021).
+
+Running the below cells cleans the data.
+There are a few new methods here:
+* `df.convert_dtypes()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.convert_dtypes.html)) conveniently converts all float dtypes into ints and is out of scope for the class.
+* `df.drop_na()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html)) will be explained in more detail next time.
+
+```{python}
+#| code-fold: true
+# 2010s census data
+census_2010s_df = pd.read_csv("data/nst-est2019-01.csv", header=3, thousands=",")
+census_2010s_df = (
+ census_2010s_df
+ .reset_index()
+ .drop(columns=["index", "Census", "Estimates Base"])
+ .rename(columns={"Unnamed: 0": "Geographic Area"})
+ .convert_dtypes() # "smart" converting of columns, use at your own risk
+ .dropna() # we'll introduce this next time
+)
+census_2010s_df['Geographic Area'] = census_2010s_df['Geographic Area'].str.strip('.')
+
+# with pd.option_context('display.min_rows', 30): # shows more rows
+# display(census_2010s_df)
+
+census_2010s_df.head(5)
+```
+
+Occasionally, you will want to modify code that you have imported. To reimport those modifications you can either use `python`'s `importlib` library:
+
+```python
+from importlib importreload
+reload(utils)
+```
+
+or use `iPython` magic which will intelligently import code when files change:
+
+```python
+%load_ext autoreload
+%autoreload 2
+```
+
+```{python}
+#| code-fold: true
+# census 2020s data
+census_2020s_df = pd.read_csv("data/NST-EST2022-POP.csv", header=3, thousands=",")
+census_2020s_df = (
+ census_2020s_df
+ .reset_index()
+ .drop(columns=["index", "Unnamed: 1"])
+ .rename(columns={"Unnamed: 0": "Geographic Area"})
+ .convert_dtypes() # "smart" converting of columns, use at your own risk
+ .dropna() # we'll introduce this next time
+)
+census_2020s_df['Geographic Area'] = census_2020s_df['Geographic Area'].str.strip('.')
+
+census_2020s_df.head(5)
+```
+
+## Joining Data (Merging `DataFrame`s)
+
+Time to `merge`! Here we use the `DataFrame` method `df1.merge(right=df2, ...)` on `DataFrame``df1` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)). Contrast this with the function `pd.merge(left=df1, right=df2, ...)` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.merge.html?highlight=pandas%20merge#pandas.merge)). Feel free to use either.
+
+```{python}
+#| code-fold: false
+# merge TB DataFrame with two US census DataFrames
+tb_census_df = (
+ tb_df
+ .merge(right=census_2010s_df,
+ left_on="U.S. jurisdiction", right_on="Geographic Area")
+ .merge(right=census_2020s_df,
+ left_on="U.S. jurisdiction", right_on="Geographic Area")
+)
+tb_census_df.head(5)
+```
+
+Having all of these columns is a little unwieldy. We could either drop the unneeded columns now, or just merge on smaller census `DataFrame`s. Let's do the latter.
+
+```{python}
+#| code-fold: false
+# try merging again, but cleaner this time
+tb_census_df = (
+ tb_df
+ .merge(right=census_2010s_df[["Geographic Area", "2019"]],
+ left_on="U.S. jurisdiction", right_on="Geographic Area")
+ .drop(columns="Geographic Area")
+ .merge(right=census_2020s_df[["Geographic Area", "2020", "2021"]],
+ left_on="U.S. jurisdiction", right_on="Geographic Area")
+ .drop(columns="Geographic Area")
+)
+tb_census_df.head(5)
+```
+
+## Reproducing Data: Compute Incidence
+
+Let's recompute incidence to make sure we know where the original CDC numbers came from.
+
+From the [CDC report](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w#T1_down): TB incidence is computed as “Cases per 100,000 persons using mid-year population estimates from the U.S. Census Bureau.”
+
+If we define a group as 100,000 people, then we can compute the TB incidence for a given state population as
+
+$$\text{TB incidence} = \frac{\text{TB cases in population}}{\text{groups in population}} = \frac{\text{TB cases in population}}{\text{population}/100000} $$
+
+$$= \frac{\text{TB cases in population}}{\text{population}} \times 100000$$
+
+Let's try this for 2019:
+
+```{python}
+#| code-fold: false
+tb_census_df["recompute incidence 2019"] = tb_census_df["TB cases 2019"]/tb_census_df["2019"]*100000
+tb_census_df.head(5)
+```
+
+Awesome!!!
+
+Let's use a for-loop and `Python` format strings to compute TB incidence for all years. `Python` f-strings are just used for the purposes of this demo, but they're handy to know when you explore data beyond this course ([documentation](https://docs.python.org/3/tutorial/inputoutput.html)).
+
+```{python}
+#| code-fold: false
+# recompute incidence for all years
+for year in [2019, 2020, 2021]:
+ tb_census_df[f"recompute incidence {year}"] = tb_census_df[f"TB cases {year}"]/tb_census_df[f"{year}"]*100000
+tb_census_df.head(5)
+```
+
+These numbers look pretty close!!! There are a few errors in the hundredths place, particularly in 2021. It may be useful to further explore reasons behind this discrepancy.
+
+```{python}
+#| code-fold: false
+tb_census_df.describe()
+```
+
+## Bonus EDA: Reproducing the Reported Statistic
+
+
+**How do we reproduce that reported statistic in the original [CDC report](https://www.cdc.gov/mmwr/volumes/71/wr/mm7112a1.htm?s_cid=mm7112a1_w)?**
+
+> Reported TB incidence (cases per 100,000 persons) increased **9.4%**, from **2.2** during 2020 to **2.4** during 2021 but was lower than incidence during 2019 (2.7). Increases occurred among both U.S.-born and non–U.S.-born persons.
+
+This is TB incidence computed across the entire U.S. population! How do we reproduce this?
+* We need to reproduce the "Total" TB incidences in our rolled record.
+* But our current `tb_census_df` only has 51 entries (50 states plus Washington, D.C.). There is no rolled record.
+* What happened...?
+
+Let's get exploring!
+
+Before we keep exploring, we'll set all indexes to more meaningful values, instead of just numbers that pertain to some row at some point. This will make our cleaning slightly easier.
+
+```{python}
+#| code-fold: true
+tb_df = tb_df.set_index("U.S. jurisdiction")
+tb_df.head(5)
+```
+
+```{python}
+#| code-fold: false
+census_2010s_df = census_2010s_df.set_index("Geographic Area")
+census_2010s_df.head(5)
+```
+
+```{python}
+#| code-fold: false
+census_2020s_df = census_2020s_df.set_index("Geographic Area")
+census_2020s_df.head(5)
+```
+
+It turns out that our merge above only kept state records, even though our original `tb_df` had the "Total" rolled record:
+
+```{python}
+#| code-fold: false
+tb_df.head()
+```
+
+Recall that `merge` by default does an **inner** merge by default, meaning that it only preserves keys that are present in **both** `DataFrame`s.
+
+The rolled records in our census `DataFrame` have different `Geographic Area` fields, which was the key we merged on:
+
+```{python}
+#| code-fold: false
+census_2010s_df.head(5)
+```
+
+The Census `DataFrame` has several rolled records. The aggregate record we are looking for actually has the Geographic Area named "United States".
+
+One straightforward way to get the right merge is to rename the value itself. Because we now have the Geographic Area index, we'll use `df.rename()` ([documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html)):
+
+```{python}
+#| code-fold: false
+# rename rolled record for 2010s
+census_2010s_df.rename(index={'United States':'Total'}, inplace=True)
+census_2010s_df.head(5)
+```
+
+```{python}
+#| code-fold: false
+# same, but for 2020s rename rolled record
+census_2020s_df.rename(index={'United States':'Total'}, inplace=True)
+census_2020s_df.head(5)
+```
+
+<br/>
+
+Next let's rerun our merge. Note the different chaining, because we are now merging on indexes (`df.merge()`[documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)).
+
+```{python}
+#| code-fold: false
+tb_census_df = (
+ tb_df
+ .merge(right=census_2010s_df[["2019"]],
+ left_index=True, right_index=True)
+ .merge(right=census_2020s_df[["2020", "2021"]],
+ left_index=True, right_index=True)
+)
+tb_census_df.head(5)
+```
+
+<br/>
+
+Finally, let's recompute our incidences:
+
+```{python}
+#| code-fold: false
+# recompute incidence for all years
+for year in [2019, 2020, 2021]:
+ tb_census_df[f"recompute incidence {year}"] = tb_census_df[f"TB cases {year}"]/tb_census_df[f"{year}"]*100000
+tb_census_df.head(5)
+```
+
+We reproduced the total U.S. incidences correctly!
+
+We're almost there. Let's revisit the quote:
+
+> Reported TB incidence (cases per 100,000 persons) increased **9.4%**, from **2.2** during 2020 to **2.4** during 2021 but was lower than incidence during 2019 (2.7). Increases occurred among both U.S.-born and non–U.S.-born persons.
+
+Recall that percent change from $A$ to $B$ is computed as
+$\text{percent change} = \frac{B - A}{A} \times 100$.
+
+```{python}
+#| code-fold: false
+#| tags: []
+incidence_2020 = tb_census_df.loc['Total', 'recompute incidence 2020']
+incidence_2020
+```
+
+```{python}
+#| code-fold: false
+#| tags: []
+incidence_2021 = tb_census_df.loc['Total', 'recompute incidence 2021']
+incidence_2021
+```
+
+```{python}
+#| code-fold: false
+#| tags: []
+difference = (incidence_2021 - incidence_2020)/incidence_2020 *100
+difference
+```
+
+# EDA Demo 2: Mauna Loa CO<sub>2</sub> Data -- A Lesson in Data Faithfulness
+
+[Mauna Loa Observatory](https://gml.noaa.gov/ccgg/trends/data.html) has been monitoring CO<sub>2</sub> concentrations since 1958
+
+```{python}
+#| code-fold: false
+co2_file ="data/co2_mm_mlo.txt"
+```
+
+Let's do some **EDA**!!
+
+## Reading this file into Pandas?
+Let's instead check out this `.txt` file. Some questions to keep in mind: Do we trust this file extension? What structure is it?
+
+Lines 71-78 (inclusive) are shown below:
+
+ line number | file contents
+
+ 71 | # decimal average interpolated trend #days
+ 72 | # date (season corr)
+ 73 | 1958 3 1958.208 315.71 315.71 314.62 -1
+ 74 | 1958 4 1958.292 317.45 317.45 315.29 -1
+ 75 | 1958 5 1958.375 317.50 317.50 314.71 -1
+ 76 | 1958 6 1958.458 -99.99 317.10 314.85 -1
+ 77 | 1958 7 1958.542 315.86 315.86 314.98 -1
+ 78 | 1958 8 1958.625 314.93 314.93 315.94 -1
+
+
+Notice how:
+
+- The values are separated by white space, possibly tabs.
+- The data line up down the rows. For example, the month appears in 7th to 8th position of each line.
+- The 71st and 72nd lines in the file contain column headings split over two lines.
+
+We can use `read_csv` to read the data into a `pandas``DataFrame`, and we provide several arguments to specify that the separators are white space, there is no header (**we will set our own column names**), and to skip the first 72 rows of the file.
+
+```{python}
+#| code-fold: false
+co2 = pd.read_csv(
+ co2_file, header =None, skiprows =72,
+ sep =r'\s+'#delimiter for continuous whitespace (stay tuned for regex next lecture))
+)
+co2.head()
+```
+
+Congratulations! You've wrangled the data!
+
+<br/>
+
+...But our columns aren't named.
+**We need to do more EDA.**
+
+## Exploring Variable Feature Types
+
+The NOAA [webpage](https://gml.noaa.gov/ccgg/trends/) might have some useful tidbits (in this case it doesn't).
+
+Using this information, we'll rerun `pd.read_csv`, but this time with some **custom column names.**
+
+```{python}
+#| code-fold: false
+co2 = pd.read_csv(
+ co2_file, header =None, skiprows =72,
+ sep ='\s+', #regex for continuous whitespace (next lecture)
+ names = ['Yr', 'Mo', 'DecDate', 'Avg', 'Int', 'Trend', 'Days']
+)
+co2.head()
+```
+
+## Visualizing CO<sub>2</sub>
+Scientific studies tend to have very clean data, right...? Let's jump right in and make a time series plot of CO2 monthly averages.
+
+```{python}
+#| code-fold: true
+sns.lineplot(x='DecDate', y='Avg', data=co2);
+```
+
+The code above uses the `seaborn` plotting library (abbreviated `sns`). We will cover this in the Visualization lecture, but now you don't need to worry about how it works!
+
+Yikes! Plotting the data uncovered a problem. The sharp vertical lines suggest that we have some **missing values**. What happened here?
+
+```{python}
+#| code-fold: false
+co2.head()
+```
+
+```{python}
+#| code-fold: false
+co2.tail()
+```
+
+Some data have unusual values like -1 and -99.99.
+
+Let's check the description at the top of the file again.
+
+* -1 signifies a missing value for the number of days `Days` the equipment was in operation that month.
+* -99.99 denotes a missing monthly average `Avg`
+
+How can we fix this? First, let's explore other aspects of our data. Understanding our data will help us decide what to do with the missing values.
+
+<br/>
+
+
+## Sanity Checks: Reasoning about the data
+First, we consider the shape of the data. How many rows should we have?
+
+* If chronological order, we should have one record per month.
+* Data from March 1958 to August 2019.
+* We should have $ 12 \times (2019-1957) - 2 - 4 = 738 $ records.
+
+```{python}
+#| code-fold: false
+co2.shape
+```
+
+Nice!! The number of rows (i.e. records) match our expectations.\
+
+<br/>
+
+
+Let's now check the quality of each feature.
+
+## Understanding Missing Value 1: `Days`
+`Days` is a time field, so let's analyze other time fields to see if there is an explanation for missing values of days of operation.
+
+Let's start with **months**, `Mo`.
+
+Are we missing any records? The number of months should have 62 or 61 instances (March 1957-August 2019).
+
+```{python}
+#| code-fold: false
+co2["Mo"].value_counts().sort_index()
+```
+
+As expected Jan, Feb, Sep, Oct, Nov, and Dec have 61 occurrences and the rest 62.
+
+<br/>
+
+Next let's explore **days** `Days` itself, which is the number of days that the measurement equipment worked.
+
+```{python}
+#| code-fold: true
+sns.displot(co2['Days']);
+plt.title("Distribution of days feature");# suppresses unneeded plotting output
+```
+
+In terms of data quality, a handful of months have averages based on measurements taken on fewer than half the days. In addition, there are nearly 200 missing values--**that's about 27% of the data**!
+
+<br/>
+
+Finally, let's check the last time feature, **year** `Yr`.
+
+Let's check to see if there is any connection between missing-ness and the year of the recording.
+
+```{python}
+#| code-fold: true
+sns.scatterplot(x="Yr", y="Days", data=co2);
+plt.title("Day field by Year");# the ; suppresses output
+```
+
+**Observations**:
+
+* All of the missing data are in the early years of operation.
+* It appears there may have been problems with equipment in the mid to late 80s.
+
+**Potential Next Steps**:
+
+* Confirm these explanations through documentation about the historical readings.
+* Maybe drop earliest recordings? However, we would want to delay such action until after we have examined the time trends and assess whether there are any potential problems.
+
+<br/>
+
+## Understanding Missing Value 2: `Avg`
+Next, let's return to the -99.99 values in `Avg` to analyze the overall quality of the CO2 measurements. We'll plot a histogram of the average CO<sub>2</sub> measurements
+
+```{python}
+#| code-fold: true
+# Histograms of average CO2 measurements
+sns.displot(co2['Avg']);
+```
+
+The non-missing values are in the 300-400 range (a regular range of CO2 levels).
+
+We also see that there are only a few missing `Avg` values (**<1% of values**). Let's examine all of them:
+
+```{python}
+#| code-fold: false
+co2[co2["Avg"] <0]
+```
+
+There doesn't seem to be a pattern to these values, other than that most records also were missing `Days` data.
+
+## Drop, `NaN`, or Impute Missing `Avg` Data?
+
+How should we address the invalid `Avg` data?
+
+1. Drop records
+2. Set to NaN
+3. Impute using some strategy
+
+Remember we want to fix the following plot:
+
+```{python}
+#| code-fold: true
+sns.lineplot(x='DecDate', y='Avg', data=co2)
+plt.title("CO2 Average By Month");
+```
+
+Since we are plotting `Avg` vs `DecDate`, we should just focus on dealing with missing values for `Avg`.
+
+
+Let's consider a few options:
+1. Drop those records
+2. Replace -99.99 with NaN
+3. Substitute it with a likely value for the average CO2?
+
+What do you think are the pros and cons of each possible action?
+
+<br/>
+
+
+Let's examine each of these three options.
+
+```{python}
+#| code-fold: false
+# 1. Drop missing values
+co2_drop = co2[co2['Avg'] >0]
+co2_drop.head()
+```
+
+```{python}
+#| code-fold: false
+# 2. Replace NaN with -99.99
+co2_NA = co2.replace(-99.99, np.NaN)
+co2_NA.head()
+```
+
+We'll also use a third version of the data.
+
+First, we note that the dataset already comes with a **substitute value** for the -99.99.
+
+From the file description:
+
+> The `interpolated` column includes average values from the preceding column (`average`)
+and **interpolated values** where data are missing. Interpolated values are
+computed in two steps...
+
+The `Int` feature has values that exactly match those in `Avg`, except when `Avg` is -99.99, and then a **reasonable** estimate is used instead.
+
+So, the third version of our data will use the `Int` feature instead of `Avg`.
+
+```{python}
+#| code-fold: false
+# 3. Use interpolated column which estimates missing Avg values
+co2_impute = co2.copy()
+co2_impute['Avg'] = co2['Int']
+co2_impute.head()
+```
+
+What's a **reasonable** estimate?
+
+To answer this question, let's zoom in on a short time period, say the measurements in 1958 (where we know we have two missing values).
+
+```{python}
+#| code-fold: true
+# results of plotting data in 1958
+
+def line_and_points(data, ax, title):
+# assumes single year, hence Mo
+ ax.plot('Mo', 'Avg', data=data)
+ ax.scatter('Mo', 'Avg', data=data)
+ ax.set_xlim(2, 13)
+ ax.set_title(title)
+ ax.set_xticks(np.arange(3, 13))
+
+def data_year(data, year):
+return data[data["Yr"] ==1958]
+
+# uses matplotlib subplots
+# you may see more next week; focus on output for now
+fig, axes = plt.subplots(ncols =3, figsize=(12, 4), sharey=True)
+
+year =1958
+line_and_points(data_year(co2_drop, year), axes[0], title="1. Drop Missing")
+line_and_points(data_year(co2_NA, year), axes[1], title="2. Missing Set to NaN")
+line_and_points(data_year(co2_impute, year), axes[2], title="3. Missing Interpolated")
+
+fig.suptitle(f"Monthly Averages for {year}")
+plt.tight_layout()
+```
+
+In the big picture since there are only 7 `Avg` values missing (**<1%** of 738 months), any of these approaches would work.
+
+However there is some appeal to **option C: Imputing**:
+
+* Shows seasonal trends for CO2
+* We are plotting all months in our data as a line plot
+
+<br/>
+
+
+Let's replot our original figure with option 3:
+
+```{python}
+#| code-fold: true
+sns.lineplot(x='DecDate', y='Avg', data=co2_impute)
+plt.title("CO2 Average By Month, Imputed");
+```
+
+Looks pretty close to what we see on the NOAA [website](https://gml.noaa.gov/ccgg/trends/)!
+
+## Presenting the data: A Discussion on Data Granularity
+
+From the description:
+
+* monthly measurements are averages of average day measurements.
+* The NOAA GML website has datasets for daily/hourly measurements too.
+
+The data you present depends on your research question.
+
+**How do CO2 levels vary by season?**
+
+* You might want to keep average monthly data.
+
+**Are CO2 levels rising over the past 50+ years, consistent with global warming predictions?**
+
+* You might be happier with a **coarser granularity** of average year data!
+
+```{python}
+#| code-fold: true
+co2_year = co2_impute.groupby('Yr').mean()
+sns.lineplot(x='Yr', y='Avg', data=co2_year)
+plt.title("CO2 Average By Year");
+```
+
+Indeed, we see a rise by nearly 100 ppm of CO2 since Mauna Loa began recording in 1958.
+
+# Summary
+We went over a lot of content this lecture; let's summarize the most important points:
+
+## Dealing with Missing Values
+There are a few options we can take to deal with missing data:
+
+* Drop missing records
+* Keep `NaN` missing values
+* Impute using an interpolated column
+
+## EDA and Data Wrangling
+There are several ways to approach EDA and Data Wrangling:
+
+* Examine the **data and metadata**: what is the date, size, organization, and structure of the data?
+* Examine each **field/attribute/dimension** individually.
+* Examine pairs of related dimensions (e.g. breaking down grades by major).
+* Along the way, we can:
+ * **Visualize** or summarize the data.
+ * **Validate assumptions** about data and its collection process. Pay particular attention to when the data was collected.
+ * Identify and **address anomalies**.
+ * Apply data transformations and corrections (we'll cover this in the upcoming lecture).
+ * **Record everything you do!** Developing in Jupyter Notebook promotes *reproducibility* of your own work!
diff --git a/docs/eda/eda_files/figure-html/cell-62-output-1.png b/docs/eda/eda_files/figure-html/cell-62-output-1.png
index f392d5f9..a04218cf 100644
Binary files a/docs/eda/eda_files/figure-html/cell-62-output-1.png and b/docs/eda/eda_files/figure-html/cell-62-output-1.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-67-output-1.png b/docs/eda/eda_files/figure-html/cell-67-output-1.png
deleted file mode 100644
index be96b8c9..00000000
Binary files a/docs/eda/eda_files/figure-html/cell-67-output-1.png and /dev/null differ
diff --git a/docs/eda/eda_files/figure-html/cell-67-output-2.png b/docs/eda/eda_files/figure-html/cell-67-output-2.png
new file mode 100644
index 00000000..31857f62
Binary files /dev/null and b/docs/eda/eda_files/figure-html/cell-67-output-2.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-68-output-1.png b/docs/eda/eda_files/figure-html/cell-68-output-1.png
index ffd29ff8..67c3959d 100644
Binary files a/docs/eda/eda_files/figure-html/cell-68-output-1.png and b/docs/eda/eda_files/figure-html/cell-68-output-1.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-69-output-1.png b/docs/eda/eda_files/figure-html/cell-69-output-1.png
deleted file mode 100644
index 29088928..00000000
Binary files a/docs/eda/eda_files/figure-html/cell-69-output-1.png and /dev/null differ
diff --git a/docs/eda/eda_files/figure-html/cell-69-output-2.png b/docs/eda/eda_files/figure-html/cell-69-output-2.png
new file mode 100644
index 00000000..fb28f5d5
Binary files /dev/null and b/docs/eda/eda_files/figure-html/cell-69-output-2.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-71-output-1.png b/docs/eda/eda_files/figure-html/cell-71-output-1.png
index 49ef3d6a..39cac822 100644
Binary files a/docs/eda/eda_files/figure-html/cell-71-output-1.png and b/docs/eda/eda_files/figure-html/cell-71-output-1.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-75-output-1.png b/docs/eda/eda_files/figure-html/cell-75-output-1.png
index 15a5fe82..6382e58a 100644
Binary files a/docs/eda/eda_files/figure-html/cell-75-output-1.png and b/docs/eda/eda_files/figure-html/cell-75-output-1.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-76-output-1.png b/docs/eda/eda_files/figure-html/cell-76-output-1.png
index 40b1fc71..db2b0dee 100644
Binary files a/docs/eda/eda_files/figure-html/cell-76-output-1.png and b/docs/eda/eda_files/figure-html/cell-76-output-1.png differ
diff --git a/docs/eda/eda_files/figure-html/cell-77-output-1.png b/docs/eda/eda_files/figure-html/cell-77-output-1.png
index 99b6c2d1..897b8b39 100644
Binary files a/docs/eda/eda_files/figure-html/cell-77-output-1.png and b/docs/eda/eda_files/figure-html/cell-77-output-1.png differ
diff --git a/docs/feature_engineering/feature_engineering.html b/docs/feature_engineering/feature_engineering.html
index fe821b57..9cfedab8 100644
--- a/docs/feature_engineering/feature_engineering.html
+++ b/docs/feature_engineering/feature_engineering.html
@@ -64,6 +64,7 @@
+
@@ -234,6 +235,18 @@
14Sklearn and Feature Engineering
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
+
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
Notice that we use double brackets to extract this column. Why double brackets instead of just single brackets? The .fit method, by default, expects to receive 2-dimensional data – some kind of data that includes both rows and columns. Writing penguins["flipper_length_mm"] would return a 1D Series, causing sklearn to error. We avoid this by writing penguins[["flipper_length_mm"]] to produce a 2D DataFrame.
@@ -558,7 +571,7 @@
print(f"The RMSE of the model is {np.sqrt(np.mean((Y-Y_hat_two_features)**2))}")
-
The RMSE of the model is 0.9881331104079045
+
The RMSE of the model is 0.9881331104079044
We can also see that we obtain the same predictions using sklearn as we did when applying the ordinary least squares formula before!
@@ -928,7 +941,7 @@
print(f"MSE of model with (hp^2) feature: {np.mean((Y-hp2_model_predictions)**2)}")
-
MSE of model with (hp^2) feature: 18.984768907617216
+
MSE of model with (hp^2) feature: 18.984768907617223