Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quickgrid/b89662fd10b23de0bc7ebe8dce32b192 to your computer and use it in GitHub Desktop.
Save quickgrid/b89662fd10b23de0bc7ebe8dce32b192 to your computer and use it in GitHub Desktop.
Simple c++ program to fit a line though given points in 2-space. This implementation is made following the machine learning course on coursera by Andrew NG, linear regression gradient descent. It uses tolerance level to terminate execution.
/**
* Author: Asif Ahmed
* Site: https://quickgrid.blogspot.com
* Problem: Linear Regression gradient descent curve fitting though points in 2-space.
* Details: Please remove windows.h header if any trouble with compilation.
*/
#include<bits/stdc++.h>
#include<windows.h>
#define M 5
#define TOLERANCE 0.3
int x[M] = {1, 2, 3, 4, 5};
int y[M] = {2, 4, 6, 6, 10};
float alpha = 0.1;
float J(float theta0, float theta1){
float Jerror = 0;
for(int i = 0; i < M; ++i){
Jerror = Jerror + ( y[i] - theta0 - ( theta1 * x[i] ) ) * ( y[i] - theta0 - ( theta1 * x[i] ) );
}
return Jerror / ( 2 * M );
}
int main(){
float theta0 = 0;
float theta1 = 0;
printf("Initial Hypothesis: y = %f + %fx\n", theta0, theta1);
printf("Error: %f\n", J(theta0, theta1) );
while( J(theta0, theta1) > TOLERANCE ){
//
float temp0 = 0;
for(int i = 0; i < M; ++i){
temp0 = temp0 - ( y[i] - theta0 - (theta1 * x[i]) );
}
temp0 = temp0 / M;
//
float temp1 = 0;
for(int i = 0; i < M; ++i){
temp1 = temp1 - ( ( y[i] - theta0 - (theta1 * x[i]) ) * x[i] );
}
temp1 = temp1 / M;
//Sleep(1000);
//printf("DEBUG\n");
//
theta0 = theta0 - alpha * temp0;
theta1 = theta1 - alpha * temp1;
}
printf("DONE\n");
printf("Initial Hypothesis: y = %f + %fx\n", theta0, theta1);
printf("Error: %f\n", J(theta0, theta1) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment