当前位置: 首页 > news >正文

网站建设的例子成都网站建设学习

网站建设的例子,成都网站建设学习,从0到建网站,绍兴网站建设公司电话任务描述 本关任务#xff1a;使用决策树进行分类 相关知识 为了完成本关任务#xff0c;你需要掌握#xff1a;1.使用决策树进行分类 使用决策树进行分类 依靠训练数据构造了决策树之后#xff0c;我们可以将它用于实际数据的分类。在执行数据分类时#xff0c;需要…任务描述 本关任务使用决策树进行分类 相关知识 为了完成本关任务你需要掌握1.使用决策树进行分类 使用决策树进行分类 依靠训练数据构造了决策树之后我们可以将它用于实际数据的分类。在执行数据分类时需要决策树以及用于构造树的标签向量。然后程序比较测试数据与决策树上的数值递归执行该过程直到进人叶子节点最后将测试数据定义为叶子节点所属的类型。 使用决策树的分类函数如下 Parameters:inputTree - 已经生成的决策树featLabels - 存储选择的最优特征标签testVec - 测试数据列表顺序对应最优特征标签Returns:classLabel - 分类结果# 函数说明:使用决策树分类def classify(inputTree, featLabels, testVec):firstStr next(iter(inputTree)) #获取决策树结点secondDict inputTree[firstStr] #下一个字典featIndex featLabels.index(firstStr)for key in secondDict.keys():if testVec[featIndex] key:if type(secondDict[key]).__name__ dict:classLabel classify(secondDict[key], featLabels, testVec)else: classLabel secondDict[key]return classLabel 编程要求 根据提示在右侧编辑器补充代码运用决策树分类 测试说明 平台会对你编写的代码进行测试 开始你的任务吧祝你成功 完整代码如下 # -*- coding: UTF-8 -*- from math import log import operator Parameters:dataSet - 数据集 Returns:shannonEnt - 经验熵(香农熵)# 函数说明:计算给定数据集的经验熵(香农熵) def calcShannonEnt(dataSet):numEntires len(dataSet) #返回数据集的行数labelCounts {} #保存每个标签(Label)出现次数的字典for featVec in dataSet: #对每组特征向量进行统计currentLabel featVec[-1] #提取标签(Label)信息if currentLabel not in labelCounts.keys(): #如果标签(Label)没有放入统计次数的字典,添加进去labelCounts[currentLabel] 0labelCounts[currentLabel] 1 #Label计数shannonEnt 0.0 #经验熵(香农熵)for key in labelCounts: #计算香农熵prob float(labelCounts[key]) / numEntires#选择该标签(Label)的概率shannonEnt - prob * log(prob, 2) #利用公式计算return shannonEnt #返回经验熵(香农熵) Parameters:无 Returns:dataSet - 数据集labels - 特征标签# 函数说明:创建测试数据集 def createDataSet():dataSet [[0, 0, 0, 0, no],#数据集[0, 0, 0, 1, no],[0, 1, 0, 1, yes],[0, 1, 1, 0, yes],[0, 0, 0, 0, no],[1, 0, 0, 0, no],[1, 0, 0, 1, no],[1, 1, 1, 1, yes],[1, 0, 1, 2, yes],[1, 0, 1, 2, yes],[2, 0, 1, 2, yes],[2, 0, 1, 1, yes],[2, 1, 0, 1, yes],[2, 1, 0, 2, yes],[2, 0, 0, 0, no]]labels [年龄, 有工作, 有自己的房子, 信贷情况]#特征标签return dataSet, labels#返回数据集和分类属性 Parameters:dataSet - 待划分的数据集axis - 划分数据集的特征value - 需要返回的特征的值 Returns:无# 函数说明:按照给定特征划分数据集 def splitDataSet(dataSet, axis, value):retDataSet [] #创建返回的数据集列表for featVec in dataSet: #遍历数据集if featVec[axis] value:reducedFeatVec featVec[:axis] #去掉axis特征reducedFeatVec.extend(featVec[axis1:])#将符合条件的添加到返回的数据集retDataSet.append(reducedFeatVec)return retDataSet #返回划分后的数据集 Parameters:dataSet - 数据集 Returns:bestFeature - 信息增益最大的(最优)特征的索引值# 函数说明:选择最优特征 def chooseBestFeatureToSplit(dataSet):numFeatures len(dataSet[0]) - 1 #特征数量baseEntropy calcShannonEnt(dataSet) #计算数据集的香农熵bestInfoGain 0.0 #信息增益bestFeature -1 #最优特征的索引值for i in range(numFeatures): #遍历所有特征#获取dataSet的第i个所有特征featList [example[i] for example in dataSet]uniqueVals set(featList) #创建set集合{},元素不可重复newEntropy 0.0 #经验条件熵for value in uniqueVals: #计算信息增益subDataSet splitDataSet(dataSet, i, value) #subDataSet划分后的子集prob len(subDataSet) / float(len(dataSet)) #计算子集的概率newEntropy prob * calcShannonEnt(subDataSet)#根据公式计算经验条件熵infoGain baseEntropy - newEntropy #信息增益# print(第%d个特征的增益为%.3f % (i, infoGain)) #打印每个特征的信息增益if (infoGain bestInfoGain): #计算信息增益bestInfoGain infoGain #更新信息增益找到最大的信息增益bestFeature i #记录信息增益最大的特征的索引值return bestFeature #返回信息增益最大的特征的索引值 Parameters:classList - 类标签列表 Returns:sortedClassCount[0][0] - 出现此处最多的元素(类标签)# 函数说明:统计classList中出现此处最多的元素(类标签) def majorityCnt(classList):classCount {}for vote in classList: #统计classList中每个元素出现的次数if vote not in classCount.keys():classCount[vote] 0classCount[vote] 1sortedClassCount sorted(classCount.items(), key operator.itemgetter(1), reverse True)#根据字典的值降序排序return sortedClassCount[0][0] #返回classList中出现次数最多的元素 Parameters:dataSet - 训练数据集labels - 分类属性标签featLabels - 存储选择的最优特征标签 Returns:myTree - 决策树# 函数说明:创建决策树 def createTree(dataSet, labels, featLabels):classList [example[-1] for example in dataSet] #取分类标签(是否放贷:yes or no)if classList.count(classList[0]) len(classList): #如果类别完全相同则停止继续划分return classList[0]if len(dataSet[0]) 1: #遍历完所有特征时返回出现次数最多的类标签return majorityCnt(classList)bestFeat chooseBestFeatureToSplit(dataSet) #选择最优特征bestFeatLabel labels[bestFeat] #最优特征的标签featLabels.append(bestFeatLabel)myTree {bestFeatLabel:{}} #根据最优特征的标签生成树del(labels[bestFeat]) #删除已经使用特征标签featValues [example[bestFeat] for example in dataSet]#得到训练集中所有最优特征的属性值uniqueVals set(featValues) #去掉重复的属性值for value in uniqueVals: #遍历特征创建决策树。myTree[bestFeatLabel][value] createTree(splitDataSet(dataSet, bestFeat, value), labels, featLabels)return myTree Parameters:inputTree - 已经生成的决策树featLabels - 存储选择的最优特征标签testVec - 测试数据列表顺序对应最优特征标签 Returns:classLabel - 分类结果# 函数说明:使用决策树分类 def classify(inputTree, featLabels, testVec):firstStr next(iter(inputTree)) #获取决策树结点secondDict inputTree[firstStr] #下一个字典featIndex featLabels.index(firstStr)for key in secondDict.keys():if testVec[featIndex] key:if type(secondDict[key]).__name__ dict:classLabel classify(secondDict[key], featLabels, testVec)else: classLabel secondDict[key]return classLabelif __name__ __main__:##########请输入你的代码dataSet, labels createDataSet() #得到数据集featLabels []myTree createTree(dataSet, labels, featLabels) #创造树testVec [0,1] #测试数据result classify(myTree, featLabels, testVec) #进行分类#########if result yes:print(放贷)if result no:print(不放贷)
http://www.w-s-a.com/news/397814/

相关文章:

  • 做信息图网站网站建设的软件介绍
  • 网站开发语言数据库有几种魏县审批建设的网站
  • 北京公司网站建设推荐海口建设
  • 不懂编程如何做网站婚礼网站模板
  • 像京东一样的网站wordpress入门视频教程7 - 如何在文章里加入视频和音乐
  • 惠州网站建设排名wordpress3万篇文章优化
  • 创建网站的三种方法北京建王园林工程有限公司
  • jsp网站建设模板下载十大免费excel网站
  • 网络公司网站图片网站建立好了自己怎么做优化
  • 云主机是不是可以搭建无数个网站百度快速seo优化
  • 房地产怎么做网站推广建立音乐网站
  • 川畅科技联系 网站设计网站开发的教学视频
  • 为什么学网站开发凡科登陆
  • 设计师常备设计网站大全中山精品网站建设信息
  • 杭州建设工程网seo服务是什么
  • 兼职做问卷调查的网站wordpress mysql设置
  • 怎么在百度上能搜到自己的网站山西seo谷歌关键词优化工具
  • 网站搭建免费模板飞鱼crm下载
  • 网站开发竞品分析app制作公司深圳
  • 网站建设ssc源码修复设计班级网站建设
  • 网站重定向凡科做网站不要钱
  • 佛山html5网站建设微信营销软件破解版
  • 网站单页做301南京百度推广
  • 私人做网站要多少钱展芒设计网页
  • 怎样网站制作设计如何在网上推广农产品
  • 做关键词排名卖网站聚名网
  • 吉林省住房城乡建设厅网站首页体育器材网站建设方案
  • 网站建设及维护专业手机金融界网站
  • 常州网站建设工作室建立网站有怎么用途
  • 如何盗取网站推广策划书模板