資源簡介
c++實現決策樹之CART算法

代碼片段和文件信息
#include?“LDecisionTree.h“
#include?
#include?
#include?
using?std::set;
///?@brief?計算數據的熵
///?
///?對于任意一個隨機變量?X?它的熵定義如下:
///?變量的不確定性越大?熵也就越大?把它搞清楚所需要的信息量也就越大
///?@param[in]?dataList?數據列表
///?@return?數據的熵
template
static?float?CalculateEntropy(IN?const?LArray&?dataList)
{
????float?entropy?=?0.0f;
????map?typeCountMap;
????for?(int?i?=?0;?i?????{
????????++typeCountMap[dataList.Data[i]];
????}
????for?(auto?iter?=?typeCountMap.begin();?iter?!=?typeCountMap.end();?iter++)
????{
????????float?prob?=?(float)(iter->second)/(float)(dataList.Length);
????????entropy?-=?prob?*?log(prob)/log(2.0f);
????}
????return?entropy;
}
static?float?CalculateEntropy(IN?const?map&?dataMap)
{
????float?entropy?=?0.0f;
????int?totalCount?=?0;
????for?(auto?iter?=?dataMap.begin();?iter?!=?dataMap.end();?iter++)
????{
????????totalCount?+=?iter->second;
????}
????for?(auto?iter?=?dataMap.begin();?iter?!=?dataMap.end();?iter++)
????{
????????float?prob?=?(float)(iter->second)/(float)(totalCount);
????????entropy?-=?prob?*?log(prob)/log(2.0f);
????}
????return?entropy;
}
///?@brief決策樹節點
struct?LDecisionTreeNode
{
????LDecisionTreeNode()
????{
????????m_checkColumn?=?-1;
????????m_pTrueChildren?=?0;
????????m_pFalseChildren?=?0;
????}
????LDecisionTreeNode(
????????IN?int?column?
????????IN?const?LVariant&?checkValue?
????????IN?LDecisionTreeNode*?pTrueChildren?
????????IN?LDecisionTreeNode*?pFalseChildren)
????{
????????m_checkColumn?=?column;
????????m_checkValue?=?checkValue;
????????m_pTrueChildren?=?pTrueChildren;
????????m_pFalseChildren?=?pFalseChildren;
????}
????LDecisionTreeNode(IN?const?map&?resultMap)
????{
????????m_resultMap?=?resultMap;
????}
????int?m_checkColumn;?///????LVariant?m_checkValue;?///
????LDecisionTreeNode*?m_pTrueChildren;?///????LDecisionTreeNode*?m_pFalseChildren;?///
????map?m_resultMap;?///};
LVariant::LVariant()
{
????m_pValueInt?=?0;
????m_pValueStr?=?0;
????m_type?=?UNKNOWN;
}
LVariant::LVariant(IN?int?value)
{
????
????m_pValueInt?=?0;
????m_pValueStr?=?0;
????m_pValueInt?=?new?int;
????(*m_pValueInt)?=?value;
????m_type?=?INT;
????
}
LVariant::LVariant(IN?const?string&?value)
{
????m_pValueInt?=?0;
????m_pValueStr?=?0;
????m_pValueStr?=?new?string;
????(*m_pValueStr)?=?value;
????m_type?=?STRING;
}
LVariant::LVariant(IN?const?LVariant&?rhs)
{
????this->m_type?=?rhs.m_type;
????this->m_pValueInt?=?0;
????this->m_pValueStr?=?0;
????if?(rhs.m_pValueInt?!=?0)
????{
????????this->m_pValueInt?=?new?int(*rhs.m_pValueInt);
????}
????if?(rhs.m_pValueStr?!=?0)
????{
????????this->m_pValueStr?=?new?string(*rhs.m_pValueStr);
????}
}
LVa
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????874??2015-06-15?10:57??CART\CART.sln
????..A..H.??????8192??2015-06-15?10:57??CART\CART.suo
?????文件???????3445??2015-06-15?10:58??CART\CART.vcxproj
?????文件???????1387??2015-06-15?10:58??CART\CART.vcxproj.filters
?????文件????????143??2015-06-15?10:57??CART\CART.vcxproj.user
?????文件???????2712??2015-05-19?15:08??CART\LArray.h
?????文件??????22099??2015-06-12?17:18??CART\LDecisionTree.cpp
?????文件???????5011??2015-06-15?10:58??CART\LDecisionTree.h
?????文件???????4642??2015-06-15?10:57??CART\main.cpp
?????文件?????????59??2015-06-05?11:22??CART\ReadMe.txt
?????目錄??????????0??2015-06-15?10:58??CART
-----------?---------??----------?-----??----
????????????????48564????????????????????11
- 上一篇:Horiba MFC數字通信協議
- 下一篇:簡易版的QT5實現RS232通信
評論
共有 條評論