Skip to content
← All questions

What is the least squares cost function?

The least squares cost function measures the average squared difference between the predicted values (based on the model parameters and features) and the actual target values. It is commonly used in linear regression to evaluate model performance.

Conditions

  • Used for regression problems where the goal is to predict a continuous value.
  • Assumes a linear relationship between the features and the target variable.

Reasoning, step by step

  1. Identify the predicted value for a data point: θ1x1+θ2x2\theta_1 x_1 + \theta_2 x_2.
  2. Identify the actual target value: yy.
  3. Calculate the error (difference) for the data point: (θ1x1+θ2x2−y)(\theta_1 x_1 + \theta_2 x_2 - y).
  4. Square the error: (θ1x1+θ2x2−y)2(\theta_1 x_1 + \theta_2 x_2 - y)^2.
  5. Sum the squared errors over all mm training examples and divide by 2m2m.
  6. The cost function is J(θ1,θ2)=12m∑(θ1x1+θ2x2−y)2J(\theta_1, \theta_2) = \frac{1}{2m} \sum (\theta_1 x_1 + \theta_2 x_2 - y)^2.

Example

The video displays the formula on-screen as "J(θ1,θ2)=12m∑(θ1x1+θ2x2−y)2J(\theta_1, \theta_2) = \frac{1}{2m} \sum (\theta_1 x_1 + \theta_2 x_2 - y)^2" and refers to it as the least square cost function.

Common misconceptions

  • Forgetting the factor of 12m\frac{1}{2m}, which averages the squared errors and simplifies the derivative calculation.
  • Assuming the cost function measures the absolute difference rather than the squared difference.

Watch the explanation

Explore next

Answers are generated from source material and independently checked. Consult the original video or creator if something is unclear.