【发布时间】:2022-12-25 17:01:53
【问题描述】:
我必须从 .csv 文件中读取患者数据,并根据每个患者读取的数据,使用决策树确定肿瘤是良性还是恶性。
我真的在为如何开始这个而苦苦挣扎。到目前为止,我已经编写了从 .csv 文件中读取数据并将数据存储到一个向量中的代码,如下所示,分布在几个头文件和 cpp 文件中。
根据我收集到的信息,我可以创建一个父决策类,然后我要处理的每个属性都是子类。不确定这是否有意义。请告诉我。
您将在下面找到我要处理的属性以及一个图形树,该图形树显示了如何确定肿瘤是良性还是恶性的,我的代码需要以此为基础。我还将包含一小部分 .csv 文件示例。
请给我一些指导,我如何做到这一点。我在使用指针表示法时遇到了最大的困难。任何指导将不胜感激。
CSVLine.h
#ifndef CSVLINE_H
#define CSVLINE_H
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class CSVLine
{
private:
vector<string> data;
public:
CSVLine() {}
CSVLine(const CSVLine& other)
{
data = other.data;
}
CSVLine operator = (const CSVLine& other)
{
data = other.data;
}
~CSVLine() {}
void parse(string line, char delimiter = ',');
string getString(int columnNumber);
int getInt(int columnNumber);
};
#endif
CSVLine.cpp
#include "CSVLine.h"
void CSVLine::parse(string line, char delimiter)
{
stringstream inLine(line);
string tempColumn = "";
while (getline(inLine, tempColumn, delimiter))
{
data.push_back(tempColumn);
}
}
string CSVLine::getString(int columnNumber)
{
return data[columnNumber];
}
int CSVLine::getInt(int columnNumber)
{
return atoi(data[columnNumber].c_str());
}
CSVReader.h
#ifndef CSVREADER_H
#define CSVREADER_H
#include <vector>
#include <fstream>
#include <iostream>
#include "CSVLine.h"
using namespace std;
class CSVReader
{
public:
CSVReader() {}
vector<CSVLine> read(string fileName);
};
#endif
CSVReader.cpp
#include "CSVReader.h"
vector<CSVLine> CSVReader::read(string fileName)
{
ifstream inputFile;
vector<CSVLine> lines;
inputFile.open(fileName.c_str());
string line = "";
while (getline(inputFile, line))
{
CSVLine csvLine;
csvLine.parse(line);
lines.push_back(csvLine);
}
return lines;
}
【问题讨论】:
标签: c++ pointers nodes data-mining decision-tree