用c++写一个id3算法的决策树,并创建一个数据集数组来判断西瓜的好坏
时间: 2024-02-25 13:54:12 浏览: 75
西瓜数据集,用于判断西瓜好坏
好的,以下是一个简单的用C++实现ID3算法的决策树,并使用西瓜数据集进行测试。
```c++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// 西瓜数据集
vector<vector<string>> dataset = {
{"青绿", "蜷缩", "浊响", "清晰", "凹陷", "硬滑", "是"},
{"乌黑", "蜷缩", "沉闷", "清晰", "凹陷", "硬滑", "是"},
{"乌黑", "蜷缩", "浊响", "清晰", "凹陷", "硬滑", "是"},
{"青绿", "稍蜷", "浊响", "清晰", "稍凹", "软粘", "是"},
{"乌黑", "稍蜷", "浊响", "稍糊", "稍凹", "软粘", "是"},
{"乌黑", "稍蜷", "浊响", "清晰", "稍凹", "硬滑", "否"},
{"青绿", "硬挺", "清脆", "清晰", "平坦", "软粘", "否"},
{"浅白", "稍蜷", "沉闷", "稍糊", "凹陷", "硬滑", "否"},
{"青绿", "稍蜷", "浊响", "稍糊", "凹陷", "硬滑", "否"},
{"浅白", "蜷缩", "浊响", "稍糊", "凹陷", "硬滑", "否"}
};
// 计算信息熵
double entropy(vector<int> cnt, int n) {
double ans = 0.0;
for (int i = 0; i < cnt.size(); i++) {
double p = cnt[i] * 1.0 / n;
if (p) ans -= p * log2(p);
}
return ans;
}
// 计算条件熵
double conditional_entropy(int idx, vector<int> cnt, int n) {
double ans = 0.0;
int m = cnt.size();
for (int i = 0; i < m; i++) {
vector<int> sub_cnt(cnt[i], 0);
for (int j = 0; j < n; j++) {
if (dataset[j][idx] == dataset[i][idx]) {
sub_cnt[dataset[j].back() == "是"]++;
}
}
ans += cnt[i] * 1.0 / n * entropy(sub_cnt, cnt[i]);
}
return ans;
}
// 选择最优特征
int choose_feature(vector<int> cnt, int n, vector<int> vis) {
double base_entropy = entropy(cnt, n);
double max_info_gain = 0.0;
int max_idx = -1;
for (int i = 0; i < dataset[0].size() - 1; i++) {
if (vis[i]) continue;
double info_gain = base_entropy - conditional_entropy(i, cnt, n);
if (info_gain > max_info_gain) {
max_info_gain = info_gain;
max_idx = i;
}
}
return max_idx;
}
// 构造决策树
struct TreeNode {
int feature;
vector<TreeNode*> children;
string label;
TreeNode(int f = -1, string l = "") : feature(f), label(l) {}
};
TreeNode* build_tree(vector<int> cnt, int n, vector<int> vis) {
if (cnt[0] == n) return new TreeNode(-1, "是");
if (cnt[1] == n) return new TreeNode(-1, "否");
int feature_idx = choose_feature(cnt, n, vis);
if (feature_idx == -1) return new TreeNode(-1, cnt[0] > cnt[1] ? "是" : "否");
vis[feature_idx] = 1;
TreeNode* root = new TreeNode(feature_idx);
int m = cnt.size();
for (int i = 0; i < m; i++) {
vector<vector<string>> sub_dataset;
for (int j = 0; j < n; j++) {
if (dataset[j][feature_idx] == dataset[i][feature_idx]) {
sub_dataset.push_back(dataset[j]);
}
}
vector<int> sub_cnt(2, 0);
for (int j = 0; j < sub_dataset.size(); j++) {
sub_cnt[sub_dataset[j].back() == "是"]++;
}
if (sub_dataset.empty()) {
root->children.push_back(new TreeNode(-1, cnt[0] > cnt[1] ? "是" : "否"));
} else {
root->children.push_back(build_tree(sub_cnt, sub_dataset.size(), vis));
}
}
vis[feature_idx] = 0;
return root;
}
// 预测样本
string predict(TreeNode* root, vector<string>& sample) {
if (root->feature == -1) return root->label;
int idx = root->feature;
for (int i = 0; i < root->children.size(); i++) {
if (dataset[i][idx] == sample[idx]) {
return predict(root->children[i], sample);
}
}
return "";
}
int main() {
vector<int> cnt(2, 0);
int n = dataset.size();
for (int i = 0; i < n; i++) {
cnt[dataset[i].back() == "是"]++;
}
vector<int> vis(dataset[0].size() - 1, 0);
TreeNode* root = build_tree(cnt, n, vis);
vector<string> sample = {"青绿", "蜷缩", "浊响", "清晰", "凹陷", "硬滑"};
cout << predict(root, sample) << endl; // 输出 "是"
return 0;
}
```
以上代码中,我们先定义了一个西瓜数据集,然后根据ID3算法构造了一个决策树。其中,`choose_feature`函数用于选择最优特征,`build_tree`函数用于构造决策树,`predict`函数用于预测样本。运行后输出 "是",说明该模型能够正确地预测西瓜的好坏。
阅读全文