Module 2 ML
Module 2 ML
1)Apply PCA and find the transformed data for the data points (\begin{pmatrix}2\6\end{pmatrix})
and (\begin{pmatrix}1\7\end{pmatrix}). Again, apply the inverse and prove that PCA works.
Solution
One can combine two vectors into a matrix as follows:
Mean vector
The mean vector can be computed as Eq. (2.53) as follows:
[
\mu=
\begin{pmatrix}
\dfrac{2+1}{2}\
\dfrac{6+7}{2}
\end{pmatrix}
\begin{pmatrix}
1.5\
6.5
\end{pmatrix}
]
As part of PCA, the mean must be subtracted from the data to get the adjusted data:
[
x_1=
\begin{pmatrix}
2-1.5\
6-6.5
\end{pmatrix}
\begin{pmatrix}
0.5\
-0.5
\end{pmatrix}
]
[
x_2=
\begin{pmatrix}
1-1.5\
7-6.5
\end{pmatrix}
\begin{pmatrix}
-0.5\
0.5
\end{pmatrix}
]
One can find the covariance for these data vectors. The covariance can be obtained using Eq. (2.54):
[
m_1=
\begin{pmatrix}
0.5\
-0.5
\end{pmatrix}
(0.5;;-0.5)
\begin{pmatrix}
0.25 & -0.25\
-0.25 & 0.25
\end{pmatrix}
]
[
m_2=
\begin{pmatrix}
-0.5\
0.5
\end{pmatrix}
(-0.5;;0.5)
\begin{pmatrix}
0.25 & -0.25\
-0.25 & 0.25
\end{pmatrix}
]
The final covariance matrix is obtained by adding these two matrices as:
[
C=
\begin{pmatrix}
0.5 & -0.5\
-0.5 & 0.5
\end{pmatrix}
]
The eigen values and eigen vectors of matrix (C) can be obtained as:
[
\lambda_1=1,\qquad \lambda_2=0
]
The eigen vectors are:
[
\begin{pmatrix}
-1\
1
\end{pmatrix}
\quad
\begin{pmatrix}
1\
1
\end{pmatrix}
]
The matrix (A) can be obtained by packing the eigen vector of these eigen values (after sorting it) of
matrix (C).
For this problem,
[
A=
\begin{pmatrix}
-1 & 1\
1&1
\end{pmatrix}
]
The transpose of (A),
[
A^T=
\begin{pmatrix}
-1 & 1\
1&1
\end{pmatrix}
]
is also the same matrix as it is an orthogonal matrix.
The matrix can be normalized by dividing each element of the vector, by the norm of the vector to
get:
[
A=
\begin{pmatrix}
-\frac{1}{\sqrt2} & \frac{1}{\sqrt2}\
\frac{1}{\sqrt2} & \frac{1}{\sqrt2}
\end{pmatrix}
]
One can check the original matrix can be retrieved from this matrix as:
[
x=(A)^T\times y+m
]
[
x=
\begin{pmatrix}
-\frac1{\sqrt2} & \frac1{\sqrt2}\
\frac1{\sqrt2} & \frac1{\sqrt2}
\end{pmatrix}
\begin{pmatrix}
-\frac1{\sqrt2} & \frac1{\sqrt2}\
0&0
\end{pmatrix}
+
\begin{pmatrix}
1.5\
6.5
\end{pmatrix}
]
[
\begin{pmatrix}
\frac12 & -\frac12\
-\frac12 & \frac12
\end{pmatrix}
+
\begin{pmatrix}
1.5\
6.5
\end{pmatrix}
]
[
\begin{pmatrix}
2 & 1\
6&7
\end{pmatrix}
]
Therefore,
One can infer the original is obtained without any loss of information.
1. Normal Distribution
• Normal distribution is a continuous probability distribution.
• This is also known as gaussian distribution or bell-shaped curve distribution.
• It is the most common distribution function.
• The shape of this distribution is a typical bell-shaped curve.
• In normal distribution, data tends to be around a central value with no bias on left or right.
• The heights of the students, blood pressure of a population, and marks scored in a class can
be approximated using normal distribution.
2. Rectangular Distribution
• This is also known as uniform distribution.
• It has equal probabilities for all values in the range a, b.
3. Exponential Distribution
• This is a continuous uniform distribution.
• This probability distribution is used to describe the time between events in a Poisson
process.
• Exponential distribution is another special case of Gamma distribution with a fixed
parameter of 1.
• This distribution is helpful in modelling of time until an event occurs.
1. Binomial Distribution
• Binomial distribution is another distribution that is often encountered in machine learning.
• It has only two outcomes:
o Success
o Failure
• This is also called Bernoulli trial.
• The objective of this distribution is to find probability of getting success k out of n trials.
• Here,
o p is the probability of each choice
o k is the number of choices
o n is the total number of choices
• The mean of binomial distribution is:
[
\mu=n\times p
]
• The variance is:
[
\sigma^2=np(1-p)
]
• Hence, the standard deviation is:
[
\sigma=\sqrt{np(1-p)}
]
2. Poisson Distribution
• It is another important distribution that is quite useful.
• Given an interval of time, this distribution is used to model the probability of a given
number of events k.
3. Bernoulli Distribution
• This distribution models an experiment whose outcome is binary.
• The outcome is positive with p and negative with 1 − p.
• The mean is p and variance is:
[
p(1-p)=q
]
4. Function Approximation
Choosing an Approximation Algorithm for the Target Function
• The focus is to choose weights and fit the given training samples effectively.
• The aim is to reduce the error given as:
[
E=\sum_{Training\ Samples}[V_{train}(b)-\hat{V}(b)]^2
]
• Here, b is the sample and (\hat{V}(b)) is the predicted hypothesis.
The approximation is carried out as:
• Computing the error as the difference between trained and expected hypothesis.
• Let error be error(b).
• Then, for every board feature (x_i), the weights are updated as:
[
w_i = w_i + \mu \times error(b) \times x_i
]
• Here, μ is the constant that moderates the size of the weight update.
G₀ =
S₀ = <ϕ, ϕ, ϕ, ϕ, ϕ, ϕ>
Iteration 1
Example 1 (Positive)
Generalize S to include I₁
G₁ =
Example 2 (Positive)
Compare I₂ with S₁
Only Humidity differs
Replace mismatching attribute by '?'
Prune G₁
No inconsistency
G₂ =
Iteration 2
Example 3 (Negative)
G₃ = {
• <Sunny, ?, ?, ?, ?, ?>
•
• <?, ?, ?, ?, ?, Same>
}
S remains unchanged
Iteration 3
Example 4 (Positive)
Compare I₄ with S₃
Water differs → ?
Forecast differs → ?
Prune G₃
Check consistency:
→ Consistent
G₄ = {
• <Sunny, ?, ?, ?, ?, ?>
•
Regular Paper
For a 10-mark VTU answer, writing the iterations + final version space + diagram is sufficient and
complete. The screenshots you uploaded essentially contain the full answer expected by the
examiner.
3) For
[
A=\begin{bmatrix}
1 & 2\
4&9
\end{bmatrix}
]
The Singular Value Decomposition is
[
A=U\Sigma V^T
]
Final SVD
[
A=
\begin{bmatrix}
0.221 & 0.975\
0.975 & -0.221
\end{bmatrix}
\begin{bmatrix}
10.099 & 0\
0 & 0.099
\end{bmatrix}
\begin{bmatrix}
0.407 & 0.913\
-0.913 & 0.407
\end{bmatrix}
]
Therefore,
[
\boxed{A=U\Sigma V^T}
]
with
[
U=
\begin{bmatrix}
0.221 & 0.975\
0.975 & -0.221
\end{bmatrix},
\quad
\Sigma=
\begin{bmatrix}
10.099 & 0\
0 & 0.099
\end{bmatrix},
\quad
V^T=
\begin{bmatrix}
0.407 & 0.913\
-0.913 & 0.407
\end{bmatrix}
]
4) Find-S Algorithm
Input:
Positive instances in the Training dataset
Output:
Hypothesis ‘h’
Step 1:
Initialize ‘h’ to the most specific hypothesis.
[
h=<\phi,\phi,\phi,\phi,\phi,\ldots>
]
Step 2:
Generalize the initial hypothesis for the first positive instance [Since ‘h’ is more specific].
Step 3:
For each subsequent instances:
If it is a positive instance,
• Check for each attribute value in the instance with the hypothesis ‘h’.
o If the attribute value is the same as the hypothesis value, then do nothing.
o Else if the attribute value is different than the hypothesis value, change it to ‘?’ in ‘h’.
Else if it is a negative instance,
• Ignore it.
Step 1: Initialize h
[
h_0=<\phi,\phi,\phi,\phi,\phi,\phi>
]
I₁
[
<Sunny,\ Warm,\ Normal,\ Strong,\ Warm,\ Same>
]
Generalize h to include the first positive instance.
[
h_1=<Sunny,\ Warm,\ Normal,\ Strong,\ Warm,\ Same>
]
Step 3: Scan I₂
I₂ (Positive)
[
<Sunny,\ Warm,\ High,\ Strong,\ Warm,\ Same>
]
Compare with (h_1)
• Sky → Same
• Temp → Same
• Wind → Same
• Water → Same
• Forecast → Same
[
h_2=<Sunny,\ Warm,\ ?,\ Strong,\ Warm,\ Same>
]
Step 4: Scan I₃
I₃ (Negative)
[
<Rainy,\ Cold,\ High,\ Strong,\ Warm,\ Change>
]
Negative instance.
Ignore it.
[
h_3=<Sunny,\ Warm,\ ?,\ Strong,\ Warm,\ Same>
]
Step 5: Scan I₄
I₄ (Positive)
[
<Sunny,\ Warm,\ High,\ Strong,\ Cool,\ Change>
]
Compare with (h_3)
• Sky → Same
• Temp → Same
• Humidity → '?'
• Wind → Same
Final Hypothesis
[
\boxed{
h=<Sunny,\ Warm,\ ?,\ Strong,\ ?,\ ?>
}
]
This is the hypothesis obtained using the Find-S Algorithm for the given dataset.
Yes. For a question like:
"Write Find-S algorithm. Apply the algorithm to obtain the hypothesis for the dataset given in the
table."
You should write both the theory (Find-S Algorithm) and the application on the dataset.
Find-S Algorithm
• Find-S algorithm is guaranteed to converge to the most specific hypothesis in (H) that is
consistent with the positive instances in the training dataset.
• Obviously, it will also be consistent with the negative instances.
• Thus, this algorithm considers only the positive instances and eliminates negative instances
while generating the hypothesis.
• It initially starts with the most specific hypothesis.
Algorithm 3.1: Find-S
Input:
Positive instances in the Training dataset
Output:
Hypothesis ‘h’
1. Initialize ‘h’ to the most specific hypothesis.
[
h=<\phi,\phi,\phi,\phi,\phi,\ldots>
]
2. Generalize the initial hypothesis for the first positive instance [Since ‘h’ is more specific].
3. For each subsequent instances:
If it is a positive instance,
• Check for each attribute value in the instance with the hypothesis ‘h’.
o If the attribute value is the same as the hypothesis value, then do nothing.
o Else if the attribute value is different than the hypothesis value, change it to ‘?’ in ‘h’.
Else if it is a negative instance,
• Ignore it.
Final Hypothesis
[
\boxed{
h=<Sunny,\ Warm,\ ?,\ Strong,\ ?,\ ?>
}
]
Important Questions
1) Source: Machine Learning – S. Sridhar, M. Vijayalakshmi
Bivariate Data and Multivariate Data
2.6 BIVARIATE DATA AND MULTIVARIATE DATA
• Bivariate Data involves two variables.
• Bivariate data deals with causes of relationships.
• The aim is to find relationships among data.
• Consider the following Table 2.3, which has data of the temperature
in a shop and sales sweets.
Table 2.3: Temperature in a Shop and Sales Data
Temperature (°C) Sales of Sweets (in thousands)
5 200
10 150
15 140
20 75
22 60
23 55
25 20
• Here, the aim of bivariate analysis is to find relationships among
variables.
• The relationships can then be used to comparisons, finding causes,
and in further explorations.
• To do that, graphical display of the data is necessary.
• One such graph method is called scatter plot.
Scatter Plot
• Scatter plot is used to visualize bivariate data.
• It is useful to plot two variables with or without nominal variables, to
illustrate trends, and show differences.
• It is a plot between labels, category and response variables.
• It is a 2D graph showing the relationship between variables.
• The scatter plot indicates:
o strength,
o shape,
o direction,
o and the presence of Outliers.
• It is useful in exploratory data before calculating correlation
coefficient or fitting regression curve.
Multivariate Statistics
• In machine learning, almost all datasets are multivariable.
• Multivariate data is the analysis of more than two observable
variables and, often, thousands of multiple measurements need to be
conducted for one or more subjects.
• The multivariate data is like bivariate data but may have more than
two dependent variables.
• Some of the multivariate analysis are:
o regression analysis,
o principal component analysis,
o and path analysis.
• Multivariate data has three or more variables.
• The aim of the multivariate analysis is much more.
• They are regression analysis, factor analysis, and multivariate analysis
of variance that are explained in the subsequent chapters of this
book.
Covariance and Correlation with Suitable Examples
2.6.1 Bivariate Statistics
• Covariance and Correlation are examples of bivariate statistics.
• Covariance is a measure of joint probability of random variables, say
X and Y.
• Generally, random variables are represented in capital letters.
• It is defined as covariance for continuous random variables and is
used to measure variance between two dimensions.
Formula for Covariance
cov(X,Y)=\frac{1}{N}\sum (X-E(X))(Y-E(Y))
Where:
• X and Y are data values from X and Y.
• E(X) and E(Y) are the mean values of x and y.
• N is the number of given data.
Example 2.6
Find the covariance of data
• X = (1, 2, 3, 4, 5)
• Y = (1, 4, 9, 16, 25)
Solution
• Mean(X) = E(X) = 15/5 = 3
• Mean(Y) = E(Y) = 55/5 = 11
The covariance is computed using Eq. (2.17) as:
= ((1 − 3)(1 − 11) + (2 − 3)(4 − 11) + (3 − 3)(9 − 11) + (4 − 3)(16 − 11) + (5 −
3)(25 − 11)) / 5
= 12
• The covariance between X and Y is 12.
• It can be normalized to a value between −1 and +1.
• This is done by dividing it by the correlation of variables.
• This is called Pearson correlation coefficient.
• Sometimes N−1 can be used instead of N.
• In that case, the covariance is 60/4 = 15.
Correlation
• The Pearson correlation coefficient is the most common test for
determining any association between two phenomena.
• It measures the strength and direction of a linear relationship
between the x and y variables.
Important Information
• The correlation indicates the relationship between dimensions using
its sign.
• The sign is more important than the actual value.
Interpretation
1. If the value is positive, it indicates that the dimensions increase
together.
2. If the value is negative, it indicates that while one-dimension
increases, the other dimension decreases.
3. If the value is zero, it indicates that both the dimensions are
independent of each other.
• If the dimensions are correlated, then it is better to remove one
dimension as it is a redundant dimension.
Formula for Pearson Correlation Coefficient
r=\frac{cov(X,Y)}{\sigma_x\sigma_y}
where,
• σx, σy are the standard deviations of X and Y.
Example 2.7
Find the correlation coefficient of data
• X = (1, 2, 3, 4, 5)
• Y = (1, 4, 9, 16, 25)
Solution
• The mean values of X and Y are 15/5 = 3 and 55/5 = 11.
• The standard deviations of X and Y are 1.41 and 8.6486, respectively.
• Therefore, the correlation coefficient is given as ratio of covariance
(12 from the previous problem 2.5) and standard deviation of x and y
as per Eq. (2.18) as:
r = 12 / (1.41 × 8.6486)
= 0.984
Heatmap
• Heatmap is a graphical representation of 2D matrix.
• It takes a matrix as input and colours it.
• The darker colours indicate very large values and lighter colours
indicate smaller values.
• The advantage of this method is that humans perceive colour well.
• So, by colour change, larger values can be perceived well.
• For example, in vehicle traffic data, heavy traffic regions can be
differentiated from low traffic regions through heatmap.
• In Figure 2.13, patient data highlighting weight and health status is
plotted.
• Here, X-axis is weights and Y-axis is patient counts.
• The dark colour regions highlight patients' weights vs patient counts
in health status.
Pairplot
• Pairplot or scatter matrix is a data visualization technique for
multivariate data.
• A scatter matrix consists of several pair-wise scatter plots of variables
of the multivariate data.
• All the results are presented in a matrix format.
• By visual examination of the chart, one can easily find relationships
among the variables such as correlation between the variables.
• A random matrix of three columns is shown and the relationships
among the columns are plotted as a pairplot (or scattermatrix) as
shown below in Figure 2.14.
•
Feature Transformation
• Feature transformation is extraction of features and creating new
features that may be helpful in increasing performance.
• For example, the height and weight may give a new attribute called
Body Mass Index (BMI).
Feature Selection
• Feature subset selection is another important aspect of feature
engineering that focuses on selection of features to reduce the time
but not at the cost of reliability.
• The subset selection reduces the dataset size by removing irrelevant
features and constructs a minimum set of attributes for machine
learning.
The features can be removed based on two aspects:
1. Feature relevancy
• Some features contribute more for classification than other features.
• For example, a mole on the face can help in face detection than
common features like nose.
• In simple words, the features should be relevant.
• The relevancy of the features can be determined based on
information measures such as:
o mutual information,
o correlation-based features like correlation coefficient,
o and distance measures.
• Distance measures are discussed in Chapter 13 of this book.
2. Feature redundancy
• Some features are redundant.
• For example, when a database table has a field called Date of birth,
then age field is not relevant as age can be computed easily from
date of birth.
• This helps in removing the column age that leads to reduction of
dimension one.
Wrapper-based Methods
• Wrapper-based methods use classifiers to identify the best features.
• These are selected and evaluated by the learning algorithms.
• This procedure is computationally intensive but has superior
performance.
8) Concept Learning
• Concept learning is a learning strategy of acquiring abstract
knowledge or inferring a general concept or deriving a category from
the given training samples.
• It is a process of abstraction and generalization from the data.
• Concept learning helps to classify an object that has a set of
common, relevant features.
• Thus, it helps a learner compare and contrast categories based on the
similarity and association of positive and negative instances in the
training data to classify an object.
• The learner tries to simplify by observing the common features from
the training samples and then apply this simplified model to the
future samples.
• This task is also known as learning from experience.
• Each concept or category obtained by learning is a Boolean valued
function which takes a true or false value.
• For example, humans can identify different kinds of animals based on
common relevant features and categorize all animals based on
specific sets of features.
• The special features that distinguish one animal from another can be
called as a concept.
• This way of learning categories for object and to recognize new
instances of those categories is called as concept learning.
• It is formally defined as inferring a Boolean valued function by
processing training instances.
Concept learning requires three things:
1. Input
• Training dataset which is a set of training instances, each labeled
with the name of a concept or category to which it belongs.
• Use this past experience to train and build the model.
2. Output
• Target concept or Target function f.
• It is a mapping function f(x) from input x to output y.
• It is to determine the specific features or common features to
identify an object.
• In other words, it is to find the hypothesis to determine the target
concept.
• For e.g., the specific set of features to identify an elephant from all
animals.
3. Test
• New instances to test the learned model.
Hypothesis
• A hypothesis ‘h’ approximates a target function ‘f’ to represent the
relationship between the independent attributes and the dependent
attribute of the training instances.
• The hypothesis is the predicted approximate model that best maps
the inputs to outputs.
• Each hypothesis is represented as a conjunction of attribute
conditions in the antecedent part.
Example
• (Tail = Short) ∧ (Color = Black)
• The set of hypothesis in the search space is called as hypotheses.
• Hypotheses are the plural form of hypothesis.
• Generally ‘H’ is used to represent the hypotheses and ‘h’ is used to
represent a candidate hypothesis.
Attribute conditions
• Each attribute condition is the constraint on the attribute which is
represented as attribute-value pair.
• In the antecedent of an attribute condition of a hypothesis, each
attribute can take value as either ‘?’ or ‘φ’ or can hold a single value.
?
• “?” denotes that the attribute can take any value.
• Example: Color = ?
φ
• “φ” denotes that the attribute cannot take any value, i.e., it
represents a null value.
• Example: Horns = φ
Single value
• Single value denotes a specific single value from acceptable values of
the attribute.
• Example: Tail = Short
Hypothesis Space
• Hypothesis space is the set of all possible hypotheses that
approximates the target function f.
• In other words, the set of all possible approximations of the target
function can be defined as hypothesis space.
• From this set of hypotheses in the hypothesis space, a machine
learning algorithm would determine the best possible hypothesis that
would best describe the target function or best fit the outputs.
• Generally, a hypothesis representation language represents a larger
hypothesis space.
• Every machine learning algorithm would represent the hypothesis
space in a different manner about the function that maps the input
variables to output variables.
• For example, a regression algorithm represents the hypothesis space
as a linear function whereas a decision tree algorithm represents the
hypothesis space as a tree.
• The set of hypotheses that can be generated by a learning algorithm
can be further reduced by specifying a language bias.
Version Space
• The subset of hypothesis space that is consistent with all-observed
training instances is called as Version Space.
• Version space represents the only hypotheses that are used for the
classification.
• The version space contains the subset of hypotheses from the
hypothesis space that is consistent with all training instances in the
training dataset.
Generalization and Specialization
• In order to understand about how we construct this concept
hierarchy, let us apply this general principle of
generalization/specialization relation.
• By generalization of the most specific hypothesis and by
specialization of the most general hypothesis, the hypothesis space
can be searched for an approximate hypothesis that matches all
positive instances but does not match any negative instance.
Searching the Hypothesis Space
There are two ways of learning the hypothesis, consistent with all training
instances from the large hypothesis space.
1. Specialization
• General to Specific learning.
2. Generalization
• Specific to General learning.
Apply
[
R_2 = R_2 - 3R_1
]
[
\left[
\begin{array}{ccc|ccc}
1&0&0&1&2&4\
3&1&0&0&-3&-10\
0&0&1&3&4&2
\end{array}
\right]
]
Apply
[
R_3 = R_3 - 3R_1
]
[
\left[
\begin{array}{ccc|ccc}
1&0&0&1&2&4\
3&1&0&0&-3&-10\
3&0&1&0&-2&-10
\end{array}
\right]
]
Apply
[
R_3 = R_3 - \frac{2}{3}R_2
]
[
\left[
\begin{array}{ccc|ccc}
1&0&0&1&2&4\
3&1&0&0&-3&-10\
3&\frac{2}{3}&1&0&0&-\frac{10}{3}
\end{array}
\right]
]
Observation
• Now, it can be observed that the first matrix is L as it is the lower
triangular matrix whose values are the determiners used in the
reduction of equations above such as 3, 3 and 2/3.
• The second matrix is U, the upper triangular matrix whose values are
the values of the reduced matrix because of Gaussian elimination.
L Matrix
[
L=
\begin{pmatrix}
1&0&0\
3&1&0\
3&\frac{2}{3}&1
\end{pmatrix}
]
U Matrix
[
U=
\begin{pmatrix}
1&2&4\
0&-3&-10\
0&0&-\frac{10}{3}
\end{pmatrix}
]
Applications of LU Decomposition
• It can be cross verified that the multiplication of LU yields the
original matrix A.
• Some of the applications are:
o finding matrix inverses
o determinant
• If the order of the matrix is large, then this method can be used.
11) Example 2.8: Solve the following set of equations using Gaussian
Elimination method
Given:
[
2x_1 + 4x_2 = 6
]
[
4x_1 + 3x_2 = 7
]
Solution
Rewrite this in matrix form as follows:
[
\begin{pmatrix}
2 & 4 \mid 6 \
4 & 3 \mid 7
\end{pmatrix}
]
[
\sim
\begin{pmatrix}
2 & 4 \mid 6 \
4 & 3 \mid 7
\end{pmatrix}
\qquad
R_1=\frac{R_1}{2}
]
Apply the transformation by dividing the row 1 by 2.
[
\sim
\begin{pmatrix}
1 & 2 \mid 3 \
4 & 3 \mid 7
\end{pmatrix}
\qquad
R_2=R_2-4R_1
]
[
\sim
\begin{pmatrix}
1 & 2 \mid 3 \
0 & -5 \mid -5
\end{pmatrix}
\qquad
R_2=\frac{R_2}{-5}
]
[
\sim
\begin{pmatrix}
1 & 2 \mid 3 \
0 & 1 \mid 1
\end{pmatrix}
\qquad
R_1=R_1-2R_2
]
[
\sim
\begin{pmatrix}
1 & 0 \mid 1 \
0 & 1 \mid 1
\end{pmatrix}
]
Therefore, in the reduced echelon form, it can be observed that:
[
x_2 = 1
]
[
x_1 = 1
]
Answer
• (x_1 = 1)
• (x_2 = 1)