機器學習:帶你瞭解決策樹

什麼是決策樹?

決策樹是類似流程圖的結構,其中每個內部節點表示對屬性的“測試”(例如,硬幣翻轉是否出現在頭部或尾部),每個分支代表測試的結果,並且每個葉節點表示class label(在計算所有屬性後做出的決定)。

簡而言之,決策樹檢查屬性或屬性集是否滿足條件,並且基於檢查結果,執行後續檢查。樹根據這些檢查將數據分成不同的部分。

導入必要的Python庫

import pandas as pd

import numpy as np

from sklearn.preprocessing import StandardScaler

import tflearn.data_utils as du

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split

import seaborn as sns

from sklearn.metrics import confusion_matrix

from sklearn.externals.six import StringIO

from IPython.display import Image

from sklearn.tree import export_graphviz

import pydotplus

讀取數據集

data = pd.read_csv('../input/column_3C_weka.csv')

這裡使用的數據集是orthopedic patients的生物力學特徵(https://www.kaggle.com/uciml/biomechanical-features-of-orthopedic-patients)

相關性是什麼?

相關性是一個統計學術語,通常指的是兩個變量之間的線性關係有多近。

例如,兩個線性相關的變量(比如x和y, x = 2y)比兩個非線性相關的變量(比如u和v, u = sqr(v))具有更高的相關性

可視化相關性

# Calculating the correlation matrix

corr = data.corr()

# Generating a heatmap

sns.heatmap(corr,xticklabels=corr.columns, yticklabels=corr.columns)

機器學習:帶你瞭解決策樹

Correlation heat map

sns.pairplot(data)

機器學習:帶你瞭解決策樹

Pair plot to visualize the correlation

在上面的兩個圖中,您可以清楚地看到具有較高相關性的自變量對具有比具有相對較小相關性的自變量具有更多線性散點圖。

將數據集分割成自變量(x)和因變量(y)

x = data.iloc[:,:6].values

y = data.iloc[:,6].values

將數據集拆分為訓練和測試數據

訓練數據用於訓練模型和測試數據以驗證模型的性能

x_train , x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)

縮放自變量

sc = StandardScaler()

x_train = sc.fit_transform(x_train)

x_test = sc.transform(x_test)

建立決策樹

這裡的準則是熵。標準參數決定了函數來衡量分割的質量。當熵被用作準則時,每一次分割都試圖減少數據那部分的隨機性。

使用的另一個參數是max_depth。這決定了樹的深度。

classifier = DecisionTreeClassifier(criterion = 'entropy', max_depth = 4)

classifier.fit(x_train, y_train)

對測試數據進行預測

y_pred = classifier.predict(x_test)

什麼是混淆矩陣?

混淆矩陣是用於總結分類算法的性能的技術。如果每個類中的觀察數量不等,或者數據集中有兩個以上的類,則單獨的分類準確性可能會產生誤導。計算混淆矩陣可以讓您更好地瞭解分類模型的正確性以及它所犯的錯誤類型

cm = confusion_matrix(y_test, y_pred)

accuracy = sum(cm[i][i] for i in range(3)) / y_test.shape[0]

print("accuracy = " + str(accuracy))

可視化決策樹

dot_data = StringIO()

export_graphviz(classifier, out_file=dot_data,

filled=True, rounded=True,

special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

Image(graph.create_png())

機器學習:帶你瞭解決策樹

具有max_depth參數的決策樹

構建沒有max_depth參數的模型

classifier2 = DecisionTreeClassifier(criterion = 'entropy')

classifier2.fit(x_train, y_train)

y_pred2 = classifier2.predict(x_test)

cm2 = confusion_matrix(y_test, y_pred2)

accuracy2 = sum(cm2[i][i] for i in range(3)) / y_test.shape[0]

print("accuracy = " + str(accuracy2))

在沒有max_depth參數的情況下可視化決策樹

dot_data = StringIO()

export_graphviz(classifier2, out_file=dot_data,

filled=True, rounded=True,

special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

Image(graph.create_png())

機器學習:帶你瞭解決策樹

沒有max_depth參數的決策樹

現在,考慮包含和不包含max_depth參數的樹的葉子節點(終端節點)。您將注意到,在沒有max_depth參數的情況下,樹中的所有終端節點的熵都為零,而在有這個參數的情況下,樹中的三個節點的熵為非零。這是因為在沒有提到參數的情況下,遞歸地進行分割,直到終端節點的熵為零。

如需瞭解更多機器學習,人工智能,Python,大數據,Java等自學資料,可點擊關注小編頭條號+私信“資料”自行了解哦,我們主打自學,不會胡亂速成,打牢基礎才是根本,在這裡小編送上《Java學習資料》一份,需要的朋友可以留意下方獲取方式。

機器學習:帶你瞭解決策樹

機器學習:帶你瞭解決策樹

機器學習:帶你瞭解決策樹

獲取方式:點點關注+私信“Java”即可獲取。

我們相信自學能成才,高薪不是空想。


分享到:


相關文章: