9
Vectorial implementation in Octave 1

Octave tutorial 2

Embed Size (px)

Citation preview

Page 1: Octave tutorial 2

Vectorial implementation in Octave

1

Page 2: Octave tutorial 2

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

Page 3: Octave tutorial 2

>> 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

Page 4: Octave tutorial 2

4

Page 5: Octave tutorial 2

Update theta:>> theta = [0;0]theta = 0 0>> j = costFunctionJ(X, y, theta)j = 2.3333>>

5

(12+22+32)/6=2.3333

Page 6: Octave tutorial 2

House sizes:

Page 7: Octave tutorial 2

Vectorization example.Our usual hypothesis of linear regression

If we have n = 2 features in the model.

Page 8: Octave tutorial 2

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;

Page 9: Octave tutorial 2

9

>> p r e d i c t i o n = theta' * x ;