Module 2
Module 2
Understanding Data – 2:
ii. Correlation -
Correlation measures the strength and direction of a linear relationship between x and
y
The correlation indicates the relationship between dimensions using its sign. Sign is
important than value
If value is positive – dimensions increase together
If value is negative – one dimension increases and other dimension decreases
If Zero – Both dimensions are independent of each other
Person correlation coefficient is most commonly used to determine the association between
two phonomena.
PairPlot
Pairplot or scatter matrix is data visual technique for multivariate data.
2.8 Essential Mathematics for Multivariate Data
Computed as
If there is a unique solution, then system is called consistent independent.
If there are various solutions, then the system is called consistent dependent.
If there is no solutions and if equations are contradictory, then system is called
inconsistent
For solving large number of system equations, Gaussian elimination can be used.
Procedure is as follows
It is necessary to reduce a matrix to its constituent parts so that complex matrix operations
can be performed. These methods are also known as matrix factorization methods.
The most popular matrix decomposition is called eigen decomposition.
This is the way to reduce matrix with eigen values and eigen vectors.
LU Decomposition
One of the simplest matrix decomposition is LU decomposition where the matrix A can be
decomposed matrices
Here,
Now follow the steps given below to solve the above system of linear equations by LU
Decomposition method
Step 1: Generate a matrix A = LU such that L is the lower triangular matrix with principal
diagonal elements being equal to 1 and U is the upper triangular matrix.
That means,
and
Example:
Step 2: LUX = B
Step 3: Let UX = Y
Step 4: From the previous two steps, we have LY = B
Thus,
So,
y1 = 1
3y1 + y2 = 5
y1 + (3/2) y2 + y3 = 10
Solving these equations, we get;
y1 = 1, y2 = 2, y3 = 6
Step 5: Now, consider UX = Y. So,
By expanding this equation, we get;
x1 + x2 + x3 = 1
-2x2 – 6x3 = 2
3x3 = 6
Solving these equations, we can get;
x3 = 2, x2 = -7 and x1 = 6
ML is linked with probability and statistics similar to linear algebra, statistics and this is
treated as heart of ML.
The probability play in ML as it is assumed that data probability distribution.
The datasets consists of multiple data generated by many distribution therefore knowledge
of probability distribution and random variables is must for better understanding of
machine learning concepts
Probability Distribution
Probability distribution of variable X is summarized as probability associated with X’s
event. Distribution is parameterized mathematical function that describes relationship
between the observed sample space.
Consider a set of data and it is said follow a distribution if it obeys a mathematical function
that characterizes that distribution The function can be used to calculate the of individual
observation
Probability distributions are of two types:
1. Discrete probability distribution
2. Continuous probability distribution
Mostly, one uses the normal distribution curve of mean 0 and SD of 1. In normal
distribution, mean, median and mode are same. The distribution extends from to
2. Rectangular Distribution
This is also know as uniform distribution. It has equal probabilities for all the values in
range a,b
3. Exponential Distribution
This is continious uniform distribution. This probability distribution is used to describe the
time between the events in Poisson process. Exponential distribution is another special case
of Gamma distribution with fixed parameter of 1.
X is random variable
Lamda is called rate parameter
Discrete Distribution
Binomail, Poisson and Bernolli distribution fall under this category
1. Binomial Distribution – This distribution is most encourged in machine learning. It
has two outcomes either success or failure. This is called Bernoulli trial
The objective of this distribution is to find probability of getting success k out of n
trials.
2. Poisson Distribution Given an interval of time, this distribution is used to model the
probability of a given number of events k. The mean rule lamda is inclusive of
previous events.
Example: number of emails received, number of customers visiting the shop, The
number of phone calls received by office.
3. Bernoulli Distribution
Feature Engineering is about determining the subset of features that from an important part of
input that improves the performance of model. The model may classify or any other model in
Machine Learning.
ii. Feature transformation – Focuses on selection 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
minimum set of attributes for machine learning.
For ‘n’ attributes there can be 2n possible subsets. If the value of ‘n’ is high, the problem
becomes intractable. This is called ‘curse of dimensionality’ hence need for the need of
reduction of attributes.
The feature subset selection is typically a graph search problem. These algorithms are based
on greedy strategy. In such algorithms the assumption is made that decision is made initially
and it is assumed that it leads to optimal solution.
Features can be removed based on two aspects
i. Feature Relevancy – Some features contribute more for classification that others
in building model. Example : mole on face is than nose for detection of face. The
feature should be relevant. The relevant features can be determined based on the
information such as mutual information, correlation based features like correlation
coefficient and distance measures
ii. Feature redundancy – There are some features present that are redundant. The
redundant feature should be taken off. Ex. In the database if there is date of birth
and age attributes, the age is redundant.
Approaches:
i. Filter based selection uses statistical measures for assessing features. This
approach doesn’t use any algorithms. Correlation and information gain
measures like mutual information and entropy are examples of this app
ii. Wrapper-based method use classifiers to identify the best features. These
identified features are selected and evaluated by learning algorithms. These are
computationally intensive but superior in performance.
Algorithms
Procedure starts with an empty set of attributes, Till a good reduced set of attributes are
obtained, every time an attribute is tested for statistical significance for best quality and is
added to reduced set.
The Combined Approach both forward and back selection methods so that the reduced set
has best attributes.
The idea of principal component analysis (PCA) or KL transform is to transform a given set
of measurements to new set of features so that the features exhibit high information packing
properties. This leads to a reduced and compact set of features. This uses elimination of
redundant information
/* Step by Step PCA (Example)
There are several steps involved while conducting a PCA. Let’s dive into it.
Step 1: Standardize the Data Set
Let’s say we have a data set with 4 variables and 4 observations, as shown below:
The first step is to standardize, which means transforming all variables as they will have
means of zeros and standard deviations of one, hence variances of one.
This is done to ensure that there is no imbalance in the contribution of the variables due to
unit differences. Otherwise, the variables that have higher variances would contribute more
than the ones with lower variances in identifying the principal components, although it does
not reflect reality. For further explanation, see the PCA Using Correlation & Covariance
Matrix tutorial.
Regarding standardization formula is given below:
z=Value−Mean(μ)StandardDeviation(σ)
For each feature, the mean and the standard deviation were as follows before the
standardization.
As you see, the variability does not vary much for this sample; hence standardization is not
a must in this case. However, for the sake of illustration, the variables were standardized,
and the following values they have taken.
Z=(x-mean)
Cov(x,y)=∑(xi–x¯¯¯)∗(yi–y¯¯¯)/N
The resulting covariance matrix is given below.
Positive covariance implies that the variable pair is positively related. In other words, when
the magnitude of one variable tends to increase (or decrease), the other does too.
Negative covariance implies that the variable pair is inversely related. In other words, when
the magnitude of one variable tends to increase, the other tends to decrease or vice versa.
In our case, only the variables x3 and x4 are negatively correlated, whereas the other
variables are positively correlated.
Step 3: Calculate the Eigenvalues and Eigenvectors of the Covariance Matrix
To determine principal components, we need eigenvectors and eigenvalues, which inform
us about the directions and the magnitude of the spread of our data. The first thing we need
to understand is that they always come in pairs: every eigenvector has an eigenvalue to
describe its magnitude.
As early stated, the “principal components” are the new variables that are formed via the
linear transformations of the original variables. Eigenvectors give the weights to be used in
this linear transformation and eigenvalues tell how much variance is explained by those
newly transformed variables.
Ranking our eigenvectors based on their eigenvalues, from the highest to the lowest, allows
us to select the principal components, which explain most of the variation in the dataset.
The following decomposition is employed for a (nonzero) vector v of dimension N for a N
× N square matrix A to compute the eigenvectors and eigenvalues:
//Aν=λν
The eigenvalues are ranked in descending order as λ1, λ2, λ3, and λ4. Based on the result,
we can choose the top 2 eigenvectors:
For more information on how to select the ideal number of components, you can see our
tutorial.
Step 4: Recast the Data
Now we can reorient the data to the new axes: the ones represented by the principal
components, hence the original (standardized) variables can be expressed in terms of
principal component scores.
Regarding linear transformation is shown below:
V is linear projection
2.10.4 Singular Value Decomposition
Basic Learning Theory:
Design of Learning System, Introduction to Concept of Learning, Modelling in Machine
Learning.
Mitchell – A program can learn from experience E, and performance P improves with
experience.
Let x be the input and X be the input space, which is set of all the inputs and Y be the
output space with the answers of ‘yes’ or ‘no’
Let D be the input dataset with examples, (x1,y1), (x2,y2), …(xn,yn) for n inputs
Let the unknown target function be f:XY that maps the input space to output space
Let H be the set of all formulae from which learning algorithm chooses.
The choice is good when the hypothesis ‘g’ replicates ‘f’ for all samples
Let D be the input dataset with both negative and positive examples
- Adaptive systems
System interacts with inputs for getting labelled data as direct inputs are not available.
In reinforcement learning, a learning agent interacts with environment and in return gets
feedback.
Based on feedback, the learning agent generates input samples for learning, which are
used for generating the learning model
Such learning agents are not static and change their behaviour according to external
signal received from environment.
The feedback is known as reward and learning here is the ability of the learning agent
adapting to the environment based on the reward. These are the characteristics of an adaptive
system.
In Direct Experience- A board move is selected and is determined whether it is a good move
or not against all other moves.
If best move, then it is chosen as BM, where B and M are legal moves.
In Indirect experience – all legal moves are accepted and a score is generated for each
V=w0 +w1x1+w2x2+w3x3
Where x1, x2 and x3 represent different board features and w0, w1 w2and w3 represent weights
The focus is to choose weights and fit the given training samples effectively.
Here b is the sample and V(cap) is 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 xi, the weights are updated as
Wi= wi + Mu X errob(b) X xi
Concept learning helps to classify an object that has common and relevant feature
It helps to compare abd contrast categories based on similarity and association of positive and
negative instances in the training data to classify object.
The learner tries to aimplify by observing the common feature from training samples and then
apply simplified model to future samples.
Each concept or category obtained by learning is a boolean valued function that takes a true
or false value.
Ex: humans can identify different kinds of animals based on common relevant features and
categorize all animals based on specific sets of feature.
The features that distiguish one animal from another called concept.
This way of learning categories for object and to recognize new instances of those categories
is called concept learning.
It is formally defined as inferring a Boolean valued function by processing training instances
i. Input – Training dataset which is set of training instances. Each one is labelled
ii. Output- Target concept ot function f. Mapping function f(x) from input x to
output y.
iii. Test – New instances to test the learned model.
Ex:
A hypothesis ‘h’ approximates a target function ‘f’ to represrent the relationship between the
independent attributes and the dependent attribute of training instances
The hypothesis is the predicted approximate model that maps the inputs to outputs.
The set of hypothesis (h) in the search space is called as hypotheses (H)
3.4.2 Hypothesis Space
Hypothesis space is the set of all possible hypotheses that approximate the target function ‘f’
The ML algorithm will choose best possible hypothesis that describes trget function ‘f’ or
best fits the output
Every machine learning algorithm would represents a larger hypothesis space in a different
manner. Ex: regrssion- linear system, decision tree – tree
The set of hypotheses that can be generated by learning algorithm can be further reduced by
specifing a language bias
The subset of hypothesis space that consistent with all observed training instances is called as
Version Space.
Version Space represents the only hypotheses that are used for classification.
Dataset mentioned in table 7 variables takes 2 values and one variable takes 3 values so there
can be (2 X 2 X 2 X 2 X 2 X 3 X 2 X 2 ) = 384 instances can be drawn
The hypothesis space is much larger and hence we need efficient learning algorithm to search
for the best hypothesis from hypotheses
Hypothesis ordering is also important where in the hypotheses are ordered from most specific
to most general one.
h1={No, ?, No, }
1. Considers only positive instances and if dataset is consistent then Find-S works well
2. Finds only one hyposthesis and there may exists more than one hypothesis consistent
with dataset
3. If dataset is erronious then hypothesis also will be not consistent
This algorithm limits the version space for most gereric and most specific.
Algorithm
Ex:
Initialise G to all ? S to ɸ
STEP 1: G= {
? ? ? ? ? ?
}
STEP 2: S = {
ɸ ɸ ɸ ɸ ɸ ɸ
}
Genraralise the initial hypothesis for the first positive instance I1
I1: >=9 Yes Excellent Good Fast Yes +ve
S1 = >=9 Yes Excellent Good Fast Yes
G1= ? ? ? ? ? ?
STEP 3:
Scan next subsequent instance I2 it is +ve instance
I2: >=9 Yes Good Good Fast Yes +ve
S2= >=9 Yes ? Good Fast Yes
Prune G1 to exclude all inconsistent hypotheses with positive instance, Here no change
G2= ? ? ? ? ? ?
Iteration 2: >=8 No Good Good Fast No -Ve
Since it is -ve instance
Specialise G to exclude the -ve but stay consistent with S2. So generate hypothesis for each
non-matching attribute value of S2 and fill with value of S2. The attributes 1, 2 and 6 differ
with S2 values so generate 3 hypothesis
G3 = < >=9 ? ? ? ? ?>
<? Yes ? ? ? ?>
<? ? ? ? ? Yes >
S3= < >=9 Yes ? Good Fast Yes >
Iteration 3:
Scan I4, it is +ve instance Check for mismatch in S3 with I4
I4= >=9 Yes Good Good slow No +ve
Change S3 tpo S4 as
S4 = < >=9 Yes ? Good ? ?>
Prune G3 to exclude all inconsistant hypothesis with +ve Instant I4
G3 < >=9 ? ? ? ? ?>
<? Yes ? ? ? ?>
<? ? ? ? ? Yes >
inconsitant
Resulting G4 = < >=9 ? ? ? ? ?>
<? Yes ? ? ? ?>
Conbining both G4 and S4 by generalising we get version space as
< >=9 Yes ? ? ? ?>
< >=9 ? ? Good ? ?>
<? Yes ? Good ? ?>
A machine learning model is an abstract of the training dataset that can perform a predictoin
of new data.
Training the model means feeding instances to the machine learning algorithm.
Training dataset are used to fit and tune the model.
After training a machine learning algorithm with training data, a predictive model is
generated as output to which a new data is fed to make predictions.
The process of modelling means training a machine learning algorithm with training dataset,
tuning it to increase performance, validating it and making predictions for a new unseen data.
The major concern in machine learning s what model to select,
i. how to train the model,
ii. time required to train
iii. the dataset to be used
iv. what performance to expect and so on
Learning parameter is the main goal in machine learning algorithm
There are two types of parameters
i. Model parameters – parameters directly from data like regession coefficient,
spliting of attributes in decision tree, weights and bias in neural network
ii. Hyper parameters – parameters are high leavel parameters which cannot be learnit
directly Ex. Regularisation lamda ƛ used in regularisation of regression, Number
of decision trees to include in random forest etc.,
Evaluating the selected machine learning model is also equally important as training the
model.
Dataset is split in to two sets called training and dataset and test dataset. The test dataset is
used to evaluate the model that is trained with dataset.
During prediction, an error occurs when the estimated output doesnot match with the true
output.
Training error is called in-sample error that is obtained at the time trainied samples are
applied on predicted model
The other error out-sample error is one that is drawn when testing data is applied on model
The error function or loss function is Mean Squared Error (MSE), which is average of
squared differences between the true values Yi and predicted values f(x) for an input value Xi
Smaller the value better the model