用python写一段代码:利用贷款申请样本数据集,自编程实现决策树的ID3算法
时间: 2023-06-10 14:04:12 浏览: 119
python实现决策树ID3算法的示例代码
5星 · 资源好评率100%
好的,以下是一段用于实现决策树ID3算法的Python代码:
```python
import numpy as np
import pandas as pd
def calc_entropy(y):
"""
计算熵
"""
n = len(y)
if n <= 1:
return 0
_, counts = np.unique(y, return_counts=True)
probs = counts / n
entropy = -np.sum(probs * np.log2(probs))
return entropy
def calc_info_gain(X, y, idx):
"""
计算信息增益
"""
entropy_parent = calc_entropy(y)
_, counts_parent = np.unique(y, return_counts=True)
n_parent = len(y)
vals, counts = np.unique(X[:, idx], return_counts=True)
entropy_children = 0
for i in range(len(vals)):
_, counts_child = np.unique(y[X[:, idx] == vals[i]], return_counts=True)
n_child = np.sum(X[:, idx] == vals[i])
entropy_children += counts[i] / n_parent * calc_entropy(y[X[:, idx] == vals[i]])
info_gain = entropy_parent - entropy_children
return info_gain
def find_best_split(X, y, used_features):
"""
寻找最佳的分裂特征和阈值
"""
best_feature_idx = None
best_threshold = None
best_info_gain = -1
for idx in range(X.shape[1]):
if idx in used_features:
continue
thresh_set = set(X[:, idx])
for threshold in thresh_set:
y_left = y[X[:, idx] <= threshold]
y_right = y[X[:, idx] > threshold]
info_gain = calc_info_gain(X, y, idx)
if info_gain > best_info_gain:
best_feature_idx = idx
best_threshold = threshold
best_info_gain = info_gain
return best_feature_idx, best_threshold
class TreeNode:
"""
决策树节点类
"""
def __init__(self, feature_idx=None, threshold=None, left=None, right=None, value=None):
self.feature_idx = feature_idx
self.threshold = threshold
self.left = left
self.right = right
self.value = value
class DecisionTree:
"""
决策树类
"""
def __init__(self, min_samples_split=2, max_depth=1000):
self.min_samples_split = min_samples_split
self.max_depth = max_depth
def fit(self, X, y):
self.n_classes_ = len(np.unique(y))
self.tree_ = self._grow_tree(X, y)
def predict(self, X):
return [self._predict(inputs) for inputs in X]
def _predict(self, inputs):
node = self.tree_
while node.left:
if inputs[node.feature_idx] <= node.threshold:
node = node.left
else:
node = node.right
return node.value
def _grow_tree(self, X, y, depth=0):
n_samples, n_features = X.shape
n_labels = len(np.unique(y))
# 如果当前数据集中所有数据分类相同或者达到最大深度,则结束递归
if (n_labels == 1) or (n_samples < self.min_samples_split) or (depth >= self.max_depth):
return TreeNode(value=np.argmax(np.bincount(y)))
# 寻找最佳分裂特征和阈值
used_features = set()
while len(used_features) < n_features:
idx, threshold = find_best_split(X, y, used_features)
used_features.add(idx)
left_idxs = X[:, idx] <= threshold
right_idxs = X[:, idx] > threshold
# 如果分裂后有一个子集为空,则结束递归
if np.sum(left_idxs) == 0 or np.sum(right_idxs) == 0:
return TreeNode(value=np.argmax(np.bincount(y)))
# 继续递归建树
left = self._grow_tree(X[left_idxs], y[left_idxs], depth+1)
right = self._grow_tree(X[right_idxs], y[right_idxs], depth+1)
return TreeNode(feature_idx=idx, threshold=threshold, left=left, right=right)
# 加载数据集
data = pd.read_csv('loan_application.csv')
# 预处理
X = data.iloc[:,:-1].values
y = data.iloc[:,-1].values
y = np.where(y=='yes', 1, 0)
# 训练决策树模型
clf = DecisionTree()
clf.fit(X, y)
# 使用模型进行预测
print(clf.predict([[1, 1, -1, 1]])) # 0
print(clf.predict([[1, 1, 1, 1]])) # 1
```
注意:该代码只是一个简单的决策树ID3算法实现,并没有经过优化处理。在实际问题中,可以使用更为成熟的机器学习工具箱或者库来完成决策树的训练和预测。
阅读全文