Introduction To Machine Learning
Introduction To Machine Learning
🎥 Click the image above for a video discussing the difference between machine learning, AI,
and deep learning.
Pre-lecture quiz
Welcome to this course on classical machine learning for beginners! Whether you're completely new
to this topic, or an experienced ML practitioner looking to brush up on an area, we're happy to have
you join us! We want to create a friendly launching spot for your ML study and would be happy to
evaluate, respond to, and incorporate your feedback.
🎥 Click the image above for a video: MIT's John Guttag introduces machine learning
Google Trends shows the recent 'hype curve' of the term 'machine learning'
A mysterious universe
We live in a universe full of fascinating mysteries. Great scientists such as Stephen Hawking, Albert
Einstein, and many more have devoted their lives to searching for meaningful information that
uncovers the mysteries of the world around us. This is the human condition of learning: a human child
learns new things and uncovers the structure of their world year by year as they grow to adulthood.
Some terminology
Although the terms can be confused, machine learning (ML) is an important subset of artificial
intelligence. ML is concerned with using specialized algorithms to uncover meaningful
information and find hidden patterns from perceived data to corroborate the rational decision-
making process.
Concepts to cover
In this curriculum, we are going to cover only the core concepts of machine learning that a beginner
must know. We cover what we call 'classical machine learning' primarily using Scikit-learn, an
excellent library many students use to learn the basics. To understand broader concepts of artificial
intelligence or deep learning, a strong fundamental knowledge of machine learning is indispensable,
and so we would like to offer it here.
Examples of applied ML
You can use machine learning in many ways:
To predict the likelihood of disease from a patient's medical history or reports.
To leverage weather data to predict weather events.
To understand the sentiment of a text.
To detect fake news to stop the spread of propaganda.
Finance, economics, earth science, space exploration, biomedical engineering, cognitive science,
and even fields in the humanities have adapted machine learning to solve the arduous, data-
processing heavy problems of their domain.
Conclusion
Machine learning automates the process of pattern-discovery by finding meaningful insights from
real-world or generated data. It has proven itself to be highly valuable in business, health, and
financial applications, among others.
In the near future, understanding the basics of machine learning is going to be a must for people from
any domain due to its widespread adoption.
🚀 Challenge
Sketch, on paper or using an online app like Excalidraw, your understanding of the differences
between AI, ML, deep learning, and data science. Add some ideas of problems that each of these
techniques are good at solving.
Post-lecture quiz
Review & Self Study
To learn more about how you can work with ML algorithms in the cloud, follow this Learning Path.
Take a Learning Path about the basics of ML.
Assignment
Get up and running
Pre-lecture quiz
In this lesson, we will walk through the major milestones in the history of machine learning and
artificial intelligence.
The history of artificial intelligence (AI) as a field is intertwined with the history of machine learning, as
the algorithms and computational advances that underpin ML fed into the development of AI. It is
useful to remember that, while these fields as distinct areas of inquiry began to crystallize in the
1950s, important algorithmic, statistical, mathematical, computational and technical discoveries
predated and overlapped this era. In fact, people have been thinking about these questions for
hundreds of years: this article discusses the historical intellectual underpinnings of the idea of a
'thinking machine.'
Notable discoveries
1763, 1812 Bayes Theorem and its predecessors. This theorem and its applications underlie
inference, describing the probability of an event occurring based on prior knowledge.
1805 Least Square Theory by French mathematician Adrien-Marie Legendre. This theory, which
you will learn about in our Regression unit, helps in data fitting.
1913 Markov Chains, named after Russian mathematician Andrey Markov, is used to describe a
sequence of possible events based on a previous state.
1957 Perceptron is a type of linear classifier invented by American psychologist Frank Rosenblatt
that underlies advances in deep learning.
1967 Nearest Neighbor is an algorithm originally designed to map routes. In an ML context it is
used to detect patterns.
1970 Backpropagation is used to train feedforward neural networks.
1982 Recurrent Neural Networks are artificial neural networks derived from feedforward neural
networks that create temporal graphs.
✅ Do a little research. What other dates stand out as pivotal in the history of ML and AI?
Every aspect of learning or any other feature of intelligence can in principle be so precisely
described that a machine can be made to simulate it.
The lead researcher, mathematics professor John McCarthy, hoped "to proceed on the basis of the
conjecture that every aspect of learning or any other feature of intelligence can in principle be so
precisely described that a machine can be made to simulate it." The participants included another
luminary in the field, Marvin Minsky.
The workshop is credited with having initiated and encouraged several discussions including "the rise
of symbolic methods, systems focussed on limited domains (early expert systems), and deductive
systems versus inductive systems." (source).
Eliza, an early 'chatterbot', could converse with people and act as a primitive 'therapist'. You'll
learn more about Eliza in the NLP lessons.
A version of Eliza, a chatbot
"Blocks world" was an example of a micro-world where blocks could be stacked and sorted, and
experiments in teaching machines to make decisions could be tested. Advances built with libraries
such as SHRDLU helped propel language processing forward.
🎥 Click the image above for a video: Blocks world with SHRDLU
1993 - 2011
This epoch saw a new era for ML and AI to be able to solve some of the problems that had been
caused earlier by the lack of data and compute power. The amount of data began to rapidly increase
and become more widely available, for better and for worse, especially with the advent of the
smartphone around 2007. Compute power expanded exponentially, and algorithms evolved
alongside. The field began to gain maturity as the freewheeling days of the past began to crystallize
into a true discipline.
Now
Today machine learning and AI touch almost every part of our lives. This era calls for careful
understanding of the risks and potentials effects of these algorithms on human lives. As Microsoft's
Brad Smith has stated, "Information technology raises issues that go to the heart of fundamental
human-rights protections like privacy and freedom of expression. These issues heighten
responsibility for tech companies that create these products. In our view, they also call for thoughtful
government regulation and for the development of norms around acceptable uses" (source).
It remains to be seen what the future holds, but it is important to understand these computer systems
and the software and algorithms that they run. We hope that this curriculum will help you to gain a
better understanding so that you can decide for yourself.
🎥 Click the image above for a video: Yann LeCun discusses the history of deep learning in
this lecture
🚀Challenge
Dig into one of these historical moments and learn more about the people behind them. There are
fascinating characters, and no scientific discovery was ever created in a cultural vacuum. What do
you discover?
Post-lecture quiz
Review & Self Study
Here are items to watch and listen to:
This podcast where Amy Boyd discusses the evolution of AI
Assignment
Create a timeline
Pre-lecture quiz
Introduction
In this curriculum, you will start to discover how machine learning can and is impacting our everyday
lives. Even now, systems and models are involved in daily decision-making tasks, such as health care
diagnoses or detecting fraud. So it is important that these models work well in order to provide fair
outcomes for everyone.
Imagine what can happen when the data you are using to build these models lacks certain
demographics, such as race, gender, political view, religion, or disproportionally represents such
demographics. What about when the model's output is interpreted to favor some demographic?
What is the consequence for the application?
In this lesson, you will:
Raise your awareness of the importance of fairness in machine learning.
Learn about fairness-related harms.
Learn about unfairness assessment and mitigation.
Prerequisite
As a prerequisite, please take the "Responsible AI Principles" Learn Path and watch the video below
on the topic:
Learn more about Responsible AI by following this Learning Path
This statement sounds extreme, but it is true that data can be manipulated to support any
conclusion. Such manipulation can sometimes happen unintentionally. As humans, we all have bias,
and it's often difficult to consciously know when you are introducing bias in data.
Guaranteeing fairness in AI and machine learning remains a complex sociotechnical challenge.
Meaning that it cannot be addressed from either purely social or technical perspectives.
Fairness-related harms
What do you mean by unfairness? "Unfairness" encompasses negative impacts, or "harms", for a
group of people, such as those defined in terms of race, gender, age, or disability status.
The main fairness-related harms can be classified as:
Allocation, if a gender or ethnicity for example is favored over another.
Quality of service. If you train the data for one specific scenario but reality is much more
complex, it leads to a poor performing service.
Stereotyping. Associating a given group with pre-assigned attributes.
Denigration. To unfairly criticize and label something or someone.
Over- or under- representation. The idea is that a certain group is not seen in a certain
profession, and any service or function that keeps promoting that is contributing to harm.
Letʼs take a look at the examples.
Allocation
Consider a hypothetical system for screening loan applications. The system tends to pick white men
as better candidates over other groups. As a result, loans are withheld from certain applicants.
Another example would be an experimental hiring tool developed by a large corporation to screen
candidates. The tool systemically discriminated against one gender by using the models were trained
to prefer words associated with another. It resulted in penalizing candidates whose resumes contain
words such as "womenʼs rugby team".
✅ Do a little research to find a real-world example of something like this
Quality of Service
Researchers found that several commercial gender classifiers had higher error rates around images
of women with darker skin tones as opposed to images of men with lighter skin tones. Reference
Another infamous example is a hand soap dispenser that could not seem to be able to sense people
with dark skin. Reference
Stereotyping
Stereotypical gender view was found in machine translation. When translating “he is a nurse and she
is a doctor” into Turkish, problems were encountered. Turkish is a genderless language which has
one pronoun, “o” to convey a singular third person, but translating the sentence back from Turkish to
English yields the stereotypical and incorrect as “she is a nurse and he is a doctor”.
Denigration
An image labeling technology infamously mislabeled images of dark-skinned people as gorillas.
Mislabeling is harmful not just because the system made a mistake because it specifically applied a
label that has a long history of being purposefully used to denigrate Black people.
🎥 Click the image above for a video: AI, Ain't I a Woman - a performance showing the harm
caused by racist denigration by AI
Allocation Quality
service
of Stereotyping Denigration Over- or under-
representation
Automated hiring x x x x
system
Machine
translation
Photo labeling
Detecting unfairness
There are many reasons why a given system behaves unfairly. Social biases, for example, might be
reflected in the datasets used to train them. For example, hiring unfairness might have been
exacerbated by over reliance on historical data. By using the patterns in resumes submitted to the
company over a 10-year period, the model determined that men were more qualified because the
majority of resumes came from men, a reflection of past male dominance across the tech industry.
Inadequate data about a certain group of people can be the reason for unfairness. For example,
image classifiers a have higher rate of error for images of dark-skinned people because darker skin
tones were underrepresented in the data.
Wrong assumptions made during development cause unfairness too. For example, a facial analysis
system intended to predict who is going to commit a crime based on images of peopleʼs faces can
lead to damaging assumptions. This could lead to substantial harms for people who are misclassified.
Assessment methods
1. Identify harms (and benefits). The first step is to identify harms and benefits. Think about how
actions and decisions can affect both potential customers and a business itself.
2. Identify the affected groups. Once you understand what kind of harms or benefits that can
occur, identify the groups that may be affected. Are these groups defined by gender, ethnicity, or
social group?
3. Define fairness metrics. Finally, define a metric so you have something to measure against in
your work to improve the situation.
Mitigating unfairness
To mitigate unfairness, explore the model to generate various mitigated models and compare the
tradeoffs it makes between accuracy and fairness to select the most fair model.
This introductory lesson does not dive deeply into the details of algorithmic unfairness mitigation,
such as post-processing and reductions approach, but here is a tool that you may want to try.
Fairlearn
Fairlearn is an open-source Python package that allows you to assess your systems' fairness and
mitigate unfairness.
The tool helps you to assesses how a model's predictions affect different groups, enabling you to
compare multiple models by using fairness and performance metrics, and supplying a set of
algorithms to mitigate unfairness in binary classification and regression.
Learn how to use the different components by checking out the Fairlearn's GitHub
Explore the user guide, examples
Try some sample notebooks.
Learn how to enable fairness assessments of machine learning models in Azure Machine
Learning.
Check out these sample notebooks for more fairness assessment scenarios in Azure Machine
Learning.
🚀 Challenge
To prevent biases from being introduced in the first place, we should:
have a diversity of backgrounds and perspectives among the people working on systems
invest in datasets that reflect the diversity of our society
develop better methods for detecting and correcting bias when it occurs
Think about real-life scenarios where unfairness is evident in model-building and usage. What else
should we consider?
Post-lecture quiz
Review & Self Study
In this lesson, you have learned some basics of the concepts of fairness and unfairness in machine
learning.
Watch this workshop to dive deeper into the topics:
YouTube: Fairness-related harms in AI systems: Examples, assessment, and mitigation by Hanna
Wallach and Miro Dudik Fairness-related harms in AI systems: Examples, assessment, and
mitigation - YouTube
Also, read:
Microsoftʼs RAI resource center: Responsible AI Resources – Microsoft AI
Microsoftʼs FATE research group: FATE: Fairness, Accountability, Transparency, and Ethics in AI -
Microsoft Research
Explore the Fairlearn toolkit
Fairlearn
Read about Azure Machine Learning's tools to ensure fairness
Azure Machine Learning
Assignment
Explore Fairlearn
Pre-lecture quiz
Introduction
On a high level, the craft of creating machine learning (ML) processes is comprised of a number of
steps:
1. Decide on the question. Most ML processes start by asking a question that cannot be answered
by a simple conditional program or rules-based engine. These questions often revolve around
predictions based on a collection of data.
2. Collect and prepare data. To be able to answer your question, you need data. The quality and,
sometimes, quantity of your data will determine how well you can answer your initial question.
Visualizing data is an important aspect of this phase. This phase also includes splitting the data
into a training and testing group to build a model.
3. Choose a training method. Depending on your question and the nature of your data, you need to
choose how you want to train a model to best reflect your data and make accurate predictions
against it. This is the part of your ML process that requires specific expertise and, often, a
considerable amount of experimentation.
4. Train the model. Using your training data, you'll use various algorithms to train a model to
recognize patterns in the data. The model might leverage internal weights that can be adjusted to
privilege certain parts of the data over others to build a better model.
5. Evaluate the model. You use never before seen data (your testing data) from your collected set to
see how the model is performing.
6. Parameter tuning. Based on the performance of your model, you can redo the process using
different parameters, or variables, that control the behavior of the algorithms used to train the
model.
7. Predict. Use new inputs to test the accuracy of your model.
Pre-building tasks
Before starting to build your model, there are several tasks you need to complete. To test your
question and form a hypothesis based on a model's predictions, you need to identify and configure
several elements.
Data
To be able to answer your question with any kind of certainty, you need a good amount of data of the
right type. There are two things you need to do at this point:
Collect data. Keeping in mind the previous lesson on fairness in data analysis, collect your data
with care. Be aware of the sources of this data, any inherent biases it might have, and document
its origin.
Prepare data. There are several steps in the data preparation process. You might need to collate
data and normalize it if it comes from diverse sources. You can improve the data's quality and
quantity through various methods such as converting strings to numbers (as we do in Clustering).
You might also generate new data, based on the original (as we do in Classification). You can
clean and edit the data (as we will prior to the Web App lesson). Finally, you might also need to
randomize it and shuffle it, depending on your training techniques.
✅ After collecting and processing your data, take a moment to see if its shape will allow you to
address your intended question. It may be that the data will not perform well in your given task, as we
discover in our Clustering lessons!
Building a model
Using your training data, your goal is to build a model, or a statistical representation of your data,
using various algorithms to train it. Training a model exposes it to data and allows it to make
assumptions about perceived patterns it discovers, validates, and accepts or rejects.
Train a model
Armed with your training data, you are ready to 'fit' it to create a model. You will notice that in many
ML libraries you will find the code '[Link]' - it is at this time that you send in your feature variable as
an array of values (usually 'X') and a target variable (usually 'y').
Prediction
This is the moment where you can use completely new data to test your model's accuracy. In an
'applied' ML setting, where you are building web assets to use the model in production, this process
might involve gathering user input (a button press, for example) to set a variable and send it to the
model for inference, or evaluation.
In these lessons, you will discover how to use these steps to prepare, build, test, evaluate, and predict
- all the gestures of a data scientist and more, as you progress in your journey to become a 'full stack'
ML engineer.
🚀Challenge
Draw a flow chart reflecting the steps of a ML practitioner. Where do you see yourself right now in the
process? Where do you predict you will find difficulty? What seems easy to you?
Post-lecture quiz
Review & Self Study
Search online for interviews with data scientists who discuss their daily work. Here is one.
Assignment
Interview a data scientist
Get started with Python and Scikit-learn
for regression models
Pre-lecture quiz
This lesson is available in R!
Introduction
In these four lessons, you will discover how to build regression models. We will discuss what these
are for shortly. But before you do anything, make sure you have the right tools in place to start the
process!
In this lesson, you will learn how to:
Configure your computer for local machine learning tasks.
Work with Jupyter notebooks.
Use Scikit-learn, including installation.
Explore linear regression with a hands-on exercise.
🎥 Click the image above for a video: using Python within VS Code.
1. Install Python. Ensure that Python is installed on your computer. You will use Python for many
data science and machine learning tasks. Most computer systems already include a Python
installation. There are useful Python Coding Packs available as well, to ease the setup for some
users.
Some usages of Python, however, require one version of the software, whereas others require a
different version. For this reason, it's useful to work within a virtual environment.
2. Install Visual Studio Code. Make sure you have Visual Studio Code installed on your computer.
Follow these instructions to install Visual Studio Code for the basic installation. You are going to
use Python in Visual Studio Code in this course, so you might want to brush up on how to
configure Visual Studio Code for Python development.
Get comfortable with Python by working through this collection of Learn modules
3. Install Scikit-learn, by following these instructions. Since you need to ensure that you use Python
3, it's recommended that you use a virtual environment. Note, if you are installing this library on a
M1 Mac, there are special instructions on the page linked above.
4. Install Jupyter Notebook. You will need to install the Jupyter package.
hello notebook
You can interleaf your code with comments to self-document the notebook.
✅ Think for a minute how different a web developer's working environment is versus that of a data
scientist.
In the [Link] file associated to this lesson, clear out all the cells by pressing the 'trash can'
icon.
In this section, you will work with a small dataset about diabetes that is built into Scikit-learn for
learning purposes. Imagine that you wanted to test a treatment for diabetic patients. Machine
Learning models might help you determine which patients would respond better to the treatment,
based on combinations of variables. Even a very basic regression model, when visualized, might
show information about variables that would help you organize your theoretical clinical trials.
✅ There are many types of regression methods, and which one you pick depends on the answer
you're looking for. If you want to predict the probable height for a person of a given age, you'd use
linear regression, as you're seeking a numeric value. If you're interested in discovering whether a
type of cuisine should be considered vegan or not, you're looking for a category assignment so you
would use logistic regression. You'll learn more about logistic regression later. Think a bit about some
questions you can ask of data, and which of these methods would be more appropriate.
Let's get started on this task.
Import libraries
For this task we will import some libraries:
matplotlib. It's a useful graphing tool and we will use it to create a line plot.
numpy. numpy is a useful library for handling numeric data in Python.
sklearn. This is the Scikit-learn library.
Import some libraries to help with your tasks.
1. Add imports by typing the following code:
python
Above you are importing matplotlib , numpy and you are importing datasets ,
linear_model and model_selection from sklearn . model_selection is used for
splitting data into training and test sets.
In a new code cell, load the diabetes dataset by calling load_diabetes() . The input
return_X_y=True signals that X will be a data matrix, and y will be the regression target.
1. Add some print commands to show the shape of the data matrix and its first element:
python
X, y = datasets.load_diabetes(return_X_y=True)
print([Link])
print(X[0])
What you are getting back as a response, is a tuple. What you are doing is to assign the two first
values of the tuple to X and y respectively. Learn more about tuples.
You can see that this data has 442 items shaped in arrays of 10 elements:
text
(442, 10)
[ 0.03807591 0.05068012 0.06169621 0.02187235 -0.0442235 -0.03482076
-0.04340085 -0.00259226 0.01990842 -0.01764613]
✅ Think a bit about the relationship between the data and the regression target. Linear
regression predicts relationships between feature X and target variable y. Can you find the target
for the diabetes dataset in the documentation? What is this dataset demonstrating, given that
target?
2. Next, select a portion of this dataset to plot by arranging it into a new array using numpy's
newaxis function. We are going to use linear regression to generate a line between values in
this data, according to a pattern it determines.
python
X = X[:, [Link], 2]
4. Now you are ready to train your model! Load up the linear regression model and train it with your X
and y training sets using [Link]() :
python
model = linear_model.LinearRegression()
[Link](X_train, y_train)
✅ Think a bit about what's going on here. A straight line is running through many small dots of
data, but what is it doing exactly? Can you see how you should be able to use this line to predict
where a new, unseen data point should fit in relationship to the plot's y axis? Try to put into words
the practical use of this model.
Congratulations, you built your first linear regression model, created a prediction with it, and
displayed it in a plot!
🚀Challenge
Plot a different variable from this dataset. Hint: edit this line: X = X[:, [Link], 2] . Given
this dataset's target, what are you able to discover about the progression of diabetes as a disease?
Post-lecture quiz
Review & Self Study
In this tutorial, you worked with simple linear regression, rather than univariate or multiple linear
regression. Read a little about the differences between these methods, or take a look at this video
Read more about the concept of regression and think about what kinds of questions can be
answered by this technique. Take this tutorial to deepen your understanding.
Assignment
A different dataset
Introduction
Now that you are set up with the tools you need to start tackling machine learning model building
with Scikit-learn, you are ready to start asking questions of your data. As you work with data and
apply ML solutions, it's very important to understand how to ask the right question to properly unlock
the potentials of your dataset.
In this lesson, you will learn:
How to prepare your data for model-building.
How to use Matplotlib for data visualization.
🎥 Click the image above for a video covering key aspects of this lesson
Preparing data
This data is in the public domain. It can be downloaded in many separate files, per city, from the
USDA web site. To avoid too many separate files, we have concatenated all the city data into one
spreadsheet, thus we have already prepared the data a bit. Next, let's take a closer look at the data.
import pandas as pd
pumpkins = pd.read_csv('../data/[Link]')
[Link]()
✅ What function would you use to view the last five rows?
2. Check if there is missing data in the current dataframe:
python
[Link]().sum()
There is missing data, but maybe it won't matter for the task at hand.
3. To make your dataframe easier to work with, drop several of its columns, using drop() ,
keeping only the columns you need:
python
month = [Link](pumpkins['Date']).month
✅ Feel free to print any data you'd like to check using print(month) .
2. Now, copy your converted data into a fresh Pandas dataframe:
python
Printing out your dataframe will show you a clean, tidy dataset on which you can build your new
regression model.
Digging into the original data, it's interesting that anything with Unit of Sale equalling 'EACH' or
'PER BIN' also have the Package type per inch, per bin, or 'each'. Pumpkins seem to be very hard
to weigh consistently, so let's filter them by selecting only pumpkins with the string 'bushel' in their
Package column.
1. Add a filter at the top of the file, under the initial .csv import:
python
pumpkins = pumpkins[pumpkins['Package'].[Link]('bushel', case=True
If you print the data now, you can see that you are only getting the 415 or so rows of data
containing pumpkins by the bushel.
But wait! There's one more thing to do
Did you notice that the bushel amount varies per row? You need to normalize the pricing so that you
show the pricing per bushel, so do some math to standardize it.
1. Add these lines after the block creating the new_pumpkins dataframe:
python
new_pumpkins.loc[new_pumpkins['Package'].[Link]('1/2'), 'Price'] =
✅ According to The Spruce Eats, a bushel's weight depends on the type of produce, as it's a volume
measurement. "A bushel of tomatoes, for example, is supposed to weigh 56 pounds... Leaves and
greens take up more space with less weight, so a bushel of spinach is only 20 pounds." It's all pretty
complicated! Let's not bother with making a bushel-to-pound conversion, and instead price by the
bushel. All this study of bushels of pumpkins, however, goes to show how very important it is to
understand the nature of your data!
Now, you can analyze the pricing per unit based on their bushel measurement. If you print out the
data one more time, you can see how it's standardized.
✅ Did you notice that pumpkins sold by the half-bushel are very expensive? Can you figure out why?
Hint: little pumpkins are way pricier than big ones, probably because there are so many more of them
per bushel, given the unused space taken by one big hollow pie pumpkin.
Visualization Strategies
Part of the data scientist's role is to demonstrate the quality and nature of the data they are working
with. To do this, they often create interesting visualizations, or plots, graphs, and charts, showing
different aspects of data. In this way, they are able to visually show relationships and gaps that are
otherwise hard to uncover.
Visualizations can also help determine the machine learning technique most appropriate for the data.
A scatterplot that seems to follow a line, for example, indicates that the data is a good candidate for a
linear regression exercise.
One data visualization library that works well in Jupyter notebooks is Matplotlib (which you also saw
in the previous lesson).
Make it useful
To get charts to display useful data, you usually need to group the data somehow. Let's try creating a
plot where the y axis shows the months and the data demonstrates the distribution of data.
1. Add a cell to create a grouped bar chart:
python
new_pumpkins.groupby(['Month'])['Price'].mean().plot(kind='bar')
[Link]("Pumpkin Price")
This is a more useful data visualization! It seems to indicate that the highest price for pumpkins
occurs in September and October. Does that meet your expectation? Why or why not?
🚀Challenge
Explore the different types of visualization that Matplotlib offers. Which types are most appropriate
for regression problems?
Post-lecture quiz
Review & Self Study
Take a look at the many ways to visualize data. Make a list of the various libraries available and note
which are best for given types of tasks, for example 2D visualizations vs. 3D visualizations. What do
you discover?
Assignment
Exploring visualization
Introduction
So far you have explored what regression is with sample data gathered from the pumpkin pricing
dataset that we will use throughout this lesson. You have also visualized it using Matplotlib.
Now you are ready to dive deeper into regression for ML. In this lesson, you will learn more about two
types of regression: basic linear regression and polynomial regression, along with some of the math
underlying these techniques.
Throughout this curriculum, we assume minimal knowledge of math, and seek to make it
accessible for students coming from other fields, so watch for notes, 🧮 callouts, diagrams,
and other learning tools to aid in comprehension.
Prerequisite
You should be familiar by now with the structure of the pumpkin data that we are examining. You can
find it preloaded and pre-cleaned in this lesson's [Link] file. In the file, the pumpkin price is
displayed per bushel in a new dataframe. Make sure you can run these notebooks in kernels in Visual
Studio Code.
Preparation
As a reminder, you are loading this data so as to ask questions of it.
When is the best time to buy pumpkins?
What price can I expect of a case of miniature pumpkins?
Should I buy them in half-bushel baskets or by the 1 1/9 bushel box? Let's keep digging into this
data.
In the previous lesson, you created a Pandas dataframe and populated it with part of the original
dataset, standardizing the pricing by the bushel. By doing that, however, you were only able to gather
about 400 datapoints and only for the fall months.
Take a look at the data that we preloaded in this lesson's accompanying notebook. The data is
preloaded and an initial scatterplot is charted to show month data. Maybe we can get a little more
detail about the nature of the data by cleaning it more.
We do so since we want to model a line that has the least cumulative distance from all of our data
points. We also square the terms before adding them since we are concerned with its magnitude
rather than its direction.
Y = a + bX
X is the 'explanatory variable'. Y is the 'dependent variable'. The slope of the line is b
and a is the y-intercept, which refers to the value of Y when X = 0 .
First, calculate the slope b . Infographic by Jen Looper
In other words, and referring to our pumpkin data's original question: "predict the price of a
pumpkin per bushel by month", X would refer to the price and Y would refer to the month
of sale.
Calculate the value of Y. If you're paying around $4, it must be April! Infographic by Jen
Looper
The math that calculates the line must demonstrate the slope of the line, which is also
dependent on the intercept, or where Y is situated when X = 0 .
You can observe the method of calculation for these values on the Math is Fun web site. Also
visit this Least-squares calculator to watch how the numbers' values impact the line.
Correlation
One more term to understand is the Correlation Coefficient between given X and Y variables. Using
a scatterplot, you can quickly visualize this coefficient. A plot with datapoints scattered in a neat line
have high correlation, but a plot with datapoints scattered everywhere between X and Y have a low
correlation.
A good linear regression model will be one that has a high (nearer to 1 than 0) Correlation Coefficient
using the Least-Squares Regression method with a line of regression.
✅ Run the notebook accompanying this lesson and look at the City to Price scatterplot. Does the
data associating City to Price for pumpkin sales seem to have high or low correlation, according to
your visual interpretation of the scatterplot?
If you look at the new_pumpkins dataframe now, you see that all the strings are now numeric. This
makes it harder for you to read but much more intelligible for Scikit-learn! Now you can make more
educated decisions (not just based on eyeballing a scatterplot) about the data that is best suited to
regression.
Try to find a good correlation between two points of your data to potentially build a good predictive
model. As it turns out, there's only weak correlation between the City and Price:
python
print(new_pumpkins['City'].corr(new_pumpkins['Price']))
0.32363971816089226
However there's a bit better correlation between the Package and its Price. That makes sense, right?
Normally, the bigger the produce box, the higher the price.
python
print(new_pumpkins['Package'].corr(new_pumpkins['Price']))
0.6061712937226021
A good question to ask of this data will be: 'What price can I expect of a given pumpkin package?'
Let's build this regression model
new_pumpkins.dropna(inplace=True)
new_pumpkins.info()
Then, create a new dataframe from this minimal set and print it out:
python
lin_pumpkins
output
Package Price
70 0 13.636364
71 0 16.363636
72 0 16.363636
73 0 15.454545
74 0 13.636364
... ... ...
1738 2 30.000000
1739 2 28.750000
1740 2 25.750000
1741 2 24.000000
1742 2 24.000000
415 rows × 2 columns
✅ What's going on here? You're using Python slice notation to create arrays to populate X and
y .
pred = lin_reg.predict(X_test)
accuracy_score = lin_reg.score(X_train,y_train)
print('Model Accuracy: ', accuracy_score)
Because the correlation isn't particularly good, the model produced isn't terribly accurate.
output
Model Accuracy: 0.3315342327998987
[Link]('Package')
[Link]('Price')
[Link]()
array([[33.15655975]])
That number makes sense, if the logic of the regression line holds true.
🎃 Congratulations, you just created a model that can help predict the price of a few varieties of
pumpkins. Your holiday pumpkin patch will be beautiful. But you can probably create a better model!
Polynomial regression
Another type of linear regression is polynomial regression. While sometimes there's a linear
relationship between variables - the bigger the pumpkin in volume, the higher the price - sometimes
these relationships can't be plotted as a plane or straight line.
✅ Here are some more examples of data that could use polynomial regression
Take another look at the relationship between Variety to Price in the previous plot. Does this
scatterplot seem like it should necessarily be analyzed by a straight line? Perhaps not. In this case,
you can try polynomial regression.
✅ Polynomials are mathematical expressions that might consist of one or more variables and
coefficients
Polynomial regression creates a curved line to better fit nonlinear data.
1. Let's recreate a dataframe populated with a segment of the original pumpkin data:
python
poly_pumpkins
A good way to visualize the correlations between data in dataframes is to display it in a 'coolwarm'
chart:
2. Use the Background_gradient() method with coolwarm as its argument value:
python
corr = poly_pumpkins.corr()
[Link].background_gradient(cmap='coolwarm')
This code creates a heatmap:
Looking at this chart, you can visualize the good correlation between Package and Price. So you
should be able to create a somewhat better model than the last one.
Create a pipeline
Scikit-learn includes a helpful API for building polynomial regression models - the make_pipeline
API. A 'pipeline' is created which is a chain of estimators. In this case, the pipeline includes
polynomial features, or predictions that form a nonlinear path.
1. Build out the X and y columns:
python
X=poly_pumpkins.iloc[:,3:4].values
y=poly_pumpkins.iloc[:,4:5].values
[Link]([Link](X_train), y_train)
y_pred=[Link](X_test)
Create a sequence
At this point, you need to create a new dataframe with sorted data so that the pipeline can create a
sequence.
Add the following code:
python
You created a new dataframe by calling [Link] . Then you sorted the values by calling
sort_values() . Finally you created a polynomial plot:
You can see a curved line that fits your data better.
Let's check the model's accuracy:
python
accuracy_score = [Link](X_train,y_train)
print('Model Accuracy: ', accuracy_score)
And voila!
output
Do a prediction
Can we input a new value and get a prediction?
Call predict() to make a prediction:
python
[Link]( [Link]([ [2.75] ]) )
array([[46.34509342]])
It does make sense, given the plot! And, if this is a better model than the previous one, looking at the
same data, you need to budget for these more expensive pumpkins!
🏆 Well done! You created two regression models in one lesson. In the final section on regression,
you will learn about logistic regression to determine categories.
🚀Challenge
Test several different variables in this notebook to see how correlation corresponds to model
accuracy.
Post-lecture quiz
Review & Self Study
In this lesson we learned about Linear Regression. There are other important types of Regression.
Read about Stepwise, Ridge, Lasso and Elasticnet techniques. A good course to study to learn more
is the Stanford Statistical Learning course
Assignment
Build a Model
Introduction
In this final lesson on Regression, one of the basic classic ML techniques, we will take a look at
Logistic Regression. You would use this technique to discover patterns to predict binary categories. Is
this candy chocolate or not? Is this disease contagious or not? Will this customer choose this product
or not?
In this lesson, you will learn:
A new library for data visualization
Techniques for logistic regression
✅ Deepen your understanding of working with this type of regression in this Learn module
Prerequisite
Having worked with the pumpkin data, we are now familiar enough with it to realize that there's one
binary category that we can work with: Color .
Let's build a logistic regression model to predict that, given some variables, what color a given
pumpkin is likely to be (orange 🎃 or white 👻 ).
Why are we talking about binary classification in a lesson grouping about regression? Only for
linguistic convenience, as logistic regression is really a classification method, albeit a linear-
based one. Learn about other ways to classify data in the next lesson group.
🎃 Fun fact, we sometimes call white pumpkins 'ghost' pumpkins. They aren't very easy to
carve, so they aren't as popular as the orange ones but they are cool looking!
Binary classification
Logistic regression does not offer the same features as linear regression. The former offers a
prediction about a binary category ("orange or not orange") whereas the latter is capable of
predicting continual values, for example given the origin of a pumpkin and the time of harvest, how
much its price will rise.
Infographic by Dasani Madipalli
Other classifications
There are other types of logistic regression, including multinomial and ordinal:
Multinomial, which involves having more than one category - "Orange, White, and Striped".
Ordinal, which involves ordered categories, useful if we wanted to order our outcomes logically,
like our pumpkins that are ordered by a finite number of sizes (mini,sm,med,lg,xl,xxl).
new_pumpkins.dropna(inplace=True)
new_pumpkins = new_pumpkins.apply(LabelEncoder().fit_transform)
new_pumpkins.info
g = [Link](new_pumpkins)
[Link]([Link])
By observing data side-by-side, you can see how the Color data relates to the other columns.
✅ Given this scatterplot grid, what are some interesting explorations you can envision?
Use a swarm plot
Since Color is a binary category (Orange or Not), it's called 'categorical data' and needs 'a more
specialized approach to visualization'. There are other ways to visualize the relationship of this
category with other variables.
You can visualize variables side-by-side with Seaborn plots.
1. Try a 'swarm' plot to show the distribution of values:
python
Violin plot
A 'violin' type plot is useful as you can easily visualize the way that data in the two categories is
distributed. Violin plots don't work so well with smaller datasets as the distribution is displayed more
'smoothly'.
1. As parameters x=Color , kind="violin" and call catplot() :
python
where the sigmoid's midpoint finds itself at x's 0 point, L is the curve's maximum value, and k
is the curve's steepness. If the outcome of the function is more than 0.5, the label in question
will be given the class '1' of the binary choice. If not, it will be classified as '0'.
Build your model
Building a model to find these binary classification is surprisingly straightforward in Scikit-learn.
1. Select the variables you want to use in your classification model and split the training and test sets
calling train_test_split() :
python
X = new_pumpkins[Selected_features]
y = new_pumpkins['Color']
2. Now you can train your model, by calling fit() with your training data, and print out its result:
python
model = LogisticRegression()
[Link](X_train, y_train)
predictions = [Link](X_test)
print(classification_report(y_test, predictions))
print('Predicted labels: ', predictions)
print('Accuracy: ', accuracy_score(y_test, predictions))
Take a look at your model's scoreboard. It's not too bad, considering you have only about 1000
rows of data:
output
Predicted labels: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 1 0 0 0 1 0]
🎓 A 'confusion matrix' (or 'error matrix') is a table that expresses your model's true vs. false
positives and negatives, thus gauging the accuracy of predictions.
array([[162, 4],
[ 33, 0]])
In Scikit-learn, confusion matrices Rows (axis 0) are actual labels and columns (axis 1) are predicted
labels.
0 1
0 TN FP
0 1
1 FN TP
What's going on here? Let's say our model is asked to classify pumpkins between two binary
categories, category 'orange' and category 'not-orange'.
If your model predicts a pumpkin as not orange and it belongs to category 'not-orange' in reality
we call it a true negative, shown by the top left number.
If your model predicts a pumpkin as orange and it belongs to category 'not-orange' in reality we
call it a false negative, shown by the bottom left number.
If your model predicts a pumpkin as not orange and it belongs to category 'orange' in reality we
call it a false positive, shown by the top right number.
If your model predicts a pumpkin as orange and it belongs to category 'orange' in reality we call it
a true positive, shown by the bottom right number.
As you might have guessed it's preferable to have a larger number of true positives and true
negatives and a lower number of false positives and false negatives, which implies that the model
performs better.
How does the confusion matrix relate to precision and recall? Remember, the classification report
printed above showed precision (0.83) and recall (0.98).
Precision = tp / (tp + fp) = 162 / (162 + 33) = 0.8307692307692308
Recall = tp / (tp + fn) = 162 / (162 + 4) = 0.9759036144578314
✅ Q: According to the confusion matrix, how did the model do? A: Not too bad; there are a good
number of true negatives but also several false negatives.
Let's revisit the terms we saw earlier with the help of the confusion matrix's mapping of TP/TN and
FP/FN:
🎓 Precision: TP/(TP + FP) The fraction of relevant instances among the retrieved instances (e.g.
which labels were well-labeled)
🎓 Recall: TP/(TP + FN) The fraction of relevant instances that were retrieved, whether well-labeled
or not
🎓 f1-score: (2 * precision * recall)/(precision + recall) A weighted average of the precision and recall,
with best being 1 and worst being 0
🎓 Support: The number of occurrences of each label retrieved
🎓 Accuracy: (TP + TN)/(TP + TN + FP + FN) The percentage of labels predicted accurately for a
sample.
🎓 Macro Avg: The calculation of the unweighted mean metrics for each label, not taking label
imbalance into account.
🎓 Weighted Avg: The calculation of the mean metrics for each label, taking label imbalance into
account by weighting them by their support (the number of true instances for each label).
✅ Can you think which metric you should watch if you want your model to reduce the number of
false negatives?
y_scores = model.predict_proba(X_test)
# calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_test, y_scores[:,1])
[Link]([0, 1], [0, 1])
[Link](fpr, tpr)
Using Seaborn again, plot the model's Receiving Operating Characteristic or ROC. ROC curves are
often used to get a view of the output of a classifier in terms of its true vs. false positives. "ROC
curves typically feature true positive rate on the Y axis, and false positive rate on the X axis." Thus,
the steepness of the curve and the space between the midpoint line and the curve matter: you want a
curve that quickly heads up and over the line. In our case, there are false positives to start with, and
then the line heads up and over properly:
Finally, use Scikit-learn's roc_auc_score API to compute the actual 'Area Under the Curve'
(AUC):
python
auc = roc_auc_score(y_test,y_scores[:,1])
print(auc)
The result is 0.6976998904709748 . Given that the AUC ranges from 0 to 1, you want a big score,
since a model that is 100% correct in its predictions will have an AUC of 1; in this case, the model is
pretty good.
In future lessons on classifications, you will learn how to iterate to improve your model's scores. But
for now, congratulations! You've completed these regression lessons!
🚀Challenge
There's a lot more to unpack regarding logistic regression! But the best way to learn is to experiment.
Find a dataset that lends itself to this type of analysis and build a model with it. What do you learn?
tip: try Kaggle for interesting datasets.
Post-lecture quiz
Review & Self Study
Read the first few pages of this paper from Stanford on some practical uses for logistic regression.
Think about tasks that are better suited for one or the other type of regression tasks that we have
studied up to this point. What would work best?
Assignment
Retrying this regression
Pre-lecture quiz
Building an app
There are several ways to build web apps to consume machine learning models. Your web
architecture may influence the way your model is trained. Imagine that you are working in a business
where the data science group has trained a model that they want you to use in an app.
Considerations
There are many questions you need to ask:
Is it a web app or a mobile app? If you are building a mobile app or need to use the model in an
IoT context, you could use TensorFlow Lite and use the model in an Android or iOS app.
Where will the model reside? In the cloud or locally?
Offline support. Does the app have to work offline?
What technology was used to train the model? The chosen technology may influence the
tooling you need to use.
Using Tensor flow. If you are training a model using TensorFlow, for example, that ecosystem
provides the ability to convert a TensorFlow model for use in a web app by using [Link].
Using PyTorch. If you are building a model using a library such as PyTorch, you have the
option to export it in ONNX (Open Neural Network Exchange) format for use in JavaScript web
apps that can use the Onnx Runtime. This option will be explored in a future lesson for a Scikit-
learn-trained model.
Using [Link] or Azure Custom Vision. If you are using an ML SaaS (Software as a Service)
system such as [Link] or Azure Custom Vision to train a model, this type of software provides
ways to export the model for many platforms, including building a bespoke API to be queried in
the cloud by your online application.
You also have the opportunity to build an entire Flask web app that would be able to train the model
itself in a web browser. This can also be done using [Link] in a JavaScript context.
For our purposes, since we have been working with Python-based notebooks, let's explore the steps
you need to take to export a trained model from such a notebook to a format readable by a Python-
built web app.
Tool
For this task, you need two tools: Flask and Pickle, both of which run on Python.
✅ What's Flask? Defined as a 'micro-framework' by its creators, Flask provides the basic features of
web frameworks using Python and a templating engine to build web pages. Take a look at this Learn
module to practice building with Flask.
✅ What's Pickle? Pickle 🥒 is a Python module that serializes and de-serializes a Python object
structure. When you 'pickle' a model, you serialize or flatten its structure for use on the web. Be
careful: pickle is not intrinsically secure, so be careful if prompted to 'un-pickle' a file. A pickled file
has the suffix .pkl .
import pandas as pd
import numpy as np
ufos = pd.read_csv('./data/[Link]')
[Link]()
2. Convert the ufos data to a small dataframe with fresh titles. Check the unique values in the
Country field.
python
[Link]()
3. Now, you can reduce the amount of data we need to deal with by dropping any null values and
only importing sightings between 1-60 seconds:
python
[Link](inplace=True)
[Link]()
4. Import Scikit-learn's LabelEncoder library to convert the text values for countries to a
number:
✅ LabelEncoder encodes data alphabetically
python
ufos['Country'] = LabelEncoder().fit_transform(ufos['Country'])
[Link]()
Selected_features = ['Seconds','Latitude','Longitude']
X = ufos[Selected_features]
y = ufos['Country']
print(classification_report(y_test, predictions))
print('Predicted labels: ', predictions)
print('Accuracy: ', accuracy_score(y_test, predictions))
The accuracy isn't bad (around 95%), unsurprisingly, as Country and Latitude/Longitude
correlate.
The model you created isn't very revolutionary as you should be able to infer a Country from its
Latitude and Longitude , but it's a good exercise to try to train from raw data that you
cleaned, exported, and then use this model in a web app.
import pickle
model_filename = '[Link]'
[Link](model, open(model_filename,'wb'))
model = [Link](open('[Link]','rb'))
print([Link]([[50,44,-12]]))
The model returns '3', which is the country code for the UK. Wild! 👽
web-app/
static/
css/
templates/
[Link]
[Link]
scikit-learn
pandas
numpy
flask
5. In your terminal type pip install , to install the libraries listed in [Link]:
bash
6. Now, you're ready to create three more files to finish the app:
1. Create [Link] in the root.
2. Create [Link] in templates directory.
3. Create [Link] in static/css directory.
7. Build out the [Link] file with a few styles:
css
body {
width: 100%;
height: 100%;
font-family: 'Helvetica';
background: black;
color: #fff;
text-align: center;
letter-spacing: 1.4px;
font-size: 30px;
}
input {
min-width: 150px;
}
.grid {
width: 300px;
border: 1px solid #2d2d2d;
display: grid;
justify-content: center;
margin: 20px auto;
}
.box {
color: #fff;
background: #2d2d2d;
padding: 12px;
display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 🛸 UFO Appearance Prediction! 👽 </title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/styl
</head>
<body>
<div class="grid">
<div class="box">
</div>
</div>
</body>
</html>
Take a look at the templating in this file. Notice the 'mustache' syntax around variables that will be
provided by the app, like the prediction text: {{}} . There's also a form that posts a prediction
to the /predict route.
Finally, you're ready to build the python file that drives the consumption of the model and the
display of predictions:
9. In [Link] add:
python
import numpy as np
from flask import Flask, request, render_template
import pickle
app = Flask(__name__)
@[Link]("/")
def home():
return render_template("[Link]")
@[Link]("/predict", methods=["POST"])
def predict():
output = prediction[0]
return render_template(
"[Link]", prediction_text="Likely country: {}".format(countr
)
if __name__ == "__main__":
[Link](debug=True)
💡 Tip: when you add debug=True while running the web app using Flask, any
changes you make to your application will be reflected immediately without the need to
restart the server. Beware! Don't enable this mode in a production app.
If you run python [Link] or python3 [Link] - your web server starts up, locally, and you
can fill out a short form to get an answer to your burning question about where UFOs have been
sighted!
Before doing that, take a look at the parts of [Link] :
1. First, dependencies are loaded and the app starts.
2. Then, the model is imported.
3. Then, [Link] is rendered on the home route.
On the /predict route, several things happen when the form is posted:
1. The form variables are gathered and converted to a numpy array. They are then sent to the model
and a prediction is returned.
2. The Countries that we want displayed are re-rendered as readable text from their predicted
country code, and that value is sent back to [Link] to be rendered in the template.
Using a model this way, with Flask and a pickled model, is relatively straightforward. The hardest
thing is to understand what shape the data is that must be sent to the model to get a prediction. That
all depends on how the model was trained. This one has three data points to be input in order to get a
prediction.
In a professional setting, you can see how good communication is necessary between the folks who
train the model and those who consume it in a web or mobile app. In our case, it's only one person,
you!
🚀 Challenge
Instead of working in a notebook and importing the model to the Flask app, you could train the model
right within the Flask app! Try converting your Python code in the notebook, perhaps after your data
is cleaned, to train the model from within the app on a route called train . What are the pros and
cons of pursuing this method?
Post-lecture quiz
Review & Self Study
There are many ways to build a web app to consume ML models. Make a list of the ways you could
use JavaScript or Python to build a web app to leverage machine learning. Consider architecture:
should the model stay in the app or live in the cloud? If the latter, how would you access it? Draw out
an architectural model for an applied ML web solution.
Assignment
Try a different model
Introduction to classification
In these four lessons, you will explore a fundamental focus of classic machine learning - classification.
We will walk through using various classification algorithms with a dataset about all the brilliant
cuisines of Asia and India. Hope you're hungry!
Classification is a form of supervised learning that bears a lot in common with regression techniques.
If machine learning is all about predicting values or names to things by using datasets, then
classification generally falls into two groups: binary classification and multiclass classification.
🎥 Click the image above for a video: MIT's John Guttag introduces classification
Remember:
Linear regression helped you predict relationships between variables and make accurate
predictions on where a new datapoint would fall in relationship to that line. So, you could predict
what price a pumpkin would be in September vs. December, for example.
Logistic regression helped you discover "binary categories": at this price point, is this pumpkin
orange or not-orange?
Classification uses various algorithms to determine other ways of determining a data point's label or
class. Let's work with this cuisine data to see whether, by observing a group of ingredients, we can
determine its cuisine of origin.
Pre-lecture quiz
This lesson is available in R!
Introduction