Question 11 First Implement Algorithm Class Linearregression Constructor Class Receives Pa Q26403442

Question 1.1

We will first implement the algorithm in the classLinearRegression. The constructor of this classreceives as a

parameter the number of sample points with which it will work.These samples are then provided one by one using

the method addSample. Once all the samples areprovided, the method gradientDescent can becalled. That

method received two parameters : the step _ to use, andthe number of iterations to be performed during that call

of the method (the method will itself be called several times).The class provides a number of other methods that

are required for its own purpose or by other classes that youwill have to fill in the details of.

In order to test our implementation, we will first use a trivialsample set made of 1,000 values on the line y = x;

we will select the values from x = 0 to x =999. The method setLine of the classAssignment is what we will use

for this. This method should do the following.


• Create a LinearRegression object of the appropriate size tocreate a line consisting of the points (i, i) for

0 _ i _ 999, and create a corresponding displayobject.

• Iterate the algorithm a total of 5,000 times. We will do thisby performing gradient descent 100 times with a

small positive _ = 0.000000003 and numOfStepsset to 100, looped 50 times. At each iteration of the loop, you

should update the graph and print the current value of thehypothesis and cost function.

If our implementation is correct, the hypothesis function shouldtend toward the line y = x.

Your console output for the first few iterations might look likethis.

> java Assignment


Current hypothesis: 0.0+0.0x

Current cost: 332833.5

Press return to continue….

Current hypothesis:0.0012974389329410189+0.8645276608877351x

Current cost: 6108.235980006332

Press return to continue….

Current hypothesis: 0.001473201544681895+0.9816455619876667x

Current cost: 112.09973445673143

Press return to continue….

Figures 2, 3 and 4 show what the Display object representing thesystem looks like after different number of iterations.

The line in red is the current hypothesis. As you can see,initially the red line is y = 0, our starting point, andthe

more we iterate, the closer if gets to y =x.

Figure 2 – Initial situation, and after 100 iterations.

Figure 3 – After 200 and 300 iterations.

The classes and JavaDocs are found here :


Figure 4 – After 1000 and 3000 iterations.

• JavaDoc Documentation

• Assignment.java

• LinearRegression.java

• Display.java

• StudentInfo.java

You have to fill out all the missing methods to obtain thedescribed behaviour.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *