Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

INTRODUCTION TO

MACHINE LEARNING
What is Machine Learning?
Learn to improve algorithms from data.

Traditional approach Machine Learning


Domain or expert knowledge Data-driven

Why?
• Human expertise does not exist (ex: complex medical processes we don’t fully
understand)
• Humans are unable to explain their expertise (speech recognition)
• Solution change or adapt in time (routing on a computer network)
2
Example 1: Digit Recognition

• Problem: Recognize a digit from the image


• MNIST dataset challenge
• Dataset developed in 1990s to spur AI research on a challenging problem for the time
• Data taken from census forms
• Became a classic benchmark for machine vision problems
• We will see this dataset extensively in this class

3
Classical “Expert” Approach

• Idea: Use your knowledge about digits


• You are an “expert” since you can do the task
• Construct simple rules and code them

• Expert rule example: “Image is a digit 7 if…”:


• There is a single horizontal line, and
• There is a single vertical line

• Rule seems simple and reasonable


• But,…

4
Problems with Expert Rules

• Simple expert rule breaks down in practice


• Hard to define a “line” precisely
• Orientation, length, thickness, …
• May be multiple lines…

• General problem: We cannot easily code our


knowledge
• We can do the task
• But, it is hard to translate to simple mathematical formula

5
ML Approach: Learn from Data
?

6
Example 2: Face Detection

• Problem: For each image region, determine if face or non-face


• More challenging than digit recognition
• Even harder to describe a face via “rules” in a robust way

7
Machine Learning in Many Fields
• Retail: Market basket analysis, Customer relationship management
(CRM)
• Finance: Credit scoring, fraud detection
• Manufacturing: Control, robotics, troubleshooting
• Medicine: Medical diagnosis
• Telecommunications: Spam filters, intrusion detection
• Bioinformatics: Motifs, alignment
• Web mining: Search engines
• ...

8
Definition of Machine Learning
• Arthur Samuel (1959). Machine Learning: Field of study that gives
computers the ability to learn without being explicitly programmed.

• Tom Mitchell (1998) Well-posed Learning Problem: A computer


program is said to learn from experience E with respect to some task
T and some performance measure P, if its performance on T, as
measured by P, improves with experience E.
How it is different from traditional Programming:

In Traditional Programming, we feed the Input,


Program logic and run the program to get
output.
In Machine Learning, we feed the input, output
and run it on machine during training and the
machine creates its own logic, which is being
evaluated while testing.
Machine learning algorithms:
• Supervised learning
• regression: predict numerical values
• classification: predict categorical values, i.e., labels
• Unsupervised learning
• clustering: group data according to "distance"
• association: find frequent co-occurrences
• link prediction: discover relationships in data
• data reduction: project features to fewer features
• Reinforcement learning
Classification

Savings
Low risk

High risk

Income

12
Classification
Object recognition

13
Regression
Happiness Score

Income

14
Regression
Colorize B&W images automatically

15
Unsupervised Learning

Example: Document classification

16
Reinforcement Learning
Move:
…Bd4

State: Current position


Reward: Win or Lose
• Agent learns to make actions that interact with an environment to maximize a
reward
• Agent typically acts in a closed loop system
• Key tradeoffs:
• Exploitation (Learn from past actions) vs. exploration (try new choices)
• Credit assignment: Which actions in the past led to the current reward?

17
Reinforcement
learning
Learning to play Break Out

18
What ML is Doing Today?
• Autonomous driving
• Very difficult games
• Machine translation

• Many, many others…

19
Why Now?
• Machine learning is an old field
• Much of the pioneering statistical work dates to the 1950s
• So what is new now?
• Big Data:
• Massive storage. Large data centers
• Massive connectivity
• Sources of data from Internet and elsewhere
• Computational advances
• Distributed machines, clusters
• GPUs and hardware
Google Tensor Processing Unit (TPU)

20
The machine learning framework
• Apply a prediction function to a feature representation of the
image to get the desired output:

f( ) = “apple”
f( ) = “tomato”
f( ) = “cow”
Slide credit: L. Lazebnik
The machine learning framework
y = f(x)
output prediction Image
function feature

• Training: given a training set of labeled examples {(x1,y1), …,


(xN,yN)}, estimate the prediction function f by minimizing the
prediction error on the training set
• Testing: apply f to a never before seen test example x and output
the predicted value y = f(x)

Slide credit: L. Lazebnik


Steps
Training Training
Labels
Training
Images
Image Learned
Training
Features model

Testing

Image Learned
Prediction
Features model
Test Image Slide credit: D. Hoiem and L. Lazebnik
Supervised Learning

x2

x1
Unsupervised Learning

x2

x1
Linear Regression
Getting Data
• Data from UCI dataset library:
https://archive.ics.uci.edu/ml/index.php

33
Python Packages
• Python has many powerful packages
• This demo uses three key packages
• Pandas:
• Used for reading and writing data files
• Loads data into dataframes
• Numpy
• Numerical operations including linear algebra
• Data is stored in ndarray structure
• We convert from dataframes to ndarray
• Matplotlib:
• MATLAB-like plotting and visualization

34
Visualizing the Data
• When possible, look at data before
doing anything
• Python has MATLAB-like plotting
• Matplotlib module

35
Data

36
Linear Model

Regression line

37
Least Squares Model Fitting

38
Finding Parameters via Optimization
A general ML recipe
General ML problem Simple linear regression
• Find a model with
parameters
• Get data
• Pick a loss function
• Measures goodness of fit
model to data
• Function of the parameters

• Find parameters that


minimizes loss

39
Linear Regression
• Single variable linear regression
• single variable means there is only one independent variable (x)
• Multi-variable linear regression
• there are multi independent variable (x1,x2,x…)
Code in python
• Problem:
predicting the price of a house
based on the area of house.

• Given below is a sample of


data showing area of the
house and corresponding
price for houses in Monroe,
New Jersey, USA.
Importing libraries
• First we will import some important libraries.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
Load dataset
• Here, read_csv is a method used to load our dataset in the
file house_prices.csv.

df = pd.read_csv("house_prices.csv")
Plot
• plot a scatter plot to see the distribution of our house prices
data.

plt.xlabel("Area(sq.ft.)")
plt.ylabel("Price($)")
plt.scatter(df.Area, df.Price)
Training model
• we will fit our data using fit method. Fitting the data means we
are training the linear regression model using the available data
in our dataset.

reg = linear_model.LinearRegression()
reg.fit(df[['Area']], df.Price)
Prediction
• Once we execute the above code, linear regression model will
fit our data and will be ready to predict the price of houses for
given data.

test_x=[[3400]]
test_y=reg.predict(test_x)
Draw best fit line
• Now let’s see the linear regression line on our scatter plot. For
that we need to add another line in Python as shown below.

plt.xlabel("Area(sq.ft.)")
plt.ylabel("Price($)")
plt.scatter(df.Area, df.Price)
plt.plot(df.Area, reg.predict(df[['Area']]), color='blue')
Draw predicted output
• Draw predicted value on plot

• plt.scatter(test_x, test_y, color='green')


Linear regression on multi-variable
• Equation for multivariable linear regression
Multivalued dataset
Prediction

• predict the price of a house with 3000 sq. ft. area, 3 bedrooms,
and 40 years of age.

You might also like