日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 消費(fèi)電子 > 消費(fèi)電子
[導(dǎo)讀]今天,我們介紹機(jī)器學(xué)習(xí)里比較常用的一種分類算法,決策樹。決策樹是對人類認(rèn)知識別的一種模擬,給你一堆看似雜亂無章的數(shù)據(jù),如何用盡可能少的特征,對這些數(shù)據(jù)進(jìn)行有效的

今天,我們介紹機(jī)器學(xué)習(xí)里比較常用的一種分類算法,決策樹。決策樹是對人類認(rèn)知識別的一種模擬,給你一堆看似雜亂無章的數(shù)據(jù),如何用盡可能少的特征,對這些數(shù)據(jù)進(jìn)行有效的分類。

決策樹借助了一種層級分類的概念,每一次都選擇一個區(qū)分性最好的特征進(jìn)行分類,對于可以直接給出標(biāo)簽 label 的數(shù)據(jù),可能最初選擇的幾個特征就能很好地進(jìn)行區(qū)分,有些數(shù)據(jù)可能需要更多的特征,所以決策樹的深度也就表示了你需要選擇的幾種特征。

在進(jìn)行特征選擇的時候,常常需要借助信息論的概念,利用最大熵原則。

決策樹一般是用來對離散數(shù)據(jù)進(jìn)行分類的,對于連續(xù)數(shù)據(jù),可以事先對其離散化。

在介紹決策樹之前,我們先簡單的介紹一下信息熵,我們知道,熵的定義為:

 

 

我們先構(gòu)造一些簡單的數(shù)據(jù):

from sklearn import datasets

import numpy as np

import matplotlib.pyplot as plt

import math

import operator

def Create_data():

dataset = [[1, 1, 'yes'],

[1, 1, 'yes'],

[1, 0, 'no'],

[0, 1, 'no'],

[0, 1, 'no'],

[3, 0, 'maybe']]

feat_name = ['no surfacing', 'flippers']

return dataset, feat_name

然后定義一個計算熵的函數(shù):

def Cal_entrpy(dataset):

n_sample = len(dataset)

n_label = {}

for featvec in dataset:

current_label = featvec[-1]

if current_label not in n_label.keys():

n_label[current_label] = 0

n_label[current_label] += 1

shannonEnt = 0.0

for key in n_label:

prob = float(n_label[key]) / n_sample

shannonEnt -= prob * math.log(prob, 2)

return shannonEnt

要注意的是,熵越大,說明數(shù)據(jù)的類別越分散,越呈現(xiàn)某種無序的狀態(tài)。

下面再定義一個拆分?jǐn)?shù)據(jù)集的函數(shù):

def Split_dataset(dataset, axis, value):

retDataSet = []

for featVec in dataset:

if featVec[axis] == value:

reducedFeatVec = featVec[:axis]

reducedFeatVec.extend(featVec[axis+1 :])

retDataSet.append(reducedFeatVec)

return retDataSet

結(jié)合前面的幾個函數(shù),我們可以構(gòu)造一個特征選擇的函數(shù):

def Choose_feature(dataset):

num_sample = len(dataset)

num_feature = len(dataset[0]) - 1

baseEntrpy = Cal_entrpy(dataset)

best_Infogain = 0.0

bestFeat = -1

for i in range (num_feature):

featlist = [example[i] for example in dataset]

uniquValus = set(featlist)

newEntrpy = 0.0

for value in uniquValus:

subData = Split_dataset(dataset, i, value)

prob = len(subData) / float(num_sample)

newEntrpy += prob * Cal_entrpy(subData)

info_gain = baseEntrpy - newEntrpy

if (info_gain > best_Infogain):

best_Infogain = info_gain

bestFeat = i

return bestFeat

然后再構(gòu)造一個投票及計票的函數(shù)

def Major_cnt(classlist):

class_num = {}

for vote in classlist:

if vote not in class_num.keys():

class_num[vote] = 0

class_num[vote] += 1

Sort_K = sorted(class_num.iteritems(),

key = operator.itemgetter(1), reverse=True)

return Sort_K[0][0]

有了這些,就可以構(gòu)造我們需要的決策樹了:

def Create_tree(dataset, featName):

classlist = [example[-1] for example in dataset]

if classlist.count(classlist[0]) == len(classlist):

return classlist[0]

if len(dataset[0]) == 1:

return Major_cnt(classlist)

bestFeat = Choose_feature(dataset)

bestFeatName = featName[bestFeat]

myTree = {bestFeatName: {}}

del(featName[bestFeat])

featValues = [example[bestFeat] for example in dataset]

uniqueVals = set(featValues)

for value in uniqueVals:

subLabels = featName[:]

myTree[bestFeatName][value] = Create_tree(Split_dataset

(dataset, bestFeat, value), subLabels)

return myTree

def Get_numleafs(myTree):

numLeafs = 0

firstStr = myTree.keys()[0]

secondDict = myTree[firstStr]

for key in secondDict.keys():

if type(secondDict[key]).__name__ == 'dict' :

numLeafs += Get_numleafs(secondDict[key])

else:

numLeafs += 1

return numLeafs

def Get_treedepth(myTree):

max_depth = 0

firstStr = myTree.keys()[0]

secondDict = myTree[firstStr]

for key in secondDict.keys():

if type(secondDict[key]).__name__ == 'dict' :

this_depth = 1 + Get_treedepth(secondDict[key])

else:

this_depth = 1

if this_depth > max_depth:

max_depth = this_depth

return max_depth

我們也可以把決策樹繪制出來:

def Plot_node(nodeTxt, centerPt, parentPt, nodeType):

Create_plot.ax1.annotate(nodeTxt, xy=parentPt,

xycoords='axes fraction',

xytext=centerPt, textcoords='axes fraction',

va="center", ha="center", bbox=nodeType, arrowprops=arrow_args)

def Plot_tree(myTree, parentPt, nodeTxt):

numLeafs = Get_numleafs(myTree)

Get_treedepth(myTree)

firstStr = myTree.keys()[0]

cntrPt = (Plot_tree.xOff + (1.0 + float(numLeafs))/2.0/Plot_tree.totalW,

Plot_tree.yOff)

Plot_midtext(cntrPt, parentPt, nodeTxt)

Plot_node(firstStr, cntrPt, parentPt, decisionNode)

secondDict = myTree[firstStr]

Plot_tree.yOff = Plot_tree.yOff - 1.0/Plot_tree.totalD

for key in secondDict.keys():

if type(secondDict[key]).__name__=='dict':

Plot_tree(secondDict[key],cntrPt,str(key))

else:

Plot_tree.xOff = Plot_tree.xOff + 1.0/Plot_tree.totalW

Plot_node(secondDict[key], (Plot_tree.xOff, Plot_tree.yOff),

cntrPt, leafNode)

Plot_midtext((Plot_tree.xOff, Plot_tree.yOff), cntrPt, str(key))

Plot_tree.yOff = Plot_tree.yOff + 1.0/Plot_tree.totalD

def Create_plot (myTree):

fig = plt.figure(1, facecolor = 'white')

fig.clf()

axprops = dict(xticks=[], yticks=[])

Create_plot.ax1 = plt.subplot(111, frameon=False, **axprops)

Plot_tree.totalW = float(Get_numleafs(myTree))

Plot_tree.totalD = float(Get_treedepth(myTree))

Plot_tree.xOff = -0.5/Plot_tree.totalW; Plot_tree.yOff = 1.0;

Plot_tree(myTree, (0.5,1.0), '')

plt.show()

def Plot_midtext(cntrPt, parentPt, txtString):

xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]

yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]

Create_plot.ax1.text(xMid, yMid, txtString)

def Classify(myTree, featLabels, testVec):

firstStr = myTree.keys()[0]

secondDict = myTree[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

最后,可以測試我們的構(gòu)造的決策樹分類器:

decisionNode = dict(boxstyle="sawtooth", fc="0.8")

leafNode = dict(boxstyle="round4", fc="0.8")

arrow_args = dict(arrowstyle="<-")

myData, featName = Create_data()

S_entrpy = Cal_entrpy(myData)

new_data = Split_dataset(myData, 0, 1)

best_feat = Choose_feature(myData)

myTree = Create_tree(myData, featName[:])

num_leafs = Get_numleafs(myTree)

depth = Get_treedepth(myTree)

Create_plot(myTree)

predict_label = Classify(myTree, featName, [1, 0])

print("the predict label is: ", predict_label)

print("the decision tree is: ", myTree)

print("the best feature index is: ", best_feat)

print("the new dataset: ", new_data)

print("the original dataset: ", myData)

print("the feature names are: ", featName)

print("the entrpy is:", S_entrpy)

print("the number of leafs is: ", num_leafs)

print("the dpeth is: ", depth)

print("All is well.")

構(gòu)造的決策樹最后如下所示:

 

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

太陽的光線出現(xiàn)在生活中的每一個地方,人們的生活已經(jīng)離不開太陽,太陽能不僅為植物生長提供光源,而且也能為人類提供能源,現(xiàn)在的光伏發(fā)電就是很大程度上利用了太陽能。據(jù)最新一期《美國國家科學(xué)院院刊》報道,美國萊斯大學(xué)利用廉價塑料...

關(guān)鍵字: 氫燃料 電源技術(shù)解析 太陽能海水 淡化系統(tǒng)

在現(xiàn)在的生活中,太陽能產(chǎn)品處處可見,人們用太陽能煮飯,還有太陽能熱水器等等,無處不見太陽能產(chǎn)品,當(dāng)然,最重要的還是太陽能發(fā)電,但是目前的技術(shù)并不能讓人們很好利用太陽能發(fā)電。日前,科技部發(fā)布了《國家重點(diǎn)研發(fā)計劃“可再生能源...

關(guān)鍵字: 電池組件 電源技術(shù)解析 鈣鈦礦 協(xié)鑫

隨著社會的進(jìn)步,科技的發(fā)展,人們對能源的需求越來越大,而現(xiàn)有的能源有限,需要人們不斷發(fā)展新能源,而太陽能就是一個不錯的選擇,人們開始大力發(fā)展太陽能能發(fā)電。武漢大學(xué)高等研究院科研人員日前提出新的逐層刮涂技術(shù),該技術(shù)不僅使薄...

關(guān)鍵字: 光伏技術(shù) 太陽能電池 電源技術(shù)解析 新涂膜技術(shù)

在科技的發(fā)展道路上,離不開能源的助力,特別是再科技飛速發(fā)展的今天,而地球上的能源有限,就需要科研人員不斷開發(fā)新能源,這就再當(dāng)下最需要研發(fā)太陽能的使用。中國要實(shí)現(xiàn)在太空中建造一座兆瓦級太陽能發(fā)電站,將面臨很多前所未有的挑戰(zhàn)...

關(guān)鍵字: 太陽能電池 電源技術(shù)解析 石墨烯 傳統(tǒng)硅片

在科技的發(fā)展道路上,離不開能源的助力,特別是再科技飛速發(fā)展的今天,而地球上的能源有限,就需要科研人員不斷開發(fā)新能源,這就再當(dāng)下最需要研發(fā)太陽能的使用。儲能電池技術(shù)是制約新能源儲能產(chǎn)業(yè)發(fā)展的關(guān)鍵技術(shù)之一。光伏電站儲能、風(fēng)電...

關(guān)鍵字: 儲能電池技術(shù) 電源技術(shù)解析 鋰離子電池 碳鉛電池

太陽的光線出現(xiàn)在生活中的每一個地方,人們的生活已經(jīng)離不開太陽,太陽能不僅為植物生長提供光源,而且也能為人類提供能源,現(xiàn)在的光伏發(fā)電就是很大程度上利用了太陽能。近日,自從進(jìn)入夏季以來,持續(xù)的高溫已經(jīng)“蒸烤”一段時間了。據(jù)中...

關(guān)鍵字: 光伏電站 光伏組件 光伏逆變器 電源技術(shù)解析

太陽的光線出現(xiàn)在生活中的每一個地方,人們的生活已經(jīng)離不開太陽,太陽能不僅為植物生長提供光源,而且也能為人類提供能源,現(xiàn)在的光伏發(fā)電就是很大程度上利用了太陽能。在太陽能離網(wǎng)系統(tǒng)中,光伏控制器的作用是把光伏組件發(fā)出來的電,經(jīng)...

關(guān)鍵字: 光伏控制器 太陽能 電源技術(shù)解析 離網(wǎng)系統(tǒng)

在現(xiàn)在的生活中,太陽能產(chǎn)品處處可見,人們用太陽能煮飯,還有太陽能熱水器等等,無處不見太陽能產(chǎn)品,當(dāng)然,最重要的還是太陽能發(fā)電,但是目前的技術(shù)并不能讓人們很好利用太陽能發(fā)電。隨著越來越多的分布式光伏電站走進(jìn)千家萬戶,電站所...

關(guān)鍵字: 光伏電站 電源技術(shù)解析 組串逆變器 分布式光伏電站

太陽的光線出現(xiàn)在生活中的每一個地方,人們的生活已經(jīng)離不開太陽,太陽能不僅為植物生長提供光源,而且也能為人類提供能源,現(xiàn)在的光伏發(fā)電就是很大程度上利用了太陽能。從目前太陽能光伏電站的運(yùn)行管理工作實(shí)際經(jīng)驗(yàn)看,要保證光伏發(fā)電系...

關(guān)鍵字: 光伏電站 電源技術(shù)解析 光伏電站運(yùn)維管理 古瑞瓦特

隨著社會的進(jìn)步,科技的發(fā)展,人們對能源的需求越來越大,而現(xiàn)有的能源有限,需要人們不斷發(fā)展新能源,而太陽能就是一個不錯的選擇,人們開始大力發(fā)展太陽能能發(fā)電。有機(jī)-無機(jī)雜化鈣鈦礦材料由于具有吸收系數(shù)高,激子束縛能低和載流子壽...

關(guān)鍵字: 太陽能電池 電源技術(shù)解析 西安 鈣鈦礦電池
關(guān)閉