-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictor.cpp
More file actions
99 lines (77 loc) · 2.81 KB
/
Predictor.cpp
File metadata and controls
99 lines (77 loc) · 2.81 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
#include <iostream>
#include <fstream>
#include <curl/curl.h>
#include <mlpack/core.hpp>
#include <mlpack/methods/linear_regression/linear_regression.hpp>
#include <mlpack/methods/ridge_regression/ridge_regression.hpp>
using namespace mlpack;
using namespace mlpack::regression;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
void fetchData(const std::string& url, const std::string& filename) {
CURL* curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
std::ofstream outFile(filename);
outFile << readBuffer;
outFile.close();
} else {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
}
}
void preprocessData(const std::string& inputFile, const std::string& outputFile) {
std::ifstream inFile(inputFile);
std::ofstream outFile(outputFile);
std::string line;
std::getline(inFile, line); // Skip the header
while (std::getline(inFile, line)) {
for (char& c : line) {
if (c == ',') {
c = ' ';
}
}
outFile << line << std::endl;
}
inFile.close();
outFile.close();
}
void trainModel(const std::string& filename) {
arma::mat dataset;
data::Load(filename, dataset, true);
arma::rowvec responses = dataset.row(dataset.n_rows - 1);
dataset.shed_row(dataset.n_rows - 1);
RidgeRegression lr(dataset, responses, 0.1); // L2 regularization with lambda = 0.1
data::Save("ridge_regression_model.xml", "model", lr);
}
void predict(const std::string& modelFile, const arma::mat& testSet) {
RidgeRegression lr;
data::Load(modelFile, "model", lr);
arma::rowvec predictions;
lr.Predict(testSet, predictions);
std::cout << "Predictions: " << predictions << std::endl;
}
int main() {
std::string apiKey = "your_api_key";
std::string symbol = "RELIANCE.BSE";
std::string url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + symbol + "&apikey=" + apiKey + "&datatype=csv";
std::string rawFilename = "raw_data.csv";
std::string processedFilename = "data.csv";
fetchData(url, rawFilename);
preprocessData(rawFilename, processedFilename);
trainModel(processedFilename);
arma::mat testSet;
data::Load(processedFilename, testSet, true);
predict("ridge_regression_model.xml", testSet);
return 0;
}