這篇文章主要介紹了python怎么實(shí)現(xiàn)決策樹(shù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇python怎么實(shí)現(xiàn)決策樹(shù)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括浚縣網(wǎng)站建設(shè)、浚縣網(wǎng)站制作、浚縣網(wǎng)頁(yè)制作以及??h網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,??h網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到??h省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
背景介紹
這是我最喜歡的算法之一,我經(jīng)常使用它。它是一種監(jiān)督學(xué)習(xí)算法,主要用于分類(lèi)問(wèn)題。令人驚訝的是,它適用于分類(lèi)和連續(xù)因變量。在該算法中,我們將總體分成兩個(gè)或更多個(gè)同類(lèi)集。這是基于最重要的屬性/獨(dú)立變量來(lái)完成的,以盡可能地作為不同的組。
在上圖中,您可以看到人口根據(jù)多個(gè)屬性分為四個(gè)不同的組,以識(shí)別“他們是否會(huì)玩”。為了將人口分成不同的異構(gòu)群體,它使用各種技術(shù),如基尼,信息增益,卡方,熵。
理解決策樹(shù)如何工作的最好方法是玩Jezzball--一款來(lái)自微軟的經(jīng)典游戲(如下圖所示)?;旧希阌幸粋€(gè)移動(dòng)墻壁的房間,你需要?jiǎng)?chuàng)建墻壁,以便最大限度的區(qū)域被球清除。
所以,每次你用墻隔開(kāi)房間時(shí),你都試圖在同一個(gè)房間里創(chuàng)造2個(gè)不同的人口。決策樹(shù)以非常類(lèi)似的方式工作,通過(guò)將人口分成盡可能不同的群體。
接下來(lái)看使用Python Scikit-learn的決策樹(shù)案例:
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# read the train and test dataset
train_data = pd.read_csv('train-data.csv')
test_data = pd.read_csv('test-data.csv')
# shape of the dataset
print('Shape of training data :',train_data.shape)
print('Shape of testing data :',test_data.shape)
train_x = train_data.drop(columns=['Survived'],axis=1)
train_y = train_data['Survived']
test_x = test_data.drop(columns=['Survived'],axis=1)
test_y = test_data['Survived']
model = DecisionTreeClassifier()
model.fit(train_x,train_y)
# depth of the decision tree
print('Depth of the Decision Tree :', model.get_depth())
# predict the target on the train dataset
predict_train = model.predict(train_x)
print('Target on train data',predict_train)
# Accuray Score on train dataset
accuracy_train = accuracy_score(train_y,predict_train)
print('accuracy_score on train dataset : ', accuracy_train)
# predict the target on the test dataset
predict_test = model.predict(test_x)
print('Target on test data',predict_test)
# Accuracy Score on test dataset
accuracy_test = accuracy_score(test_y,predict_test)
print('accuracy_score on test dataset : ', accuracy_test)
上面代碼運(yùn)行結(jié)果:
Shape of training data : (712, 25)Shape of testing data : (179, 25)Depth of the Decision Tree : 19Target on train data [0 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 0 01 0 0 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 0 1 0 1 11 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 00 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 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0 1 0 1 0 1 1 1 0 0 1 0]accuracy_score on train dataset : 0.9859550561797753Target on test data [0 0 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 01 1 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 00 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 1 11 0 1 1 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 01 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 1 0 1 0 0 0 0 0]accuracy_score on test dataset : 0.770949720670391
關(guān)于“python怎么實(shí)現(xiàn)決策樹(shù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“python怎么實(shí)現(xiàn)決策樹(shù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。