what is wrong with this code #include "src/XGBRegressor.h" // to include the generated…
- User
what is wrong with this code
#include "src/XGBRegressor.h" // to include the generated XGBReg model header file #include #include #include #include #include #include #include
struct DataRecord { int dayOfWeek; int season; double timeOfDay; int zoneID; double temperature; double humidity; double illuminance; double eCO2; double tvoc; double sound; double aqi; double overallScore; // The actual score to compare with };
// Function to read the CSV file and populate the data std::vector readCSV(const std::string& filename) { std::ifstream file(filename); std::vector data; std::string line;
// Skip the header std::getline(file, line); while (std::getline(file, line)) { std::stringstream ss(line); DataRecord record; std::string temp; // Parse each value from the CSV and store it in the record std::getline(ss, temp, ','); record.dayOfWeek = std::stoi(temp); std::getline(ss, temp, ','); record.season = std::stoi(temp); std::getline(ss, temp, ','); record.timeOfDay = std::stod(temp); std::getline(ss, temp, ','); record.zoneID = std::stoi(temp); std::getline(ss, temp, ','); record.temperature = std::stod(temp); std::getline(ss, temp, ','); record.humidity = std::stod(temp); std::getline(ss, temp, ','); record.illuminance = std::stod(temp); std::getline(ss, temp, ','); record.eCO2 = std::stod(temp); std::getline(ss, temp, ','); record.tvoc = std::stod(temp); std::getline(ss, temp, ','); record.sound = std::stod(temp); std::getline(ss, temp, ','); record.aqi = std::stod(temp); std::getline(ss, temp, ','); record.overallScore = std::stod(temp); data.push_back(record); } return data;}
// Function to randomly sample 20% of the data std::vector sampleData(const std::vector& data, double sampleFraction = 0.2) { std::vector sampledData = data; std::random_device rd; std::mt19937 g(rd()); std::shuffle(sampledData.begin(), sampledData.end(), g);
size_t sampleSize = static_cast<size_t>(sampleFraction * data.size()); sampledData.resize(sampleSize); return sampledData;}
// Predict the overall score using the trained XGBoost model float predictOverallScore(int dayOfWeek, int season, float timeOfDay, int zoneID, float temperature, float humidity, float illuminance, float eCO2, float tvoc, float sound, float aqi) { double input_features[] = { static_cast(dayOfWeek), static_cast(season), timeOfDay, static_cast(zoneID), temperature, humidity, illuminance, eCO2, tvoc, sound, aqi }; return score(input_features); // Use the XGBRegressor model's score function }
// Function to export prediction and actual results to a CSV for plotting void exportData(const std::vector& actual, const std::vector& predicted) { std::ofstream outFile("predictions.csv"); outFile << "Actual,Predicted\n"; for (size_t i = 0; i < actual.size(); ++i) { outFile << actual[i] << "," << predicted[i] << "\n"; } outFile.close(); }
- Assistant