Bird
Raised Fist0

Linear Regression - From Least Squares to Gradient Descent

THEORYbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/5
Data Points

Data points are plotted on a 2D plane, showing a linear trend.

🎯 Understanding the distribution of data is crucial for selecting the right model.
import matplotlib.pyplot as plt
plt.scatter(X, y)
plt.title('Data Points')
plt.show()
Least Squares

Initial error calculated as the sum of squared differences between actual and predicted values.

🎯 Least Squares provides a closed-form solution for linear regression.
import numpy as np
error = np.sum((y - (m*X + c))**2)
Gradient Descent Initialization

Parameters m and c are initialized, and learning rate α is set.

🎯 Choosing the right learning rate is critical for convergence.
m, c = 0, 0
alpha = 0.01
Gradient Descent Update

Parameters m and c are updated using partial derivatives of the error.

🎯 Gradient Descent iteratively reduces the error by updating parameters.
m = m - alpha * np.sum(-2*X*(y - (m*X + c)))
c = c - alpha * np.sum(-2*(y - (m*X + c)))
Convergence

The error decreases with each iteration, indicating convergence.

🎯 Convergence is achieved when parameter updates become negligible.
for _ in range(1000):
    m = m - alpha * np.sum(-2*X*(y - (m*X + c)))
    c = c - alpha * np.sum(-2*(y - (m*X + c)))
📊
Diagram
Diagram
┌───────────────────────────────┐
│ Linear Regression Mechanism   │
├───────────────────────────────┤
│ Data Points                   │
│   o o o o o                   │
│   o o o o o                   │
│                               │
│ Least Squares                 │
│   ┌───────────────┐           │
│   │ Error = Σ(y - │           │
│   │ (mx + c))^2   │           │
│   └───────────────┘           │
│                               │
│ Gradient Descent              │
│   ┌───────────────┐           │
│   │ m = m - α∂/∂m │           │
│   │ c = c - α∂/∂c │           │
│   └───────────────┘           │
└───────────────────────────────┘
  • Data Points: Observations used for fitting the model
  • Least Squares: Method to minimize the sum of squared errors
  • Gradient Descent: Iterative optimization technique
  • Error: Difference between predicted and actual values
  • Parameters (m, c): Slope and intercept of the regression line
🔑
Key Moments
▲
Q: Why is the learning rate important in Gradient Descent?
A: The learning rate determines the size of the steps taken towards the minimum. A rate too high can overshoot the minimum, while too low can slow convergence.
Q: How does Least Squares differ from Gradient Descent?
A: Least Squares provides a direct solution by minimizing the sum of squared errors, while Gradient Descent iteratively updates parameters to minimize the error.
Q: What happens if the data points are not linearly distributed?
A: If data points are not linearly distributed, a linear regression model may not fit well, leading to high error. Alternative models or transformations may be needed.

This diagram and trace explain the transition from Least Squares to Gradient Descent in linear regression. It begins with plotting data points and calculating initial error using Least Squares. Gradient Descent is then introduced as an iterative method to optimize the model parameters by updating them based on the gradient of the error. Key insights include the importance of the learning rate and the iterative nature of Gradient Descent, which contrasts with the direct solution provided by Least Squares. Understanding these concepts is crucial for effectively applying linear regression in practice.

Primary Focus

✓ Understanding the transition from Least Squares to Gradient Descent in Linear Regression

Practice

(1/5)
1. What will be the output of the following Python code snippet using NumPy for weighted sampling?
import numpy as np
np.random.seed(0)
weights = [0.1, 0.3, 0.6]
choices = ['A', 'B', 'C']
result = np.random.choice(choices, p=weights)
print(result)
easy
A. Randomly 'A', 'B', or 'C' with specified probabilities
B. Always 'A'
C. Always 'C'
D. Randomly 'A', 'B', or 'C' with equal probability

Solution

  1. Step 1: Understand weighted sampling.

    np.random.choice with 'p' parameter uses specified probabilities for sampling.
  2. Step 2: Analyze the code.

    Weights [0.1, 0.3, 0.6] mean 'C' is most likely, but not guaranteed.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    np.random.choice uses 'p' for weighted sampling [[check]]
Hint: np.random.choice uses 'p' for weighted sampling [[check]]
Common Mistakes:
2. You conducted an A/B test using the interleaving method, but the results are inconsistent with previous tests. What is the most likely cause?
medium
A. Data leakage between test groups
B. Inadequate sample size
C. Temporal trends affecting results
D. Incorrect randomization of test subjects

Solution

  1. Step 1: Identify potential issues

    Interleaving requires proper randomization to ensure fair comparison.
  2. Step 2: Eliminate wrong options

    Data leakage and temporal trends are less likely in interleaving. Inadequate sample size affects power, not consistency.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Randomization is key in interleaving. [OK]
Hint: Randomization is key in interleaving. [OK]
Common Mistakes:
3. When should you prefer using Gini Impurity over Entropy in decision trees?
medium
A. When computational efficiency is a priority.
B. When you need a more interpretable model.
C. When the dataset is highly imbalanced.
D. When you need to handle continuous features.

Solution

  1. Step 1: Compare computational efficiency.

    Gini Impurity is computationally less intensive than Entropy.
  2. Step 2: Eliminate wrong options.

    Interpretability is not affected by the choice, imbalance handling is not specific to Gini, and both can handle continuous features.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Gini is faster than Entropy. [OK]
Hint: Gini is faster than Entropy. [OK]
Common Mistakes:
4. In a scenario where computational resources are limited, which segmentation approach would be more appropriate to use and why?
medium
A. Instance segmentation, because it provides detailed instance-level information.
B. Object detection, because it is a form of segmentation that is computationally efficient.
C. Semantic segmentation, because it requires less computational power than instance segmentation.
D. Panoptic segmentation, because it combines both semantic and instance segmentation.

Solution

  1. Step 1: Evaluate computational needs

    Semantic segmentation is generally less computationally intensive than instance or panoptic segmentation.
  2. Step 2: Eliminate wrong options

    Instance segmentation is more detailed and resource-heavy, panoptic combines both, and object detection is not segmentation.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Semantic = Less computation than instance [OK]
Hint: Semantic = Less computation than instance [OK]
Common Mistakes:
5. You are tasked with designing a system to detect fraudulent transactions. Which approach is most appropriate given the need for adaptability to new fraud patterns?
hard
A. Use a rule-based system with predefined fraud patterns.
B. Rely on manual inspection of transactions.
C. Use a static decision tree with fixed thresholds.
D. Implement a machine learning model that can be retrained with new data.

Solution

  1. Step 1: Consider the need for adaptability.

    Fraud patterns change over time, requiring a system that can learn and adapt.
  2. Step 2: Evaluate the options for adaptability.

    Only a machine learning model that can be retrained offers the necessary adaptability.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    ML adapts, rules are static. [OK]
Hint: ML adapts, rules are static. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard