|
| 1 | +#include<vector> |
| 2 | +#include<fstream> |
| 3 | +#include<sstream> |
| 4 | +#include<iostream> |
| 5 | +#include<unordered_map> |
| 6 | +#include<initializer_list> |
| 7 | +#include "dataset.h" |
| 8 | + |
| 9 | + |
| 10 | +void DataSet::ReadCSVFile(std::string file_name) { |
| 11 | + |
| 12 | + std::ifstream file(file_name.c_str()); |
| 13 | + if (!file) { |
| 14 | + file.open(file_name.c_str()); |
| 15 | + } |
| 16 | + if (!file) { |
| 17 | + std::cerr << "Error No" << file_name << " found. " << std::endl; |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + std::stringstream buffer; |
| 22 | + buffer << file.rdbuf(); |
| 23 | + std::string line; |
| 24 | + std::vector<std::string>lines; |
| 25 | + while (getline(buffer, line, '\n')) { |
| 26 | + lines.push_back(line); |
| 27 | + } |
| 28 | + |
| 29 | + //the other lines contain the features for each floaer |
| 30 | + |
| 31 | + for (int i = 1; i < lines.size(); ++i) { |
| 32 | + std::vector<float>features =ReadCSVLine(lines[i]); |
| 33 | + x1_.push_back(features[0]); |
| 34 | + x2_.push_back(features[1]); |
| 35 | + x3_.push_back(features[2]); |
| 36 | + x4_.push_back(features[3]); |
| 37 | + y_.push_back(features[4]); |
| 38 | + |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +std::vector<float>DataSet::ReadCSVLine(std::string line) { |
| 43 | + std::vector<float> line_data; |
| 44 | + std::stringstream lineStream(line); |
| 45 | + std::string cell; |
| 46 | + while (std::getline(lineStream, cell, ',')) |
| 47 | + { |
| 48 | + if (cell != "setosa" && cell != "versicolor" && cell != "virginica") { |
| 49 | + line_data.push_back(std::stod(cell.c_str())); |
| 50 | + } |
| 51 | + else { |
| 52 | + if (cell == "setosa") { |
| 53 | + line_data.push_back(0.0f); |
| 54 | + } |
| 55 | + else if (cell == "versicolor") { |
| 56 | + line_data.push_back(1.0f); |
| 57 | + } |
| 58 | + else { |
| 59 | + line_data.push_back(2.0f); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return line_data; |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +std::initializer_list<float> DataSet::input(float petal_length, float petal_width, float sepal_length, float sepal_width) { |
| 69 | + return { petal_length,petal_width,sepal_length,sepal_width }; |
| 70 | +} |
| 71 | + |
| 72 | +std::string DataSet::output(std::vector<float> one_hot_encoding_species) { |
| 73 | + if (one_hot_encoding_species[0] == 1) { |
| 74 | + return "setosa"; |
| 75 | + } |
| 76 | + else if (one_hot_encoding_species[1] == 1) { |
| 77 | + return "versicolor"; |
| 78 | + } |
| 79 | + else { |
| 80 | + return "virginica"; |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments