forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression.java
72 lines (57 loc) · 2.21 KB
/
LinearRegression.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.thealgorithms.machinelearning;
import java.util.ArrayList;
import java.util.List;
/**
* Author : Gowtham Kamalasekar
* LinkedIn : https://www.linkedin.com/in/gowtham-kamalasekar/
*
* Wiki : https://en.wikipedia.org/wiki/Linear_regression
* Linear Regression Machine Learning Algorithm is a regression algorithm.
* This programs used for computing y = mx + c
* Where m is slope and c is intercept
* We can use this too predict for a given x.
*/
class LinearRegression {
private ArrayList<Double> dependentX = new ArrayList<Double>();
private ArrayList<Double> independentY = new ArrayList<Double>();
private double m;
private double c;
/**
* @param : X (dependent variable), Y (independent variable) as ArrayList
*/
LinearRegression(ArrayList<Double> dependentX, ArrayList<Double> independentY) {
this.dependentX = dependentX;
this.independentY = independentY;
this.equate();
}
private double sumation(List<Double> arr) {
double sum = 0.0;
for (int i = 0; i < arr.size(); i++) {
sum += arr.get(i);
}
return sum;
}
private List<Double> multiplyNumber(List<Double> arr1, List<Double> arr2) {
List<Double> temp = new ArrayList<Double>();
for (int i = 0; i < arr1.size(); i++) {
temp.add((arr1.get(i) * arr2.get(i)));
}
return temp;
}
private void equate() {
int n = dependentX.size();
this.m = (n * sumation(multiplyNumber(independentY, dependentX)) - (sumation(dependentX) * sumation(independentY)));
this.m = this.m / (n * (sumation(multiplyNumber(dependentX, dependentX))) - (sumation(dependentX) * sumation(dependentX)));
this.c = (sumation(independentY) * sumation(multiplyNumber(dependentX, dependentX)) - (sumation(dependentX) * sumation(multiplyNumber(independentY, dependentX))));
this.c = this.c / (n * (sumation(multiplyNumber(dependentX, dependentX))) - (sumation(dependentX) * sumation(dependentX)));
}
public double getM() {
return this.m;
}
public double getC() {
return this.c;
}
public double predictForX(double x) {
return (this.m * x) + this.c;
}
}