|
3 | 3 | import java.util.ArrayList;
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| -* Author : Gowtham Kamalasekar |
7 |
| -* LinkedIn : https://www.linkedin.com/in/gowtham-kamalasekar/ |
8 |
| -* |
9 |
| -* Linear Regression Machine Learning Algorithm is a regression algorithm. |
10 |
| -* This programs used for computing y = mx + c |
11 |
| -* Where m is slope and c is intercept |
12 |
| -* We can use this too predict for a given x. |
13 |
| -*/ |
| 6 | + * Author : Gowtham Kamalasekar |
| 7 | + * LinkedIn : https://www.linkedin.com/in/gowtham-kamalasekar/ |
| 8 | + * |
| 9 | + * Linear Regression Machine Learning Algorithm is a regression algorithm. |
| 10 | + * This programs used for computing y = mx + c |
| 11 | + * Where m is slope and c is intercept |
| 12 | + * We can use this too predict for a given x. |
| 13 | + */ |
14 | 14 |
|
15 | 15 | class LinearRegression {
|
16 |
| - private ArrayList<Double> dependentX = new ArrayList<Double>(); |
17 |
| - private ArrayList<Double> independentY = new ArrayList<Double>(); |
18 |
| - private double m; |
19 |
| - private double c; |
| 16 | + private ArrayList<Double> dependentX = new ArrayList<Double>(); |
| 17 | + private ArrayList<Double> independentY = new ArrayList<Double>(); |
| 18 | + private double m; |
| 19 | + private double c; |
20 | 20 |
|
21 |
| - /** |
22 |
| - * @param : X (dependent variable), Y (independent variable) as ArrayList |
23 |
| - */ |
24 |
| - public LinearRegression(ArrayList<Double> dependentX, ArrayList<Double> independentY) { |
25 |
| - this.dependentX = dependentX; |
26 |
| - this.independentY = independentY; |
27 |
| - this.Equate(); |
28 |
| - } |
29 |
| - |
30 |
| - private double Sumation(ArrayList<Double> arr){ |
31 |
| - double sum = 0.0; |
| 21 | + /** |
| 22 | + * @param : X (dependent variable), Y (independent variable) as ArrayList |
| 23 | + */ |
| 24 | + public LinearRegression(ArrayList<Double> dependentX, |
| 25 | + ArrayList<Double> independentY) { |
| 26 | + this.dependentX = dependentX; |
| 27 | + this.independentY = independentY; |
| 28 | + this.Equate(); |
| 29 | + } |
32 | 30 |
|
33 |
| - for(int i = 0; i < arr.size(); i++){ |
34 |
| - sum += arr.get(i); |
35 |
| - } |
| 31 | + private double Sumation(ArrayList<Double> arr) { |
| 32 | + double sum = 0.0; |
36 | 33 |
|
37 |
| - return sum; |
| 34 | + for (int i = 0; i < arr.size(); i++) { |
| 35 | + sum += arr.get(i); |
38 | 36 | }
|
39 | 37 |
|
40 |
| - private ArrayList<Double> MultiplyNumber(ArrayList<Double> arr1, ArrayList<Double> arr2) { |
41 |
| - ArrayList<Double> temp = new ArrayList<Double>(); |
42 |
| - for(int i = 0; i < arr1.size(); i++) { |
43 |
| - temp.add((arr1.get(i) * arr2.get(i))); |
44 |
| - } |
45 |
| - return temp; |
| 38 | + return sum; |
| 39 | + } |
| 40 | + |
| 41 | + private ArrayList<Double> MultiplyNumber(ArrayList<Double> arr1, |
| 42 | + ArrayList<Double> arr2) { |
| 43 | + ArrayList<Double> temp = new ArrayList<Double>(); |
| 44 | + for (int i = 0; i < arr1.size(); i++) { |
| 45 | + temp.add((arr1.get(i) * arr2.get(i))); |
46 | 46 | }
|
| 47 | + return temp; |
| 48 | + } |
47 | 49 |
|
48 |
| - private void Equate(){ |
49 |
| - int n = dependentX.size(); |
50 |
| - this.m = (n * Sumation(MultiplyNumber(independentY, dependentX)) - (Sumation(dependentX) * Sumation(independentY))); |
51 |
| - this.m = this.m / (n * (Sumation(MultiplyNumber(dependentX, dependentX))) - (Sumation(dependentX) * Sumation(dependentX))); |
| 50 | + private void Equate() { |
| 51 | + int n = dependentX.size(); |
| 52 | + this.m = (n * Sumation(MultiplyNumber(independentY, dependentX)) - |
| 53 | + (Sumation(dependentX) * Sumation(independentY))); |
| 54 | + this.m = this.m / (n * (Sumation(MultiplyNumber(dependentX, dependentX))) - |
| 55 | + (Sumation(dependentX) * Sumation(dependentX))); |
52 | 56 |
|
53 |
| - this.c = (Sumation(independentY) * Sumation(MultiplyNumber(dependentX, dependentX)) - (Sumation(dependentX) * Sumation(MultiplyNumber(independentY, dependentX)))); |
54 |
| - this.c = this.c / (n * (Sumation(MultiplyNumber(dependentX, dependentX))) - (Sumation(dependentX) * Sumation(dependentX))); |
55 |
| - } |
| 57 | + this.c = (Sumation(independentY) * |
| 58 | + Sumation(MultiplyNumber(dependentX, dependentX)) - |
| 59 | + (Sumation(dependentX) * |
| 60 | + Sumation(MultiplyNumber(independentY, dependentX)))); |
| 61 | + this.c = this.c / (n * (Sumation(MultiplyNumber(dependentX, dependentX))) - |
| 62 | + (Sumation(dependentX) * Sumation(dependentX))); |
| 63 | + } |
56 | 64 |
|
57 |
| - public double getM(){ |
58 |
| - return this.m; |
59 |
| - } |
| 65 | + public double getM() { return this.m; } |
60 | 66 |
|
61 |
| - public double getC(){ |
62 |
| - return this.c; |
63 |
| - } |
| 67 | + public double getC() { return this.c; } |
64 | 68 |
|
65 |
| - public double PredictForX(double x) { |
66 |
| - return (this.m * x) + this.c; |
67 |
| - } |
| 69 | + public double PredictForX(double x) { return (this.m * x) + this.c; } |
68 | 70 | }
|
0 commit comments