-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearRegression.cpp
115 lines (87 loc) · 3.99 KB
/
LinearRegression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ============================================================================
// <copyright file="LinearRegression.cpp" company="VRK">
// Copyright (c) Venkata 2020. All rights reserved.
// </copyright>
// ============================================================================
// ============================================================================
// Local includes, e.g. files in the same folder, typically corresponding declarations.
#include "LinearRegression.h"
// ============================================================================
// System includes, e.g. STL.
#include <cassert>
#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iomanip>
// ============================================================================
// Other includes with complete path as Layer/Component/third party and so on...
// ============================================================================
// Definitions
// =============================================================================
// namespace declarations;
using namespace std;
using namespace VRK;
double LinearRegression::ComputeCost(std::vector<std::vector<double>> X,
std::vector<double> Y,
vector<double> theta) {
int numberOfExamples = Y.size();
vector<double> vecPredictions(numberOfExamples, 0);
/*
calculate predictions using below formula
ypredction = x0 * theta0 + feature1 * theta1 + .... + featuren * thetan
*/
for (unsigned int inputIdx = 0; inputIdx < numberOfExamples; inputIdx++) {
vecPredictions[inputIdx] = std::inner_product(X[inputIdx].begin(), X[inputIdx].end(), theta.begin(), 0.0);
}
double cost = std::inner_product(vecPredictions.begin(), vecPredictions.end(), Y.begin(), 0.0,
[](auto a, auto b) { return a + b; },
[](auto a, auto b) { return (a - b) * (a - b); });
return (cost / (2.0 * numberOfExamples));
}
vector<double> LinearRegression::GradientDescentMulipleFeatures(std::vector<std::vector<double>> X,
std::vector<double> Y,
int numberOfIterations) {
// check input assumptions.
assert(X.size() > 0);
assert(Y.size() > 0);
assert(X[0].size() > 0);
int numberOfExamples = Y.size();
int numberOfFeatures = X[0].size();
int iCurrentIteration = 0;
double prevCost = 0.0;
double currCost = 100.0;
vector<double> theta(numberOfFeatures, 0);
for (; abs(currCost - prevCost)> 0.0001; iCurrentIteration++) {
prevCost = currCost;
vector<double> vecPredictions(numberOfExamples, 0);
/*
calculate predictions using below formula
ypredction = x0 * theta0 + feature1 * theta1 + .... + featuren * thetan
*/
for (unsigned int inputIdx = 0; inputIdx < numberOfExamples; inputIdx++) {
vecPredictions[inputIdx] = std::inner_product(X[inputIdx].begin(), X[inputIdx].end(), theta.begin(), 0.0);
}
/* calculate average error for each example.
avgErrorforfeatureidx = (1/numberofExamples) (summation of all examples of
(difference between prediction and actual value) * featureidxvalueof that example
*/
vector<double> slpDirection(numberOfFeatures, 0);
for (unsigned int featIdx = 0; featIdx < numberOfFeatures; featIdx++) {
int idx = 0;
slpDirection[featIdx] = std::inner_product(vecPredictions.begin(), vecPredictions.end(),
Y.begin(), 0.0,
[](auto a, auto b) { return a + b; },
[&X, &idx, &featIdx](auto a, auto b) { return (a - b) * X[idx++][featIdx]; });
}
double stepSize = m_stepSize;
int idx = 0;
std::transform(theta.begin(), theta.end(), theta.begin(),
[&idx, stepSize, numberOfExamples, &slpDirection](auto a) { return (a - ((stepSize / numberOfExamples) * slpDirection[idx++])); });
currCost = ComputeCost(X, Y, theta);
vecCost.push_back(currCost);
}
return theta;
}