自助法划分鸢尾花数据集
时间: 2023-11-20 22:50:38 浏览: 90
基于MATLAB的鸢尾花数据集分类
5星 · 资源好评率100%
可以使用sklearn库中的train_test_split函数来划分鸢尾花数据集。具体步骤如下:
1. 导入数据集
```python
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
```
2. 划分数据集
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
其中,test_size表示测试集所占比例,random_state表示随机种子,保证每次划分的结果一致。
3. 训练模型
```python
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
```
4. 预测结果
```python
y_pred = clf.predict(X_test)
```
5. 评估模型
```python
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
阅读全文