-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (141 loc) · 4.96 KB
/
Copy pathmain.cpp
File metadata and controls
171 lines (141 loc) · 4.96 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include "models/weather_model.h"
using namespace std;
// Read the historical weather data from a CSV file
bool ReadData(const string& filename, vector<vector<float>>& features, vector<float>& labels) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Unable to open file: " << filename << endl;
return false;
}
string line;
int line_num = 0;
while (getline(file, line)) {
line_num++;
if (line.empty()) continue;
stringstream ss(line);
string field;
vector<float> feature;
float label;
try {
int i = 0;
while (getline(ss, field, ',')) {
if (i == 0) {
label = stof(field);
} else {
feature.push_back(stof(field));
}
i++;
}
// Skip lines with insufficient data
if (feature.empty()) {
cerr << "Warning: Skipping line " << line_num << " due to insufficient data" << endl;
continue;
}
features.push_back(feature);
labels.push_back(label);
} catch (const std::invalid_argument& e) {
cerr << "Warning: Invalid data at line " << line_num << ", skipping. Error: " << e.what() << endl;
}
}
if (features.empty()) {
cerr << "Error: No valid data found in file: " << filename << endl;
return false;
}
return true;
}
// Split data into training and validation sets
void SplitData(const vector<vector<float>>& features, const vector<float>& labels,
vector<vector<float>>& train_features, vector<float>& train_labels,
vector<vector<float>>& val_features, vector<float>& val_labels,
float val_ratio = 0.2) {
// Create indices
vector<size_t> indices(features.size());
for (size_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
// Shuffle indices
random_shuffle(indices.begin(), indices.end());
// Calculate split point
size_t val_size = size_t(val_ratio * features.size());
// Split data
for (size_t i = 0; i < indices.size(); i++) {
size_t idx = indices[i];
if (i < val_size) {
val_features.push_back(features[idx]);
val_labels.push_back(labels[idx]);
} else {
train_features.push_back(features[idx]);
train_labels.push_back(labels[idx]);
}
}
}
int main() {
try {
cout << "Weather Prediction System starting up...\n";
// Read the historical weather data
vector<vector<float>> features;
vector<float> labels;
string data_file = "weather.csv";
cout << "Reading data from " << data_file << "..." << endl;
if (!ReadData(data_file, features, labels)) {
cerr << "Failed to read data. Exiting." << endl;
return 1;
}
cout << "Successfully loaded " << features.size() << " data points." << endl;
// Validate all feature vectors have the same size
size_t feature_size = features[0].size();
for (size_t i = 1; i < features.size(); i++) {
if (features[i].size() != feature_size) {
cerr << "Error: Inconsistent feature vector size at index " << i << endl;
return 1;
}
}
// Split data into training and validation sets
vector<vector<float>> train_features, val_features;
vector<float> train_labels, val_labels;
SplitData(features, labels, train_features, train_labels, val_features, val_labels);
cout << "Split data: " << train_features.size() << " training samples, "
<< val_features.size() << " validation samples" << endl;
// Train the weather forecasting model
cout << "Initializing model..." << endl;
WeatherModel model(feature_size);
if (!model.GetLastError().empty()) {
cerr << "Model initialization failed: " << model.GetLastError() << endl;
return 1;
}
cout << "Training model..." << endl;
model.Train(train_features, train_labels, 100);
if (!model.GetLastError().empty()) {
cerr << "Training failed: " << model.GetLastError() << endl;
return 1;
}
// Validate the model
cout << "Validating model..." << endl;
float total_error = 0.0f;
for (size_t i = 0; i < val_features.size(); i++) {
float prediction = model.Predict(val_features[i]);
total_error += fabs(prediction - val_labels[i]);
}
float avg_error = total_error / val_features.size();
cout << "Validation MAE: " << avg_error << endl;
// Use the model for a new prediction
cout << "Making a new prediction..." << endl;
vector<float> input = {2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
float prediction = model.Predict(input);
if (!model.GetLastError().empty()) {
cerr << "Prediction failed: " << model.GetLastError() << endl;
return 1;
}
cout << "Temperature prediction: " << prediction << endl;
return 0;
} catch (const std::exception& e) {
cerr << "Unhandled exception: " << e.what() << endl;
return 1;
}
}