Skip to content

Commit fbf7a77

Browse files
authored
Update LinearRegression.java with clang format
Clang formatted
1 parent 6959433 commit fbf7a77

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

src/main/java/com/thealgorithms/machinelearning/LinearRegression.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,68 @@
33
import java.util.ArrayList;
44

55
/**
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+
*/
1414

1515
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;
2020

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+
}
3230

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;
3633

37-
return sum;
34+
for (int i = 0; i < arr.size(); i++) {
35+
sum += arr.get(i);
3836
}
3937

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)));
4646
}
47+
return temp;
48+
}
4749

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)));
5256

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+
}
5664

57-
public double getM(){
58-
return this.m;
59-
}
65+
public double getM() { return this.m; }
6066

61-
public double getC(){
62-
return this.c;
63-
}
67+
public double getC() { return this.c; }
6468

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; }
6870
}

0 commit comments

Comments
 (0)