【濾波器】基於matlab脈衝響應不變法+雙線性變換法數字濾波器設計【含Matlab原始碼 884期】
阿新 • • 發佈:2021-06-21
實驗四 決策樹演算法及應用
一、實驗目的
理解決策樹演算法原理,掌握決策樹演算法框架;
理解決策樹學習演算法的特徵選擇、樹的生成和樹的剪枝;
能根據不同的資料型別,選擇不同的決策樹演算法;
針對特定應用場景及資料,能應用決策樹演算法解決實際問題。
二、實驗內容
設計演算法實現熵、經驗條件熵、資訊增益等方法。
實現ID3演算法。
熟悉sklearn庫中的決策樹演算法;
針對iris資料集,應用sklearn的決策樹演算法進行類別預測。
針對iris資料集,利用自編決策樹演算法進行類別預測。
三、實驗報告要求
對照實驗內容,撰寫實驗過程、演算法及測試結果;
程式碼規範化:命名規則、註釋;
分析核心演算法的複雜度;
查閱文獻,討論ID3、5演算法的應用場景;
查詢文獻,分析決策樹剪枝策略。
四、實驗程式碼
1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from collections import Counter
import math
from math import log
import pprint
2
def create_data(): datasets = [['青年', '否', '否', '一般', '否'], ['青年', '否', '否', '好', '否'], ['青年', '是', '否', '好', '是'], ['青年', '是', '是', '一般', '是'], ['青年', '否', '否', '一般', '否'], ['中年', '否', '否', '一般', '否'], ['中年', '否', '否', '好', '否'], ['中年', '是', '是', '好', '是'], ['中年', '否', '是', '非常好', '是'], ['中年', '否', '是', '非常好', '是'], ['老年', '否', '是', '非常好', '是'], ['老年', '否', '是', '好', '是'], ['老年', '是', '否', '好', '是'], ['老年', '是', '否', '非常好', '是'], ['老年', '否', '否', '一般', '否'], ] labels = [u'年齡', u'有工作', u'有自己的房子', u'信貸情況', u'類別'] # 返回資料集和每個維度的名稱 return datasets, labels
3
datasets, labels = create_data()
4
train_data = pd.DataFrame(datasets, columns=labels)
5
train_data
6
# 熵 def calc_ent(datasets): data_length = len(datasets) label_count = {} for i in range(data_length): label = datasets[i][-1] if label not in label_count: label_count[label] = 0 label_count[label] += 1 ent = -sum([(p / data_length) * log(p / data_length, 2) for p in label_count.values()]) return ent # def entropy(y): # """ # Entropy of a label sequence # """ # hist = np.bincount(y) # ps = hist / np.sum(hist) # return -np.sum([p * np.log2(p) for p in ps if p > 0]) # 經驗條件熵 def cond_ent(datasets, axis=0): data_length = len(datasets) feature_sets = {} for i in range(data_length): feature = datasets[i][axis] if feature not in feature_sets: feature_sets[feature] = [] feature_sets[feature].append(datasets[i]) cond_ent = sum( [(len(p) / data_length) * calc_ent(p) for p in feature_sets.values()]) return cond_ent # 資訊增益 def info_gain(ent, cond_ent): return ent - cond_ent def info_gain_train(datasets): count = len(datasets[0]) - 1 ent = calc_ent(datasets) # ent = entropy(datasets) best_feature = [] for c in range(count): c_info_gain = info_gain(ent, cond_ent(datasets, axis=c)) best_feature.append((c, c_info_gain)) print('特徵({}) - info_gain - {:.3f}'.format(labels[c], c_info_gain)) # 比較大小 best_ = max(best_feature, key=lambda x: x[-1]) return '特徵({})的資訊增益最大,選擇為根節點特徵'.format(labels[best_[0]])
7
info_gain_train(np.array(datasets))
8
# 定義節點類 二叉樹
class Node:
def __init__(self, root=True, label=None, feature_name=None, feature=None):
self.root = root
self.label = label
self.feature_name = feature_name
self.feature = feature
self.tree = {}
self.result = {
'label:': self.label,
'feature': self.feature,
'tree': self.tree
}
def __repr__(self):
return '{}'.format(self.result)
def add_node(self, val, node):
self.tree[val] = node
def predict(self, features):
if self.root is True:
return self.label
return self.tree[features[self.feature]].predict(features)
class DTree:
def __init__(self, epsilon=0.1):
self.epsilon = epsilon
self._tree = {}
# 熵
@staticmethod
def calc_ent(datasets):
data_length = len(datasets)
label_count = {}
for i in range(data_length):
label = datasets[i][-1]
if label not in label_count:
label_count[label] = 0
label_count[label] += 1
ent = -sum([(p / data_length) * log(p / data_length, 2)
for p in label_count.values()])
return ent
# 經驗條件熵
def cond_ent(self, datasets, axis=0):
data_length = len(datasets)
feature_sets = {}
for i in range(data_length):
feature = datasets[i][axis]
if feature not in feature_sets:
feature_sets[feature] = []
feature_sets[feature].append(datasets[i])
cond_ent = sum([(len(p) / data_length) * self.calc_ent(p)
for p in feature_sets.values()])
return cond_ent
# 資訊增益
@staticmethod
def info_gain(ent, cond_ent):
return ent - cond_ent
def info_gain_train(self, datasets):
count = len(datasets[0]) - 1
ent = self.calc_ent(datasets)
best_feature = []
for c in range(count):
c_info_gain = self.info_gain(ent, self.cond_ent(datasets, axis=c))
best_feature.append((c, c_info_gain))
# 比較大小
best_ = max(best_feature, key=lambda x: x[-1])
return best_
def train(self, train_data):
"""
input:資料集D(DataFrame格式),特徵集A,閾值eta
output:決策樹T
"""
_, y_train, features = train_data.iloc[:, :
-1], train_data.iloc[:,
-1], train_data.columns[:
-1]
# 1,若D中例項屬於同一類Ck,則T為單節點樹,並將類Ck作為結點的類標記,返回T
if len(y_train.value_counts()) == 1:
return Node(root=True, label=y_train.iloc[0])
# 2, 若A為空,則T為單節點樹,將D中例項樹最大的類Ck作為該節點的類標記,返回T
if len(features) == 0:
return Node(
root=True,
label=y_train.value_counts().sort_values(
ascending=False).index[0])
# 3,計算最大資訊增益 同5.1,Ag為資訊增益最大的特徵
max_feature, max_info_gain = self.info_gain_train(np.array(train_data))
max_feature_name = features[max_feature]
# 4,Ag的資訊增益小於閾值eta,則置T為單節點樹,並將D中是例項數最大的類Ck作為該節點的類標記,返
if max_info_gain < self.epsilon:
return Node(
root=True,
label=y_train.value_counts().sort_values(
ascending=False).index[0])
# 5,構建Ag子集
node_tree = Node(
root=False, feature_name=max_feature_name, feature=max_feature)
feature_list = train_data[max_feature_name].value_counts().index
for f in feature_list:
sub_train_df = train_data.loc[train_data[max_feature_name] ==
f].drop([max_feature_name], axis=1)
# 6, 遞迴生成樹
sub_tree = self.train(sub_train_df)
node_tree.add_node(f, sub_tree)
# pprint.pprint(node_tree.tree)
return node_tree
def fit(self, train_data):
self._tree = self.train(train_data)
return self._tree
def predict(self, X_test):
return self._tree.predict(X_test)
9
datasets, labels = create_data()
data_df = pd.DataFrame(datasets, columns=labels)
dt = DTree()
tree = dt.fit(data_df)
10
tree
11
dt.predict(['老年', '否', '否', '一般'])
12
# data
def create_data():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df.columns = [
'sepal length', 'sepal width', 'petal length', 'petal width', 'label'
]
data = np.array(df.iloc[:100, [0, 1, -1]])
# print(data)
return data[:, :2], data[:, -1]
X, y = create_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
13
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
import graphviz
14
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train,)
15
clf.score(X_test, y_test)
16
tree_pic = export_graphviz(clf, out_file="mytree.pdf")
with open('mytree.pdf') as f:
dot_graph = f.read()
17
graphviz.Source(dot_graph)
18
from sklearn.tree import DecisionTreeClassifier
from sklearn import preprocessing
import numpy as np
import pandas as pd
from sklearn import tree
import graphviz
features = ["年齡", "有工作", "有自己的房子", "信貸情況"]
X_train = pd.DataFrame([
["青年", "否", "否", "一般"],
["青年", "否", "否", "好"],
["青年", "是", "否", "好"],
["青年", "是", "是", "一般"],
["青年", "否", "否", "一般"],
["中年", "否", "否", "一般"],
["中年", "否", "否", "好"],
["中年", "是", "是", "好"],
["中年", "否", "是", "非常好"],
["中年", "否", "是", "非常好"],
["老年", "否", "是", "非常好"],
["老年", "否", "是", "好"],
["老年", "是", "否", "好"],
["老年", "是", "否", "非常好"],
["老年", "否", "否", "一般"]
])
y_train = pd.DataFrame(["否", "否", "是", "是", "否",
"否", "否", "是", "是", "是",
"是", "是", "是", "是", "否"])
# 資料預處理
le_x = preprocessing.LabelEncoder()
le_x.fit(np.unique(X_train))
X_train = X_train.apply(le_x.transform)
le_y = preprocessing.LabelEncoder()
le_y.fit(np.unique(y_train))
y_train = y_train.apply(le_y.transform)
# 呼叫sklearn.DT建立訓練模型
model_tree = DecisionTreeClassifier()
model_tree.fit(X_train, y_train)
# 視覺化
dot_data = tree.export_graphviz(model_tree, out_file=None,
feature_names=features,
class_names=[str(k) for k in np.unique(y_train)],
filled=True, rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
graph
19
import numpy as np
class LeastSqRTree:
def __init__(self, train_X, y, epsilon):
# 訓練集特徵值
self.x = train_X
# 類別
self.y = y
# 特徵總數
self.feature_count = train_X.shape[1]
# 損失閾值
self.epsilon = epsilon
# 迴歸樹
self.tree = None
def _fit(self, x, y, feature_count, epsilon):
# 選擇最優切分點變數j與切分點s
(j, s, minval, c1, c2) = self._divide(x, y, feature_count)
# 初始化樹
tree = {"feature": j, "value": x[s, j], "left": None, "right": None}
if minval < self.epsilon or len(y[np.where(x[:, j] <= x[s, j])]) <= 1:
tree["left"] = c1
else:
tree["left"] = self._fit(x[np.where(x[:, j] <= x[s, j])],
y[np.where(x[:, j] <= x[s, j])],
self.feature_count, self.epsilon)
if minval < self.epsilon or len(y[np.where(x[:, j] > s)]) <= 1:
tree["right"] = c2
else:
tree["right"] = self._fit(x[np.where(x[:, j] > x[s, j])],
y[np.where(x[:, j] > x[s, j])],
self.feature_count, self.epsilon)
return tree
def fit(self):
self.tree = self._fit(self.x, self.y, self.feature_count, self.epsilon)
@staticmethod
def _divide(x, y, feature_count):
# 初始化損失誤差
cost = np.zeros((feature_count, len(x)))
# 公式5.21
for i in range(feature_count):
for k in range(len(x)):
# k行i列的特徵值
value = x[k, i]
y1 = y[np.where(x[:, i] <= value)]
c1 = np.mean(y1)
y2 = y[np.where(x[:, i] > value)]
c2 = np.mean(y2)
y1[:] = y1[:] - c1
y2[:] = y2[:] - c2
cost[i, k] = np.sum(y1 * y1) + np.sum(y2 * y2)
# 選取最優損失誤差點
cost_index = np.where(cost == np.min(cost))
# 選取第幾個特徵值
j = cost_index[0][0]
# 選取特徵值的切分點
s = cost_index[1][0]
# 求兩個區域的均值c1,c2
c1 = np.mean(y[np.where(x[:, j] <= x[s, j])])
c2 = np.mean(y[np.where(x[:, j] > x[s, j])])
return j, s, cost[cost_index], c1, c2
20
train_X = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]).T
y = np.array([4.50, 4.75, 4.91, 5.34, 5.80, 7.05, 7.90, 8.23, 8.70, 9.00])
model_tree = LeastSqRTree(train_X, y, .2)
model_tree.fit()
model_tree.tree
五、執行截圖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
五、實驗小結
通過這次試驗,我理解了決策樹的演算法原理,決策樹演算法是一種逼近離散函式值的方法。它是一種典型的分類方法,首先對資料進行處理,利用歸納演算法生成可讀的規則和決策樹,然後使用決策對新資料進行分析。本質上決策樹是通過一系列規則對資料進行分類的過程。