写一个决策树的python代码
时间: 2023-03-11 07:53:53 浏览: 94
import numpy as np
from sklearn import tree# [Height, Weight, Shoe Size]
X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],
[190, 90, 47], [175, 64, 39],
[177, 70, 40], [159, 55, 37], [171, 75, 42], [181, 85, 43]]Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
'female', 'male', 'male']# Create a Decision Tree Classifier
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)prediction = clf.predict([[190, 70, 43]])print(prediction)
相关问题
编写一个决策树python代码
以下是一个简单的决策树Python代码:
```
# 导入所需的库
from sklearn import tree
import pandas as pd
# 创建数据集
data = pd.DataFrame({'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy', 'Rainy', 'Overcast', 'Sunny', 'Sunny', 'Rainy', 'Sunny', 'Overcast', 'Overcast', 'Rainy'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']})
# 将分类变量编码为数字
data = data.apply(lambda x: pd.factorize(x)[0])
# 创建决策树分类器
clf = tree.DecisionTreeClassifier()
# 将数据拆分为特征和目标变量
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 拟合决策树分类器
clf.fit(X, y)
# 预测新的实例
new_instance = [[0, 1, 0, 0]] # Sunny, Mild, High, Weak
prediction = clf.predict(new_instance)
print('预测结果:', prediction)
```
该代码实现了一个简单的分类问题,数据集包括天气状况和是否打球。使用决策树分类器对数据进行拟合,并对新实例进行预测。
写一个Python决策树代码
以下是一个简单的Python决策树代码示例:
```python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
iris = load_iris()
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42)
# 创建决策树模型
clf = DecisionTreeClassifier(random_state=42)
# 训练模型
clf.fit(X_train, y_train)
# 在测试集上进行预测并计算准确率
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("决策树模型的准确率为:", accuracy)
```
在这个示例中,首先我们使用`load_iris()`函数加载了鸢尾花数据集,然后使用`train_test_split()`函数将数据集划分为训练集和测试集。接着我们使用`DecisionTreeClassifier`类创建了一个决策树模型,使用`fit()`方法在训练集上训练模型。最后在测试集上使用`predict()`方法进行预测,并使用`accuracy_score()`函数计算模型的准确率。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)