Skip to content

Implementing Linear Regression in Java and creating Machine Learning algorithms #6045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.machinelearning;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import org.junit.jupiter.api.Test;

class LinearRegressionTest {

@Test
void testLinearRegression() {
ArrayList<Double> dependentX = new ArrayList<>();
ArrayList<Double> independentY = new ArrayList<>();

dependentX.add(1.0);
independentY.add(2.0);
dependentX.add(2.0);
independentY.add(3.0);
dependentX.add(3.0);
independentY.add(4.0);
dependentX.add(4.0);
independentY.add(5.0);
dependentX.add(5.0);
independentY.add(6.0);

// Create LinearRegression object
LinearRegression lr = new LinearRegression(dependentX, independentY);

// Check the slope (m) and intercept (c)
assertEquals(1.0, lr.getM(), 0.001);
assertEquals(1.0, lr.getC(), 0.001);

// Check prediction for X = 6
double predictedY = lr.predictForX(6.0);
assertEquals(7.0, predictedY, 0.001);
}
}
Loading