sklearn库中决策树分类方法的接口,预处理处理鸢尾花数据
时间: 2024-05-13 07:14:31 浏览: 68
sklearn库中的决策树分类方法接口是`DecisionTreeClassifier`。可以按照以下步骤进行预处理和分类:
1. 导入需要的库和数据集:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
iris = load_iris()
X = iris.data
y = iris.target
```
2. 将数据集拆分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
3. 实例化决策树分类器:
```python
clf = DecisionTreeClassifier()
```
4. 训练模型:
```python
clf.fit(X_train, y_train)
```
5. 使用测试集进行预测:
```python
y_pred = clf.predict(X_test)
```
6. 对模型进行评估:
```python
from sklearn.metrics import accuracy_score
print("Accuracy:", accuracy_score(y_test, y_pred))
```
完整代码如下:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
```
阅读全文