We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<iomanip>usingnamespacestd;doubledot_product(vector<double>x,vector<double>y){doublesum=0;for(inti=0;i<x.size();++i){sum+=x[i]*y[i];}returnsum;}doublesum(vector<double>x,intp=1){doublesum=0;for(inti=0;i<x.size();i++)sum+=pow(x[i],p);returnsum;}doublemean(vector<double>x){doubletotal=sum(x);returntotal/x.size();}doublelinear_regression(vector<double>x,vector<double>y,doublet){doublen=double(x.size());doublesumX=sum(x,1);doublesumX2=sum(x,2);doublesumY=sum(y,1);doublemeanX=mean(x);doublemeanY=mean(y);doubleb=(n*dot_product(x,y)-sumX*sumY)/(n*sumX2-sumX*sumX);doublea=meanY-b*meanX;returna+b*t;}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */inta=5;vector<double>math(a);vector<double>stat(a);doublem,s;for(inti=0;i<a;++i){cin>>m>>s;math.push_back(m);stat.push_back(s);}doublemath_grade=80;doublestat_grade=linear_regression(math,stat,math_grade);cout<<fixed<<setprecision(3);cout<<stat_grade<<endl;return0;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 8: Least Square Regression Line
You are viewing a single comment's thread. Return to all comments →
C++