Vectorial implementation in Octave
1
House Size:123
2
0 1 2 30
1
2
3
y
x
𝜃0=0
h𝜃 (𝑥 )=𝜃0+𝜃1𝑥Predictions:
1 11 21 3
Prices:123
Theta: 01
>> X = [1 1; 1 2; 1 3]X = 1 1 1 2 1 3>> y=[1; 2; 3]y = 1 2 3
3
>> theta = [0;1]theta = 0 1
>> j = costFunctionJ(X, y, theta)j = 0
4
Update theta:>> theta = [0;0]theta = 0 0>> j = costFunctionJ(X, y, theta)j = 2.3333>>
5
(12+22+32)/6=2.3333
House sizes:
Vectorization example.Our usual hypothesis of linear regression
If we have n = 2 features in the model.
Vectorization example.
Unvectorized implementationdouble prediction = 0.0;for (int j = 0; j < n; j++) prediction += theta[j] * x[j];
Vectorized implementationdouble prediction = theta.transpose() * x;
9
>> p r e d i c t i o n = theta' * x ;