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

下载本文档

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

文档简介

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

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

3、空间复杂性分析;主函数流程图:Attributevalue.cpp流程图。2。Basefun 流程图:Datapiont.cpp流程图:Dataset主要流程图:。3。三、所用仪器、材料(设备名称、型号、规格等或使用软件)1 台 PC及 VISUAL C+6.0 软件四、实验方法、步骤(或:程序代码或操作过程)工程源代码:Main.cpp:#include #include #include 。4。#include #include #include #include AttributeValue.h#include DataPoint.h#include DataSet.hDataPoint

4、 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 ) );AttributeValue v = at

5、tributes.back();。5。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( proc

6、essLine(sLine) );std:list processQ;std:vector finishedDataSet;processQ.push_back(initDataset);。6。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);。7。std:cout The dicision tree is: std:endl;for (int i = 0; i finishedDataSet.size(); +i)finishedDataSeti.display();Attributevalue.cpp:#include Att

8、ributeValue.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;elsethrow DataErrException();Basefun.cpp:#include float log2 (float x)。8。return 1.0 / log10(2) * log10(x)

9、;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( 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;elsestd:cout tN;。9。std:cout std:endl;Dataset.cpp:#include #include #include base.h#include DataSet.hvoid SplitAttributeValue:displa

11、y()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.isPositive()

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

13、s);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);typed

14、ef std:map AttributeValueStat;typedef std:map:iterator AttributeValueStat_iterator;typedefstd:map:const_iteratorAttributeValueStat_const_iterator;/ go through data once, and collect needed statistics to calculate entropy std:vector splittingStats; splittingStats.resize( splittingAttributeIds.size()

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

16、;stat.id= 0;elsestat.nPos = 0;stat.nNeg = 1;stat.id= 0;splittingStatsj.insert(std:pair(v, stat);elseif ( m_datai.isPositive() )it-second.nPos+;elseit-second.nNeg+;/ display collected statisticsfor (j=0; jsplittingAttributeIds.size(); +j)std:cout Attribute( splittingAttributeIdsj ): std:endl; std:cou

17、t 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; jsecond.nPos +

18、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+;splittedSets.resize( k);for (i=0; ik; +i)for (j=0; jsecond.id.m_splitAttributes.push_back(SplitAttributeValue(itt -first, attrId);。13。for (i=0; isecond.id.addDataPoint(m_datai);elsethrow Da

温馨提示

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

评论

0/150

提交评论