Module 1 Introduction to Machine Learning
Machine learning was defined in 90’s by Arthur Samuel
knowledge to machines without hard coding it.
A computer algorithm or program is said to learn from performance measure P and experience E
with some class of tasks T if its performance at tasks in T, as measured by P, improves with
experience E.
Why do we need Machine Learning?
For understanding data which is impossible to understand with higher speed and higher accuracy.
More than 80 % of data is unstructured like audios, videos, photos, documents, graphs etc.
Finding data patterns for the human brain is impossible.
Data is very massive and time consuming to compute, that is why Machine Learning comes into
action to process data in minimum time.
Machine learning is a sub field of Artificial Intelligence.
AI solves tasks that human intelligence is required. while ML that solves specific tasks.
ML is AI but all AI is not ML.
For AI, we need better and Intelligent machine.
Types of Machine Learning:
1) Supervised Machine Learning
2) Unsupervised Machine Learning
3) Reinforcement Machine Learning
[Link] Learning:
This is the first type of machine learning. In which labeled data is used to train algorithms.
In this type, marked data is used to train algorithms, whereas input and output are known.
We input the data in the learning algorithm as a inputs, which is called Features, which is
denoted by X along and corresponding output along Y.
Types of Supervised learning:
There are two types of supervised learning.
1) Classification
2) Regression
Classification-
Generalized input will be classified by its type, size etc.
Regression-
• Regression is the type of Supervised Learning in which labelled data is used.
• This data is used to make predictions in a continuous form.
• The graph of output is linear.
• Regression is a predictive modelling technique, which investigates the relationship between a
dependent variable [Outputs] and independent variable [Inputs].
• This technique is used for forecasting the weather, time series modelling, and process optimization.
[Link] Learning:
• Unlabeled data are used to train the algorithm.
• The data used has no historical labels.
• In unsupervised learning the data is unlabeled.
• It makes cluster of data with new lable.
• This learning technique works on transactional data.
• E.g. it can find the primary qualities that separate customer segments from each other.
• These algorithms are also used to segment text topics, recommend items.
Clustering-
• It is the type of Unsupervised Learning in which unlabeled data is used.
• It is the process of grouping similar entities together, and then the grouped data is used to make
clusters.
• this Technique of unsupervised machine learning is to find similarities.
Dimensionality Reduction-
• It is the type of Unsupervised Learning,
• In which the dimensions of the data is reduced to remove the unwanted data from the input.
• This technique is used to remove the unwanted features of the data.
• These techniques are used to solve machine learning problems to obtain better features.
3. Reinforcement Learning
• It is the third type of machine learning.
• In which no raw data is given as input.
• Instead, reinforcement learning algorithms must figure out the situation on their own.
• It is frequently used for robotics, gaming, and navigation.
• The algorithms are discovered by trial-and-error method.
• It has three main components 1) agent- in which, describe as the learner or decision maker. 2)
environment- in which, described everything that agent interacts with 3) Actions- in which
represented as what the agent can do.
Data Visualization
What is data visualization?
1) It is the process of generating graphical representations of data.
2) These graphical representations are commonly known as plots or charts in data science.
Why is Data Visualization Important?
1) Data Visualization is important because it includes communication of result or findings.
2) Monitoring the model’s performance at the evaluation stage.
3) hyperparameter tuning, identifying trends, patterns, and correlation between dataset features,
4) data cleaning such as outlier detection, and validating model assumptions.
Scatter Plot:
A scatter plot is also called a scatter chart, scattergram, or scatter plot, XY graph.
Scatter plots are numerical data pairs. It has one variable on each axis to show their relationship.
When to use scatter plot?
1) When we have paired numerical data.
2) When there are multiple values of the dependent variable for a unique value of an independent
variable.
3) In determining the relationship between variables in some scenarios, such as identifying potential
root causes of problems.
4) checking whether two products that appear to be related both occur with the exact cause and so
on.
What Is a Histogram?
1) A histogram is a bar graph. It is representation of data into columns along the x-axis. This data is
in the form range.
2) The y-axis represents the number count or percentage of occurrences in the data for each column.
3) Columns can be used to visualize data distributions patterns.
Bar Chart:
A bar plot is used to show the frequency categorical data.
Each category is represented by a bar.
The bars can be created vertically or horizontally.
Their heights or lengths are proportional to actual data.
Pie Chart:
It is a circular graph made with slices equal to the number of categories.
A categorical variable in pie chart shows each category's values in the form slices.
Slice sizes are proportional to each quantity.
Box and whisker plot:
The data distribution shows by way using 5 summaries. It is called a Box plot.
It is also termed as Box and Whiskar Plot
1)Minimum 2) Q1 (First Quartile) 3) Median, 4) Q3(third Quartile) 5) Maximum
It does not show the distribution like stem and leaf plot or Histogram.
It is primarily used to indicate a distribution is skewed.
if there are potentially unusual observations present in the data set.
Boxplots are also very beneficial when large numbers of data sets are involved.
Line graph:
Line graphs consist of two axes x-axis (horizontal) and y-axis (vertical)
It is graphically denoted as (x,y).
A line graph connects individual data points.
It typically displays values over a specified time interval.
In technical analysis field, line graphs are quite informative to visualize trends.
Line graphs are used in many different fields for various purposes. Their most common
function is to show changes in values over time, graphically.
In finance, line graphs are used to create visual representation in the prices of securities with
respect to time.
Paire Plot:
A pair plot is another important plot in Data Science.
It is the grid-based graphical representation along the X and Y axis, either positive correlated
or negatively correlated.
We can eliminate the negatively correlated pairs by positively correlated pairs for further
analysis.
This is very similar to Heat Map.
It is best for the feature selection process.
Module 2 Data Processing
What is data pre-processing?
• Removing noise, missing values, duplicate values, performing transformation and reduction for
maintaining the quality of data.
What’s the goal or purpose of this activity?
• To maintain quality of data.
• Because quality will compromise, then result will be compromised.
Data Quality-
• Accuracy
• Completeness
• Consistency
• Timeliness
• Believability
• Interpretability
Data Cleaning-
• Clean the data by filling in missing values, smoothing noisy data, identifying, or removing outliers,
and resolving inconsistencies.
• Naming inconsistencies.
Missing Values:
1) Fill in the missing values manually.
2) Use a global constant to fill in the missing value.
3) Can use mean or median to fill the missing values.
4) Use the most probable value to fill the missing value.
Noisy Data:
1) Noise is a random error.
2) It can be removed by the smoothing technique.
o Data Smoothing Techniques:
1) Binning.
Binning methods smooth a sorted data value by consulting its “neighborhood,”
that is, the values around it.
The sorted values are distributed into several “buckets” or “Bins”.
Binning methods consult the neighborhood values, it performs local smoothing.
In smoothing by bin means, each value in a bin is replaced by the mean value of
the bin. E.g., the mean of the values 4, 8, and 15 for Bin 1 is 9. Therefore, each
original value in this bin is replaced by the value 9.
In smoothing by bin medians, in which each bin value is replaced by the bin
median.
In smoothing by bin boundaries, the minimum and maximum values in each bin
are identified as the bin boundaries. Each bin value is then replaced by the closest
boundary value.
2) Regression.
Data smoothing can also be done by regression.
Linear regression involves finding the “best” to fit the two attributes. so that one
attribute can be used to predict the other.
Multiple linear regression is an extension of linear regression, in this, more than
two attributes are involved.
3) Out Liar Analysis.
Outliers may be detected by clustering.
E.g., Similar values are organized into Cluster. Values which is outside of the
clusters may be considered outliers.
Handling categorical features-
A categorical variable takes only a limited number of values.
E.g., Consider a survey that asks how often you eat breakfast and provides four options: 1) Never,
2) Rarely, 3) Most days, 4) Every day.
In the above case, the data is categorical, because responses come into a fixed set of categories.
There are three approaches for Handling categorical features.
1) Drop Categorical Variable.
2) Label Encoding.
3) One Hot Encoding.
a. Drop Categorical Variable.
The easiest approach to dealing with categorical variables.
It is simply removed them in dataset.
if the columns did not contain useful information. That case this approach work well.
b. Label Encoding.
Label encoding assigns each unique value to a different integer.
c. One Hot Encoding.
One-hot encoding creates new columns indicating the presence of each possible
value in the original data.
Feature Selection-
• The method of choosing the important parameters of our data is called Feature Selection.
• The input variables that we give to our machine learning models are called features.
• Each column in our dataset constitutes a feature.
• We have too many features, the model can capture the unimportant patterns and learn from noise.
There are three methods of feature selection.
1) Filter Method
2) Wrapper Method
3) Intrinsic Method
Filter Method
o In this method, features are dropped based on their relation to the output.
o By using correlation to check if the features are positively or negatively correlated.
Wrapper Method
o We split our data into subsets and train a model using this.
o Based on the output of the model, we add and subtract the feature and train the model
again
Internisc Method.
o This method combines the qualities of both the Filter and Wrapper method to create the
best subset.
o This method takes care of the machine training iterative process.
Feature Reduction-
• It reduces the data set size by removing irrelevant or redundant attributes.
1) Stepwise Forword Selection
2) Stepwise Backward elimination.
3) Combination of forward selection and backward elimination.
4) Decision tree induction.
Histograms (non-parametric)-
Histograms use binning to approximate data distributions.
It is also a popular form of data reduction.
If each bucket represents only a single attribute value/frequency pair, the buckets are called
singleton buckets.
Bucket represents continuous ranges for the given attribute.
Data Transformation by Normalization-
Module 3 Supervised Machine Learning algorithms
Naïve Bayes Classifier Algorithm-
• Naïve Bayes algorithm is a supervised learning algorithm.
• Which is based on Bayes theorem and used for solving classification problems.
• It is mainly used in text classification that includes a high-dimensional training dataset.
• it predicts on the basis of the probability so It is a probabilistic classifier.
• E.g., spam filtration, Sentimental analysis, and classifying articles.
Why is it called Naïve Bayes?
The Naïve Bayes algorithm has two words Naïve and Bayes.
Naïve: It is called Naïve because it assumes that the occurrence of a certain feature is
independent of the occurrence of other features.
o Such as if the fruit is identified on the bases of color, shape, and taste, then red,
spherical, and sweet fruit is recognized as an apple.
o Hence each feature individually contributes to identifying that it is an apple without
depending on each other.
: It is called Bayes because it depends on the principle of Bayes' Theorem
• Bayes' Theorem- Bayes' theorem is also known as Bayes' Rule or Bayes' law, which is used to
determine the probability of a hypothesis with prior knowledge. It depends on the conditional
probability.
P(A/B) P(A)
• The formula for Baye’s theorem is given as P(A/B) = ---------------------
• P(A|B) is Posterior probability. P(B)
• P (B (evidence) |A (hypothesis)) is Likelihood probability.
• P(A) is Prior Probability.
Algorithm of Naïve Baye’s Classifier:
• Compute the prior probability for the target class. i.e., P(A)
• Compute frequency matrix and Likelihood probability {P(B/A)} for each feature.
• Use Baye’s theorem to calculate the posterior probability{P(A|B)} of all hypotheses.
Advantages of Naïve Bayes Classifier:
• Naïve Baye’s is one of the fast and easy ML algorithms to predict a class of datasets.
• It can be used for Binary as well as Multi-class Classifications.
• It performs well in multi-class predictions as compared to the other Algorithms.
• It is the most popular choice for text classification problems.
Disadvantages of Naïve Bayes Classifier:
• Naive Bayes assumes that all features are independent or unrelated,
• It cannot learn the relationship between features.
Applications of Naïve Bayes Classifier
• It is used for Credit Scoring.
• It is used in medical data classification.
• It can be used in real-time predictions.
• It is used in Text classification such as Spam filtering and Sentiment analysis.
Types of Naïve Bayes Model-
1) Gaussian
2) Multinomial
3) Bernoulli
Gaussian
o The Gaussian model assumes that features follow a normal distribution. The model
assumes that these values are sampled from the Gaussian distribution.
Multinomial
o This classifier is used when the data is multinomial distributed. It is primarily used
for document classification problems.
Bernoulli
o The Bernoulli classifier works like the Multinomial classifier. but the predictor
variables are the independent Booleans variables. This model is also famous for
document classification tasks.
KNN(K-Nearest Neighbor)-
• K-NN algorithm assumes the similarity between the new data and available data.
• The new data into the category that is most like the available categories.
• K-NN algorithm stores all the available data and classifies a new data point based on the similarity.
• This means when new data appears then it can be easily classified into a well suite category by using
K- NN algorithm.
• It is also called a lazy learner algorithm because it does not learn from the training set immediately.
• KNN algorithm at the training phase just stores the dataset. When it gets new data, then it classifies
that data into a category that is much like the new data.
The K-NN working can be explained based on the below algorithm:
o Step-1: Select the number K of the neighbors.
o Step-2: Calculate the Euclidean distance of K number of neighbors.
o Step-3: Take the K nearest neighbors as per the calculated Euclidean distance.
o Step-4: Among these K neighbors, count the number of data points in each category.
o Step-5: Assign the new data points to that category for which the number of the
neighbor is maximum.
o Step-6: Our model is ready.
Advantages of KNN Algorithm:
•It is simple to implement.
•It is robust to the noisy training data.
•It can be more effective if the training data is large.
Disadvantages of KNN Algorithm:
• Always needs to determine the value of K which may be complex some time.
• The computation cost is high because of calculating the distance between the data points for all the
training samples.
Decision tree
It is called a decision tree because, like a tree, it starts with the root node, which expands on further
branches and constructs a tree-like structure.
Decision Tree is a Supervised learning technique.
It can be used for both classification and Regression problems.
Mostly it is preferred for solving Classification problems.
It is a tree-structured classifier, where internal nodes represent the features of a dataset, branches
represent the decision rules, and each leaf node represents the outcome.
In a Decision tree, there are two nodes, 1) Decision Node 2) Leaf Node.
o Decision nodes are used to make any decision and have multiple branches,
o Leaf nodes are the output of decisions and do not contain any branches.
It is a graphical representation of all possible solutions.
To build a tree, we use CART algorithm, which stands for Classification and Regression Tree algorithm.
A decision tree simply asks a question and based on the answer (Yes/No),
it further split the tree into subtrees.
Why use Decision Trees?
• Decision Trees work like mimic human thinking ability while deciding, so it is easy to understand.
• It can be easily understood because of its tree-like structure.
Decision Tree Terminologies
1) Root Node
2) Leaf Node
3) Decision Node
o Root Node: Root node is from where the decision tree starts. It represents the entire
dataset, which further gets divided into two or more homogeneous sets.
o Leaf Node: Leaf nodes are the final output node. tree cannot be segregated further after
getting a leaf node.
o Decision Node: Internal Nodes.
Working of algorithm
• Step-1: Begin the tree with the root node, says S, which contains the complete dataset.
• Step-2: Find the best attribute from dataset by using Attribute Selection Measure (ASM).
• Step-3: Divide the S (Root Node) into subsets that contain possible values for the best attributes.
• Step-4: Generate the decision tree node, which contains the best attribute.
• Step-5: Make new decision trees using the subsets of the dataset created in step -3.
o Continue this process until a stage is reached where you cannot further classify the nodes
and called as the final node as a leaf node.
Attribute Selection Measures (ASM)
• While implementing a Decision tree, the main issue arises that how to select the best attribute for the
root node and for sub-nodes.
• So, to solve such problems there is a technique which is called Attribute selection measure or ASM.
• By this measurement, we can easily select the best attribute for the nodes of the tree.
• There are two popular techniques for ASM, which are:
o Information Gain
o Gini Index
1) Information Gain:
i) It calculates how much information a feature provides us with about a class.
ii) According to the value of information gained, we split the node and build the
decision tree.
iii) Entropy: Entropy is a metric to measure the impurity in each attribute. It specifies
randomness in data. Entropy can be calculated as
2) Gini Index:
i) Gini index is a measure of impurity or purity used while creating a decision tree in
the CART (Classification and Regression Tree) algorithm.
ii) The low Gini index should be preferred as compared to the high Gini index.
iii) It only creates binary splits.
iv) Gini index can be calculated using the below formula:
v) Gini Index= 1- ∑ j P j2
Steps to Calculate Gini for a split-
• Calculate Gini for sub-nodes, using formula sum of square of probability for success and failure
p²+q²).
• Calculate Gini for split using weighted Gini score of each node of that split.
Advantages of the Decision Tree-
• It is simple to understand as it follows the same process which a human follows while making any
decision in real-life.
• It can be very useful for solving decision-related problems.
• It helps to think about all the possible outcomes for a problem.
• There is less requirement of data cleaning compared to other algorithms.
Disadvantages of the Decision Tree-
• The decision tree contains lots of layers, which makes it complex.
• It may have an overfitting issue, which can be resolved using the Random Forest algorithm.
Linear regression-
• Linear regression is one of the easiest and most popular Machine Learning algorithms.
• It is a statistical method that is used for predictive analysis.
• Linear regression makes predictions for continuous/real or numeric variables such as sales, salary,
age, product price, etc.
• The linear regression algorithm shows a linear relationship between a dependent (y) and one or more
independent (y) variables, hence called linear regression.
• linear regression shows the linear relationship.
• linear regression model provides a sloped straight line.
Types of Linear Regression-
1) Simple Linear Regression
2) Multiple Linear Regression.
1) Simple Linear Regression.
o If a single independent variable is used to predict the value of a numerical dependent
variable, then such a Linear Regression algorithm is called Simple Linear Regression.
2) Multiple Linear regression.
o If more than one independent variable is used to predict the value of a numerical
dependent variable, then such a Linear Regression algorithm is called Multiple Linear
Regression.
Linear Regression Line-
• A linear line showing the relationship between the dependent and independent variables is called
a regression line.
Positive Linear Relationship-
• Dependent variables increased on Y- axis and independent variables on X- axis such relation is
known as Positive Linear Relationship
Negative Linear Relationship-
If the dependent variable decreased on Y- axis and independent variable on X- axis such relation is
known as Negative Linear Relationship.
Multivariate \ Multiple linear regression-
Response variable is affected by more than one predictor variable in that case Multiple linear
algorithms is used.
Multiple Linear Regression is one of the important regression algorithms.
Module 6 Reinforcement Learning
What is Reinforcement Learning?
• The agent interacts with the environment and explores it by itself.
• The primary goal of an agent in reinforcement learning is to improve performance by getting the
maximum positive rewards.
• Reinforcement learning is a type of machine learning method where an intelligent agent (computer
program) interacts with the environment and learns to act within it.
• It is a core part of Artificial intelligence.
• all AI agent works on the concept of reinforcement learning.
o Example: Suppose there is an AI agent present within a maze environment, and his
goal is to find the diamond. The agent interacts with the environment by performing
some actions, and based on those actions, the state of the agent gets changed, and it
also receives a reward or penalty as feedback.
Terms used in Reinforcement Learning-
• Agent: An entity that can perceive/explore the environment and act upon it.
• Environment: A situation in which an agent is present or surrounded by.
• Action: Actions are the moves taken by an agent within the environment.
• State: State is a situation returned by the environment after each action taken by the agent.
• Reward: A feedback returned to the agent from the environment to evaluate the action of the agent.
• Policy: Policy is a strategy applied by the agent for the next action based on the current state.
• Value: It is expected to be long-term returned with the discount factor and opposite to the short-term
reward.
• Q-value: It is mostly like the value, but it takes one additional parameter as a current action (a).
Key Features of Reinforcement Learning-
• In RL, the agent is not instructed about the environment and what actions need to be taken.
• It is based on the hit and trial process.
• The agent takes the next action and changes states according to the feedback of the previous action.
• The agent may get a delayed reward.
• The environment is stochastic.
• The agent needs to explore it to reach the maximum positive rewards.
Approaches to implement Reinforcement Learning-
• There are mainly three ways to implement reinforcement-learning in ML, which are:
•
1) Value-based:
2) Policy-based:
3) Model-based:
Value-based:
o The value-based approach is about to find the optimal value function
Policy-based:
o A policy-based approach is to find the optimal policy for the maximum future rewards
without using the value function.
Model-based:
o In the model-based approach, a virtual model is created for the environment, and the
agent explores that environment to learn it.
Elements of Reinforcement Learning-
four main elements of Reinforcement Learning
1) Policy
2) Reward Signal
3) Value Function
4) Model of the environment
a. Policy
i. A policy can be defined as the way an agent behaves at a given time.
b. Reward Signal
i. The goal of reinforcement learning is defined by the reward signal. At each state,
the environment sends an immediate signal to the learning agent, and this signal is
known as a reward signal.
c. Value Function
i. The value function gives information about how good the situation and action are
and how much reward an agent can expect.
d. Model of the environment
i. The last element of reinforcement learning is the model, which mimics the behavior
of the environment.
How does Reinforcement Learning Work?
• To understand the working process of the RL, we need to consider two main things:
• Environment: It can be anything such as a room, maze, football ground, etc.
• Agent: An intelligent agent such as an AI robot.
• Let's take an example of a maze environment that the agent needs to explore.