决策树分析python 代码
时间: 2023-10-13 13:21:04 浏览: 39
下面是一份使用Python实现决策树分类算法的示例代码,其中使用了scikit-learn库:
```python
from sklearn import tree
from sklearn.datasets import load_iris
# 加载数据集
iris = load_iris()
# 构建决策树模型
clf = tree.DecisionTreeClassifier()
# 使用数据集训练模型
clf = clf.fit(iris.data, iris.target)
# 预测新数据
print(clf.predict([[5.1, 3.5, 1.4, 0.2]]))
```
在这个示例中,我们首先从scikit-learn库中导入了决策树分类器,并加载了一个经典的鸢尾花数据集,然后使用数据集训练了模型,并使用模型对新的数据进行了预测。在这个例子中,我们使用了默认的参数,但是决策树分类器有许多参数可以调整以优化模型的性能。
相关问题
决策树c4.5 python代码 txt
决策树是一种常用的机器学习算法,可以用来进行分类和回归问题的预测。C4.5是一种决策树算法的改进版,它采用了信息增益比来选择最优的划分属性。
以下是一个用Python实现C4.5算法的代码示例:
```
import numpy as np
import pandas as pd
from math import log2
# 计算信息熵
def calculate_entropy(data):
labels = data.iloc[:, -1]
label_counts = labels.value_counts()
probs = label_counts / label_counts.sum()
entropy = (-probs * np.log2(probs)).sum()
return entropy
# 计算信息增益
def calculate_information_gain(data, feature):
total_entropy = calculate_entropy(data)
feature_values = data[feature].unique()
weighted_entropy = 0
for value in feature_values:
subset = data[data[feature]==value]
subset_entropy = calculate_entropy(subset)
weighted_entropy += (subset.shape[0] / data.shape[0]) * subset_entropy
information_gain = total_entropy - weighted_entropy
return information_gain
# 计算信息增益比
def calculate_information_gain_ratio(data, feature):
information_gain = calculate_information_gain(data, feature)
intrinsic_value = 0
feature_values = data[feature].unique()
for value in feature_values:
subset = data[data[feature]==value]
prob = subset.shape[0] / data.shape[0]
intrinsic_value += prob * log2(prob)
information_gain_ratio = information_gain / (-intrinsic_value)
return information_gain_ratio
# 选择最优的划分属性
def select_best_feature(data):
features = data.columns[:-1]
best_feature = None
best_information_gain_ratio = 0
for feature in features:
information_gain_ratio = calculate_information_gain_ratio(data, feature)
if information_gain_ratio > best_information_gain_ratio:
best_information_gain_ratio = information_gain_ratio
best_feature = feature
return best_feature
# 构建决策树
def build_decision_tree(data):
labels = data.iloc[:, -1]
if len(set(labels)) == 1:
return labels.iloc[0]
if data.shape[1] == 1:
return labels.value_counts().idxmax()
best_feature = select_best_feature(data)
decision_tree = {best_feature: {}}
feature_values = data[best_feature].unique()
for value in feature_values:
subset = data[data[best_feature]==value].drop(best_feature, axis=1)
decision_tree[best_feature][value] = build_decision_tree(subset)
return decision_tree
# 预测新样本
def predict(decision_tree, sample):
if isinstance(decision_tree, str):
return decision_tree
feature = list(decision_tree.keys())[0]
value = sample[feature]
sub_tree = decision_tree[feature][value]
return predict(sub_tree, sample)
# 示例数据
data = pd.DataFrame({
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy', 'Rainy', 'Overcast', 'Sunny', 'Sunny', 'Rainy'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal'],
'Windy': ['False', 'True', 'False', 'False', 'False', 'True', 'True', 'False', 'False', 'False'],
'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes']
})
decision_tree = build_decision_tree(data)
# 对新样本进行预测
sample = {'Outlook': 'Sunny', 'Temperature': 'Hot', 'Humidity': 'Normal', 'Windy': 'False'}
prediction = predict(decision_tree, sample)
print("叶子节点的预测结果:", decision_tree)
print("新样本的预测结果:", prediction)
```
以上代码是一个简单的C4.5算法的实现,通过构建决策树来进行分类预测。代码中使用了Pandas库来处理和分析数据,使用了NumPy库来进行数学运算。首先,定义了计算信息熵、信息增益和信息增益比的函数,然后定义了选择最优划分属性的函数,接着定义了构建决策树和预测新样本的函数。最后,使用一个示例数据来构建决策树,并对一个新样本进行预测。
决策树AHP层次分析法代码实现
决策树和AHP层次分析法是两种不同的决策方法,我可以为你介绍它们的代码实现。
首先是决策树的代码实现。决策树是一种基于树结构的分类和回归算法,常用于解决分类和回归问题。以下是一个简单的决策树分类的Python代码示例:
```python
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建决策树分类器
clf = DecisionTreeClassifier()
# 训练模型
clf.fit(X_train, y_train)
# 预测
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("准确率:", accuracy)
```
接下来是AHP层次分析法的代码实现。AHP层次分析法是一种用于多准则决策的方法,它将问题分解为多个层次,通过对比不同层次的准则权重来进行决策。以下是一个简单的AHP层次分析法的Python代码示例:
```python
import numpy as np
# 构建判断矩阵
judgment_matrix = np.array([[1, 3, 5],
[1/3, 1, 2],
[1/5, 1/2, 1]])
# 计算权重
eigenvalues, eigenvectors = np.linalg.eig(judgment_matrix)
weights = eigenvectors[:, np.argmax(eigenvalues)]
# 归一化权重
weights /= np.sum(weights)
# 输出权重
for i, weight in enumerate(weights):
print("准则{}的权重:{}".format(i+1, weight))
```
以上是决策树和AHP层次分析法的简单代码实现示例。如果你有更具体的问题或者需要更复杂的实现,可以提供更多详细的要求。