5 kyu

Simple Linear Regression Cost Function (Machine Learning)

293 of 297Ban-Ath
Description
Loading description...
Machine Learning
Algorithms
Mathematics
Data Science
  • Please sign in or sign up to leave a comment.
  • Et9797 Avatar

    Rounding issues should be fixed with this kata. The user is not provided feedback on the training set used in testing. It should show expected as well as user answer, now only showing expected

    Expected costFunctionJ(trainingSet2, 1, 1.1) to equal 10.028

    Anyone stumbling upon the same problem: use Math.round(J * 1000) / 1000 for rounding to 3 decimals, not toFixed(3)

  • dfhwze Avatar

    The example in the description isn't that well picked. It doesn't show the full glory of h(x)

  • user8436785 Avatar

    Test.expect(Math.abs(costFunctionJ(trainingSet, 1, 1) - 1.5) < 0.0005, "Expected costFunctionJ(trainingSet, 0.5, 1) to equal 1.625");

    You expect it to equal 1.5. The error message is wrong.

  • user8436785 Avatar

    Tests shouldnt use expect.

  • user8436785 Avatar

    Needs random tests.

  • marcobrx Avatar

    This is nice, a first step into supervised learning. I added a translation in C++.

  • jankapunkt Avatar

    Basic math question on the example: Isn't ((0.5 + 9) - 8)^2 supposed to be 2.25 and not 6.25? I mean neither the naive approach nor binomic formula result in 6.25 for (9.5 - 8)². Am I missing something here, that is about correct setting of braces or something?

  • db222 Avatar

    Hey, just wanted to reiterate issues that have been pointed out, in hopes this kata gets these fixes.

    1st - mentioned previously by Sifawm

    "I saw two errors in this kata: 
      The example is wrong:
      1- The fifth one would be, ((0.5 + 9) - 8)^2 = 6.25 
      2- The sixth and final one would be, ((0.5 + 9) - 11)^2 = 0.25
    
      Both are 2.25:
      1- ((0.5 + 9) - 8)^2 = (1.5)^2 = 2.25
      2- ((0.5 + 9) - 11)^2 = (-1.5)^2 = 2.25
    
    And, for the last test case, 10.0275 rounded is 10.028 but the 
    difference is 0.0005. For this: 
    Math.abs(costFunctionJ(trainingSet, 1, 3.8) - 140.01) < 0.0005 => 0.0005 < 0.0005 
    is False, but the result is correct. 
    This test case is wrong. The correct is <= 0.0005"
        
        
    

    2nd - This second issue was brought up by JulianNicholls, in response constablebrew put forth the formatting J(θ1, θ2) = 1/(2 ⋅ m) ⋅ Σ( (h(x) - y)2 ). I see Ban-Ath that in response you choose to leave the equation as is because it stays true to material you are reading. If you would compare this

                 1 
    J(θ1, θ2) = ---  *  Σ( (h(x) - y)2 )      vs     J(θ1, θ2) = 1/2m * Σ( (h(x) - y)2 )
                 2m
                      
    

    The left is probably how it is formatted in your readings and is very clear. The right does read as 0.5 * m so I hope you reconsider and go with

                          J(θ1, θ2) = 1/(2 ⋅ m) ⋅ Σ( (h(x) - y)2 )
                         
    

    Thanks for listening.

  • tansaku Avatar

    I've done a related kata that involves implementing both gradient descent and a cost function - I only just saw this one after I submitted mine :-)

    http://www.codewars.com/kata/linear-regression-with-one-variable/ruby

  • IVBakker Avatar

    I might be not awake but: The sixth and final one would be, ((0.5 + 9) - 11)^2 = 0.25 is wrong: (9.5-11)^2=(-1.5)^2=2.25

  • wiennat Avatar

    The sample test case in the guide is incorrect.

  • jmtt89 Avatar

    This comment has been hidden.

  • GuardianGI Avatar

    a fun expansion would be to be send directly to the next part (after completing this kata) where you get to implement the gradient descent algorithm, I've done this once for a CS course, and at the time didn't understand it too well (+ it was in a weird language I barely understood), it would be fun to do in JS (I think)

    as this kata stands now, it's a fun introduction, but it doesn't really teach anything about machine learning :/

  • user3025892 Avatar

    This comment has been hidden.

  • JulianNicholls Avatar

    I think that you need to be a little clearer about the bracket / exponentiation placement in the description. I did interpret it correctly, but maybe something like this

    J(θ1, θ2) = (SumOf (h(x) - y)^2) / 2m      where
    h(x) = θ1 + θ2 * x                         and
    m = Number of co-ordinate pairs
    

    is better. YMMV :-)

  • nklein Avatar

    In some other Kata, my code didn't work because I rounded to three decimal places using Math.round(1000*n)/1000 instead of n.toFixed(3).

    Here, with this Kata, n.toFixed(3) doesn't work because it says that 1.5 isn't equal to 1.500, but Math.round(1000*n)/1000 does work.

    I think Test cases for Katas that require rounding should do the rounding in the test case, provide a rounding function we should use, or just check that Math.abs(actual - expected) < 0.0005.