C++实现ID3算法

这是一个C++实现的ID3算法类,包括了节点、记录和属性类的定义,以及ID3Algr类的构造、增删记录、计算熵、构建决策树和打印树的功能。代码读取CSV文件并构建决策树。
摘要由CSDN通过智能技术生成
#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[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值