用Python構建和可視化決策樹

做者|Nikhil Adithyan 編譯|VK 來源|Towards Data Sciencepython

決策樹

決策樹是當今最強大的監督學習方法的組成部分。決策樹基本上是一個二叉樹的流程圖,其中每一個節點根據某個特徵變量將一組觀測值拆分。算法

決策樹的目標是將數據分紅多個組,這樣一個組中的每一個元素都屬於同一個類別。決策樹也能夠用來近似連續的目標變量。在這種狀況下,樹將進行拆分,使每一個組的均方偏差最小。編程

決策樹的一個重要特性是它們很容易被解釋。你根本不須要熟悉機器學習技術就能夠理解決策樹在作什麼。決策樹圖很容易解釋。數組

利弊

決策樹方法的優勢是:

  • 決策樹可以生成可理解的規則。網絡

  • 決策樹在不須要大量計算的狀況下進行分類。dom

  • 決策樹可以處理連續變量和分類變量。機器學習

  • 決策樹提供了一個明確的指示,哪些字段是最重要的。編程語言

決策樹方法的缺點是:

  • 決策樹不太適合於目標是預測連續屬性值的估計任務。函數

  • 決策樹在類多、訓練樣本少的分類問題中容易出錯。工具

  • 決策樹的訓練在計算上可能很昂貴。生成決策樹的過程在計算上很是昂貴。在每一個節點上,每一個候選拆分字段都必須進行排序,才能找到其最佳拆分。在某些算法中,使用字段組合,必須搜索最佳組合權重。剪枝算法也多是昂貴的,由於許多候選子樹必須造成和比較。

Python決策樹

Python是一種通用編程語言,它爲數據科學家提供了強大的機器學習包和工具。在本文中,咱們將使用python最著名的機器學習包scikit-learn來構建決策樹模型。咱們將使用scikit learn提供的「DecisionTreeClassifier」算法建立模型,而後使用「plot_tree」函數可視化模型。

步驟1:導入包

咱們構建模型的主要軟件包是pandas、scikit learn和NumPy。按照代碼在python中導入所需的包。

import pandas as pd # 數據處理
import numpy as np # 使用數組
import matplotlib.pyplot as plt # 可視化
from matplotlib import rcParams # 圖大小
from termcolor import colored as cl # 文本自定義

from sklearn.tree import DecisionTreeClassifier as dtc # 樹算法
from sklearn.model_selection import train_test_split # 拆分數據
from sklearn.metrics import accuracy_score # 模型準確度
from sklearn.tree import plot_tree # 樹圖

rcParams['figure.figsize'] = (25, 20)

在導入構建咱們的模型所需的全部包以後,是時候導入數據並對其進行一些EDA了。

步驟2:導入數據和EDA

在這一步中,咱們將使用python中提供的「Pandas」包來導入並在其上進行一些EDA。咱們將創建咱們的決策樹模型,數據集是一個藥物數據集,它是基於特定的標準給病人開的處方。讓咱們用python導入數據!

Python實現:
df = pd.read_csv('drug.csv')
df.drop('Unnamed: 0', axis = 1, inplace = True)

print(cl(df.head(), attrs = ['bold']))

輸出:

Age Sex      BP Cholesterol  Na_to_K   Drug
0   23   F    HIGH        HIGH   25.355  drugY
1   47   M     LOW        HIGH   13.093  drugC
2   47   M     LOW        HIGH   10.114  drugC
3   28   F  NORMAL        HIGH    7.798  drugX
4   61   F     LOW        HIGH   18.043  drugY

如今咱們對數據集有了一個清晰的概念。導入數據後,讓咱們使用「info」函數獲取有關數據的一些基本信息。此函數提供的信息包括條目數、索引號、列名、非空值計數、屬性類型等。

Python實現:
df.info()

輸出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 200 entries, 0 to 199
Data columns (total 6 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   Age          200 non-null    int64  
 1   Sex          200 non-null    object 
 2   BP           200 non-null    object 
 3   Cholesterol  200 non-null    object 
 4   Na_to_K      200 non-null    float64
 5   Drug         200 non-null    object 
dtypes: float64(1), int64(1), object(4)
memory usage: 9.5+ KB

步驟3:數據處理

咱們能夠看到像Sex, BP和Cholesterol這樣的屬性在本質上是分類的和對象類型的。問題是,scikit-learn中的決策樹算法本質上不支持X變量(特徵)是「對象」類型。所以,有必要將這些「object」值轉換爲「binary」值。讓咱們用python來實現

Python實現:
for i in df.Sex.values:
    if i  == 'M':
        df.Sex.replace(i, 0, inplace = True)
    else:
        df.Sex.replace(i, 1, inplace = True)

for i in df.BP.values:
    if i == 'LOW':
        df.BP.replace(i, 0, inplace = True)
    elif i == 'NORMAL':
        df.BP.replace(i, 1, inplace = True)
    elif i == 'HIGH':
        df.BP.replace(i, 2, inplace = True)

for i in df.Cholesterol.values:
    if i == 'LOW':
        df.Cholesterol.replace(i, 0, inplace = True)
    else:
        df.Cholesterol.replace(i, 1, inplace = True)

print(cl(df, attrs = ['bold']))

輸出:

Age  Sex  BP  Cholesterol  Na_to_K   Drug
0     23    1   2            1   25.355  drugY
1     47    1   0            1   13.093  drugC
2     47    1   0            1   10.114  drugC
3     28    1   1            1    7.798  drugX
4     61    1   0            1   18.043  drugY
..   ...  ...  ..          ...      ...    ...
195   56    1   0            1   11.567  drugC
196   16    1   0            1   12.006  drugC
197   52    1   1            1    9.894  drugX
198   23    1   1            1   14.020  drugX
199   40    1   0            1   11.349  drugX

[200 rows x 6 columns]

咱們能夠觀察到全部的「object」值都被處理成「binary」值來表示分類數據。例如,在膽固醇屬性中,顯示「低」的值被處理爲0,「高」則被處理爲1。如今咱們準備好從數據中建立因變量和自變量。

步驟4:拆分數據

在將咱們的數據處理爲正確的結構以後,咱們如今設置「X」變量(自變量),「Y」變量(因變量)。讓咱們用python來實現

Python實現:
X_var = df[['Sex', 'BP', 'Age', 'Cholesterol', 'Na_to_K']].values # 自變量
y_var = df['Drug'].values # 因變量

print(cl('X variable samples : {}'.format(X_var[:5]), attrs = ['bold']))
print(cl('Y variable samples : {}'.format(y_var[:5]), attrs = ['bold']))

輸出:

X variable samples : [[ 1.     2.    23.     1.    25.355]
 [ 1.     0.    47.     1.    13.093]
 [ 1.     0.    47.     1.    10.114]
 [ 1.     1.    28.     1.     7.798]
 [ 1.     0.    61.     1.    18.043]]
Y variable samples : ['drugY' 'drugC' 'drugC' 'drugX' 'drugY']

咱們如今可使用scikit learn中的「train_test_split」算法將數據分紅訓練集和測試集,其中包含咱們定義的X和Y變量。按照代碼在python中拆分數據。

Python實現:
X_train, X_test, y_train, y_test = train_test_split(X_var, y_var, test_size = 0.2, random_state = 0)

print(cl('X_train shape : {}'.format(X_train.shape), attrs = ['bold'], color = 'black'))
print(cl('X_test shape : {}'.format(X_test.shape), attrs = ['bold'], color = 'black'))
print(cl('y_train shape : {}'.format(y_train.shape), attrs = ['bold'], color = 'black'))
print(cl('y_test shape : {}'.format(y_test.shape), attrs = ['bold'], color = 'black'))

輸出:

X_train shape : (160, 5)
X_test shape : (40, 5)
y_train shape : (160,)
y_test shape : (40,)

如今咱們有了構建決策樹模型的全部組件。因此,讓咱們繼續用python構建咱們的模型。

步驟5:創建模型和預測

在scikit學習包提供的「DecisionTreeClassifier」算法的幫助下,構建決策樹是可行的。以後,咱們可使用咱們訓練過的模型來預測咱們的數據。最後,咱們的預測結果的精度能夠用「準確度」評估指標來計算。讓咱們用python來完成這個過程!

Python實現:
model = dtc(criterion = 'entropy', max_depth = 4)
model.fit(X_train, y_train)

pred_model = model.predict(X_test)

print(cl('Accuracy of the model is {:.0%}'.format(accuracy_score(y_test, pred_model)), attrs = ['bold']))

輸出:

Accuracy of the model is 88%

在代碼的第一步中,咱們定義了一個名爲「model」變量的變量,咱們在其中存儲DecisionTreeClassifier模型。接下來,咱們將使用咱們的訓練集對模型進行擬合和訓練。以後,咱們定義了一個變量,稱爲「pred_model」變量,其中咱們將模型預測的全部值存儲在數據上。最後,咱們計算了咱們的預測值與實際值的精度,其準確率爲88%。

步驟6:可視化模型

如今咱們有了決策樹模型,讓咱們利用python中scikit learn包提供的「plot_tree」函數來可視化它。按照代碼從python中的決策樹模型生成一個漂亮的樹圖。

Python實現:
feature_names = df.columns[:5]
target_names = df['Drug'].unique().tolist()

plot_tree(model, 
          feature_names = feature_names, 
          class_names = target_names, 
          filled = True, 
          rounded = True)

plt.savefig('tree_visualization.png')

輸出:

結論

有不少技術和其餘算法用於優化決策樹和避免過擬合,好比剪枝。雖然決策樹一般是不穩定的,這意味着數據的微小變化會致使最優樹結構的巨大變化,但其簡單性使其成爲普遍應用的有力候選。在神經網絡流行以前,決策樹是機器學習中最早進的算法。其餘一些集成模型,好比隨機森林模型,比普通決策樹模型更強大。

決策樹因爲其簡單性和可解釋性而很是強大。決策樹和隨機森林在用戶註冊建模、信用評分、故障預測、醫療診斷等領域有着普遍的應用。我爲本文提供了完整的代碼。

完整代碼:

import pandas as pd # 數據處理
import numpy as np # 使用數組
import matplotlib.pyplot as plt # 可視化
from matplotlib import rcParams # 圖大小
from termcolor import colored as cl # 文本自定義

from sklearn.tree import DecisionTreeClassifier as dtc # 樹算法
from sklearn.model_selection import train_test_split # 拆分數據
from sklearn.metrics import accuracy_score # 模型準確度
from sklearn.tree import plot_tree # 樹圖

rcParams['figure.figsize'] = (25, 20)

df = pd.read_csv('drug.csv')
df.drop('Unnamed: 0', axis = 1, inplace = True)

print(cl(df.head(), attrs = ['bold']))

df.info()

for i in df.Sex.values:
    if i  == 'M':
        df.Sex.replace(i, 0, inplace = True)
    else:
        df.Sex.replace(i, 1, inplace = True)
        
for i in df.BP.values:
    if i == 'LOW':
        df.BP.replace(i, 0, inplace = True)
    elif i == 'NORMAL':
        df.BP.replace(i, 1, inplace = True)
    elif i == 'HIGH':
        df.BP.replace(i, 2, inplace = True)
        
for i in df.Cholesterol.values:
    if i == 'LOW':
        df.Cholesterol.replace(i, 0, inplace = True)
    else:
        df.Cholesterol.replace(i, 1, inplace = True)
        
print(cl(df, attrs = ['bold']))

X_var = df[['Sex', 'BP', 'Age', 'Cholesterol', 'Na_to_K']].values # 自變量
y_var = df['Drug'].values # 因變量

print(cl('X variable samples : {}'.format(X_var[:5]), attrs = ['bold']))
print(cl('Y variable samples : {}'.format(y_var[:5]), attrs = ['bold']))

X_train, X_test, y_train, y_test = train_test_split(X_var, y_var, test_size = 0.2, random_state = 0)

print(cl('X_train shape : {}'.format(X_train.shape), attrs = ['bold'], color = 'red'))
print(cl('X_test shape : {}'.format(X_test.shape), attrs = ['bold'], color = 'red'))
print(cl('y_train shape : {}'.format(y_train.shape), attrs = ['bold'], color = 'green'))
print(cl('y_test shape : {}'.format(y_test.shape), attrs = ['bold'], color = 'green'))

model = dtc(criterion = 'entropy', max_depth = 4)
model.fit(X_train, y_train)

pred_model = model.predict(X_test)

print(cl('Accuracy of the model is {:.0%}'.format(accuracy_score(y_test, pred_model)), attrs = ['bold']))

feature_names = df.columns[:5]
target_names = df['Drug'].unique().tolist()

plot_tree(model, 
          feature_names = feature_names, 
          class_names = target_names, 
          filled = True, 
          rounded = True)

plt.savefig('tree_visualization.png')

原文連接:https://towardsdatascience.com/building-and-visualizing-decision-tree-in-python-2cfaafd8e1bb

歡迎關注磐創AI博客站: http://panchuang.net/

sklearn機器學習中文官方文檔: http://sklearn123.com/

歡迎關注磐創博客資源彙總站: http://docs.panchuang.net/

相關文章
相關標籤/搜索