Machine Learning and Data Mining Overview
Machine Learning and Data Mining Overview
UNIT 2
MACHINE LEARNING AND DATA MINING
Introduction to machine learning and data mining-Supervised and unsupervised learning
algorithms-Feature extraction and dimensionality reduction-Data pre-processing and cleaning
techniques- Evaluation and validation of machine learning models-metrics-Cross-validation-
Over fitting and under fitting
Key Components:
• Data Collection: Gathering raw data from various sources such as sensors, logs, APIs,
and databases.
Applications:
Machine Learning (ML) is a branch of artificial intelligence (AI) that enables systems
to learn patterns from data and make decisions or predictions without being explicitly
programmed.
• Reinforcement Learning: Learning through trial and error based on feedback from the
environment.
Applications:
• Speech and image recognition, spam filtering, predictive maintenance, personalized
recommendations, autonomous vehicles.
Relationship Between Data Science and Machine Learning
Data Science is a broader field that includes data acquisition, cleaning, exploration,
visualization, and modeling. Machine Learning forms the core of the modeling and prediction
part in Data Science.
Example:
In a movie recommendation system:
• Data Science handles collecting user data, cleaning, analyzing user preferences.
• Supervised Learning
• Unsupervised Learning
These approaches differ based on whether the data used for training contains labels (i.e., target
outputs).
Supervised Learning
Supervised learning involves training a model using labeled data. The algorithm learns
a mapping function from input to output based on example input-output pairs. Once trained, it
can predict outcomes for unseen data.
Common Applications:
Linear Regression
Linear regression is a regression algorithm that models the relationship between one
or more independent variables (X) and a dependent variable (Y) using a linear equation:
Use Cases:
Advantages:
• Computationally efficient
Limitations:
• Assumes linearity
• Sensitive to outliers
Multiple linear regression involves more than one independent variable and one
dependent variable. The equation for multiple linear regression is:
The goal of the algorithm is to find the best Fit Line equation that can predict the values
based on the independent variables. In regression set of records are present with X and Y values
and these values are used to learn a function so if you want to predict Y from an unknown X
this learned function can be used. In regression we have to find the value of Y, So, a function
is required that predicts continuous Y in the case of regression given X as independent features.
A decision tree in machine learning is a supervised learning algorithm used for both
classification and regression tasks. It visually represents a series of decisions as a tree-like
structure, guiding data through a process of splitting and branching until a final outcome is
reached.
• Supervised Learning: Decision trees learn from labeled data, where the desired outcome
is known.
Tree Structure:
• Root Node: The starting point of the tree, representing the entire dataset.
• Leaf Nodes: Terminal nodes that provide the final prediction or classification.
• Splitting: The process of dividing data based on attribute values to create branches.
• Greedy Search: Decision tree algorithms use a greedy approach to find the best split
at each node, optimizing for a specific criterion like information gain or Gini impurity.
Information Gain:
If we make attribute Ai, with v values, as the root of the current tree, this will partition D into
v subsets D1, D2 …, Dv. The expected entropy if Ai is used as the current root:
v | Dj |
entropyAi ( D) = | D | entropy( D )
j =1
j
We evaluate every attribute. We choose the attribute with the highest gain to branch/split the
current tree.
Let us consider the Table 2.1, and let us calculate the Entropy and Information gain to decide
the parameter for the first split
Table 2.1 Dataset for Loan Approval
6 6 9 9
entropy( D) = − log 2 − log 2 = 0.971
15 15 15 15
6 9
entropyOwn _ house ( D) = entropy( D1 ) + entropy( D2 )
15 15
6 9
= 0 + 0.918
15 15
= 0.551
5 5 5
entropyAge ( D) = entropy( D1 ) + entropy( D2 ) + entropy( D3 )
15 15 15
5 5 5
= 0.971 + 0.971 + 0.722
15 15 15
= 0.888
From the above calculations it is clear that Own_house has the highest information
gain and hence it is chosen for the split. The split is done as in Fig 2.1
Fig Decision Tree for Table2.1
The algorithm chooses the attribute that best separates the data based on a chosen
metric.
3. Create branches:
Based on the attribute's values, the data is split into different branches.
This process continues recursively until a stopping criterion is met (e.g., reaching a
maximum depth, having a minimum number of samples in a leaf, or achieving a pure split).
The final leaf nodes represent the predicted outcome for the data points that reach them.
• Easy to understand and interpret: The tree structure makes it easy to visualize the
decision-making process.
• Can handle both categorical and numerical data: Decision trees can be applied to a
wide range of datasets.
• Feature selection: Decision trees can implicitly identify important features by how they
are used to split the data.
• Non-parametric: They don't assume any specific data distribution.
Disadvantages of Decision Trees:
• Prone to overfitting: Decision trees can create overly complex structures
that fit the training data too closely, leading to poor generalization on new
data.
• Can be unstable: Small changes in the data can lead to significantly
different tree structures.
• Pruning: Techniques like pruning are often necessary to prevent
overfitting.
Example:
Imagine predicting whether someone will play sports based on the weather.
A decision tree might first split based on whether it's "Sunny" or "Rainy". Then,
if it's sunny, it might split based on humidity (High or Low). Finally, if it's rainy,
it might check if it's "Cold" or "Warm". The leaf nodes would then indicate
whether or not the person is likely to play sports.
The main idea behind the Naive Bayes classifier is to use Bayes' Theorem to classify data
based on the probabilities of different classes given the features of the data. It is used mostly
in high-dimensional text classification
• The Naive Bayes Classifier is a simple probabilistic classifier and it has very few
number of parameters which are used to build the ML models that can predict at a faster
speed than other classification algorithms.
Consider a fictional dataset that describes the weather conditions for playing a game of tennis.
Given the weather conditions, each tuple classifies the conditions as fit(“Yes”) or unfit(“No”)
for playing tennis. Here is a tabular representation of our dataset.
The dataset is divided into two parts, namely, feature matrix and the response vector.
• Feature matrix contains all the vectors(rows) of dataset in which each vector consists
of the value of dependent features. In above dataset, features are ‘Outlook’,
‘Temperature’, ‘Humidity’ and ‘Windy’.
• Response vector contains the value of class variable(prediction or output) for each row
of feature matrix. In above dataset, the class variable name is ‘Play golf’.
The Learning Phase
Unsupervised Algorithms
Unsupervised learning is a part of machine learning which works differently from
supervised because there is no teacher(supervisor) involved to guide the machine. In this
approach the machine is given with data that has no labels or categories. It analyzes the data
on its own to find patterns, groups or relationships without any prior knowledge. The machine
learns by discovering hidden structures within the data without being told what the correct
output should be.
For example, unsupervised learning can analyze animal data and group the animals by
their traits and behavior. These groups might represent different species which allows the
machine to organize animals without any prior labels or categories.
Now, if a new image is given to the model that contains both dogs and cats it won’t be
able to directly label them as “dog” or “cat.” It will group parts of the image based on
similarities and differences in features like shape or texture. It might separate the image into
two groups one with dog-like features and other with cat-like features.
This happens because unsupervised learning doesn’t rely on prior knowledge or training
with labeled data. It finds patterns and organizes data on its own helps in discovering
information that wasn’t given before.
1. Clustering
A clustering is used to group similar data points together. Clustering algorithms work by
repeatedly moving data points closer to to the center of their group (cluster) and farther from
points in other groups. This helps the algorithm to create clear and meaningful clusters. Some
popular clustering algorithms include:
1. K-means clustering
2. Hierarchical clustering
K-mean Clustering
K-means is a partitional clustering [Link] the set of data points (or instances) D be
where xi = (xi1, xi2, …, xir) is a vector in a real-valued space X Rr, and r is the number
of attributes (dimensions) in the data.
The k-means algorithm partitions the given data into k clusters. Each cluster has a
cluster center, called centroid.k is specified by the user.
Given k, the k-means algorithm works as follows:
1) Randomly choose k data points (seeds) to be the initial centroids, cluster centers
2) Assign each data point to the closest centroid
Hierarchical Clustering
Hierarchical clustering is an unsupervised machine learning algorithm that groups data
into a tree of nested clusters. The main types include agglomerative and divisive. Hierarchical
cluster analysis helps find patterns and connections in datasets. Results are presented in a
dendrogram diagram showing the distance relationships between clusters.
Fig Dendrogram
Types of hierarchical clustering
It starts with all data points in one cluster, the root. It splits the root into a set of
child clusters. Each child cluster is recursively divided further. It stops when only
singleton clusters of individual data points remain, i.e., each cluster with only a single
point
It is more popular then divisive methods. At the beginning, each data point forms a
cluster (also called a node). It merges nodes/clusters that have the least distance. The merging
goes on. Eventually all nodes belong to one cluster
❑ Single link
❑ Complete link
❑ Average link
❑ Centroids
Single Link
The distance between two clusters is the distance between two closest data points in the
two clusters, one data point from each cluster. It can find arbitrarily shaped clusters, but it may
cause the undesirable “chain effect” by noisy points
Complete Link
The distance between two clusters is the distance of two furthest data points in the two
clusters. It is sensitive to outliers because they are far away.
Average link:
In this method, the distance between two clusters is the distance between their centroids
An association rule learning used to find patterns and relationships between different
items in a dataset. It looks for rules like “people who buy X often also buy Y”.
1. Apriori Algorithm
2. Eclat Algorithm
3. FP-Growth Algorithm
An artificial intelligence (AI) model’s performance relies on the quality of its training
data. Machine learning models go through preprocessing to help ensure that data is in a suitable
format for efficient model training and performance. Feature extraction is a crucial part of the
pre-processing workflow.
During the extraction process, unstructured data is converted into a more structured and
usable format to enhance the data quality and model interpretability. Feature extraction is a
subset of feature engineering, the broader process of creating, modifying and selecting features
within raw data to optimize model performance.
How Dimensionality Reduction Works?
Imagine a dataset where each data point exists in a 3D space defined by axes X, Y and
Z. If most of the data variance occurs along X and Y then the Z-dimension may contribute very
little to understanding the structure of the data.
• On the right after reducing the dimensionality the data is represented in lower-
dimensional spaces. The top plot (X-Y) maintains the meaningful structure while the
bottom plot (Z-Y) shows that the Z-dimension contributed little useful information.
This process makes data analysis more efficient, improving computation speed and
visualization while minimizing redundancy
1. Feature Selection
Feature selection chooses the most relevant features from the dataset without altering them. It
helps remove redundant or irrelevant features, improving model efficiency. Some common
methods are:
• Filter methods rank the features based on their relevance to the target variable.
• Wrapper methods use the model performance as the criteria for selecting features.
• Embedded methods combine feature selection with the model training process.
2. Feature Extraction
Feature extraction involves creating new features by combining or transforming the original
features. These new features retain most of the dataset’s important information in fewer
dimensions. Common feature extraction methods are:
4. Forward Feature Selection: Forward Feature Selection Begins with one feature, adds
others incrementally and keeps those improving model performance.
5. Random Forest: Random forest Uses decision trees to evaluate feature importance,
automatically selecting the most relevant features without the need for manual coding,
enhancing model accuracy.
6. Factor Analysis: Groups variables by correlation and keeps the most relevant ones for
further analysis.
1. Text Categorization: With vast amounts of online data dimensionality reduction helps
classify text documents into predefined categories by reducing the feature space like
word or phrase features while maintaining accuracy.
2. Image Retrieval: As image data grows indexing based on visual content like color,
texture, shape rather than just text descriptions has become essential. This allows for
better retrieval of images from large databases.
High dimensionality makes models inefficient. Let's now summarize the key advantages of
reducing dimensionality.
• Faster Computation: With fewer features machine learning algorithms can process
data more quickly. This results in faster model training and testing which is particularly
useful when working with large datasets.
• Better Visualization: As we saw in the earlier figure reducing dimensions makes it
easier to visualize data and reveal hidden patterns.
• Prevent Overfitting: With few features models are less likely to memorize the training
data and overfit. This helps the model generalize better to new, unseen data improve its
ability to make accurate predictions.
• Data Loss & Reduced Accuracy: Some important information may be lost during
dimensionality reduction and affect model performance.
• Choosing the Right Components: Deciding how many dimensions to keep is difficult
as keeping too few may lose valuable information while keeping too many can led to
overfitting.
Data preprocessing and cleaning are crucial steps in machine learning, involving
transforming raw data into a usable format for algorithms. This process includes handling
missing values, removing outliers, scaling numerical features, and encoding categorical
variables. Effective preprocessing ensures data quality, consistency, and suitability for analysis,
leading to improved model performance.
• Removal: Delete rows or columns with missing data, but this can lead to information
loss.
• Imputation: Replace missing values with statistical measures like mean, median, or
mode, or with more sophisticated methods like K-Nearest Neighbors.
2. Outlier Detection and Removal/Treatment:
• Outlier Detection: Identify extreme values that deviate significantly from the norm
using techniques like z-score, IQR, or visualization methods.
• Outlier Treatment: Decide whether to remove outliers or transform them (e.g., using
winsorizing or capping) based on the context.
3. Feature Scaling:
4. Categorical Encoding:
• One-Hot Encoding: Create binary columns for each category, representing the
presence or absence of a specific category.
• Label Encoding: Assign a unique numerical value to each category, suitable for
algorithms that can handle ordinal data.
5. Data Transformation:
6. Data Reduction:
• Feature Selection: Choose the most relevant features for the model, reducing
dimensionality and complexity.
• Dimensionality Reduction: Use techniques like PCA to reduce the number of features
while retaining important information.
7. Data Integration:
• Merging Data: Combine data from multiple sources into a unified dataset.
8. Data Discretization:
• Binning: Group continuous data into intervals or bins, often for visualization or to
simplify analysis.
By applying these techniques, data preprocessing and cleaning ensures that the data is of
high quality, consistent, and suitable for machine learning algorithms, ultimately leading to
better model performance and more reliable results.
While often used interchangeably, model evaluation differs from model validation.
Validation typically occurs during the model training phase to fine-tune parameters, whereas
evaluation happens after training to measure final performance. Together, they ensure the
model is both optimized and trustworthy.
Model evaluation is essential for determining the reliability and accuracy of machine
learning models. Without evaluation, models risk overfitting (performing well on training data
but poorly on new data) or underfitting (failing to capture the underlying patterns). Reliable
evaluation methods help identify and address these issues.
Another critical aspect is generalizability—ensuring the model performs consistently
on unseen datasets. This is especially vital for applications like medical diagnostics or financial
predictions, where errors can have significant consequences. Proper evaluation builds trust in
the model and lays the foundation for its successful deployment in real-world scenarios.
Train-Test Split
The train-test split method divides the dataset into two subsets: training data to build
the model and test data to evaluate it. Typically, the split ratio is 80:20 or 70:30. While
straightforward and computationally efficient, this method has limitations, such as high
variance in results when data is limited. Moreover, a single split may not capture the full range
of variability in the dataset, potentially leading to biased evaluations.
Cross-Validation
Cross-validation improves upon the train-test split by ensuring more robust evaluation.
The most common type is k-fold cross-validation, where the dataset is divided into k subsets
(folds). Each fold serves as a testing set once, while the remaining folds are used for training.
This process repeats k times, and the results are averaged for a comprehensive performance
measure. Stratified k-fold cross-validation ensures that each fold maintains the same class
distribution, making it particularly useful for imbalanced datasets. Cross-validation reduces
variance and provides a more reliable estimate of a model’s generalizability.
Holdout Validation
Holdout validation involves setting aside a portion of the dataset as a final validation
set after training and testing. This method is particularly useful for models undergoing
hyperparameter tuning, as it helps avoid overfitting to the training and test sets. However, it
requires a sufficiently large dataset to allocate separate subsets for training, testing, and
validation.
Each of these techniques offers unique benefits and trade-offs, and the choice depends
on the size of the dataset, the complexity of the model, and the desired evaluation rigor.
EVALUATION METRICS FOR CLASSIFICATION MODELS
Accuracy
Accuracy measures the proportion of correct predictions out of all predictions made by the
model. It is calculated as:
Limitations: Accuracy can be misleading when dealing with imbalanced datasets. For
instance, in a fraud detection scenario where only 1% of transactions are fraudulent, a model
predicting all transactions as non-fraudulent would achieve 99% accuracy but fail to detect any
fraud.
• Recall (Sensitivity): The ratio of true positives to all actual positives. It is critical in
cases like medical diagnosis, where missing a positive case (e.g., a disease) could have
severe consequences.
When to prioritize: Use precision when the cost of false positives is high (e.g., spam
detection). Prioritize recall when false negatives have greater consequences (e.g., identifying
diseases).
F1 Score
The F1 score is the harmonic mean of precision and recall, offering a balanced metric,
especially when class distributions are uneven:
It is particularly useful in situations where both false positives and false negatives need to be
minimized, such as fraud detection.
Confusion Matrix
This matrix helps in calculating all other metrics and offers a clear visualization of model
performance.
Predicted Predicted
Positive Negative
Actual Positive 80 20
Actual 50 850
Negative
Evaluation Metrics
The AUC-ROC (Area Under the Receiver Operating Characteristic Curve) evaluates a
model’s ability to distinguish between classes. The ROC curve plots the true positive rate
(recall) against the false positive rate at various thresholds.
• AUC (Area Under Curve): A higher AUC indicates better model performance. An
AUC of 1.0 represents a perfect model, while 0.5 suggests random guessing.
The Mean Absolute Error (MAE) is a straightforward metric that calculates the average
absolute difference between predicted and actual values. It is expressed as:
The Mean Squared Error (MSE) computes the average of squared differences between
predicted and actual values:
Root Mean Squared Error (RMSE) is the square root of MSE, which brings the error back to
the same units as the target variable:
Mean Absolute Percentage Error (MAPE)
MAPE measures errors as a percentage of the actual values, making it intuitive for comparing
model performance across datasets of different scales:
In machine learning, the goal is to build models that can generalize well to new, unseen
data, not just perform well on the data they were trained on. However, machine learning models
can encounter two common pitfalls: overfitting and underfitting, both of which hinder a model's
ability to make accurate predictions.
1. Overfitting
Overfitting occurs when a machine learning model learns the training data too well,
including the noise and irrelevant details. This can result in poor performance on new data.
Causes of overfitting
Overfitting can be caused by complex models, insufficient or noisy training data, or
excessive training time.
How to detect overfitting
To prevent overfitting, you can increase data size, simplify the model, apply
regularization, use early stopping, utilize cross-validation, or perform feature selection.
2. Underfitting
Underfitting occurs when a model is too simple to capture patterns in the training data,
leading to poor performance on both the training and test sets. This means the model fails to
learn relationships between features and labels.
Causes of underfitting
Underfitting is indicated by poor performance on both training and test data, learning
curves showing consistently high error rates, or by comparing performance to a more complex
model.
Achieving a good fit in a machine learning model involves finding the right balance
between underfitting and overfitting. This balance ensures the model captures patterns without
being overly sensitive to noise, making it capable of generalizing effectively to new data.
To intuitively grasp the concepts of overfitting and underfitting, consider the behaviors
of three different students A, B, and C each with distinct learning styles and test performances.
This scenario mirrors overfitting in machine learning. An overfit model performs very well
on the training data but fails to generalize to new, unseen data. Student B, like an overfit model,
performs perfectly in familiar environments (class tests) but falters when confronted with
variation.
This behavior reflects a well-generalized model in machine learning — one that balances
bias and variance and performs reliably on both training and testing data. Student C’s learning
strategy leads to true comprehension, much like a model that has learned the real structure of
the data.
Key Takeaways