基于C++实现的ID3算法决策树

本文介绍了一个初学者视角的C++实现ID3决策树算法的过程,包括如何计算信息增益来选择节点属性,并使用递归构建决策树。尽管在决策树的打印展示上存在不足,但整体展示了ID3算法的基本实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学机器学习决策树算法——ID3实现(C++),

通过计算划分不同子集的信息增益确定结点属性,并调用递归,层层建立决策树,

但因时间原因,此代码在打印决策树方面做的不是很完善,望谅解。

#include <iostream>
#include <math.h>
#include <vector>
#include <set>
#define amount 10
#define KEY_DOWN 0x1b5b42
using namespace std;

vector<vector<string>> samples;

vector<int> flag(3,0);
int residualFeature=3;
set<string> Cpu;
set<string> RAM;
set<string> Screen;
vector<string> feature={"CPU","RAM","Screen"};
vector<vector<string>> values;


struct Node{
    string nFeature;
    string decision;
    vector<string> nValue;
    vector<Node*> childs;
};

template <class T>
vector<T> set2Vector(set<T> & a)
{
    int count =0;
    vector<T> result(a.size());
    for(auto&s:a)
        result[count++]=s;
    sort(result.begin(),result.end());
    return result;
}

void init(){
    string data[amount][4]={"
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值