b.csv文件部分如下: 日期,天气状况,最高气温,最低气温,风力 2023/2/1,阴,2,1,3 2023/2/2,多云,2,1,1 2023/2/3,阴,2,1,2 2023/2/4,阴,2,1,1 2023/2/5,中雨,2,1,2 2023/2/6,小雨,2,1,3 2023/2/7,小雨,1,1,2 2023/2/8,阴,1,1,3 2023/2/9,晴,1,1,2 2023/2/10,晴,2,1,4 2023/2/11,多云,2,1,3 2023/2/12,中雨,3,1,2 2023/2/13,多云,2,1,4 2023/2/14,多云,2,1,1 2023/2/15,多云,2,1,4 2023/2/16,多云,3,1,3 请写出ID3的python代码
时间: 2023-08-12 17:08:52 浏览: 98
csv各地区天气数据集,新手练习
以下是使用Python实现ID3算法的代码:
```python
import pandas as pd
import numpy as np
import math
# 定义一个节点类
class Node:
def __init__(self, col=None, value=None, result=None, left=None, right=None):
self.col = col # 切分的列
self.value = value # 切分的值
self.result = result # 叶节点的预测结果
self.left = left # 左子节点
self.right = right # 右子节点
# 定义一个ID3决策树类
class ID3Tree:
def __init__(self, epsilon=0.1):
self.epsilon = epsilon
self.tree = None
# 计算信息熵
def calc_entropy(self, data):
n = len(data)
label_counts = {}
for row in data:
label = row[-1]
if label not in label_counts:
label_counts[label] = 0
label_counts[label] += 1
entropy = 0.0
for key in label_counts:
prob = float(label_counts[key]) / n
entropy -= prob * math.log(prob, 2)
return entropy
# 划分数据集
def split_data(self, data, col, value):
left_data = []
right_data = []
for row in data:
if row[col] <= value:
left_data.append(row)
else:
right_data.append(row)
return left_data, right_data
# 选择最优划分属性和划分值
def choose_best_feature(self, data):
n_features = len(data[0]) - 1
base_entropy = self.calc_entropy(data)
best_info_gain = 0.0
best_feature = -1
best_value = None
for col in range(n_features):
col_values = [row[col] for row in data]
unique_values = set(col_values)
for value in unique_values:
sub_data_left, sub_data_right = self.split_data(data, col, value)
prob_left = len(sub_data_left) / float(len(data))
new_entropy = prob_left * self.calc_entropy(sub_data_left) + (1 - prob_left) * self.calc_entropy(
sub_data_right)
info_gain = base_entropy - new_entropy
if info_gain > best_info_gain:
best_info_gain = info_gain
best_feature = col
best_value = value
return best_feature, best_value
# 构建决策树
def build_tree(self, data):
y = [row[-1] for row in data]
# 如果所有的标签都相同,返回叶节点
if len(set(y)) == 1:
return Node(result=y[0])
# 如果没有特征可用来划分数据,返回叶节点,标记为最常见的标签
if len(data[0]) == 1:
return Node(result=max(set(y), key=y.count))
# 选择最优划分属性和划分值
best_feature, best_value = self.choose_best_feature(data)
# 如果最优划分属性的信息增益小于阈值epsilon,返回叶节点,标记为最常见的标签
base_entropy = self.calc_entropy(data)
sub_data_left, sub_data_right = self.split_data(data, best_feature, best_value)
prob_left = len(sub_data_left) / float(len(data))
new_entropy = prob_left * self.calc_entropy(sub_data_left) + (1 - prob_left) * self.calc_entropy(sub_data_right)
if base_entropy - new_entropy < self.epsilon:
return Node(result=max(set(y), key=y.count))
# 构建当前节点
node = Node(col=best_feature, value=best_value)
# 递归构建左子树和右子树
node.left = self.build_tree(sub_data_left)
node.right = self.build_tree(sub_data_right)
return node
# 训练模型
def fit(self, X, y):
data = np.concatenate((X, y.reshape(-1, 1)), axis=1)
self.tree = self.build_tree(data)
# 预测单个样本
def predict_one(self, node, row):
if node.result is not None:
return node.result
if row[node.col] <= node.value:
return self.predict_one(node.left, row)
else:
return self.predict_one(node.right, row)
# 预测多个样本
def predict(self, X):
y_pred = []
for row in X:
y_pred.append(self.predict_one(self.tree, row))
return np.array(y_pred)
```
使用示例:
```python
# 读取数据
data = pd.read_csv('b.csv')
X = data.iloc[:, 2:4].values
y = data.iloc[:, -2].values
# 训练模型
tree = ID3Tree()
tree.fit(X, y)
# 预测
y_pred = tree.predict(X)
print(y_pred)
```
阅读全文