Skip to content

Instantly share code, notes, and snippets.

@quickgrid
Last active December 21, 2016 15:36
Show Gist options
  • Save quickgrid/967513f65f87f08ae5964deaf2e9c552 to your computer and use it in GitHub Desktop.
Save quickgrid/967513f65f87f08ae5964deaf2e9c552 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 fixed iteration to terminate execution. Cpp program to try to fit a line though prime numbers. The goal is to minimize the error since, it is impo…
/**
* 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 10
#define MAX_ITERATIONS_COUNT 9000
int x[M] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int y[M] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
float alpha = 0.01;
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) );
int iter = 0;
while( iter < MAX_ITERATIONS_COUNT ){
//
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;
++iter;
}
printf("DONE\n");
printf("Final 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