IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 随机森林算法及其实现(2) -> 正文阅读

[数据结构与算法]随机森林算法及其实现(2)

随机森林算法及其实现

算法实现

  1. 先实现随机化,有放回抽取样本,以及随机抽取属性(无放回)
/*
* Random instances are obtained through sampling methods with replacement.
* Return: Available instances.
*/
IntArray* RandomForestClassifier::bootStrap()
{
	int count = 0;
	int tempIndex;
	IntArray* resInstances;
	int length = trainingSet->getRows();
	int* tempIndices = new int[length];
	memset(tempIndices, 0, length * sizeof(int));

	for (int i = 0; i < length; i++)
	{
		tempIndex = rand() % length;
		if (tempIndices[tempIndex] == 0) {
			tempIndices[tempIndex] = 1;
			count++;
		}// Of if
	}// Of for

	resInstances = new IntArray(count);

	for (int i = 0, j = 0; i < length; i++)
	{
		if (tempIndices[i] == 1)
		{
			resInstances->setValue(j++, i);
		}// Of if
	}// Of for

	std::cout << resInstances->toString() << std::endl;

	delete[] tempIndices;

	return resInstances;
}// Of bootStrap


/*
* Random attributes are obtained through sampling without replacement
* Return: Available attributes.
*/
IntArray* RandomForestClassifier::getAttributes()
{
	int tempIndex;
	int numAvailableAttributes = sqrt(numAttributes);
	int* tempAttributes = new int[numAttributes];
	memset(tempAttributes, 0, numAttributes * sizeof(int));
	IntArray* resAttributes = new IntArray(numAvailableAttributes);

	for (int i = 0; i < numAvailableAttributes; i++)
	{
		tempIndex = rand() % numAttributes;
		while (tempAttributes[tempIndex] == 1) {
			tempIndex = rand() % numAttributes;
		}// Of while
		tempAttributes[tempIndex] = 1;
	}// Of for

	tempIndex = 0;
	for (int i = 0; i < numAttributes; i++)
	{
		if (tempAttributes[i] == 1) {
			resAttributes->setValue(tempIndex++, i);
		}
	}// Of for
	
	delete[] tempAttributes;
	return resAttributes;
}// Of getAttributes
  1. 构造有穷颗树
/*
* Build a tree
* Return: A tree.
*/
Tree* RandomForestClassifier::buildTree()
{
	IntArray* availableInstances = bootStrap();
	IntArray* availableAttributes = getAttributes();

	Tree* tree = new Tree(trainingSet, trainingLables, availableInstances, availableAttributes, numClasses);
	tree->train();

	delete availableInstances;
	delete availableAttributes;
	return tree;
}// Of buildTree

/*
* Build a given number of trees.
*/
void RandomForestClassifier::train()
{
	trees = new Tree * [numTrees];
	for (int i = 0; i < numTrees; i++)
	{
		trees[i] = buildTree();
	}// Of for
}// Of train
  1. 预测以及投票
/*
* Return the most frequent label by counting the number of various labels
* Return: Label.
*/
int RandomForestClassifier::vote(IntArray* paraLabels)
{
	int* tempCountClasses = new int[numClasses];
	memset(tempCountClasses, 0, numClasses * sizeof(int));
	int max = 0;
	for (int i = 0; i < paraLabels->getLength(); i++)
	{
		tempCountClasses[paraLabels->getValue(i)]++;
		if (tempCountClasses[max] < tempCountClasses[paraLabels->getValue(i)])
		{
			max = paraLabels->getValue(i);
		}// Of if
	}// Of for
	delete[] tempCountClasses;
	return vote;
}// Of if

/*
* Predict.
*/
int RandomForestClassifier::predict(DoubleMatrix* paraInstance)
{
	IntArray* tempLabels = new IntArray(numTrees);

	for (int i = 0; i < numTrees; i++)
	{
		tempLabels->setValue(i, trees[i]->predict(paraInstance));
	}// Of for

	int resLable = vote(tempLabels);

	delete tempLabels;

	return resLable;
}// Of predict

实现过程出现的问题以及解决方式

实现之后发现准确率不太行,这里有可能是算法中还有点小问题,同时我在想会不会和数据集也有关系,这里测试的数据集数量只有17个,划分成训练集和测试集后就更少了。

数据集:

0, 0, 0, 0, 0, 0, 1
1, 0, 1, 0, 0, 0, 1
1, 0, 0, 0, 0, 0, 1
0, 0, 1, 0, 0, 0, 1
2, 0, 0, 0, 0, 0, 1
0, 1, 0, 0, 1, 1, 1
1, 1, 0, 1, 1, 1, 1
1, 1, 0, 0, 1, 0, 1
1, 1, 1, 1, 1, 0, 0
0, 2, 2, 0, 2, 1, 0
2, 2, 2, 2, 2, 0, 0
2, 0, 0, 2, 2, 1, 0
0, 1, 0, 1, 0, 0, 0
2, 1, 1, 1, 0, 0, 0
1, 1, 0, 0, 1, 1, 0
2, 0, 0, 2, 2, 0, 0
0, 0, 1, 1, 1, 0, 0

实现方式是利用C++去实现,目前实现了ID3决策树算法的大部分代码,不过测试还有点小问题,正在更改,同时需要考虑数据的特性,目前只实现了基于离散属性的决策树,在这个过程中需要结合该框架下的基础类进行编码,因此需要添加很多未存在的方法,感觉会使代码比较臃肿,比如 DoubleMatrix 类,因此有些方法还是放在了当前实现的类中以单独使用。
在这里插入图片描述
在这里插入图片描述

然后又测试了那个weather数据集,结果也是不太理想,如下图:
在这里插入图片描述
数据集:
在这里插入图片描述

0, 0, 0, 0, 0
0, 0, 0, 1, 0
1, 0, 0, 0, 1
2, 1, 0, 0, 1
2, 2, 1, 0, 1
2, 2, 1, 1, 0
1, 2, 1, 1, 1
0, 1, 0, 0, 0
0, 2, 1, 0, 1
2, 1, 1, 0, 1
0, 1, 1, 1, 1
1, 1, 0, 1, 1
1, 0, 1, 0, 1
2, 1, 0, 1, 0

应该是决策树里哪个部分有偏差,当然也参考了我导师的文章,日撸 Java 三百行(61-70天,决策树与集成学习),估计是在转 C++ 的过程里有些细节问题有疏忽,因此还得继续找 bug 。。。

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-27 14:21:50  更:2021-09-27 14:23:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 10:48:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码