Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Gradient{
- def main(args :Array[String]):Unit=
- {
- println("welcome")
- var grad = new GraDescent();
- grad.implement();
- }
- }
- import scala.collection.mutable.ArrayBuffer
- class GraDescent
- {
- def implement()
- {
- val x = new ArrayBuffer[Int]
- val y = new ArrayBuffer[Int]
- x +=(1,2,3)
- y +=(4,5,6)
- val m =x.size
- val theta_0 = 1
- val theta_1 = 1
- val alpha =0.1
- var result =0
- for(i<-0 until x.length)
- {
- val x_i = theta_0 + (theta_1 * x(i))
- val s =x_i -y(i)
- result =result + s
- }
- println(result)
- val j=0
- if(j==0)
- {
- val theta_j =theta_0 - (alpha * (1/m) * result)
- println(theta_j);
- }
- else if(j==1)
- {
- for(i <- 0 until x.length)
- {
- val theta_j =theta_1 - ((alpha * (1/m) * result)*x(i))
- println(theta_j)
- }
- }
- else
- {
- println("enter a valid value of j");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement