#include <stdlib.h>
#include<iostream>
#include<string>
#include<math.h>
#include <fstream>
#include <sstream>
using namespace std;
class Attributes {
public:
Attributes(int n) {
this->Num = n;
this->attr = new string[this->Num];
}
void setAttrs(string* strs) {
//if (strs->length() != this->Num) return;
for (int i = 0; i < this->Num; i++) {
this->attr[i] = strs[i];
}
}
string* getAttrs() {
return this->attr;
}
int getNum() {
return this->Num;
}
private:
int Num;
string * attr;
};
class Record
{
public:
Record() { flag = false; };
bool flag;
Record(Attributes* attr) {
flag = true;
this->attr = attr;
this->NumOfRecord = this->attr->getNum();
this->record = new string[this->NumOfRecord];
}
void setAttr(Attributes* attr) {
flag = true;
this->attr = attr;
this->NumOfRecord = this->attr->getNum();
this->record = new string[this->NumOfRecord];
}
void setRecord(string* record) {
for (int i = 0; i < this->NumOfRecord; i++) {
this->record[i] = record[i];
}
}
string* getRecord() {
return this->record;
}
private:
Attributes * attr;
int NumOfRecord;
string* record;
};
class Node {
public:
Node() {
isLeafNode = false;
}
Node(int nChild, string name) {
this->Name = name;
this->NumberOfChild = nChild;
this->Child = new Node*[nChild]();
isLeafNode = false;
}
void setChild(int n) {
this->NumberOfChild = n;
this->Child = new Node*[n]();
isLeafNode = false;
}
int NumberOfChild;
Node** Child;
string Name;
bool isLeafNode;
};
class ID3Algr
{
public:
ID3Algr() {
this->candidates = new string*[12];
for (int i = 0; i < 12; i++) {
this->candidates[i] = new string[12]();
}
this->table = new double**[12];
for (int i = 0; i < 12; i++) {
this->table[i] = new double*[12];
for (int j = 0; j < 12; j++) {
this->table[i][j] = new double[12];
for (int k = 0; k < 12; k++) this->table[
C++实现ID3算法
最新推荐文章于 2021-01-17 17:43:40 发布
这是一个C++实现的ID3算法类,包括了节点、记录和属性类的定义,以及ID3Algr类的构造、增删记录、计算熵、构建决策树和打印树的功能。代码读取CSV文件并构建决策树。
摘要由CSDN通过智能技术生成