人工智能天气决策树源代码_第1页
人工智能天气决策树源代码_第2页
人工智能天气决策树源代码_第3页
人工智能天气决策树源代码_第4页
人工智能天气决策树源代码_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实用昆明理工大学信息工程与自动化学院学生实验报告(2011 2012 学年第1 学期)课程名称:人工智能开课实验室:信自楼计算机机房444 2011 年12月16日专业班级0学号200姓名成绩实验名称天气决策树指导教师教师评语该同学是否了解实验原理:A. 了解口该同学的实验能力:A.强 口该同学的实验是否达到要求:A.达到口实验报告是否规范:A.规范匚实验过程是否详细记录:A.详细口2011B.基本了解口CJ、了解口B.中等口 C.差 B.基本达到口C.未达到口B.基本规范匚C.不规范匚B.一M C.没有口教师签名:年12月日、上机目的及内容1 .上机内容根据下列给定的14个数据,运用Info

2、rmation Gain构造一个天气决策树例子编号属性分类大况温度湿度风况1晴热大无N2晴热大有N3多云热大无P4雨中大无P5雨冷正常无P6雨冷正常有N7多云冷正常有P8晴中大无N 19晴冷正常无P10雨中正常无P11晴中正常有P 112多云中大有P13多云热正常无P14雨中大有N2 .上机目的(1)学习用Information Gain 构造决策树的方法;(2)在给定的例子上,构造出正确的决策树;(3)理解并掌握构造决策树的技术要点。、实验原理及基本技术路线图(方框原理图或程序流程图)(1)设计并实现程序,构造出正确的决策树;(2)对所设计的算法采用大 O符号进行时间复杂性和空间复杂性分析;

3、主函数流程图:std: iErreamin. fxv.IMaltiplekAttributevalue.cpp 流程图文档Basefun流程图:Datapiont.cpp流程图:Dataset主要流程图:三、所用仪器、材料(设备名称、型号、规格等或使用软件)1 台 PC及 VISUAL C+6.0 软件四、实验方法、步骤(或:程序代码或操作过程)工程源代码:Main.cpp:#include #include #include #include #include #include #include AttributeValue.h#include DataPoint.h#include Dat

4、aSet.hDataPoint processLine(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 );AttributeV

5、alue v = 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.addDataPoint(

6、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(); +i)float prob

7、= splittedDataSetsi.getPositiveProb();if (prob = 0.0 | prob = 1.0)finishedDataSet.push_back(splittedDataSetsi);elseprocessQ.push_back(splittedDataSetsi);实用std二cout The dicision tree is: std二endl;for (int i = 0; i finishedDataSet.size(); +i)finishedDataSeti.display();Attributevalue.cpp#include Attrib

8、uteValue.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)return 1.0 / log10(2) * log10(

9、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)for (int i=0; iattributes.size();

10、 +i)m_attributes.push_back( attributes.);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 DataSet.h void SplitAttributeValue:dis

11、play() 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; im_data.size(); +i)if ( m_datai.isPosi

12、tive()nPositive+;return nPositive / m_data.size();struct Statint nPos;int nNeg;int id;;void DataSet:splitDataSet(std:vector& splittedSets)/ find all available splitting attributesint nAttributes = m_data0.getNAttributes();int i, j;std:vector splittingAttributeBV;splittingAttributeBV.resize(nAttribut

13、es);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 = splittingAttributeBVi)splittingAttributeIds.push_back(i);

14、typedef std:map AttributeValueStat;typedef std:map:iteratorAttributeValueStat_iterator; typedef std:map:const_iteratorAttributeValueStat_const_iterator;/ go through data once, and collect needed statistics to calculate entropystd:vector splittingStats;splittingStats.resize( splittingAttributeIds.siz

15、e();for (i=0; im_data.size(); +i) for (j=0; jsplittingAttributeIds.size(); +j) AttributeValueconst&m_datai.getAttribute(splittingAttributeIdsj); AttributeValueStat_iterator it = splittingStatsj.find(v); if ( splittingStatsj.end() = it ) Stat stat; if ( m_datai.isPositive() stat.nPos = 1; stat.nNeg =

16、 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; jsplittingAttributeIds.size(); +j) std:cout Attribute。 splittingAttributeIdsj ): s

17、td: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 splitting attribute float minEntropy = 0.0; int splitAttributeId = -1; for (j=0

18、; 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 Split at attribute。 splittingAttributeIdssplitAttributeId ) std:endl second.id = k+;spli

19、ttedSets.resize( k);for (i=0; ik; +i)for (j=0; jsecond.id.m_splitAttributes.push_back(SplitAttributeValue(itt -first, attrld);for (i=0; isecond.id.addDataPoint(m_datai);elsethrow DataErrException();void DataSet:display()int i;std:cout Dataset( this ) std:endl;for (i=0; im_splitAttributes.size(); +i)

20、m_splitAttributesi.display();std:cout Data: std:endl;for (i=0; im_data.size(); +i)m_datai.display();std:cout std:endl;五、实验过程原始记录(测试数据、图表、计算等)ttribute :n 4 2 3署1:g多晴雨e3冷热中e: UaluenPos无6有3plit at attributettribute Ualue 冷 热 中Value溶常Ualue无 有nPos10nFos 02nPosat attributeValue冷 中nPos1 2hNesf1 1Attribute:ValuenPosnNegr大11正常21Attribute :UaluenPosnNeg无30有9Split at attribute2lie dxc is ion tree is : atasetttPiijuteCl ?:Splitata :多云 多不 多云 多云attributeID常常 大正大正Spilt attribute ualuep p p p无有有无SplitattributeID0?Sp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论