Docsity

CS-7641-Machine-Learning-Notes-Midterm, Study notes of Information Technology

Toggle sidebar
bg1
CS7641 SL01: Decision Trees Mohamed Ameen Amer
Page | 1
SL00: Supervised Learning
• Supervised learning → Function approximation.
• Unsupervised learning → Function description.
• Reinforcement learning → Learning from delayed reward.
• Algorithms and data are co-equal.
In a supervised learning setting, the learning algorithm will be provided with instances and labels as
training data.
• Induction: Going from specifics to generalities.
• Classification: The classification task can be summarized as taking input 𝑥 (e.g. images of traffic
signs) and assign it to a label 𝑦 that belongs to a discrete number of labels 𝑌 (e.g. STOP, Speed Limit,
Keep Right, etc.).
• Regression: The regression task can be summarized as taking input 𝑥 (e.g. employee’s years of
experience) and mapping it to a real continuous value (e.g. expected salary).
Terminology:
• Instances: Inputs (Features).
• Concept: A set of functions that make good candidates to map inputs to outputs.
• Target concept: The actual function that can map inputs to outputs. In other words, the concept that
we’re willing to model.
• Hypothesis class: A subset of the concept set of functions that is easier to search through for the
target concept.
• Sample (Training set): A set of instances with correct labels.
• Candidate: The best approximation of the target concept.
• Testing set: A set of instances with correct labels that was not visible to the learning algorithm
during the training phase. It’s used to determine the algorithm performance on novel data.
SL01: Decision Trees:
A sequence of decision making points (nodes) applied to every instance to assign it to a specific class.
Example: Classifying vehicles into Sedans and Trucks.
Representation vs Algorithm:
• A decision tree is a structure of nodes, edges and leaves that can be used to represent the data.
1. Nodes represent attributes (vehicle length, vehicle height, number of doors, etc.).
2. Edges represent values. (A specific value for each attribute)
3. Leaves represent the output (vehicle’s type).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download CS-7641-Machine-Learning-Notes-Midterm and more Study notes Information Technology in PDF only on Docsity!

CS7641 SL01: Decision Trees Mohamed Ameen Amer

SL00: Supervised Learning

  • Supervised learning → Function approximation.
  • Unsupervised learning → Function description.
  • Reinforcement learning → Learning from delayed reward.
  • Algorithms and data are co-equal.

In a supervised learning setting, the learning algorithm will be provided with instances and labels as

training data.

  • Induction: Going from specifics to generalities.
  • Classification: The classification task can be summarized as taking input 𝑥 (e.g. images of traffic

signs) and assign it to a label 𝑦 that belongs to a discrete number of labels 𝑌 (e.g. STOP, Speed Limit,

Keep Right, etc.).

  • Regression: The regression task can be summarized as taking input 𝑥 (e.g. employee’s years of

experience) and mapping it to a real continuous value (e.g. expected salary).

Terminology:

  • Instances: Inputs (Features).
  • Concept: A set of functions that make good candidates to map inputs to outputs.
  • Target concept: The actual function that can map inputs to outputs. In other words, the concept that

we’re willing to model.

  • Hypothesis class: A subset of the concept set of functions that is easier to search through for the

target concept.

  • Sample (Training set): A set of instances with correct labels.
  • Candidate: The best approximation of the target concept.
  • Testing set: A set of instances with correct labels that was not visible to the learning algorithm

during the training phase. It’s used to determine the algorithm performance on novel data.

SL01: Decision Trees:

A sequence of decision making points (nodes) applied to every instance to assign it to a specific class.

Example: Classifying vehicles into Sedans and Trucks.

Representation vs Algorithm:

  • A decision tree is a structure of nodes, edges and leaves that can be used to represent the data.
    1. Nodes represent attributes (vehicle length, vehicle height, number of doors, etc.).
    2. Edges represent values. (A specific value for each attribute)
    3. Leaves represent the output (vehicle’s type).

CS7641 SL01: Decision Trees Mohamed Ameen Amer

  • A decision tree algorithm is a sequence of steps that will lead you to the desired output.
    1. Pick the best attribute (the one that can split the data roughly in half).
      • If this attribute added no valuable information (not a good split), it might cause overfitting.
    2. Ask a question about this attribute.
    3. Follow the correct answer path.
    4. Loop back to (1) till you narrow down the possibilities to one answer (the output).

Note that the usefulness of a question depends upon the answers we got to previous questions.

Appropriate Problems for Decision Trees:

  • Instances are represented by attribute-value pairs.
  • The target function has discrete output values.
  • Disjunctive descriptions maybe required.
  • The training data may contain errors.
  • The training data may contain missing attribute values.

Expressiveness:

  • Decision trees can basically express any function using the input attributes.
  • For Boolean functions (AND, OR, etc.), the truth table row will be translated into a path to a leaf.
  • Decision tree hypothesis space is very huge ( 2

( 2

𝑛

)

), this why we need to design algorithms to

efficiently search the hypothesis space for the best decision tree.

ID3 Algorithm:

  • ID3 builds decision trees using a top-down, greedy approach.
  • Pseudocode:
    1. Pick the best attribute 𝐴.
      • To select this attribute, a statistical test is used to determine for each attribute how well it

alone classifies the training examples.

  1. Split the data into subsets that correspond to the possible values of the best attribute.
  2. Assign 𝐴 as a decision attribute for a node.
  3. For each value of 𝐴, create a descendent node.
  4. Sort training examples to leaves.
  5. If (examples perfectly classified) {

Stop

} else {

Iterate over leaves

CS7641 SL01: Decision Trees Mohamed Ameen Amer

  • We say that ID3 uses a greedy approach because it will decide which attribute should be at the root

of the tree by looking just one move ahead. It compares all available attributes to find which one

classifies the data the best, but it doesn’t look ahead (or behind) at other attributes to see which

combinations of them classify the data the best. This means that while the algorithm will, in most

cases, come up with a good decision tree, a better one may exist.

  • ID3 algorithm returns an optimal decision tree, but as the size of the training data and the number

of attributes increase, it becomes more likely that running ID3 on it will return a suboptimal decision

tree.

Other considerations:

  • What if we have continuous attributes?
    • Information gain for a continuous attribute would be the full entropy of 𝑆. This is because there

is only one observation per attribute value. For each value of the attribute, the training data

agrees unanimously on a classification and the entropy for each attribute value is 0.

  • To solve this issue, we split the attribute range into equal intervals.
  • A modified ID3 can be used to find the best split.
  • Does it make sense to repeat an attribute along a path in the tree?
  • No if this attribute has a finite value.
  • Continuous attributes can be tested with different questions.
  • When do we stop?
  • When everything is classified correctly.
  • No more attributes.
  • When you have noise, don’t overfit:

→ Not too big tree.

→ We can use cross-validation to prevent overfitting.

→ Use a validation set and stop when the error is low enough.

→ Build the whole tree, the prune it. Pruning means removing sections of the tree that provide

little power in classifying instances.

  • What if it’s a regression problem?
    • Information Gain won’t be efficient because it’s not clear how to measure information over

continuous values.

  • We can measure variance, which is the deviation of a variable from its mean value.
  • We need to decide what to report on the leaves (output). Average might be an option.

CS7641 SL01: Decision Trees Mohamed Ameen Amer

  • If we have continuous variables or discrete variables with many possible values, we might end up

assigning high information gain to these variables but producing a decision tree that might not

generalize well.

  • A solution to this would be to use the GainRatio instead of information gain:

measures how broadly partitioned an attribute is and how evenly

partitioned those partitions are.

𝑣

log

𝑣

𝑣

→ If we have an attribute with many possible values, each of which occurs few times, the

fraction

|𝑆

𝑣

|

|𝑆|

will have a small value. This will affect the attribute’s chances of being the best

attribute after an iteration of the ID3 algorithm and help to avoid overfitting.

Decision Trees Complexity:

  • The run time cost to construct a balanced binary tree is 𝑂(𝑛 𝑠𝑎𝑚𝑝𝑙𝑒𝑠

𝑓𝑒𝑎𝑡𝑢𝑟𝑒𝑠

log 𝑛

𝑠𝑎𝑚𝑝𝑙𝑒𝑠

  • The query time is 𝑂(log 𝑛 𝑠𝑎𝑚𝑝𝑙𝑒𝑠
  • Assuming that the subtrees remain approximately balanced, the cost at each node consists of

searching through 𝑂(log 𝑛

𝑓𝑒𝑎𝑡𝑢𝑟𝑒𝑠

) to find the feature that offers the largest reduction in entropy.

This has a cost of 𝑂 (

𝑠𝑎𝑚𝑝𝑙𝑒𝑠

𝑓𝑒𝑎𝑡𝑢𝑟𝑒𝑠

log 𝑛

𝑠𝑎𝑚𝑝𝑙𝑒𝑠

at each node, leading to a total cost over the

entire trees (by summing the cost at each node) of 𝑂(𝑛

2

𝑠𝑎𝑚𝑝𝑙𝑒𝑠

𝑓𝑒𝑎𝑡𝑢𝑟𝑒𝑠

log 𝑛

𝑠𝑎𝑚𝑝𝑙𝑒𝑠

Advantages and Disadvantages of Decision Trees:

  • Advantages:
    • Simple to understand and to interpret.
    • Requires little data preparation. Other techniques often require data normalization, dummy

variables need to be created and blank values to be removed.

  • The cost of using the tree (i.e., predicting data) is logarithmic in the number of data points used

to train the tree.

  • Able to handle both numerical and categorical data. Other techniques are usually specialized in

analyzing datasets that have only one type of variable.

  • Able to handle multi-output problems.
  • Uses a white box model. If a given situation is observable in a model, the explanation for the

condition is easily explained by Boolean logic. By contrast, in a black box model (e.g., in an

artificial neural network), results may be more difficult to interpret.

  • Possible to validate a model using statistical tests. That makes it possible to account for the

reliability of the model.

  • Performs well even if its assumptions are somewhat violated by the true model from which the

data were generated.

CS7641 SL0 2 : Regression & Classification Mohamed Ameen Amer

SL02: Regression & Classification

Regression:

  • Regression maps continuous (not discrete) inputs to outputs. In regression, we

try to find a mathematical relationship based on measurement points. In some

sense, Regression means “falling back to the mean”.

  • Linear regression: Finding a linear relationship between some data points. The slope of that line will

always be less than 1, which makes it “regression to the mean”. The best linear function would be

the one that minimizes the squared error between the data points and their corresponding

mathematical measurements.

● How to find the best linear fit?

  • Using calculus, we can calculate the least squared error.

𝑖

2

𝑛

𝑖= 1

→ Sum of Squared Errors. It’s used because it’s easy to find it’s minimum using calculus

(Squaring the differences makes this error function differentiable).

  • 𝑛 → Number of data points.

𝑖

→ The correct value of data point 𝑖.

  • 𝑐 → The approximation function we’ll use to predict the best fit. In the simplest case it

would be just a constant 𝑐, which will result in a horizontal line across the data points.

  • So, this function will calculate the sum, over all data points, of squared difference between the

result of our prediction function, and the actual correct value.

  • To find the best 𝑐, we take the derivative of 𝐸(𝑐) (with respect to 𝑐) and set it equal to 0.

𝑖

𝑛

𝑖= 1

𝑖

𝑛

𝑖= 1

𝑖

𝑛

𝑖= 1

𝑛

𝑖= 1

𝑛

𝑖= 1

𝑖

𝑛

𝑖= 1

  • It turns out that the best approximation function will be to take the mean of all the data points.

CS7641 SL0 2 : Regression & Classification Mohamed Ameen Amer

● Polynomial regression:

  • For a set of values 𝑋 = (𝑥

1

2

𝑛

), the goal is to find a set of coefficients (weights) 𝐶 =

0

1

2

𝑛

) that will produce the best approximation function 𝑓(𝑥) where:

0

1

1

2

2

𝑛

𝑛

  • A higher order of polynomial will always model the data better (maximum order is the number

of data points), but this will result in overfitting (Training error will decrease, and testing error

will increase).

  • Arranging these sets in matrices will help us solve for the coefficients:

0

1

2

2

3

3

[

1

1

2

1

3

2

3

2

2

3

2

2

3

3

3

𝑛

𝑛

2

𝑛

3

]

[

1

2

3

𝑛

] ≈

[

1

2

3

𝑛

]

𝑇

𝑇

𝑇

− 1

𝑇

𝑇

− 1

𝑇

𝑇

− 1

𝑇

𝑇

− 1

𝑇

  • 𝑤 is the vector of weights, which tells us how important the corresponding 𝑥 is in predicting the

output.

● Regression errors:

All the datasets include error points that we don’t want to model in our algorithm. The error sources

include:

  • Sensor error: When a HW device misinterprets physical values.
  • Malicious data: Putting error points intentionally.
  • Transcription error: Human errors that can happen during the process of data

gathering/transformation.

  • Unmodeled influences: Missing other features that can affect our data.

We need to avoid fitting the error itself, we want to fit the underlying relationship.

● Cross Validation:

  • One way to have a better-fitting model is to use higher polynomial (Up until the number of

features), but this might lead to overfitting (Perfectly model the training data but fail to

generalize on real world data).

  • The goal here is to use a model that is complex enough to model the training set without

causing problems when generalizing to a test set.

  • Cross Validation is a process to quantify a model’s ability to generalize. We assume that the data

is Independent and Identically Distributed (IID), which means that there’s no inherent difference

between training, testing and real-world data.

CS7641 SL0 2 : Regression & Classification Mohamed Ameen Amer

● Other input spaces:

So far, we’ve seen only scalar/continuous input. Other scenarios could be:

  • Vector/continuous input: This will include more input features, and it will generalize by going

from a line on a dimensional plot, to a plan where the lower dimensions describe the vector of

inputs, and the highest describes the output.

  • Discrete/continuous attributes: Certain types of attributes are difficult to encode/quantify (e.g.

hair color). Here are some ways to encode these attributes:

  1. Enumerating, which means just giving a number to each possible value of this attribute.

This, however, can be misleading because a correlation between the ordering of the

enumeration and the its value can be interpolated.

  1. Represent each possible value of the attribute as a Boolean.

CS7641 SL0 3 : Neural Networks Mohamed Ameen Amer

SL0 3 : Neural Networks

Perceptron:

● A perceptron is a simple computational imitation to a brain cell. It allows

computations to fire under certain stimuli, allowing for learning.

● The Perceptron calculates the sum of products of the inputs 𝑋 and their corresponding weights 𝑊,

and then compare the result with an activation threshold. If the sum is greater than or equal the

threshold 𝜃, the Perceptron fires (outputs one).

𝑖

𝑖

𝑘

𝑖= 1

● The Perceptron can be used to model Boolean functions:

- AND: 𝑤

1

1

2

1

1

2

3

4

- OR: 𝑤

1

1

  • XOR: Requires 2 Perceptrons.

CS7641 SL0 3 : Neural Networks Mohamed Ameen Amer

→ Then we can update the weights using this rule:

𝑖

𝑖

𝑖

𝑖

𝑖

→ The Perceptron Learning Rule works by adjusting the weights based on the neuron’s

performance.

→ In the best case, the difference between the predicted value 𝑦̂ and the training example 𝑦 is

zero, which means that the perceptron was able to correctly predict the value of 𝑦, so the

weight will not be updated. It keeps repeating this process till the error saturates at 0.

→ Otherwise, the weights will be adjusted (positively or negatively) to correct the error in

prediction.

→ We’ll also update this formula by adding the learning rate 𝜂. Which controls the learning

process, so that the weights get adjusted by a small value each time to avoid overshooting.

𝑖

𝑖

𝑖

𝑖

𝑖

→ The Perceptron Learning Rule works only on linearly separable datasets, where we can

separate the positive and negative examples using a straight line (or a hyperplane).

→ If the data is linearly separable, the Perceptron will eventually find the suitable line to

separate the examples.

→ The problem is, there’s no obvious way to determine if the data is linearly separable or not,

especially with higher dimensions.

→ One workaround it to run the algorithm on the data and see if it ever stops.

  • Gradient Descent (Unthresholded):

→ Gradient Descent is an algorithm that can work with data that isn’t linearly separable.

→ In one variable, derivative gives us the slope of the tangent line. In several variables,

Gradient (the generalization of derivatives in several variables) points towards direction of

the fastest increase of the function.

→ Let 𝐷 be the training set with ordered pairs (𝑥, 𝑦), where 𝑦 is the target value. Gradient

Descent works by taking the dot product 𝑎 of 𝑤 and 𝑥 (which is essentially a simple linear

regression), and then minimize the error of this activation using calculus.

𝑖

𝑖

𝑖

2

(𝑥,𝑦)∈𝐷

CS7641 SL0 3 : Neural Networks Mohamed Ameen Amer

→ To minimize error 𝐸(𝑤), we calculate the partial derivative of 𝐸(𝑤) with respect to each of

the individual weights (The

1

2

in the equation was added to simplify the derivative):

𝑖

𝑖

2

(𝑥,𝑦)∈𝐷

𝑖

𝑖

𝑖

𝑖

2

(𝑥,𝑦)∈𝐷

× 2 ∑ (𝑦 − 𝑎)

( 𝑥,𝑦

) ∈𝐷

𝑖

𝑖

𝑖

𝑖

(𝑥,𝑦)∈𝐷

𝑖

→ The error decreases fastest in the direction opposite to the gradient.

→ Robust to data that is not linearly separable.

→ Might converge to a local optima though.

  • Perceptron Rule vs Gradient Descent:

→ Perceptron Learning ∆𝑤

𝑖

𝑖

→ Gradient Descent ∆𝑤

𝑖

𝑖

→ The reason we can’t use 𝑦̂ in Gradient Descent is that it will make the formula non-

differentiable.

  • Sigmoid Function:
    • Motivation: To use the gradient descent above, we had to use a linear unit, which is just a linear

function. So, we end up with a problem, we want the nonlinearity of the thresholded

perceptron, with the robustness of the training rule given by gradient descent.

  • The solution would be to use the Sigmoid Function, which is a smoothed function that facilitates

a differentiable threshold.

−𝑎

  • We can use Perceptrons that operate according to

the sigmoid rule:

  • And the training rule would be:

𝑖

𝑖

(𝑥,𝑦)∈𝐷

CS7641 SL0 3 : Neural Networks Mohamed Ameen Amer

  1. Then we adjust the weights using the calculated errors.

CS7641 SL0 3 : Neural Networks Mohamed Ameen Amer

Optimizing Weights:

  • There’re many optimization methods to find a global minima:
    • Momentum: An extra factor to push past the local minima.
    • Higher order derivatives: Use a combination of weights.
    • Randomized optimization.
    • Penalizing complexity: Penalizing more nodes, more layers or larger weights.

Restriction Bias:

  • The Restriction Bias determines what is the set of hypotheses we will consider, which in turn

highlights the representation power of the learning algorithm.

  • As we discussed, Perceptions can only work with linear data (half spaces). While sigmoid allow for

representing more complex data, and using hidden layers increase the representation power even

more.

  • All these components of Neural Networks allow for modeling many types of functions:
    • Boolean: Network of threshold-like units.
    • Continuous: A single hidden layer with enough hidden nodes.
    • Arbitrary: Multiple hidden layers.
  • We can conclude that Neural Networks have a low Restriction Bias because they have the ability to

model a wide variety of functions. This, however, increase the possibility of overfitting.

  • To avoid overfitting, when designing neural networks, we restrict ourselves to a bounded

number of hidden layers with a bounded number of units. We can use cross validation to decide

these numbers.

  • A Neural Network will not only overfit because of excessive complexity but can also overfit because

of excessive training (higher magnitude of the weights). We can use cross validation to decide when

to stop training.

CS7641 SL04. Instance Based Learning Mohamed Ameen Amer

SL04. Instance Based Learning

Introduction:

  • Normal ML algorithms uses input data (𝑥, 𝑦) and searches the hypotheses space

for the best generalized function 𝑓(𝑥) that can be used to predict new values.

  • In Instance Based Learning, we create a database of all 𝑥/𝑦 relationships, and

once we receive a new value 𝑥 we lookup this database to find its corresponding 𝑦.

  • Advantages:
    • The database saves the exact values of 𝑦 not an approximation of it.
    • This way is fast. No need for learning (query time is long though).
    • It’s simple.
  • Disadvantages:
    • No generalization.
    • Prone to overfitting:
      1. Models noise.
      2. Lookup can return multiple values for the same input point.

𝑘-Nearest Neighbors:

  • This algorithm simply looks at a selection 𝑘 (free parameter) of the nearest neighbors of the query

point and selects the best majority label.

  • This is how it works:
    • Given:
      1. Training data (𝐷).
      2. Distance (similarity) metric 𝑑(𝑞, 𝑥) → domain knowledge.
      3. Number of neighbors (𝑘) → domain knowledge.
      4. Query point (𝑞).
    • Find:
      1. A set of nearest neighbors (𝑁𝑁) such that 𝑑(𝑞, 𝑥) is smallest.
    • Return:
      1. Classification: We perform a vote to select 𝑦

𝑖

of 𝑁𝑁, where 𝑦

𝑖

is the most frequent

(Highest plurality - Mode).

  1. Regression: Compute the mean of all 𝑦

𝑖

  1. We can also use a weighted vote of weighted average, which means that the closer the 𝑖

point is to the query point 𝑞, the more influence its 𝑦

𝑖

has on the vote/mean.

CS7641 SL04. Instance Based Learning Mohamed Ameen Amer

  • Comparison:

Algorithm Running Time Space

Learning 1 𝑛

Query log 𝑛 1

Learning 1 𝑛

Query log 𝑛 + 𝑘 1

Linear Regression

Learning n 1

Query 1 1

Eager vs Lazy Learners:

  • Eager learners:
    • We infer a function that best fits our training data.
    • When we have new inputs, the input’s features are fed into the function, which produces an

output.

  • Once a function has been computed, the data could be lost without affecting the model’s

performance (until we obtain more data and want to re-compute our function).

  • We take the time to learn from the data first and sacrifice local sensitivity to obtain quick, global

scale estimates on new data points.

  • Lazy learners:
    • We don’t compute a function to fit the training data before new data is received.
    • New instances are compared to the training data itself to make a classification or regression

decision.

  • Memory requirements are much larger than eager learners (storing all training data versus just a

function).

  • Taking decisions on new examples takes longer to compute than eager learners.
  • Lazy learners have advantages in local scale estimation and easy integration of additional

training data.

  • In summary, lazy learners have the option to select a different hypothesis or local approximation of

the target function for each query instance. Eager learners using the same hypothesis space are

more restricted because they must commit to a single hypothesis that covers the entire instance

space (depending on the training data). Eager learners can employ hypothesis spaces that combine

multiple local approximations. However, because these combined local approximations are centered

around clusters of the training instances (instead of query instances as the case with lazy learners),

they don’t give eager learners the full ability of lazy learners to generalize to unknown instances.

  • The 𝑁𝑁 learning algorithm is a lazy learner. It procrastinates learning till it starts querying. On the

other hand, Linear Regression is an Eager learning algorithm, performing all learning initially to come

up the best hypothesis.