初学机器学习决策树算法——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]={"