Docsity

KNN Clustering practice lab, Assignments of Machine Learning

Toggle sidebar
bg1
CSCC11 Lab 2: Machine Learning Workflow
This lab is to learn the end-to-end machine learning project workflow using K-Nearest
Neighbors regression API from scikit-learn.
Learning Objectives
By the end of this lab, you will be able to:
• Load and explore a real-world dataset
• Visualize data distributions and relationships
• Preprocess data using standardization
• Split data into training and testing sets
• Use cross-validation for hyperparameter tuning
• Evaluate model performance
• Identify underfitting and overfitting in KNN regression model
Prelab
• Read the 10 minutes to pandas documentation.
• Read data preprocessing scikit-learn API: StandardScaler, and train_test_split.
• Read the scikit-learn KNeighborsRegressor, KFold, and metrics.
Background: 𝑲-Nearest Neighbors (KNN) Regression
KNN is a non-parametric model that predicts the target value based on the average of the 𝐾
nearest neighbors in the feature space. The key characteristics of KNN are
• Non-parametric: No assumptions about data distribution or the underlying
regression function form.
• Instance-based: Uses training data directly for predictions
• Sensitive to scale: Distance-based, feature scaling is crucial
• Hyperparameter 𝑲: Controls model complexity
Dataset: California Housing
The California housing dataset contains information about housing districts in California
from the 1990 census.
pf3
pf4

Partial preview of the text

Download KNN Clustering practice lab and more Assignments Machine Learning in PDF only on Docsity!

CSCC11 Lab 2: Machine Learning Workflow

This lab is to learn the end-to-end machine learning project workflow using K-Nearest Neighbors regression API from scikit-learn.

Learning Objectives

By the end of this lab, you will be able to:

  • Load and explore a real-world dataset
  • Visualize data distributions and relationships
  • Preprocess data using standardization
  • Split data into training and testing sets
  • Use cross-validation for hyperparameter tuning
  • Evaluate model performance
  • Identify underfitting and overfitting in KNN regression model

Prelab

  • Read the 10 minutes to pandas documentation.
  • Read data preprocessing scikit-learn API: StandardScaler, and train_test_split.
  • Read the scikit-learn KNeighborsRegressor, KFold, and metrics.

Background: 𝑲 - Nearest Neighbors (KNN) Regression

KNN is a non-parametric model that predicts the target value based on the average of the 𝐾 nearest neighbors in the feature space. The key characteristics of KNN are

  • Non-parametric: No assumptions about data distribution or the underlying regression function form.
  • Instance-based: Uses training data directly for predictions
  • Sensitive to scale: Distance-based, feature scaling is crucial
  • Hyperparameter 𝑲 : Controls model complexity

Dataset: California Housing

The California housing dataset contains information about housing districts in California from the 1990 census.

  • Features (8 total):
    1. MedInc: Median income in block group
    2. HouseAge: Median house age in block group
    3. AveRooms: Average number of rooms per household
    4. AveBedrms: Average number of bedrooms per household
    5. Population: Block group population
    6. AveOccup: Average number of household members
    7. Latitude: Block group latitude
    8. Longitude: Block group longitude
  • Target:
    • MedHouseVal: Median house value in hundreds of thousands of dollars

Lab Tasks

Task 1: Data Loading and Exploration

  1. Load the California housing dataset from sklearn
  2. Examine the dataset structure (shape, feature names, target)
  3. Display basic statistics using the pandas data frame describe() function and check for missing values.
  4. Print first few samples Task 2: Data Visualization
  5. Create histograms for all features and target variable
  6. Create a correlation heatmap of features.
  7. Create scatter plots showing relationship between key features and target
  8. Plot geographical distribution of house values (latitude vs longitude) Task 3: Data Preprocessing
  9. Split the data into features (𝑋) and target (𝑦)
  10. Split into training and testing sets (80/20 split, random_state=42)
  1. Why is standardization crucial for KNN?
  2. How does the choice of 𝐾 affect underfitting and overfitting?
  3. What patterns do you observe in the cross-validation curve?
  4. How do geographical features (latitude/longitude) influence house prices?
  5. What are the limitations of KNN for this problem? Take-home Challenges (optional)
  6. Compare KNN performance with different distance metrics (Manhattan, Chebyshev)
  7. Implement feature selection and compare results
  8. Try weighted KNN (distance-based weights) Tips
  • Start with small 𝐾 values and gradually increase
  • Pay attention to the scale of your features
  • Use meaningful variable names and add comments
  • Set random seed for reproducibility
  • Create functions and reuse code to avoid repetitive code