Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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;

/**
* 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
*/
public LinearRegression(ArrayList<Double> dependentX,
ArrayList<Double> independentY) {
this.dependentX = dependentX;
this.independentY = independentY;
this.Equate();
}

private double Sumation(ArrayList<Double> arr) {
double sum = 0.0;

for (int i = 0; i < arr.size(); i++) {
sum += arr.get(i);
}

return sum;
}

private ArrayList<Double> MultiplyNumber(ArrayList<Double> arr1,
ArrayList<Double> arr2) {
ArrayList<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