开箱即用的C++决策树简单实现

一个数据结构期末作业(有兴趣用的话可以高抬贵手star下⭐~)GitHub - mcxiaoxiao/c-Decision-tree: 决策树c++简单实现 🌳 c-Decision-tree 附大作业/课设参考文档.doc

🌳 c-Decision-tree

Introduction 🙌

c-Decision-tree 🌳 简单的决策树 比较严谨的c++实现,如果输出有报错可能是不支持emoji 代码以天气预测是否适合出行为例,修改起来很方便

三步实现

0️⃣ 🤔 想好想要多少个特征,line13:

#define feature 4 //改成需要的特征数量

1️⃣ 🤔 想好特征名,line18

//四个特征的名称,比如天气取值有三个:晴,阴,雨 
string attribute[] = {"天气", "温度", "湿度", "是否有风"};

3️⃣ 🖊 修改data.txt中的案例数据,修改测试数据line255

string test[] = {"晴", "温", "中", "是"};

交互(可选)

去掉以下注释,修改成自己修改后逻辑下的交互提示,line256

int main() {	
	createDataset();
	root = createTree(root, X, attributes);
	print(root, 0);
	string test[] = {"晴", "温", "中", "是"};
    // //自助交互
    // cout << "👋  请输入天气情况 ☁️ (晴/阴/雨)";
    // cin >> test[0];
    // cout << "😴  请输入温度 🌡️ (热/温/凉爽)";
    // cin >> test[1];
    // cout << "🌁  请输入湿度 💦 (高/中)";
    // cin >> test[2];
    // cout << "🚗  请输入是否刮风 🌬 (是/否)";
    // cin >> test[3];
	int i;
	cout << endl << "属性:";
	for(i=0; i<feature; i++)
		cout << attributes[i] << "\t";
	cout << endl << "输入:";
	for(i=0; i<feature; i++)
		cout << test[i] << "\t";
	cout << endl << "预测:";
	cout << classify(root, attributes, test) +"出行" << endl;
	freeNode(root);
	return 0;
}

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
天气决策树是一种基于决策树算法的预测模型,用于预测未来一段时间内的天气情况。下面是一个简单的用 C++ 实现天气决策树的示例代码: ```c++ #include <iostream> using namespace std; // 定义天气决策树结点 struct TreeNode { int feature; bool is_leaf; string label; TreeNode* left; TreeNode* right; }; // 创建天气决策树 TreeNode* create_decision_tree() { // 创建根结点 TreeNode* root = new TreeNode; root->feature = 1; root->is_leaf = false; root->left = nullptr; root->right = nullptr; // 创建左子树 TreeNode* left = new TreeNode; left->feature = 2; left->is_leaf = false; left->left = nullptr; left->right = nullptr; root->left = left; // 创建右子树 TreeNode* right = new TreeNode; right->feature = 3; right->is_leaf = false; right->left = nullptr; right->right = nullptr; root->right = right; // 创建左子树的左子树 TreeNode* left_left = new TreeNode; left_left->feature = 0; left_left->is_leaf = true; left_left->label = "Sunny"; left_left->left = nullptr; left_left->right = nullptr; left->left = left_left; // 创建左子树的右子树 TreeNode* left_right = new TreeNode; left_right->feature = 0; left_right->is_leaf = true; left_right->label = "Rainy"; left_right->left = nullptr; left_right->right = nullptr; left->right = left_right; // 创建右子树的左子树 TreeNode* right_left = new TreeNode; right_left->feature = 0; right_left->is_leaf = true; right_left->label = "Cool"; right_left->left = nullptr; right_left->right = nullptr; right->left = right_left; // 创建右子树的右子树 TreeNode* right_right = new TreeNode; right_right->feature = 0; right_right->is_leaf = true; right_right->label = "Warm"; right_right->left = nullptr; right_right->right = nullptr; right->right = right_right; return root; } // 预测天气情况 string predict_weather(TreeNode* root, int* features) { TreeNode* node = root; while (!node->is_leaf) { if (features[node->feature] == 0) { node = node->left; } else { node = node->right; } } return node->label; } int main() { // 创建天气决策树 TreeNode* root = create_decision_tree(); // 测试天气决策树 int features[4] = {1, 0, 1, 0}; string weather = predict_weather(root, features); cout << "The weather is " << weather << endl; return 0; } ``` 在上面的代码中,我们首先定义了一个 `TreeNode` 结构体,用于表示天气决策树的结点。然后,我们通过 `create_decision_tree` 函数来创建天气决策树。在这个函数中,我们按照天气决策树的结构来创建结点,并将它们连接起来,形成一颗完整的决策树。 接下来,我们定义了一个 `predict_weather` 函数,用于预测天气情况。该函数接收一个决策树的根结点和一个特征数组作为参数,返回预测的天气情况。在函数中,我们通过遍历决策树,根据特征数组的取值来决定向哪个子结点遍历,直到遍历到叶子结点为止,然后返回叶子结点的 label。 最后,在 `main` 函数中,我们调用 `create_decision_tree` 函数来创建天气决策树,并调用 `predict_weather` 函数来预测天气情况。我们定义了一个特征数组 `features`,用于表示当前的天气特征,然后将它作为参数传递给 `predict_weather` 函数,得到预测的天气情况并输出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

O&REO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值