Unit 3
1) What is Machine Learning?Explain the Modeling Process in Machine Learning. What are its key steps?
1. What is Machine Learning?
Machine Learning (ML) is a branch of artificial intelligence (AI) that enables computers to learn from data and make predictions or decisions without explicit programming. It uses algorithms to identify patterns and improve performance over time.
✅ Types of Machine Learning:
- Supervised Learning → Uses labeled data (e.g., Spam detection, Fraud detection).
- Unsupervised Learning → Works with unlabeled data (e.g., Customer segmentation, Anomaly detection).
- Reinforcement Learning → Learns by rewards and penalties (e.g., Robotics, Game playing).
2. The Machine Learning Modeling Process
The ML modeling process involves key steps that guide the development of an effective predictive model.
Key Steps in the Machine Learning Modeling Process
Step 1: Problem Definition
- Identify the business objective or problem to be solved.
- Example: Predicting customer churn in a telecom company.
Step 2: Data Collection
- Gather relevant structured (e.g., databases, spreadsheets) and unstructured (e.g., text, images) data.
- Sources: Databases, APIs, Web scraping.
Step 3: Data Preprocessing
- Handling missing values → Fill or remove missing data.
- Removing duplicates → Clean redundant records.
- Feature selection → Choose relevant variables.
- Feature scaling → Normalize/standardize data for consistency.
Step 4: Exploratory Data Analysis (EDA)
- Visualize the data using graphs, histograms, and scatter plots.
- Check data distribution and identify outliers or patterns.
Step 5: Model Selection
- Choose an appropriate ML algorithm:
- Regression → Predict continuous values (e.g., House prices).
- Classification → Predict categories (e.g., Spam vs. Non-spam).
- Clustering → Group similar data points (e.g., Customer Segmentation).
Step 6: Model Training
- Split the dataset into Training Set (for learning) and Test Set (for evaluation).
- Train the model using training data.
Step 7: Model Evaluation
- Test model performance using metrics:
- Accuracy, Precision, Recall, F1-score for classification.
- Mean Squared Error (MSE), R²-score for regression.
- Perform cross-validation to ensure generalization.
Step 8: Hyperparameter Tuning
- Optimize model parameters using techniques like:
- Grid Search → Testing multiple parameter combinations.
- Random Search → Selecting random parameters.
- Bayesian Optimization → More efficient parameter search.
Step 9: Model Deployment
- Deploy the trained model in a real-world environment using:
- APIs, Cloud Services, or Edge Devices.
Step 10: Model Monitoring & Maintenance
- Continuously monitor model performance.
- Update the model if accuracy drops (due to data drift).
3. Summary of Key Steps in Machine Learning
Step | Description |
---|---|
1. Problem Definition | Define the problem and objective. |
2. Data Collection | Gather structured & unstructured data. |
3. Data Preprocessing | Clean, normalize, and transform data. |
4. Exploratory Data Analysis (EDA) | Visualize data, check distribution. |
5. Model Selection | Choose ML algorithm based on task. |
6. Model Training | Train the model on the dataset. |
7. Model Evaluation | Test model performance using metrics. |
8. Hyperparameter Tuning | Optimize model parameters. |
9. Model Deployment | Deploy the model for real use. |
10. Monitoring & Maintenance | Continuously track model performance. |
4. Conclusion
The Machine Learning modeling process follows a systematic approach: from defining the problem, preparing data, selecting and training models, and deploying them in real-world applications. It is an iterative process, as models often require fine-tuning and continuous improvement over time.
2) Explain the following key terminologies in Machine Learning: Features, Target, Training Data, Testing Data, Overfitting, and Underfitting.
Key Terminologies in Machine Learning
1. Features
- Definition: Features are the input variables used to train a machine learning model. They represent different characteristics of the data.
- Example: In a house price prediction model, features could be size (sq. ft.), number of bedrooms, location, and age of the house.
2. Target (Label)
- Definition: The target (or label) is the output variable the model aims to predict.
- Example: In a house price prediction model, the target is the house price.
3. Training Data
- Definition: The dataset used to train the machine learning model. It contains both features (inputs) and target (outputs) so that the model can learn patterns.
- Example: If we have 1,000 houses, we might use 800 houses for training.
4. Testing Data
- Definition: A separate dataset used to evaluate the trained model’s performance. It contains features but is not seen by the model during training.
- Example: The remaining 200 houses from our dataset will be used for testing.
5. Overfitting
- Definition: When a model learns too much from training data, capturing noise and unnecessary details, making it perform poorly on unseen data.
- Cause: Too complex models, too many features, or small training data.
- Example: A decision tree memorizing training examples instead of generalizing patterns.
✅ Solution: Use regularization, more training data, and simpler models.
6. Underfitting
- Definition: When a model is too simple and fails to learn patterns, leading to poor performance on both training and testing data.
- Cause: Not enough training, choosing a model that is too simple, or missing key features.
- Example: A linear regression model trying to predict complex, non-linear data.
✅ Solution: Use a more complex model, feature engineering, and longer training.
Summary Table
Term | Definition |
---|---|
Features | Input variables used to train the model. |
Target | The output variable the model predicts. |
Training Data | Dataset used to train the model. |
Testing Data | Dataset used to evaluate the model’s performance. |
Overfitting | The model memorizes training data and performs poorly on new data. |
Underfitting | The model is too simple and fails to learn patterns. |
By understanding these concepts, we can improve model performance and generalization in machine learning. 🚀
3) What are the four main phases in the machine learning modeling process?
Four Main Phases in the Machine Learning Modeling Process
The Machine Learning Modeling Process consists of four key phases, which guide the development of an effective model.
1. Data Preparation Phase
✅ Goal: Collect, clean, and preprocess data to ensure it is ready for modeling.
🔹 Key Steps:
- Data Collection → Gather raw data from sources (databases, APIs, etc.).
- Data Cleaning → Handle missing values, remove duplicates, and correct errors.
- Feature Engineering → Create new relevant features, select important variables.
- Data Splitting → Divide into Training Set (for learning) and Testing Set (for evaluation).
📌 Example: Preparing customer purchase data for a recommendation system.
2. Model Training Phase
✅ Goal: Select and train the best machine learning model.
🔹 Key Steps:
- Choose a Model → Select an algorithm (e.g., Decision Trees, Neural Networks).
- Train the Model → Feed training data to the model to learn patterns.
- Hyperparameter Tuning → Optimize parameters for better accuracy.
📌 Example: Training a fraud detection model on past transactions.
3. Model Evaluation Phase
✅ Goal: Assess the model’s performance on unseen data.
🔹 Key Steps:
- Use Testing Data → Evaluate model performance on new data.
- Performance Metrics:
- Classification Models: Accuracy, Precision, Recall, F1-score.
- Regression Models: Mean Squared Error (MSE), R²-score.
- Cross-Validation → Ensures the model generalizes well.
📌 Example: Testing a spam detection model with real emails.
4. Model Deployment & Monitoring Phase
✅ Goal: Deploy the trained model for real-world use and monitor its performance.
🔹 Key Steps:
- Deployment Methods: APIs, Cloud, Mobile, or Edge devices.
- Performance Tracking: Monitor accuracy over time, handle model drift.
- Retraining: Update the model with new data if accuracy drops.
📌 Example: Deploying a chatbot for customer support.
Summary Table
Phase | Key Steps | Example |
---|---|---|
1. Data Preparation | Collect, clean, preprocess, and split data. | Preparing purchase data for a recommendation system. |
2. Model Training | Select algorithm, train, and tune parameters. | Training a fraud detection model. |
3. Model Evaluation | Test on new data, measure accuracy, cross-validate. | Checking a spam filter’s accuracy. |
4. Deployment & Monitoring | Deploy, track, and update models. | Deploying a self-driving car model. |
Final Thoughts
These four phases ensure a structured and efficient approach to building machine learning models, leading to better performance and real-world applicability. 🚀
4) What is the role of feature engineering in Machine Learning?
Role of Feature Engineering in Machine Learning
✅ Feature Engineering is the process of creating, transforming, or selecting relevant features from raw data to improve a machine learning model’s performance. It plays a critical role in making the model more accurate and efficient.
Why is Feature Engineering Important?
- Improves Model Accuracy → Better features help the model make better predictions.
- Reduces Overfitting → Removing irrelevant features prevents memorization.
- Handles Data Complexity → Converts raw data into a meaningful format.
- Boosts Model Efficiency → Reduces computational cost by keeping only useful features.
Key Feature Engineering Techniques
1️⃣ Feature Creation
- New Features: Combine existing features to create more informative ones.
- 📌 Example: In a customer purchase dataset, create a feature *“Total Spent = Price × Quantity”.
- Polynomial Features: Add squared/cubic versions of features for better learning.
2️⃣ Feature Transformation
- Scaling (Normalization/Standardization): Ensures all features have a similar scale.
- 📌 Example: Convert income data from dollars to a 0-1 range for neural networks.
- Log Transform: Reduces the impact of extreme values.
- 📌 Example: Transform skewed salary data using log(salary).
3️⃣ Feature Selection
- Remove Irrelevant Features: Keep only meaningful variables.
- 📌 Example: In house price prediction, “house color” may not matter.
- Statistical Tests (Chi-Square, ANOVA, etc.): Find the most important features.
4️⃣ Encoding Categorical Variables
- One-Hot Encoding: Converts categories into numeric format.
- 📌 Example: Convert City = {New York, Paris, Tokyo} into binary columns.
- Label Encoding: Assigns a unique number to each category.
Impact of Feature Engineering on Model Performance
🚀 Good Features → Simpler & More Accurate Model
📉 Poor Features → Overfitting, Underfitting & Low Accuracy
Example: Predicting house prices 🔥
Without Feature Engineering | With Feature Engineering |
---|---|
Features: Area, Location | Features: Area, Location, Price/Sq. Ft., Age, No. of Rooms |
Poor accuracy | Better predictions |
Conclusion
Feature Engineering is a crucial step in machine learning that directly impacts model accuracy and efficiency. Well-engineered features can make simple models perform better than complex ones trained on raw data! 🚀
5) What is the role of model training in machine learning? What is its significance?
Role of Model Training in Machine Learning
✅ Model training is the process where a machine learning algorithm learns patterns from data by adjusting its parameters to make accurate predictions. This phase is crucial in building an effective model that can generalize well to unseen data.
Key Steps in Model Training
1️⃣ Select a Machine Learning Algorithm
- Choose an appropriate model (e.g., Linear Regression, Decision Trees, Neural Networks).
- Example: Using Logistic Regression for spam email classification.
2️⃣ Provide Training Data
- Use labeled data (features + target) for supervised learning.
- Example: Training a fraud detection model using past transactions.
3️⃣ Learn Patterns from Data
- The model adjusts its internal parameters (e.g., weights in neural networks) to minimize error.
- Uses optimization algorithms like Gradient Descent to improve predictions.
4️⃣ Evaluate Model Performance
- Split data into training and validation sets to check if the model is learning correctly.
- Example: If the model overfits, regularization techniques like Lasso, Ridge Regression are applied.
Significance of Model Training in ML
✅ Improves Accuracy → Trained models make better predictions.
✅ Enables Generalization → Learns underlying patterns, not just memorization.
✅ Adapts to Different Problems → Works for classification, regression, clustering, etc.
✅ Prepares for Real-World Deployment → A well-trained model can be used in applications like fraud detection, self-driving cars, and recommendation systems.
📌 Example: Training a speech recognition model helps it learn voice patterns, enabling virtual assistants like Alexa or Siri to understand commands.
Final Thoughts
Model training is the core of machine learning, enabling models to learn from data, improve over time, and make accurate decisions in real-world scenarios. 🚀
6) What is model selection and validation, and why is it important in machine learning? How is model scoring used to assess the effectiveness of a machine learning model?
Model Selection and Validation in Machine Learning
✅ Model Selection refers to choosing the best machine learning model based on performance metrics. Different models (e.g., Decision Trees, Neural Networks, SVMs) may perform differently on a given dataset.
✅ Model Validation ensures the selected model generalizes well to unseen data by evaluating its performance using techniques like cross-validation and test sets.
Why is Model Selection and Validation Important?
✅ Prevents Overfitting & Underfitting → Ensures the model generalizes well.
✅ Improves Prediction Accuracy → Helps pick the best-performing model.
✅ Optimizes Model Complexity → Balances between simple and complex models.
✅ Ensures Reliability → A well-validated model performs consistently on new data.
📌 Example:
A fraud detection system needs to maximize accuracy while minimizing false positives, requiring careful model selection and validation.
How is Model Scoring Used to Assess Model Effectiveness?
Model scoring evaluates the effectiveness of a machine learning model using performance metrics. The choice of metric depends on the task:
Model Type | Performance Metrics |
---|---|
Classification Models | Accuracy, Precision, Recall, F1-score, ROC-AUC |
Regression Models | Mean Squared Error (MSE), Mean Absolute Error (MAE), R²-score |
Clustering Models | Silhouette Score, Davies-Bouldin Index |
📌 Example:
A spam email classifier is evaluated using Precision and Recall to balance spam detection and false positives.
Final Thoughts
Model selection & validation ensure that we choose the best model for the task, while model scoring helps measure its effectiveness. 🚀
7) What are some key methods for validating a Machine Learning model?
Key Methods for Validating a Machine Learning Model
Model validation ensures that a machine learning model generalizes well to new, unseen data. Below are some commonly used validation techniques:
1. Train-Test Split
✅ Splits the dataset into two parts:
- Training Set (e.g., 80%) → Used to train the model.
- Testing Set (e.g., 20%) → Used to evaluate the model’s performance.
📌 Example: A fraud detection model is trained on 80% of transactions and tested on 20% unseen data.
🔹 Pros: Simple, fast.
🔹 Cons: The model might overfit if the test set is too small.
2. Cross-Validation (K-Fold Cross-Validation)
✅ Splits the data into K subsets (“folds”) and trains the model multiple times:
- The model is trained on folds.
- The remaining 1 fold is used for validation.
- This process repeats times, with each fold used once as the test set.
📌 Example: If , the dataset is split into 5 parts, and the model is trained 5 times using different test sets.
🔹 Pros: More reliable than train-test split.
🔹 Cons: Computationally expensive for large datasets.
3. Stratified K-Fold Cross-Validation
✅ A variation of K-Fold Cross-Validation that ensures each fold has the same proportion of each class in classification problems.
📌 Example: If 30% of emails in a spam filter dataset are spam, each fold will maintain the 30% spam ratio.
🔹 Pros: Works well for imbalanced datasets.
🔹 Cons: Slightly more complex than standard K-Fold.
4. Leave-One-Out Cross-Validation (LOOCV)
✅ A special case of K-Fold where (number of samples).
- Each data point is used once as a test set, while the rest are used for training.
📌 Example: If there are 100 samples, the model is trained 99 times, leaving 1 sample for testing each time.
🔹 Pros: Uses maximum data for training.
🔹 Cons: Computationally expensive for large datasets.
5. Holdout Validation
✅ A simpler version of Train-Test Split where a portion of the dataset is never used for training and is held out for final evaluation.
📌 Example: A 20% holdout set is kept aside while tuning models on the remaining 80%.
🔹 Pros: Useful for final model testing.
🔹 Cons: Does not help in model selection.
6. Bootstrapping
✅ Random sampling with replacement is used to generate multiple datasets, training the model on each sample.
📌 Example: Used in Random Forests, where each decision tree is trained on different random subsets of data.
🔹 Pros: Works well for small datasets.
🔹 Cons: More variance in results.
Final Thoughts
Validation Method | Best For |
---|---|
Train-Test Split | Quick evaluation |
K-Fold Cross-Validation | Reliable performance assessment |
Stratified K-Fold | Imbalanced classification problems |
Leave-One-Out Cross-Validation (LOOCV) | Small datasets |
Holdout Validation | Final evaluation |
Bootstrapping | Small datasets & ensemble models |
Using cross-validation techniques ensures that the model generalizes well and prevents issues like overfitting. 🚀
8) How does a trained model make predictions on new observations?
How Does a Trained Model Make Predictions on New Observations?
Once a machine learning model is trained, it can predict outcomes for new, unseen data by applying the patterns it has learned. The prediction process follows these key steps:
1. Input Features from New Observations
- The new data (observation) is provided to the model as feature values (same format as the training data).
- Example: If predicting house prices, the input could be:
2. Apply the Learned Model Parameters
-
The trained model applies its learned patterns, weights, or decision rules to the input.
-
The method depends on the model type:
For a Linear Regression model:
The model multiplies feature values by the learned weights and adds a bias.
For a Decision Tree:
- The new observation follows a series of “if-else” rules learned during training.
- Example: If Size > 1400, then predict price = High.
For a Neural Network:
- The input is passed through layers of neurons and transformed through activations like ReLU or Sigmoid.
3. Compute the Output (Prediction)
-
Based on the processed input, the model generates a prediction.
-
The output depends on the type of machine learning task:
ML Task Example Model Output Regression Predict house price A continuous number (e.g., $250,000) Classification Classify email as spam or not A probability score (e.g., 0.85 Spam) Clustering Group customers Cluster label (e.g., Customer Type A)
4. Post-Processing & Decision Making
- For classification models, if the model outputs probabilities, they can be converted into class labels using a threshold (e.g., Spam if probability > 0.5).
- For regression models, post-processing can include rounding values or applying business rules.
📌 Example: A credit risk model assigns a risk score → The bank decides whether to approve or deny a loan.
Final Thoughts
✅ A trained model makes predictions by taking new input, applying learned patterns, and generating an output.
✅ The accuracy of predictions depends on data quality, model training, and feature selection.
✅ Continuous monitoring ensures the model adapts to new patterns in real-world data. 🚀
9) List out and explain the role of various Python tools used in machine learning for data science.
Python Tools for Machine Learning in Data Science
Python provides a rich ecosystem of libraries and tools for data science and machine learning. Below are some of the most commonly used ones:
1. NumPy
📌 Role:
- Handles numerical computations efficiently.
- Supports arrays, matrices, and mathematical functions needed for ML models.
✅ Example Use:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(np.mean(arr)) # Calculate mean
2. Pandas
📌 Role:
- Used for data manipulation, cleaning, and analysis.
- Works with DataFrames (similar to Excel tables).
✅ Example Use:
import pandas as pd
df = pd.read_csv("data.csv") # Load a dataset
print(df.head()) # View first 5 rows
3. Matplotlib & Seaborn
📌 Role:
- Used for data visualization to understand patterns and trends.
- Matplotlib → Basic plots (histograms, line charts).
- Seaborn → Advanced visualizations (heatmaps, violin plots).
✅ Example Use:
import matplotlib.pyplot as plt
import seaborn as sns
sns.histplot(df['age'], kde=True) # Histogram with KDE
plt.show()
4. Scikit-learn
📌 Role:
- Most popular machine learning library for Python.
- Provides tools for classification, regression, clustering, feature selection, and evaluation metrics.
✅ Example Use:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
X = df[['feature1', 'feature2']]
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train) # Train the model
print(model.predict(X_test)) # Make predictions
5. TensorFlow & Keras
📌 Role:
- Used for deep learning and neural networks.
- TensorFlow provides computational power.
- Keras is a high-level API that makes building models easier.
✅ Example Use:
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
6. XGBoost & LightGBM
📌 Role:
- Used for high-performance gradient boosting in classification and regression tasks.
- XGBoost is widely used in Kaggle competitions due to its speed and accuracy.
✅ Example Use:
import xgboost as xgb
model = xgb.XGBClassifier()
model.fit(X_train, y_train)
7. OpenCV
📌 Role:
- Used for image processing and computer vision.
- Detects faces, objects, and performs real-time image analysis.
✅ Example Use:
import cv2
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray_image)
cv2.waitKey(0)
8. NLTK & SpaCy
📌 Role:
- Used for Natural Language Processing (NLP).
- NLTK is for text preprocessing, sentiment analysis.
- SpaCy is for advanced NLP (tokenization, named entity recognition).
✅ Example Use:
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp("Machine Learning is fascinating!")
for token in doc:
print(token.text, token.pos_)
9. PyTorch
📌 Role:
- An alternative to TensorFlow for deep learning.
- Used in AI research and computer vision tasks.
✅ Example Use:
import torch
x = torch.tensor([1.0, 2.0, 3.0])
print(x)
10. Statsmodels
📌 Role:
- Used for statistical analysis and regression modeling.
✅ Example Use:
import statsmodels.api as sm
X = sm.add_constant(X_train) # Add intercept
model = sm.OLS(y_train, X).fit()
print(model.summary())
Summary Table
Python Tool | Primary Use |
---|---|
NumPy | Numerical computing (arrays, matrices). |
Pandas | Data manipulation and analysis. |
Matplotlib & Seaborn | Data visualization. |
Scikit-learn | Machine learning algorithms. |
TensorFlow & Keras | Deep learning & neural networks. |
XGBoost & LightGBM | Gradient boosting models. |
OpenCV | Computer vision and image processing. |
NLTK & SpaCy | Natural Language Processing (NLP). |
PyTorch | Deep learning, research. |
Statsmodels | Statistical analysis & regression. |
Final Thoughts
Python’s ecosystem provides powerful tools for data preprocessing, model training, evaluation, and deployment in machine learning. 🚀 Each library has a specific role, and using them effectively enhances the overall data science workflow.
10) What is Supervised Learning? How does it work? Explain the differences between Regression and Classification in Supervised Learning.
What is Supervised Learning?
✅ Supervised Learning is a type of Machine Learning (ML) where a model is trained on labeled data—meaning each input data point has a corresponding output (target). The goal is for the model to learn patterns and make predictions on new, unseen data.
📌 Example:
- Spam Detection: Emails (input) labeled as spam or not spam (output).
- House Price Prediction: Features like size, location (input) predict price (output).
How Does Supervised Learning Work?
Supervised Learning follows these key steps:
1️⃣ Collect Labeled Data:
- Dataset includes features (X) and labels (Y).
- Example: In a medical diagnosis dataset, symptoms (X) map to a disease (Y).
2️⃣ Split Data into Training & Testing Sets:
- Training Set (80%) → Used to teach the model.
- Testing Set (20%) → Used to evaluate performance.
3️⃣ Choose an Algorithm:
- Regression (if output is continuous) → Linear Regression, Decision Trees
- Classification (if output is categorical) → Logistic Regression, Random Forest
4️⃣ Train the Model:
- The model learns patterns from the training data by minimizing errors.
- Uses optimization techniques like Gradient Descent.
5️⃣ Evaluate & Fine-Tune:
- Test on unseen data, adjust hyperparameters, and improve accuracy.
Regression vs. Classification in Supervised Learning
Feature | Regression | Classification |
---|---|---|
Definition | Predicts continuous values | Predicts categorical labels |
Output Type | Continuous (e.g., 0 to 100) | Discrete categories (e.g., Yes/No) |
Examples | Predicting house prices, stock prices | Spam detection, Disease diagnosis |
Common Algorithms | Linear Regression, Decision Trees, Neural Networks | Logistic Regression, Random Forest, SVM |
Evaluation Metrics | Mean Squared Error (MSE), R²-score | Accuracy, Precision, Recall, F1-score |
📌 Example:
- Regression: Predicting a car’s resale value (25,000, etc.).
- Classification: Classifying whether an email is Spam or Not Spam.
Final Thoughts
✅ Supervised Learning is widely used in finance, healthcare, marketing, and more.
✅ Regression is for predicting numbers, while Classification is for categorizing data.
✅ Choosing the right model depends on the type of problem and available data. 🚀
11) What is a Naïve Bayes classifier? Explain it in the context of the case study on handwritten digit recognition.
What is a Naïve Bayes Classifier?
✅ Naïve Bayes is a probabilistic classification algorithm based on Bayes’ Theorem, assuming that features are independent given the class label. It is widely used in text classification, spam filtering, sentiment analysis, and handwritten digit recognition.
📌 Bayes’ Theorem:
where:
- → Probability of class given input (Posterior Probability)
- → Likelihood of input given class
- → Prior probability of class
- → Overall probability of input
“Naïve” Assumption: All features are independent, making computation simpler.
Naïve Bayes for Handwritten Digit Recognition
Handwritten digit recognition is a classification problem where the goal is to classify handwritten digits (0-9) based on pixel values.
🔹 Step 1: Dataset Preparation
- Dataset: MNIST (Modified National Institute of Standards and Technology) contains 28×28 grayscale images of handwritten digits.
- Features: Each pixel’s intensity (0 to 255) is treated as a feature.
- Labels: The digits (0 to 9) are the classes.
🔹 Step 2: Training the Naïve Bayes Model
- Convert grayscale images into binary values (0 for white, 1 for black) to simplify the model.
- Compute probabilities for each pixel in different digits using Bayes’ theorem.
- Assume each pixel is independent (Naïve assumption).
🔹 Step 3: Making Predictions
- For a new digit image, the model calculates the probability of it belonging to each digit (0-9).
- It assigns the digit with the highest probability as the predicted class.
Advantages of Naïve Bayes in Digit Recognition
✅ Fast & Efficient: Works well even with large datasets like MNIST.
✅ Low Computational Cost: Requires fewer resources than deep learning models.
✅ Handles Noisy Data: Performs well despite minor variations in handwriting.
📌 Example:
If a new handwritten digit is fed into the model, and it finds:
Then, the model classifies it as digit 3, since it has the highest probability.
Final Thoughts
✅ Naïve Bayes is a simple but effective classifier for handwritten digit recognition.
✅ Though deep learning (CNNs) outperforms it, Naïve Bayes remains a fast and interpretable choice for classification problems. 🚀
12) What is Unsupervised Learning? How does it differ from Supervised Learning?
What is Unsupervised Learning?
✅ Unsupervised Learning is a type of Machine Learning (ML) where the model learns patterns from unlabeled data. Unlike Supervised Learning, there are no predefined outputs (labels). The model identifies hidden structures, relationships, or groupings within the data.
📌 Examples:
- Customer Segmentation: Grouping customers based on purchasing behavior.
- Anomaly Detection: Identifying fraudulent transactions in banking.
- Dimensionality Reduction: Reducing high-dimensional data for visualization.
How Unsupervised Learning Differs from Supervised Learning
Feature | Supervised Learning | Unsupervised Learning |
---|---|---|
Definition | Learns from labeled data | Learns from unlabeled data |
Output Type | Predicts a target variable | Finds hidden patterns |
Goal | Classification or regression | Clustering or pattern discovery |
Algorithms | Decision Trees, SVM, Neural Networks | K-Means, DBSCAN, PCA |
Example | Predicting house prices | Grouping customers by behavior |
📌 Example:
- Supervised Learning: Classifying emails as Spam or Not Spam.
- Unsupervised Learning: Clustering emails into different topics without labels.
Types of Unsupervised Learning
1️⃣ Clustering: Groups similar data points together.
- Example: K-Means, DBSCAN, Hierarchical Clustering.
2️⃣ Dimensionality Reduction: Reduces large datasets to simpler ones.
- Example: PCA (Principal Component Analysis), t-SNE.
3️⃣ Anomaly Detection: Identifies unusual patterns.
- Example: Isolation Forest, Autoencoders.
Final Thoughts
✅ Supervised Learning is for prediction, while Unsupervised Learning is for pattern discovery.
✅ Unsupervised Learning helps in exploratory data analysis where labels are unavailable.
✅ It plays a key role in customer segmentation, anomaly detection, and recommendation systems. 🚀
13) Explain linear regression with suitable examples for supervised learning.
Linear Regression in Supervised Learning
✅ Linear Regression is a supervised learning algorithm used for predicting continuous numerical values based on the relationship between independent variables (features) and a dependent variable (target).
📌 Formula:
where:
- → Predicted value (dependent variable)
- → Input feature (independent variable)
- → Slope (how much changes when changes)
- → Intercept (value of when )
For multiple features, the equation extends to:
How Linear Regression Works in Supervised Learning
1️⃣ Collect Labeled Data → Dataset contains features (X) and target (Y).
2️⃣ Train the Model → The model learns the best-fit line by minimizing error (using techniques like Least Squares).
3️⃣ Make Predictions → Given new input , the model predicts using the learned function.
Examples of Linear Regression in Supervised Learning
🔹 Example 1: Predicting House Prices
- Features (X): Size of the house (sq. ft.), Number of bedrooms, Location.
- Target (Y): House Price.
- Model: Learns the relationship between house size and price to predict new house prices.
🔹 Example 2: Predicting Salary Based on Experience
- Features (X): Years of experience.
- Target (Y): Salary.
- Model Output: A straight-line equation predicting salary from experience.
✅ Visualization:
- The model fits a best-fit line on a scatter plot of experience vs. salary.
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Years of Experience
y = np.array([30000, 35000, 40000, 45000, 50000]) # Salary
model = LinearRegression()
model.fit(X, y) # Train model
prediction = model.predict([[6]]) # Predict salary for 6 years experience
print(prediction) # Expected output ~ 55000
Advantages of Linear Regression
✅ Simple & Interpretable → Easy to understand and implement.
✅ Computationally Efficient → Works well for small to medium-sized datasets.
✅ Good for Linearly Related Data → Performs well when data follows a linear trend.
Final Thoughts
✅ Linear Regression is a foundational supervised learning algorithm used in prediction tasks where the target variable is continuous. It is widely applied in finance, healthcare, and marketing analytics. 🚀
14) How can a confusion matrix help evaluate the performance of a classification model?
Confusion Matrix in Evaluating Classification Models
✅ A Confusion Matrix is a performance measurement tool for classification models. It shows how well a model predicts actual categories by comparing predicted vs. actual values.
1. Structure of a Confusion Matrix
Actual / Predicted | Positive (1) Prediction | Negative (0) Prediction |
---|---|---|
Positive (1) Actual | True Positive (TP) | False Negative (FN) |
Negative (0) Actual | False Positive (FP) | True Negative (TN) |
📌 Definitions:
- True Positive (TP) → Model correctly predicts a positive class.
- True Negative (TN) → Model correctly predicts a negative class.
- False Positive (FP) (Type I Error) → Model incorrectly predicts positive when it’s actually negative.
- False Negative (FN) (Type II Error) → Model incorrectly predicts negative when it’s actually positive.
2. How Confusion Matrix Helps Evaluate Performance
🔹 Accuracy:
✅ Shows overall correctness but is unreliable for imbalanced datasets.
🔹 Precision (Positive Predictive Value):
✅ Useful when False Positives (FP) are costly, like in spam detection.
🔹 Recall (Sensitivity / True Positive Rate):
✅ Useful when False Negatives (FN) are costly, like in medical diagnoses.
🔹 F1-Score (Balanced measure of Precision & Recall):
✅ Best for imbalanced datasets.
🔹 Specificity (True Negative Rate):
✅ Important for fraud detection to reduce false alarms.
3. Example Confusion Matrix in Python
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0] # Actual labels
y_pred = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0] # Predicted labels
cm = confusion_matrix(y_true, y_pred)
print(cm)
🔹 Output Example:
[[3 1]
[1 5]]
- 3 True Negatives (TN)
- 1 False Positive (FP)
- 1 False Negative (FN)
- 5 True Positives (TP)
4. When to Use the Confusion Matrix?
✅ Binary Classification (Spam vs. Not Spam).
✅ Multi-Class Classification (Classifying diseases).
✅ Imbalanced Datasets (Fraud detection, Medical diagnosis).
Final Thoughts
✅ The Confusion Matrix provides deeper insights than just accuracy.
✅ Metrics like Precision, Recall, and F1-score help in model selection.
✅ It helps in minimizing critical errors (false positives/false negatives). 🚀
15) What role does principal component analysis (PCA) play in unsupervised learning? OR How does PCA help in reducing input variables while maintaining important information?
Role of Principal Component Analysis (PCA) in Unsupervised Learning
✅ Principal Component Analysis (PCA) is a dimensionality reduction technique used in unsupervised learning to reduce the number of input features while retaining the most important information. It transforms the original variables into a new set of uncorrelated components (Principal Components) that capture the maximum variance in the data.
📌 Example Use Cases:
- Reducing high-dimensional datasets (e.g., compressing images in facial recognition).
- Visualizing complex data (e.g., plotting multi-featured data in 2D/3D).
- Speeding up machine learning models by eliminating redundant features.
How Does PCA Reduce Input Variables While Keeping Important Information?
🔹 Step 1: Standardize the Data
- Convert all features to the same scale (mean = 0, variance = 1) to ensure fair comparison.
🔹 Step 2: Compute the Covariance Matrix
- Measures the relationship between different features to identify correlated features.
🔹 Step 3: Compute Eigenvalues & Eigenvectors
- Eigenvalues show the amount of variance captured by each component.
- Eigenvectors define the direction of new features (Principal Components).
🔹 Step 4: Select Principal Components
- Rank components by variance (higher variance = more important).
- Keep only the top k components that explain most of the variance.
🔹 Step 5: Transform Data to New Space
- Project the original data onto the selected principal components (fewer dimensions).
📌 Example:
A dataset has 100 features, but PCA finds that 5 components capture 95% of the variance. Instead of using all 100 features, we keep only 5, reducing complexity while preserving essential information.
Benefits of PCA in Machine Learning
✅ Reduces Dimensionality → Fewer features mean faster computation and less overfitting.
✅ Removes Multicollinearity → Eliminates redundant, highly correlated features.
✅ Improves Visualization → Converts complex data into 2D/3D for easier interpretation.
✅ Enhances Model Performance → Helps classifiers focus on the most relevant features.
📌 Real-World Example:
In handwritten digit recognition, PCA helps reduce 784 pixel features (28×28 images) to 50 principal components, maintaining accuracy while speeding up computations.
Final Thoughts
✅ PCA is a powerful tool for feature reduction in unsupervised learning.
✅ It helps simplify models, improve efficiency, and retain critical data patterns.
✅ Used in image processing, finance, biology, and recommendation systems. 🚀
16) What are Clustering Algorithms? Explain their applications in Data Science. Explain K-means clustering algorithm with suitable examples.
What are Clustering Algorithms?
✅ Clustering algorithms are unsupervised learning techniques used to group similar data points into clusters based on patterns or similarities.
✅ Unlike classification, clustering works without labeled data, making it useful for data exploration and pattern discovery.
Applications of Clustering in Data Science
📌 1. Customer Segmentation
- Grouping customers based on purchasing behavior to improve marketing strategies.
📌 2. Anomaly Detection
- Identifying fraudulent transactions by detecting unusual patterns in financial data.
📌 3. Image Segmentation
- Dividing images into different regions for object recognition.
📌 4. Document Clustering
- Organizing news articles into topics without predefined labels.
📌 5. Social Network Analysis
- Grouping users based on interests or online behavior.
K-Means Clustering Algorithm
✅ K-Means is one of the most popular clustering algorithms.
✅ It partitions data into K clusters, where each data point belongs to the nearest cluster centroid.
Steps of K-Means Algorithm
1️⃣ Choose the number of clusters (K) → Predefine the number of clusters.
2️⃣ Initialize centroids randomly → Select K points as initial cluster centers.
3️⃣ Assign each data point to the nearest centroid → Based on Euclidean distance.
4️⃣ Recalculate centroids → Compute the mean of all points in each cluster.
5️⃣ Repeat steps 3 and 4 until centroids no longer change (convergence).
📌 Mathematical Formula (Euclidean Distance Calculation):
where and are data points.
Example of K-Means Clustering in Python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Sample data points (X, Y coordinates)
X = np.array([[1, 2], [2, 3], [3, 3], [5, 7], [6, 8], [7, 8]])
# Apply K-Means clustering with 2 clusters
kmeans = KMeans(n_clusters=2, random_state=0)
kmeans.fit(X)
# Plot clusters
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, marker='X', color='red')
plt.show()
✅ Output: Two distinct clusters with red centroids.
Advantages of K-Means Clustering
✔ Simple and Fast → Works well with large datasets.
✔ Scalable → Handles high-dimensional data efficiently.
✔ Works well for spherical clusters → Suitable for simple datasets.
Limitations of K-Means
❌ Needs to predefine K → Choosing the wrong K can lead to poor clustering.
❌ Sensitive to Outliers → Outliers can distort centroids.
❌ Doesn’t work well for non-spherical clusters → Struggles with irregular shapes.
Final Thoughts
✅ Clustering algorithms help discover hidden patterns in data.
✅ K-Means is a widely used method for customer segmentation, anomaly detection, and image processing.
✅ Choosing the right number of clusters (K) is critical for better performance. 🚀
17) What are the key evaluation metrics for classification and regression models? Provide examples.
Key Evaluation Metrics for Classification and Regression Models
✅ Evaluation metrics help measure how well a machine learning model performs.
✅ The choice of metric depends on whether the task is classification (predicting categories) or regression (predicting continuous values).
1. Classification Model Evaluation Metrics
📌 Example: Predicting whether an email is Spam (1) or Not Spam (0).
Metric | Formula | Use Case |
---|---|---|
Accuracy | Overall correctness of the model. Works best for balanced datasets. | |
Precision | How many predicted positives are actually correct? Important for fraud detection. | |
Recall (Sensitivity) | How many actual positives were correctly predicted? Important for medical diagnosis. | |
F1-Score | Balances Precision & Recall. Good for imbalanced data. | |
ROC-AUC Score | Based on True Positive Rate vs. False Positive Rate | Measures how well the model distinguishes classes. Used in spam detection. |
Example Confusion Matrix
Actual / Predicted | Positive (Spam) | Negative (Not Spam) |
---|---|---|
Positive (Spam) | TP = 50 | FN = 10 |
Negative (Not Spam) | FP = 5 | TN = 100 |
✅ Precision:
✅ Recall:
✅ F1-Score:
📌 Best Metric to Use:
- High False Negatives are costly (e.g., Cancer Detection)? → Use Recall.
- High False Positives are costly (e.g., Spam Filters)? → Use Precision.
- Balanced Approach? → Use F1-Score.
2. Regression Model Evaluation Metrics
📌 Example: Predicting house prices based on features like size, location, and number of bedrooms.
| Metric | Formula | Use Case | | ------------------------------------------- | --------------------------------------------------------- | --------------------------------------------------------------------- | --- | ------------------------------------------------------------------------- | | Mean Absolute Error (MAE) | | Measures average absolute difference between actual and predicted values. | | Mean Squared Error (MSE) | | Penalizes larger errors more than MAE. Good for large errors. | | Root Mean Squared Error (RMSE) | | Similar to MSE but gives errors in actual units. | | R² Score (Coefficient of Determination) | | Measures how well the model explains variance. Closer to 1 is better. |
Example Predictions vs. Actual Values
House Size (sq. ft.) | Actual Price (y) | Predicted Price (ŷ) |
---|---|---|
1000 | $150,000 | $155,000 |
1500 | $200,000 | $195,000 |
2000 | $250,000 | $240,000 |
✅ MAE Example Calculation:
📌 Best Metric to Use:
- Need easily interpretable error? → Use MAE.
- Want to penalize large errors? → Use MSE/RMSE.
- Need overall model fit? → Use R² Score.
Final Thoughts
✅ Classification models use Accuracy, Precision, Recall, F1-score, and AUC.
✅ Regression models use MAE, MSE, RMSE, and R².
✅ Choosing the right metric depends on the problem type and business impact. 🚀
18) Given house price data:
Area (sq. ft.) | Price (₹ in Lakhs) |
---|---|
1000 | 50 |
1500 | 70 |
2000 | 90 |
2500 | 110 |
Predict the price for a 1750 sq. ft. house using KNN regression with K=2.
Using KNN regression with K = 2, we follow these steps:
-
Calculate distances:
For the new area 1750 sq. ft., compute the absolute difference to each training observation:- |1750 - 1000| = 750
- |1750 - 1500| = 250
- |1750 - 2000| = 250
- |1750 - 2500| = 750
-
Identify the 2 nearest neighbors:
The smallest distances (250) are from houses with areas 1500 and 2000 sq. ft. -
Find their prices:
- House with 1500 sq. ft. has price ₹70 lakhs.
- House with 2000 sq. ft. has price ₹90 lakhs.
-
Predict by averaging:
Final Answer:
The predicted price for a 1750 sq. ft. house is ₹80 lakhs.
19) Given the dataset of house prices:
Area (sq. ft.) | Price (₹ in Lakhs) |
---|---|
1000 | 50 |
1500 | 75 |
2000 | 100 |
Find the linear regression equation (y = mx + c) and predict the price of a 1250 sq. ft. house.
Let’s determine the linear regression equation using the given data points:
Area (sq.ft.) | Price (₹ in Lakhs) |
---|---|
1000 | 50 |
1500 | 75 |
2000 | 100 |
Step 1: Calculate the Slope (m)
Using any two points, for example, and :
Step 2: Calculate the Intercept (c)
Using the point :
Regression Equation
The equation is:
Step 3: Predict the Price for a 1250 sq.ft. House
Substitute :
Final Answer:
The linear regression equation is , and the predicted price for a 1250 sq.ft. house is ₹62.5 lakhs.
20) A company’s advertisement spending (₹ Lakhs) and sales revenue (₹ Crores) is given below:
Ad Spend (₹ Lakhs) | Sales Revenue (₹ Crores) |
---|---|
1 | 10 |
2 | 20 |
3 | 30 |
4 | 40 |
Fit a linear regression model and estimate the sales revenue if the ad spend is 2.5 Lakhs.
Given the data:
Ad Spend (₹ Lakhs) | Sales Revenue (₹ Crores) |
---|---|
1 | 10 |
2 | 20 |
3 | 30 |
4 | 40 |
We can see that the relationship is perfectly linear.
Step 1: Determine the Linear Regression Model
We want an equation in the form:
Using the points, let’s compute the slope (m) using any two points (for example, (1, 10) and (2, 20)):
Next, to find the intercept (c), plug in one of the points (using (1, 10)):
Thus, the regression equation is:
where:
- = Ad Spend (in Lakhs)
- = Sales Revenue (in Crores)
Step 2: Estimate Sales Revenue for Ad Spend of 2.5 Lakhs
Substitute into the equation:
Final Answer
The estimated sales revenue for an ad spend of 2.5 Lakhs is ₹25 Crores.
21) Given student study hours and exam scores:
Hours Studied | Score |
---|---|
2 | 50 |
4 | 60 |
6 | 70 |
8 | 80 |
Calculate the linear regression equation and predict the score for a student who studies 5 hours.
Using the given data:
Hours Studied | Exam Score |
---|---|
2 | 50 |
4 | 60 |
6 | 70 |
8 | 80 |
We first calculate the linear regression equation in the form:
where:
- is the exam score,
- is the number of study hours,
- is the slope,
- is the intercept.
Step 1: Calculate the Slope (m)
Using any two points, for example, (2, 50) and (4, 60):
Step 2: Calculate the Intercept (c)
Use one of the points (for example, (2, 50)):
Regression Equation
The linear regression equation is:
Step 3: Predict the Score for a Student Who Studies 5 Hours
Substitute into the equation:
Final Answer
The linear regression equation is:
and the predicted exam score for a student who studies 5 hours is 65.
22) A company records employee experience and salary:
Experience (Years) | Salary (₹ Lakhs) |
---|---|
1 | 3 |
3 | 6 |
5 | 9 |
Find the regression equation and predict the salary for an employee with 4 years of experience.
Given the data:
Experience (Years) | Salary (₹ Lakhs) |
---|---|
1 | 3 |
3 | 6 |
5 | 9 |
We need to derive the linear regression equation in the form:
where:
- is the Salary,
- is the Experience,
- is the slope,
- is the intercept.
Step 1: Calculate the Means
Step 2: Calculate the Slope (m)
The slope is given by:
For each data point:
-
For :
Product: . -
For :
Product: . -
For :
Product: .
Now, sum up these products:
Next, compute the sum of squares for :
- For : .
- For : .
- For : .
So, the slope is:
Step 3: Calculate the Intercept (c)
The intercept is calculated by:
Regression Equation
The regression equation is:
Step 4: Predict the Salary for 4 Years of Experience
For years:
Thus, the predicted salary for an employee with 4 years of experience is ₹7.5 Lakhs.
23) A dataset has two categories: Spam and Not Spam. Given the following probabilities:
P(Spam) = 0.4, P(Not Spam) = 0.6
P(Word1 | Spam) = 0.2, P(Word1 | Not Spam) = 0.5
P(Word2 | Spam) = 0.6, P(Word2 | Not Spam) = 0.3
Calculate the probability that a new email containing Word1 and Word2 is Spam.
We can use Bayes’ theorem with the naïve assumption that the words occur independently given the class. The formula for the posterior probability is:
First, compute the numerator:
Next, the total probability of observing Word1 and Word2 is:
Substitute the given values:
Now, plug these into Bayes’ formula:
Final Answer:
The probability that a new email containing Word1 and Word2 is Spam is approximately 34.78% (about 35%).
24) In a medical dataset, a person’s likelihood of having a disease is based on symptoms:
P(Disease) = 0.1, P(No Disease) = 0.9
P(Fever | Disease) = 0.8, P(Fever | No Disease) = 0.3
P(Cough | Disease) = 0.7, P(Cough | No Disease) = 0.4
If a new patient has Fever and Cough, calculate the probability that they have the disease.
We can calculate the probability that the new patient has the disease using Bayes’ theorem with the naïve assumption (i.e. assuming that the symptoms are conditionally independent given the disease status).
Given:
Step 1: Compute the likelihoods for both conditions
-
For a patient with the disease:
-
For a patient without the disease:
Step 2: Use Bayes’ Theorem
Bayes’ theorem tells us:
where
Plug in the values:
Now, compute the posterior probability:
Final Answer:
The probability that the patient has the disease, given they have both Fever and Cough, is approximately 34.15%.
25) Given the following dataset with two features (X1, X2) and class labels:
X1 | X2 | Class |
---|---|---|
1 | 1 | A |
2 | 2 | A |
3 | 3 | B |
6 | 6 | B |
Using KNN with K=3, classify a new data point (4,4).
To classify the new data point using KNN with , we first compute the Euclidean distances between and each point in the dataset:
- Distance to (1,1), Class A:
- Distance to (2,2), Class A:
- Distance to (3,3), Class B:
- Distance to (6,6), Class B:
Now, order the distances:
- with (Class B)
- with (Class A)
- with (Class B)
- with (Class A)
Taking the 3 nearest neighbors (K = 3), we have:
- : Class B
- : Class A
- : Class B
The majority class among these neighbors is B (2 out of 3).
Final Answer:
The new data point is classified as Class B.
26) You have a dataset of fruits classified based on their weight and size.
Weight (g) | Size (cm) | Fruit Type |
---|---|---|
150 | 8 | Apple |
180 | 10 | Apple |
200 | 12 | Orange |
220 | 14 | Orange |
Classify a fruit with weight 190g and size 11cm using KNN (K=3).
To classify the new fruit using KNN with , we follow these steps:
Step 1: List the Data Points
Fruit Type | Weight (g) | Size (cm) |
---|---|---|
Apple | 150 | 8 |
Apple | 180 | 10 |
Orange | 200 | 12 |
Orange | 220 | 14 |
New fruit: Weight = 190 g, Size = 11 cm
Step 2: Compute Euclidean Distances
The Euclidean distance between two points and is:
-
Distance to (150, 8) [Apple]:
-
Distance to (180, 10) [Apple]:
-
Distance to (200, 12) [Orange]:
-
Distance to (220, 14) [Orange]:
Step 3: Identify the 3 Nearest Neighbors
Order the distances:
- (180, 10) [Apple]:
- (200, 12) [Orange]:
- (220, 14) [Orange]:
- (150, 8) [Apple]:
The 3 nearest neighbors are:
- (180, 10) → Apple
- (200, 12) → Orange
- (220, 14) → Orange
Step 4: Classify by Majority Vote
Among the 3 nearest neighbors:
- Apple: 1
- Orange: 2
The majority class is Orange.
Final Answer
The new fruit with a weight of 190 g and a size of 11 cm is classified as Orange using KNN with .
27) A dataset provides the exam scores of students based on study hours:
Hours Studied | Exam Score |
---|---|
1 | 45 |
2 | 50 |
3 | 55 |
4 | 60 |
5 | 65 |
Predict the score for a student who studies 3.5 hours using KNN regression with K=3.
Step 1: Compute the Distances
For the new point (i.e., 3.5 hours), calculate the absolute distance from each data point:
- For 1 hour (score = 45):
- For 2 hours (score = 50):
- For 3 hours (score = 55):
- For 4 hours (score = 60):
- For 5 hours (score = 65):
Step 2: Identify the 3 Nearest Neighbors
Sorting by distance, we have:
- 0.5: (3 hours, 55) and (4 hours, 60)
- 1.5: (2 hours, 50) and (5 hours, 65)
With , we choose the three closest points. If a tie occurs for the third position, many implementations break ties by order of appearance. Given the dataset order:
- (3, 55)
- (4, 60)
- (2, 50) (comes before (5,65) in the dataset)
Step 3: Predict the Score
Calculate the average score of the three nearest neighbors:
Final Answer:
Using KNN regression with , the predicted exam score for a student who studies 3.5 hours is 55.