参照“Understanding the decision tree structure”,对load_wine数据集进行分类(调参),并使用random forest对load_wine数据集进行分类(调参)。
时间: 2024-10-06 22:03:56 浏览: 30
理解决策树结构有助于我们构建有效的分类模型。对于`load_wine`数据集,这是一个经典的机器学习数据集,通常用于演示分类算法。首先,我们需要加载数据、预处理(如归一化或编码类别变量)、分割成训练集和测试集。
1. **加载数据**:
使用Python的scikit-learn库,我们可以这样做:
```python
from sklearn.datasets import load_wine
wine = load_wine()
X = wine.data
y = wine.target
```
2. **数据预处理**:
对于数值特征,可能需要标准化或归一化;对于类别特征,可以使用LabelEncoder或OneHotEncoder。
3. **划分数据集**:
```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.2, random_state=42)
```
接下来,我们将针对决策树和随机森林进行调参:
- **决策树**:
- 使用`DecisionTreeClassifier`:
```python
from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier(random_state=42)
# 搜索最优参数,例如通过GridSearchCV:
from sklearn.model_selection import GridSearchCV
param_grid = {'max_depth': [3, None], 'min_samples_split': [2, 5]}
grid_dtree = GridSearchCV(dtree, param_grid, cv=5)
grid_dtree.fit(X_train, y_train)
best_dtree = grid_dtree.best_estimator_
```
- **随机森林**:
- 使用`RandomForestClassifier`:
```python
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(random_state=42)
# 调整参数,如n_estimators, max_depth等:
param_grid_rf = {
'n_estimators': [10, 50, 100],
'max_depth': [None, 10, 20]
}
grid_rf = GridSearchCV(rf, param_grid_rf, cv=5)
grid_rf.fit(X_train, y_train)
best_rf = grid_rf.best_estimator_
```
最后,评估模型性能:
```python
from sklearn.metrics import accuracy_score
y_pred_train = best_dtree.predict(X_train)
y_pred_test = best_rf.predict(X_test)
print("Training Accuracy (DT):", accuracy_score(y_train, y_pred_train))
print("Testing Accuracy (DT):", accuracy_score(y_test, y_pred_test))
print("\n")
print("Training Accuracy (RF):", accuracy_score(y_train, y_pred_train))
print("Testing Accuracy (RF):", accuracy_score(y_test, y_pred_test))
```
阅读全文