Given a dataset with only two training samples and . Suppose we want to build a Linear Regression model with learning rate . At first, and . 1)Please calculate the cost . 2)Update the parameters and for one time. 3)Please calculate the new cost after updating.
时间: 2024-02-18 22:01:52 浏览: 87
Given:
- Two training samples: `(2, 3)` and `(4, 5)`
- Initial parameters: `theta0 = 0, theta1 = 0`
- Learning rate: `alpha = 0.01`
To build a Linear Regression model, we need to minimize the cost function. The cost function for Linear Regression is:
```
J(theta0, theta1) = (1/2m) * sum(i=1 to m)[hypothesis(xi) - yi]^2
```
where `m` is the total number of training samples, `xi` is the feature of the `i`th training sample, `yi` is the label of the `i`th training sample, and `hypothesis(xi)` is the predicted value for `xi` using the current values of `theta0` and `theta1`.
1) Calculate the initial cost:
```
hypothesis(2) = theta0 + theta1 * 2 = 0 + 0 * 2 = 0
hypothesis(4) = theta0 + theta1 * 4 = 0 + 0 * 4 = 0
J(theta0, theta1) = (1/4) * [(0 - 3)^2 + (0 - 5)^2] = 13/4 = 3.25
```
So the initial cost is `3.25`.
2) Update the parameters for one time:
```
theta0 = theta0 - alpha * (1/m) * sum(i=1 to m)[hypothesis(xi) - yi] = 0 - 0.01 * (1/2) * [(0 - 3) + (0 - 5)] = 0.04
theta1 = theta1 - alpha * (1/m) * sum(i=1 to m)[(hypothesis(xi) - yi) * xi] = 0 - 0.01 * (1/2) * [(0 - 3) * 2 + (0 - 5) * 4] = 0.11
```
So the updated parameters are `theta0 = 0.04` and `theta1 = 0.11`.
3) Calculate the new cost after updating:
```
hypothesis(2) = theta0 + theta1 * 2 = 0.04 + 0.11 * 2 = 0.26
hypothesis(4) = theta0 + theta1 * 4 = 0.04 + 0.11 * 4 = 0.46
J(theta0, theta1) = (1/4) * [(0.26 - 3)^2 + (0.46 - 5)^2] = 2.69/4 = 0.6725
```
So the new cost after updating is `0.6725`.
阅读全文