Skip to content

Instantly share code, notes, and snippets.

View digorithm's full-sized avatar
⚒️
Building

Rodrigo Araújo digorithm

⚒️
Building
View GitHub Profile
package GraphModels;
/**
*
* @author rodrigo
*/
public class Vertex {
/* This Data Structure will be capable to represent a Vertex,
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package GraphModels;
/**
*
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package GraphModels;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package graphproject;
import GraphModels.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import org.math.plot.*;
public class InitialData{
public static double[] x = {2, 4, 6, 8};
public static double[] y = {2, 5, 5, 8};
public void plotData(){
Plot2DPanel plot = new Plot2DPanel();
import javax.swing.JFrame;
import org.math.plot.*;
public class GradientDescent{
private double theta0;
private double theta1;
private int trendline;
// Algorithm settings
public double hypothesisFunction(double x){
return this.theta1*x + theta0;
}
public double deriveTheta1(){
double sum = 0;
for (int j=0; j<initial_data.x.length; j++){
sum += (initial_data.y[j] - hypothesisFunction(initial_data.x[j])) * initial_data.x[j];
}
return -2 * sum / initial_data.x.length;
}
public double deriveTheta0(){
public void execute(){
do {
this.theta1 -= alpha * deriveTheta1();
this.theta0 -= alpha * deriveTheta0();
//used for plotting
tplot[iters] = iters;
theta0plot[iters] = theta0;
theta1plot[iters] = theta1;
public void addTrendLine(Plot2DPanel plot, boolean removePrev){
if (removePrev){
plot.removePlot(trendline);
}
double[] yEnd = new double[initial_data.x.length];
for (int i=0; i<initial_data.x.length; i++)
yEnd[i] = hypothesisFunction(initial_data.x[i]);
trendline = plot.addLinePlot("final", initial_data.x, yEnd);
}