昆明理工大学人工智能试验天气决策树

  • 格式:doc
  • 大小:207.00 KB
  • 文档页数:13

下载文档原格式

  / 13
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

昆明理工大学信息工程与自动化学院学生实验报告

(2013 —2014 学年第 1 学期)

课程名称:人工智能开课实验室:信自楼计算机机房442 2013 年12月 21日

一、上机目的及内容

1.上机内容

根据下列给定的14个数据,运用Information Gain构造一个天气决策树。

2.上机目的

(1)学习用Information Gain 构造决策树的方法; (2)在给定的例子上,构造出正确的决策树; (3)理解并掌握构造决策树的技术要点。

二、实验原理及基本技术路线图(方框原理图或程序流程图)

(1)设计并实现程序,构造出正确的决策树; 问题分许:天况——晴、雨、多云 温度——热、中、冷 湿度——大、正常 风况——有、无

首先我们要根据每个属性来算出信息增益,接下来我们根据信息增益最大的来进行划分。

选择一个属性,根据该Information Gain 把数据分割为K 份。分许如下:

数据集

计算IG

划分数据集

(2)主要函数流程图:

Basefun 流程图

三、所用仪器、材料(设备名称、型号、规格等或使用软件)

1台PC及VISUAL C++6.0软件

四、实验方法、步骤(或:程序代码或操作过程)

Main.cpp:

#include

#include

#include

#include

#include

#include

#include "AttributeValue.h"

#include "DataPoint.h"

#include "DataSet.h"

DataPoint 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 = 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( 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; i

{

float prob = splittedDataSets[i].getPositiveProb();

if (prob == 0.0 || prob == 1.0)

{

finishedDataSet.push_back(splittedDataSets[i]);

}

else

{

processQ.push_back(splittedDataSets[i]);

}

}

}

std::cout << "The dicision tree is:" << std::endl;

for (int i = 0; i < finishedDataSet.size(); ++i)

{

finishedDataSet[i].display();

}

}

Attributevalue.cpp:

#include "AttributeValue.h"

#include "base.h"

AttributeValue::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(x);

}

float calEntropy(float prob)

{