Welcome to The Gradient Quest! This assignment is designed to help you understand Gradient Descent step-by-step through a hands-on coding adventure. You'll implement gradient descent from scratch and compare your results with scikit-learn's built-in implementation.
By completing this assignment, you will:
- Understand the fundamentals of gradient descent optimization
- Implement cost functions for linear regression
- Manually code gradient descent algorithm
- Visualize the convergence of gradient descent
- Experiment with different learning rates
- Compare custom implementations with scikit-learn
- Gain insights into feature scaling and learning rate selection
Dataset: Car Price Prediction
Source: Kaggle - Car Price Prediction
File: CarPrice_Assignment.csv
- Download the dataset from the link above
- Place
CarPrice_Assignment.csvin the same directory as the notebook - Ensure the file is named exactly as specified
ML-Task-2025/
βββ Assignment_Post_ML_Session.ipynb # Main assignment notebook
βββ CarPrice_Assignment.csv # Dataset (download separately)
βββ README.md # This file
Ensure you have the following libraries installed:
pip install pandas numpy matplotlib scikit-learn jupyter- Fork this repository (automatically done via GitHub Classroom)
- Clone the forked repository
- Download the dataset from Kaggle (link above)
- Open the Jupyter notebook
- Work through each stage sequentially
- Push the changes to your fork to submit
Objective: Load the dataset and explore its structure
Task: Load CarPrice_Assignment.csv and print its shape
Expected Output: Shape: (205, 26)
Clue: The number of columns
Objective: Feature selection for price prediction
Task: Select 3 numerical features most relevant to car price
Examples: enginesize, horsepower, curbweight
Clue: Number of features Γ 2
Objective: Implement the cost function
Task: Create compute_cost(X, y, theta) function
Formula: J(ΞΈ) = (1/2m) Γ Ξ£(h_ΞΈ(x_i) - y_i)Β²
Test with dummy data to verify correctness
Clue: Rounded cost value
Objective: Implement one gradient descent step
Task: Create gradient_step(X, y, theta, alpha) function
Formula: ΞΈ := ΞΈ - (Ξ±/m) Γ (X^T Β· (XΞΈ - y))
Clue: Sum of theta elements (rounded to 2 decimals)
Objective: Full gradient descent implementation
Task: Implement complete gradient_descent() function
Requirements:
- Run for multiple iterations
- Record cost at each iteration
- Plot cost vs. iteration to visualize convergence
Clue: Final cost value (rounded to 2 decimals)
Objective: Experiment with different learning rates
Task: Test Ξ± = [0.001, 0.01, 0.1] for 500 iterations each
Requirements:
- Plot all three learning rates on the same graph
- Identify which Ξ± converges fastest without diverging
Clue: Best learning rate value
Objective: Compare with scikit-learn
Task: Train a LinearRegression model using sklearn
Requirements:
- Use the same features as your custom implementation
- Compare coefficients (theta values)
- Determine if they match numerically
Clue: "MATCH" or "TRY AGAIN"
Objective: Demonstrate understanding
Task: Answer these questions in markdown cells:
- What happens if the learning rate is too high?
- Why does feature scaling help gradient descent?
- How do your results compare to sklearn's model?
Final Step: Print "THE QUEST IS COMPLETE"
- Completed Jupyter notebook (
Assignment_Post_ML_Session.ipynb) - All cells should be executed with outputs visible
- All clues should be printed correctly
- Reflection questions answered in markdown cells
- Stage 1-2 (15%): Data loading and feature selection
- Stage 3-4 (25%): Cost function and gradient step implementation
- Stage 5 (20%): Full gradient descent with visualization
- Stage 6 (15%): Learning rate experiments
- Stage 7 (15%): Comparison with scikit-learn
- Stage 8 (10%): Reflection and understanding
- β Work through stages sequentially
- β Test your functions with simple examples first
- β Print intermediate results to debug
- β Pay attention to array shapes and dimensions
- β Use feature scaling for better convergence
- β Document your thought process in markdown cells
Solution: Ensure CarPrice_Assignment.csv is in the same directory as the notebook
Solutions:
- Check your cost function implementation
- Verify gradient calculation
- Try feature scaling (StandardScaler)
- Reduce learning rate
Solution: Learning rate is too high - reduce Ξ±
Solution: Learning rate might be too small - increase Ξ±
Solutions:
- Ensure you're using the same features
- Apply feature scaling consistently
- Run enough iterations for convergence
- Check for implementation bugs
After completing the basic assignment:
- Try implementing batch, mini-batch, and stochastic gradient descent
- Experiment with momentum and adaptive learning rates
- Test with additional features
- Implement regularization (Ridge/Lasso)
Good luck on your quest to master Gradient Descent! π
Remember: The journey of understanding is more valuable than just getting the right answer.