1、人工智能天气决策树源代码昆明理工大学信息工程与自动化学院学生实验报告( 2011 2012 学年 第 1 学期 )课程名称:人工智能 开课实验室:信自楼计算机机房444 2011 年12月 16 日专业班级0学号200姓名成绩实验名称天气决策树指导教师 教师评语该同学是否了解实验原理: A.了解 B.基本了解 C.不了解该同学的实验能力: A.强 B.中等 C.差 该同学的实验是否达到要求: A.达到 B.基本达到 C.未达到实验报告是否规范: A.规范 B.基本规范 C.不规范实验过程是否详细记录: A.详细 B.一般 C.没有 教师签名: 2011 年12 月 日一、上机目的及内容1.上机
2、内容根据下列给定的14个数据,运用Information Gain构造一个天气决策树。例子编号属 性分类天况温度湿度风况1晴热大无N2晴热大有N3多云热大无P4雨中大无P5雨冷正常无P6雨冷正常有N7多云冷正常有P8晴中大无N9晴冷正常无P10雨中正常无P11晴中正常有P12多云中大有P13多云热正常无P14雨中大有N2.上机目的(1)学习用Information Gain构造决策树的方法;(2)在给定的例子上,构造出正确的决策树;(3)理解并掌握构造决策树的技术要点。二、实验原理及基本技术路线图(方框原理图或程序流程图)(1)设计并实现程序,构造出正确的决策树;(2)对所设计的算法采用大O符
3、号进行时间复杂性和空间复杂性分析;主函数流程图: Attributevalue.cpp流程图Basefun流程图:Datapiont.cpp流程图:Dataset主要流程图:三、所用仪器、材料(设备名称、型号、规格等或使用软件)1台PC及VISUAL C+6.0软件4、实验方法、步骤(或:程序代码或操作过程) 工程源代码:Main.cpp:#include #include #include #include #include #include #include AttributeValue.h#include DataPoint.h#include DataSet.hDataPoint pr
4、ocessLine(std:string const& sLine) std:istringstream isLine(sLine, std:istringstream:in); std:vector attributes; / TODO: need to handle beginning and ending empty spaces. while( isLine.good() ) std:string rawfield; isLine rawfield; attributes.push_back( AttributeValue( rawfield ) ); AttributeValue v
5、 = attributes.back(); attributes.pop_back(); bool type = v.GetType(); return DataPoint(attributes, type); void main() std:ifstream ifs(in.txt, std:ifstream:in); DataSet initDataset; while( ifs.good() ) / TODO: need to handle empty lines. std:string sLine; std:getline(ifs, sLine); initDataset.addData
6、Point( processLine(sLine) ); std:list processQ; std:vector finishedDataSet; processQ.push_back(initDataset); while ( processQ.size() 0 ) std:vector splittedDataSets; DataSet dataset = processQ.front(); dataset.splitDataSet(splittedDataSets); processQ.pop_front(); for (int i=0; isplittedDataSets.size
7、(); +i) float prob = splittedDataSetsi.getPositiveProb(); if (prob = 0.0 | prob = 1.0) finishedDataSet.push_back(splittedDataSetsi); else processQ.push_back(splittedDataSetsi); std:cout The dicision tree is: std:endl; for (int i = 0; i finishedDataSet.size(); +i) finishedDataSeti.display(); Attribut
8、evalue.cpp:#include AttributeValue.h#include base.hAttributeValue:AttributeValue(std:string const& instring): m_value(instring)bool AttributeValue:GetType() if (m_value = P) return true; else if (m_value = N) return false; else throw DataErrException(); Basefun.cpp:#include float log2 (float x) retu
9、rn 1.0 / log10(2) * log10(x);float calEntropy(float prob) float sum=0; if (prob = 0 | prob = 1) return 0; sum -= prob * log2(prob); sum -= (1 - prob) * log2 ( 1 - prob ); return sum;Datapiont.cpp:#include #include DataPoint.hDataPoint:DataPoint(std:vector const& attributes, bool type): m_type(type)
10、for (int i=0; iattributes.size(); +i) m_attributes.push_back( attributesi ); void DataPoint:display() for (int i=0; im_attributes.size(); +i) std:cout t m_attributesi.getValue(); if (true = m_type) std:cout tP; else std:cout tN; std:cout std:endl;Dataset.cpp:#include #include #include base.h#include
11、 DataSet.hvoid SplitAttributeValue:display() std:cout tSplit attribute ID( m_attributeIndex )t; std:cout Split attribute value( m_v.getValue() ) std:endl;void DataSet:addDataPoint(DataPoint const& datapoint) m_data.push_back(datapoint);float DataSet:getPositiveProb() float nPositive = 0; for(int i=0
12、; im_data.size(); +i) if ( m_datai.isPositive() ) nPositive+; return nPositive / m_data.size();struct Stat int nPos; int nNeg; int id;void DataSet:splitDataSet(std:vector& splittedSets) / find all available splitting attributes int nAttributes = m_data0.getNAttributes(); int i, j; std:vector splitti
13、ngAttributeBV; splittingAttributeBV.resize(nAttributes); for (i=0; inAttributes; +i) splittingAttributeBVi = true; for (i=0; im_splitAttributes.size(); +i) splittingAttributeBV m_splitAttributesi.getAttributeIndex() = false; std:vector splittingAttributeIds; for (i=0; inAttributes; +i) if (true = sp
14、littingAttributeBVi) splittingAttributeIds.push_back(i); typedef std:map AttributeValueStat; typedef std:map:iterator AttributeValueStat_iterator; typedef std:map:const_iterator AttributeValueStat_const_iterator; / go through data once, and collect needed statistics to calculate entropy std:vector s
15、plittingStats; splittingStats.resize( splittingAttributeIds.size() ); for (i=0; im_data.size(); +i) for (j=0; jsplittingAttributeIds.size(); +j) AttributeValue const& v = m_datai.getAttribute(splittingAttributeIdsj); AttributeValueStat_iterator it = splittingStatsj.find(v); if ( splittingStatsj.end(
16、) = it ) Stat stat; if ( m_datai.isPositive() ) stat.nPos = 1; stat.nNeg = 0; stat.id = 0; else stat.nPos = 0; stat.nNeg = 1; stat.id = 0; splittingStatsj.insert(std:pair(v, stat); else if ( m_datai.isPositive() ) it-second.nPos+; else it-second.nNeg+; / display collected statistics for (j=0; jsplit
17、tingAttributeIds.size(); +j) std:cout Attribute( splittingAttributeIdsj ): std:endl; std:cout tValue t nPos t nNeg std:endl; for (AttributeValueStat_const_iterator it = splittingStatsj.begin(); it != splittingStatsj.end(); +it) std:cout t first.getValue() t second.nPos t second.nNeg std:endl; / find
18、 splitting attribute float minEntropy = 0.0; int splitAttributeId = -1; for (j=0; jsecond.nPos + it-second.nNeg; float p = it-second.nPos; p /= nSamples; entropy += calEntropy(p) * nSamples / n; if (entropy minEntropy | -1 = splitAttributeId) minEntropy = entropy; splitAttributeId = j; std:cout Spli
19、t at attribute( splittingAttributeIdssplitAttributeId ) std:endl second.id = k+; splittedSets.resize( k); for (i=0; ik; +i) for (j=0; jsecond.id.m_splitAttributes.push_back(SplitAttributeValue(itt-first, attrId); for (i=0; isecond.id.addDataPoint(m_datai); else throw DataErrException(); void DataSet
20、:display() int i; std:cout Dataset( this ) std:endl; for (i=0; im_splitAttributes.size(); +i) m_splitAttributesi.display(); std:cout Data: std:endl; for (i=0; im_data.size(); +i) m_datai.display(); std:cout std:endl;五、实验过程原始记录( 测试数据、图表、计算等)6、实验结果、分析和结论(误差分析与数据处理、成果总结等。其中,绘制曲线图时必须用计算纸或程序运行结果、改进、收获)Attributevalue.cpp的作用是判断正反例,以便下面的的划分决策过程,basefun.cpp的作用则是计算熵,通过熵的比。较才能够为下面的划分提供条件依据,而dataset.cpp就是具体的划分过程,首先找到可用的划分项目,再第一次划分之后再手机相关的数据来计算熵。之后再显示出来,做这个实验的时候我遇到了很大的难度,感觉它的要求和自己的实际水平相差很大,所以参考了很多资料才得以完成这个程序,也感觉到了自己的能力还很不足,还需要加强的地方还有很多。感谢下载!欢迎您的下载,资料仅供参考
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1