-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Regression.cpp
More file actions
196 lines (154 loc) · 5.23 KB
/
Copy pathLinear_Regression.cpp
File metadata and controls
196 lines (154 loc) · 5.23 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
#include <map>
#include <algorithm>
using namespace std;
class LabelEncoder {
map<string, double> mapping;
int next_code = 0;
public:
double encode(const string &s) {
if (mapping.find(s) == mapping.end()) {
mapping[s] = next_code++;
}
return mapping[s];
}
};
LabelEncoder weatherEnc, trafficEnc, priorityEnc, timeEnc, vehicleEnc;
vector<string> parseCSVLine(const string &line) {
vector<string> tokens;
string token;
bool inside_quotes = false;
for (char c : line) {
if (c == '"') {
inside_quotes = !inside_quotes;
} else if (c == ',' && !inside_quotes) {
tokens.push_back(token);
token.clear();
} else {
token += c;
}
}
tokens.push_back(token);
return tokens;
}
void parseLocation(string locStr, vector<double> &row) {
locStr.erase(remove(locStr.begin(), locStr.end(), '"'), locStr.end());
locStr.erase(remove(locStr.begin(), locStr.end(), '('), locStr.end());
locStr.erase(remove(locStr.begin(), locStr.end(), ')'), locStr.end());
stringstream ss(locStr);
string segment;
while(getline(ss, segment, ',')) {
try {
row.push_back(stod(segment));
} catch(...) { row.push_back(0.0); }
}
}
bool loadCSV(const string &filename, vector<vector<double>> &X, vector<double> &y) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Could not open file: " << filename << endl;
return false;
}
string line;
getline(file, line);
int lineNum = 1;
while (getline(file, line)) {
lineNum++;
if (line.empty() || line.back() == '\r') if(!line.empty()) line.pop_back();
if (line.empty()) continue;
vector<string> tokens = parseCSVLine(line);
if (tokens.size() < 15) continue;
vector<double> features;
double target = 0.0;
try {
parseLocation(tokens[1], features);
parseLocation(tokens[2], features);
features.push_back(stod(tokens[3]));
features.push_back(weatherEnc.encode(tokens[4]));
features.push_back(trafficEnc.encode(tokens[5]));
features.push_back(stod(tokens[6]));
features.push_back(priorityEnc.encode(tokens[7]));
features.push_back(timeEnc.encode(tokens[8]));
features.push_back(vehicleEnc.encode(tokens[9]));
features.push_back(stod(tokens[10]));
features.push_back(stod(tokens[11]));
target = stod(tokens[12]);
features.push_back(stod(tokens[13]));
features.push_back(stod(tokens[14]));
X.push_back(features);
y.push_back(target);
} catch (const exception &e) {
cerr << "Skipping line " << lineNum << ": Parsing error." << endl;
continue;
}
}
file.close();
return true;
}
void featureScaling(vector<vector<double>> &X, vector<double> &mean, vector<double> &stddev) {
if (X.empty()) return;
int n = X.size();
int m = X[0].size();
mean.assign(m, 0.0);
stddev.assign(m, 0.0);
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n; ++i) mean[j] += X[i][j];
mean[j] /= n;
}
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n; ++i) stddev[j] += pow(X[i][j] - mean[j], 2);
stddev[j] = sqrt(stddev[j] / n);
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (stddev[j] > 1e-9) X[i][j] = (X[i][j] - mean[j]) / stddev[j];
}
double predict(const vector<double> &row, const vector<double> &weights, double bias) {
double res = bias;
for (size_t i = 0; i < weights.size(); ++i) res += weights[i] * row[i];
return res;
}
void trainModel(vector<vector<double>> &X, vector<double> &y, vector<double> &weights, double &bias, double lr, int epochs) {
int n = X.size();
int m = X[0].size();
weights.assign(m, 0.0);
bias = 0.0;
vector<double> dw(m);
cout << "Training on " << n << " samples with " << m << " features..." << endl;
for (int i = 0; i < epochs; ++i) {
fill(dw.begin(), dw.end(), 0.0);
double db = 0.0;
double cost = 0.0;
for (int k = 0; k < n; ++k) {
double err = predict(X[k], weights, bias) - y[k];
cost += err * err;
for (int j = 0; j < m; ++j) dw[j] += err * X[k][j];
db += err;
}
for (int j = 0; j < m; ++j) weights[j] -= (lr * 2 / n) * dw[j];
bias -= (lr * 2 / n) * db;
if (i % 500 == 0) cout << "Epoch " << i << " Cost: " << cost/n << endl;
}
}
int main() {
vector<vector<double>> X;
vector<double> y;
if (!loadCSV("Food_Delivery_Time_Prediction.csv", X, y)) return -1;
vector<double> mean, stddev;
featureScaling(X, mean, stddev);
vector<double> weights;
double bias;
trainModel(X, y, weights, bias, 0.01, 3000);
cout << "\n--- Final Model ---\n";
cout << "Bias: " << bias << endl;
cout << "Weights: ";
for(double w : weights) cout << w << " ";
cout << endl;
return 0;
}