I'm trying to implement this code for Linear Regression here but it is unable to compile due to std::sort
returning several errors.
#include "LinearRegression.h"
#include <iostream>
#include <algorithm>
#include <vector>
bool LinearRegression::custom_sort(double a, double b) /*sorts based on absolute min value or error*/
{
double a1 = abs(a-0);
double b1 = abs(b-0);
return a1<b1;
}
void LinearRegression::predict()
{
/*Intialization Phase*/
double x[] = { 1, 2, 4, 3, 5 }; //defining x values
double y[] = { 1, 3, 3, 2, 5 }; //defining y values
double err;
double b0 = 0; //initializing b0
double b1 = 0; //initializing b1
double alpha = 0.01; //intializing error rate
std::vector<double>error; // array to store all error values
/*Training Phase*/
for (int i = 0; i < 20; i++) //
{
int idx = i % 5; //for accessing index after every epoch
double p = b0 + b1 * x[idx]; //calculate prediction
err = p - y[idx]; //calculate error
b0 = b0 - alpha * err; //update b0
b1 = b1 - alpha * err * x[idx]; // updating b1
std::cout << "B0=" << b0 << " " << "B1=" << b1 << " " << "error=" << err << std::endl; //print values after every update
error.push_back(err);
}
std::sort(error.begin(),error.end(),custom_sort); //sorting based on error values
std::cout << "Final Values are: " << "B0=" << b0 << " " << "B1=" << b1 << " " << "error=" << error[0] << std::endl;
/*Testing Phase*/
std::cout << "Enter a test x value";
double test;
std::cin >> test;
double pred = b0 + b1 * test;
std::cout << std::endl;
std::cout << "The value predicted by the model= " << pred;
}
I get the errors:
non-standard syntax; use '&' to create a pointer to member
and
no matching overloaded function found
for this line
std::sort(error.begin(),error.end(),custom_sort);