1. Multiple Feature

When there are multiple features, represent the features as a vector:

where:

  • : the -th feature
  • : number of features

For the -th training example:

means the -th feature of the -th training example.

Parameters

The weights can also be represented as a vector:

is a scalar (number).

Model

The dot product is:

So:

This model is called Multiple Linear Regression.

One feature → Linear Regression
Multiple features → Multiple Linear Regression

2. Linear Regression with Mutiple Variables

Vectorization makes code shorter and faster by taking advantage of optimized numerical operations and parallel computation.

1. Vectorized Model

For multiple linear regression:

The dot product is:

Without vectorization:

f = 0
for j in range(n):
    f += w[j] * x[j]
f += b

With vectorization:

f = np.dot(w, x) + b

2. Why Vectorization?

  • Shorter code: Replaces explicit loops with vector operations.

  • Faster computation: Uses optimized numerical libraries.

  • Parallel processing: Can take advantage of modern CPU hardware and, with suitable libraries, GPUs.

3. Vectorized Gradient Descent

Without vectorization:

for j in range(n):
    w[j] = w[j] - alpha * d[j]

With vectorization:

w = w - alpha * d

Mathematically:

where:

  • : Learning rate

  • : Vector of derivatives

Key idea: Vectorization replaces element-by-element loops with efficient operations on entire vectors.

3. Gradient Descent for Multiple Regression

In multiple linear regression, there are n weights and one bias. Gradient descent updates all parameters to minimize the cost function.

1. Model and Cost Function

Model:

Cost function:

where:

  • : Number of features

  • : Number of training examples

  • : Weight vector

  • : Bias

2. Gradient Descent

Update each weight and the bias :

Partial derivatives:

For .

All parameters should be updated simultaneously using the gradients calculated from the same parameter values.

3. Normal Equation

The Normal Equation is another method for finding the optimal parameters of linear regression.

  • Does not require iterative gradient descent.

  • Applies to linear least-squares regression, but not directly to algorithms such as logistic regression or neural networks.

  • Can be computationally expensive when the number of features is large.

  • May be used internally by machine learning libraries.

Key idea: Gradient descent iteratively updates the weights and bias, while the Normal Equation solves linear least-squares regression without iterative gradient descent.

4. Feature Scaling

When features have very different ranges of values, gradient descent may converge slowly.

Feature Scaling transforms features into comparable ranges, helping gradient descent converge faster.

1. Why Feature Scaling?

  • Large feature values usually correspond to smaller weights.

  • Small feature values often correspond to larger weights.

  • Different scales can make the cost function contours elongated, causing gradient descent to take a zigzag path.

After scaling, the cost function contours may become more circular, allowing faster convergence.

2. Scaling Methods

1. Dividing by the Maximum

2. Mean Normalization

3. Z-score Normalization

where:

  • : Mean (average value)

  • : Standard deviation

  • : Maximum value

  • : Minimum value

3. When to Scale?

As a rule of thumb, try to keep features within comparable ranges, roughly around -1 to +1.

  • Ranges like [-3, 3] or [-0.3, 0.3] are also acceptable.

  • If a feature has a very large range, such as [-100, 100], consider scaling it.

  • If a feature has a very small range, such as [-0.001, 0.001], consider scaling it too.

4. Avoid Data Leakage

Calculate scaling parameters from the training set only.

  • Training set: Calculate the mean, standard deviation, maximum, and minimum.

  • Validation/Test set: Use the same scaling parameters calculated from the training set.

  • New data: Apply the same transformation without recalculating the parameters.

This prevents data leakage and ensures consistent feature scaling.

Key idea: Fit the scaler on the training set, then use the same scaler to transform all other data.

5. Checking Gradient Descent Convergence

Visualize gradient descent using a Learning Curve.

  • X-axis: Number of iterations

  • Y-axis: Cost function

If gradient descent works correctly:

  • Cost should decrease after each iteration.

  • When the curve flattens out, gradient descent has approximately converged.

  • If cost increases, the learning rate may be too large, or there may be a bug in the code.

Automatic Convergence Test:

If the decrease in cost is smaller than a small threshold , gradient descent may have converged.

where might be .

6. Choosing the Learning Rate

The learning rate controls the step size of gradient descent.

  • Too small: Slow convergence.

  • Too large: Cost may oscillate or diverge.

  • Appropriate: Cost decreases quickly and consistently.

How to choose :

  1. Start with a very small learning rate to check whether the cost decreases. If it still increases, there may be a bug.

  2. Try progressively larger learning rates, such as:

  3. Plot and compare their learning curves over several iterations.

  4. Choose a relatively large learning rate that decreases the cost quickly and consistently, without oscillation or divergence.

Key idea: Choose a learning rate that balances fast convergence and stability.

![][assets/images/梯度下降三种学习率行为.png]