0% found this document useful (0 votes)
3 views17 pages

UNIT-4 ( (A) ML Algorithm-Classification)

This document provides an overview of Machine Learning (ML) for Data Analytics, focusing on supervised and unsupervised learning techniques. It explains key algorithms such as Decision Trees, k-Nearest Neighbors (k-NN), and Naive Bayes, along with their applications and advantages. The document also includes Python code examples to illustrate how these algorithms can be implemented for classification tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

UNIT-4 ( (A) ML Algorithm-Classification)

This document provides an overview of Machine Learning (ML) for Data Analytics, focusing on supervised and unsupervised learning techniques. It explains key algorithms such as Decision Trees, k-Nearest Neighbors (k-NN), and Naive Bayes, along with their applications and advantages. The document also includes Python code examples to illustrate how these algorithms can be implemented for classification tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-3

Topics to cover are:


Machine Learning for Data Analytics
1. Introduction
o Supervised Learning

o Unsupervised Learning

1. Introduction to Machine Learning(ML)


ML is a method where computers learn patterns from data and make
decisions or predictions without being explicitly programmed. In simple
words:
➔Instead of writing rules, we train the computer using data.
Machine learning helps to:
• Predict future outcomes
• Find hidden patterns in data
• Automate decision making
• Improve accuracy using past data
Simple Real-Life Example: Suppose we give a computer student marks
and whether they passed or failed.

Marks Result
30 Fail
40 Pass
75 Pass
20 Fail

1
The computer learns the pattern and can predict whether a new student
will pass or fail.
Types of Machine Learning
There are two main types used in data analytics.
(a) Supervised Learning
(b) Unsupervised Learning

2. Supervised Learning
Supervised Learning is a ML technique where the model learns from
labeled data.
Labeled data means: Input data with correct output already known.
Here, the model learns a relationship between: Input → Output
Then it predicts the output for new data.
Example: Predict whether a student will Pass or Fail based on marks.

Marks Result
30 Fail
45 Pass
80 Pass
25 Fail

Marks → Input
Result → Output
The model learns the pattern.

2
Simple Python Program (Supervised Learning)
from [Link] import DecisionTreeClassifier // Imports the
Decision Tree model.

# Training data
marks = [[30], [45], [80], [25]] // Input values (student marks).
result = ['Fail', 'Pass', 'Pass', 'Fail'] // Correct output labels
# Create model
model = DecisionTreeClassifier() // Creates the ML model.
# Train model
[Link](marks, result) // Trains the model using the data.

# Predict new student


prediction = [Link]([[50]]) // Predicts result for 50 marks
print("Prediction:", prediction) // Displays the prediction.
Output: Prediction: ['Pass']

3. Unsupervised Learning
Unsupervised Learning is a MLtechnique where the model learns from
data without labeled outputs.
- There is no correct answer given.
- The model finds patterns or groups in data automatically.
Instead of predicting output, the algorithm:
• Finds similar groups
• Detects patterns
• Organizes data

3
Example: Suppose we have shopping customers data.

Customer Spending
A High
B High
C Low
D Low

The algorithm may create two groups:


Group 1 → High spenders
Group 2 → Low spenders
Python Program (Unsupervised Learning)
from [Link] import KMeans // Imports K-Means clustering algo
import numpy as np // Used to create numeric data.

data = [Link]([[100], [120], [20], [30]]) // Customer spending values.

# Create model
model = KMeans(n_clusters=2) // Create 2 clusters (groups).
# Train model
[Link](data) // Train the clustering model.
print("Cluster labels:", model.labels_) // Shows which cluster each data point belongs to.

Output: Cluster labels: [1 1 0 0]

4
Difference Between Supervised and Unsupervised Learning

Feature Supervised Unsupervised


Data Labeled Unlabeled
Goal Predict output Find patterns
Example Pass/Fail prediction Customer grouping

Algorithms Decision Tree, k-NN, Naive Bayes K-Means, Hierarchical

In summary, ML allows computers to learn from data.


Two main types:
Supervised Learning : Learns using labeled data to make predictions. Ex.
Classification
Unsupervised Learning : Finds hidden patterns or groups in unlabeled
data. Ex. Clustering

2. Classification Models
o Decision Trees

o k-Nearest Neighbors (k-NN)

o Naive Bayes

3. Classification Model:
(A) Decision Tree
A Decision Tree is a supervised ML algorithm used for classification and
prediction.
It works like a tree of decisions, where data is split into branches based on
conditions.
- Each branch represents a decision rule.
5
- Each leaf node represents the final prediction or class.
A Decision Tree asks a series of questions about the data.
Example:

The model learns these decision rules from training data.


Example Dataset: Predict Pass or Fail based on marks.

Marks Result
30 Fail
45 Pass
50 Pass
20 Fail
70 Pass

Marks → Input feature


Result → Output class
Python Program
from [Link] import DecisionTreeClassifier // Imports the DT
algorithm from sklearn library.

6
# Training data (marks)
X = [[30], [45], [50], [20], [70]]

# Correct output labels (Pass/Fail).


y = ['Fail', 'Pass', 'Pass', 'Fail', 'Pass']

# Create decision tree model


model = DecisionTreeClassifier()

# Train the model using the data


[Link](X, y) The model learns rules such as:
Marks < 40 → Fail
Marks ≥ 40 → Pass
# Predict result for new student with 40 marks.
prediction = [Link]([[40]])

print("Prediction:", prediction) // Displays the prediction result.


Output: Prediction: ['Pass']
-----------------------------------------
Real-Life Applications: Decision Trees are used in:
Medical diagnosis , Loan approval prediction, Email spam detection,
Student performance prediction, Fraud detection
Advantages
Easy to understand, Works with small datasets, Visual decision structure,
Requires little data preparation
In short,

7
A Decision Tree is a classification model that makes predictions using a
tree-like structure of decision rules.
- It splits data step-by-step until it reaches a final decision.

(B) k-Nearest Neighbors (k-NN)


k-Nearest Neighbors (k-NN) is a supervised ML algorithm used for
classification and prediction.
It works by finding the nearest data points (neighbors) to a new data
point and assigning the most common class among them.
➔k means the number of neighbors considered.
Explanation: The algorithm follows these steps:
1. Choose a value of k (number of neighbors).
2. Measure the distance between the new data point and all existing data
points.
3. Select the k closest neighbors.
4. Assign the most common class among those neighbors.
So the prediction is based on similar data points.
Example Dataset: Predict whether a student Passes or Fails based on
marks.

Marks Result
30 Fail
35 Fail
50 Pass
60 Pass
70 Pass

8
If a new student has 40 marks, the algorithm looks at the nearest marks
and decides the class.
Python Program
from [Link] import KNeighborsClassifier // Imports the k-nn algo

# Training data (marks)


X = [[30], [35], [50], [60], [70]]

# Output labels (Pass or Fail).


y = ['Fail', 'Fail', 'Pass', 'Pass', 'Pass']

# Create KNN model (k = 3 neighbors=> algorithm checks 3 nearest data points)

model = KNeighborsClassifier(n_neighbors=3)

# Train the model using the training data


[Link](X, y)

# Predict result for new student scoring 40 marks.


prediction = [Link]([[40]])
The algorithm finds the 3 closest marks.
Example nearest neighbors:
Marks Result
35 Fail
30 Fail
50 Pass
Majority class = Fail

9
print("Prediction:", prediction) // Displays the prediction.
Output: Prediction: ['Fail']
------------------------------------------------------------
Real-Life Applications
k-NN is used in:
Recommendation systems , Image recognition , Medical diagnosis, Pattern
recognition, Credit risk analysis
Advantages
- Simple and easy to understand, No training phase required,
- Works well with small datasets, Effective for classification problems
In short, k-NN classifies a new data point by looking at the nearest data
points and assigning the majority class among them.

(C) Naive Bayes


Naive Bayes is a supervised ML algorithm used for classification problems.
It is based on Bayes’ Theorem and assumes that the features are
independent of each other.
- It calculates the probability of each class and chooses the class with
the highest probability.
- Naive Bayes works using probability.
It answers the question: Given some features, what is the probability that
the data belongs to a particular class?

10
For example: Email → Spam or Not Spam
The algorithm calculates probability and predicts the most likely class.
Example Dataset: Predict whether an email is Spam or Not Spam based
on the number of links.

Links in Email Type


1 Not Spam
2 Not Spam
5 Spam
6 Spam
7 Spam

Now suppose a new email has 4 links.


The algorithm will calculate probabilities and classify it.
Python Program
from sklearn.naive_bayes import GaussianNB // Imports Naive Bayes algo
import numpy as np

# Training data (number of links in emails)


X = [Link]([[1], [2], [5], [6], [7]])

# Output labels for emails


y = [Link](['Not Spam', 'Not Spam', 'Spam', 'Spam', 'Spam'])

# Create the Naive Bayes model


model = GaussianNB()

# Train the model using the data


[Link](X, y)
11
# Predicts whether an email with 4 links is Spam or Not Spam.
prediction = [Link]([[4]])

print("Prediction:", prediction) // Displays the predicted result.


Output: Prediction: ['Spam']
-----------------------------------------------------------
Real-Life Applications: Naive Bayes is widely used in:
• Email spam detection, Text classification, Sentiment analysis
• Document categorization, Recommendation systems
Advantages
• Very fast algorithm, Works well with large datasets
• Simple probability-based method, Good for text classification
In simple words:
Naive Bayes calculates probability for each class and the class with
highest probability becomes the prediction.
Another Example
Example: Dataset

Weather Play
Sunny No
Sunny No
Overcast Yes
Rain Yes
Rain Yes
Sunny Yes

12
Total records = 6
New Day → Weather = Sunny
Predict → Play = Yes or No
Step 1: Class Probability
Play = Yes, Yes records = 4
𝑃(𝑌𝑒𝑠) = 4/6
= 0.67
Play = No
No records = 2 𝑃(𝑁𝑜) = 2/6
= 0.33

Step 2: Conditional Probability


Sunny when Play = Yes
Sunny & Yes = 1
Total Yes = 4
𝑃(𝑆𝑢𝑛𝑛𝑦 ∣ 𝑌𝑒𝑠) = 1/4
= 0.25
Sunny when Play = No
Sunny & No = 2
Total No = 2
𝑃(𝑆𝑢𝑛𝑛𝑦 ∣ 𝑁𝑜) = 2/2
=1

13
Step 3: Naive Bayes Formula
𝑃( 𝐶𝑙𝑎𝑠𝑠 ∣ 𝐷𝑎𝑡𝑎 ) ∝ 𝑃(𝐷𝑎𝑡𝑎 ∣ 𝐶𝑙𝑎𝑠𝑠) × 𝑃(𝐶𝑙𝑎𝑠𝑠)

Step 4: Probability Calculation


For Play = Yes
𝑃(𝑌𝑒𝑠 ∣ 𝑆𝑢𝑛𝑛𝑦)
= 𝑃(𝑆𝑢𝑛𝑛𝑦 ∣ 𝑌𝑒𝑠) × 𝑃(𝑌𝑒𝑠)
= 0.25 × 0.67
≈ 0.17

For Play = No
𝑃(𝑁𝑜 ∣ 𝑆𝑢𝑛𝑛𝑦)
= 𝑃(𝑆𝑢𝑛𝑛𝑦 ∣ 𝑁𝑜) × 𝑃(𝑁𝑜)
= 1 × 0.33
= 0.33

Step 5: Compare Probabilities

Class Probability
Play = Yes 0.17
Play = No 0.33

Final Prediction: 0.33 > 0.17, therefore , Prediction →


Play = No

14
We found, NO appeared more times in Sunny weather , so model
predict : Play = No

All 3 in 1 python program


• Decision Tree
• k-Nearest Neighbors (k-NN)
• Naive Bayes
Example: Predict whether a student will Pass or Fail based on Study
Hours and Attendance

Dataset

Study Hours Attendance Result


1 40 Fail
2 45 Fail
3 50 Fail
5 60 Pass
6 65 Pass
7 70 Pass

New Student:
Study Hours = 4
Attendance = 55
Predict → Pass or Fail
Python Program

15
from [Link] import DecisionTreeClassifier // imported DT algo
from [Link] import KNeighborsClassifier // imported k-NN algo
from sklearn.naive_bayes import GaussianNB // imported Naive Bayes algo

# Dataset or features : Study hours and Attendance


X=[
[1,40],
[2,45],
[3,50],
[5,60],
[6,65],
[7,70]
]

y = ["Fail","Fail","Fail","Pass","Pass","Pass"] // target class

# New data: student whose result need to predict


new_student = [[4,55]]

# Decision Tree
dt = DecisionTreeClassifier()
[Link](X,y)
dt_pred = [Link](new_student)

# KNN
knn = KNeighborsClassifier(n_neighbors=3)
[Link](X,y)
knn_pred = [Link](new_student)

# Naive Bayes
nb = GaussianNB()
[Link](X,y)

16
nb_pred = [Link](new_student)

print("Decision Tree Prediction:", dt_pred)


print("KNN Prediction:", knn_pred)
print("Naive Bayes Prediction:", nb_pred)
Output
Decision Tree Prediction: ['Pass']
KNN Prediction: ['Pass']
Naive Bayes Prediction: ['Pass']
------------------------------
Thus,

Algorithm Idea
Make decision by
Decision Tree
making Rule
Predicts by taking
k-NN
nearest data points
prediction based on
Naive Bayes
probability

17

You might also like