Skip to content

Commit d758bf5

Browse files
authored
Add files via upload
0 parents  commit d758bf5

File tree

5 files changed

+18128
-0
lines changed

5 files changed

+18128
-0
lines changed

dataset.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+

dataset.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include<iostream>
4+
#include<string>
5+
#include<vector>
6+
#include<unordered_map>
7+
#include<initializer_list>
8+
9+
10+
class DataSetMetaData {
11+
12+
friend class DataSet;
13+
private:
14+
float sepal_length;
15+
float sepal_width;
16+
float petal_length;
17+
float petal_width;
18+
std::string species;
19+
};
20+
21+
class DataSet {
22+
23+
public:
24+
25+
//read the given csv file and complete_x and y_
26+
void ReadCSVFile(std::string file_name);
27+
28+
29+
//Construct a data set from the given csv file path
30+
DataSet(std::string file_name) {
31+
ReadCSVFile(file_name);
32+
}
33+
34+
///getters
35+
36+
std::vector<float>& x1() { return x1_; }
37+
std::vector<float>& x2() { return x2_; }
38+
std::vector<float>& x3() { return x3_; }
39+
std::vector<float>& x4() { return x4_;}
40+
std::vector<float& y() { return y_; }
41+
42+
43+
44+
///convert one csv line to a vector of float
45+
46+
std::vector<float>ReadCSVLine(std::string line);
47+
///normalize a human input using the data set metadata
48+
std::initializer_list<float>input(float petal_length,float peta_width,float sepal_length,float sepal_width);
49+
50+
std::string output(std::vector<float>one_hot_encoding_species);
51+
52+
private:
53+
DataSetMetaData data_set_metadata;
54+
std::vector<float>x1_;
55+
std::vector<float>x2_;
56+
std::vector < float>x3_;
57+
std::vector<float>x4_;
58+
std::vector<float>y_;
59+
};
60+

net.pt

2.83 KB
Binary file not shown.

0 commit comments

Comments
 (0)