This is the 16th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

See Octave Brief Tutorials for tutorials

Let’s see how octave calculates the cost function J(θ) in practice. The data is large and can be seen at the end of the article.


The preparatory work

First save the file locally. Notice that today’s cost function only calculates a linear regression with one variable. The first set of test data also contains the characteristic quantity of the number of rooms, which can be removed later.

Recall the cost function of a single variable linear regression:


J ( Theta. 0 . Theta. 1 ) = 1 2 m i = 1 m ( h Theta. ( x ( 1 ) ) y ( i ) ) 2 = 1 2 m i = 1 m ( Theta. 0 + Theta. 1 x ( i ) y ( i ) ) 2 J\left(\theta_{0}, \theta_{1}\right) =\frac{1}{2 m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(1)}\right)-y^{(i)}\right)^{2}=\frac{1}{2 m} \sum_{i=1}^{m}\left(\theta_{0}+\theta_{1} x^{(i)}-y^{(i)}\right)^{2}

Open to

  1. First read in the data

  2. Because univariate linear regression is required, the feature matrix does not meet the requirements now, so we only need to retain the data of its first column and modify it now. performfeature = feature(:,1:1)And then there’s only one column left.

  3. Write a function

    • First of all, let’s analyze the formula
      1 2 m i = 1 m ( h Theta. ( x ( i ) ) y ( i ) ) 2 = 1 2 m i = 1 m ( Theta. 0 + Theta. 1 x ( i ) y ( i ) ) 2 \frac{1}{2 m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)^{2}=\frac{1}{2 m} \sum_{i=1}^{m}\left(\theta_{0}+\theta_{1} x^{(i)}-y^{(i)}\right)^{2}

      • Use this formula
      • M is the number of groups, so you can use itm = length(feature)orm = lenth(price)
      • Y is the price vector
      • X is the feature vector
    • To deal with
      1 2 m i = 1 m ( Theta. 0 + Theta. 1 x ( i ) y ( i ) ) 2 \frac{1}{2 m} \sum_{i=1}^{m}\left(\theta_{0}+\theta_{1} x^{(i)}-y^{(i)}\right)^{2}

      • First calculate theta. Theta 0 + 1 x (I) \ theta_ {0} + \ theta_ {1} x ^ {(I)} theta. Theta 0 + 1 x (I), can consider for the multiplication of matrix and vector: [1] x12x23x3… 47 x 2 [theta. Theta 1 2] 2 x 1 = \ begin 1 & x_ {bmatrix} {1} \ \ 2 & x_ {2} \ \ 3 & x_ {3} \ \… &… 47 \ \ end {bmatrix} _ {2} times \ begin \ theta_ {bmatrix} {1} \ \ \ theta_ {2} \ end {bmatrix} _ {1} 2 \ times = ⎣ ⎢ ⎢ ⎢ ⎡ 123… x1x2x3… ⎦ ⎥ ⎥ ⎥ ⎤ 47 x 2 [theta. Theta 1 2] 2 x = 1

        From this step we can see that our X is a column vector, and now we want to convert it to a 47 by 2 matrix with all 1’s in the first column, using the following code:

        o = ones(47.1);
        feature = [o,feature];
        Copy the code
      • The result of the previous step is going to be a 47 by 1 column vector, minus the y vector.

      • And then you just take the sum of the squares of the y vector.

    • The final function looks like this:
    function J = cost_function(X,y,theta) m = length(y); M predict = X*theta; X* Theta result = (predict-y).^2; % subtraction squared J =1/ (2*m) * sum(result); Sum and divide the result by %2m
        endfunction
    Copy the code
  4. I put the data and functions in the current directory.

    Now the complete step is to make the data into two files, feature. TXT and price. TXT, and make the functions into cost_function.m and put them in the current directory. The full code for Octave looks like this:

    load feature.txt
    load price.txt
    feature = feature(:,1:1);
    o = ones(47.1);
    feature = [o,feature];
    theta = [0;1]; Cost_function (feature,price,theta) %Copy the code

    Note: my test is theta. Theta 0 = 0, theta. 1 = 1 \ theta_0 = 0, \ theta_1 = 1 theta 0 = 0, theta. 1 = 1, you need to modify the theta.


The test data

Size and number of rooms

2104 3 1600 3 2400 3 1416 2 3000 4 1985 4 1534 3 1427 3 1380 3 1494 3 1940 4 2000 3 1890 3 4478 5 1268 3 2300 4 1320 2 1236 3 2609 4 3031 4 1767 3 1888 2 1604 3 1962 4 3890 3 1100 3 1458 3 2526 3 2200 3 2637 3 1839 2 1000 1 2040 4 3137 3 1811 4 1437 3 1239 3 2132 4 4215 4 2162 4 1664 2 2238 3 2567 4 1200 3 852 2 1852 4 1203 3 Copy codeCopy the code

House prices

399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
239999
347000
329999
699900
259900
449900
299900
199900
499998
599000
252900
255000
242900
259900
573900
249900
464500
469000
475000
299900
349900
169900
314900
579900
285900
249900
229900
345000
549000
287000
368500
329900
314000
299000
179900
299900
239500
Copy the code