请给出完整的代码:参照“Understanding the decision tree structure”,对load_wine数据集进行分类(调参),并使用random forest对load_wine数据集进行分类(调参)。
时间: 2024-10-06 15:03:52 浏览: 28
Planet: Understanding the Amazon from Space 地球:从太空了解亚马逊-数据集
首先,我们需要导入所需的库,如`sklearn`, `pandas`, 和 `numpy`,然后加载`load_wine`数据集。这个数据集通常位于`sklearn.datasets`模块中。接下来,我们将使用决策树和随机森林进行分类,并对它们的超参数进行调整。
```python
# 导入所需库
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
# 加载数据集
wine = load_wine()
X = wine.data
y = wine.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 定义决策树模型和参数网格
dt_params = {
'criterion': ['gini', 'entropy'],
'max_depth': range(1, 11),
'min_samples_split': [2, 5, 10],
}
# 决策树分类器
dt_clf = DecisionTreeClassifier()
# 使用GridSearchCV进行参数搜索
dt_grid_search = GridSearchCV(dt_clf, dt_params, cv=5, scoring='accuracy')
dt_grid_search.fit(X_train, y_train)
# 打印最佳参数及其准确率
best_dt_params = dt_grid_search.best_params_
print(f"Best Decision Tree parameters: {best_dt_params}")
dt_best_model = dt_grid_search.best_estimator_
dt_accuracy = accuracy_score(y_test, dt_best_model.predict(X_test))
print(f"Decision Tree Accuracy: {dt_accuracy}")
# 定义随机森林模型和参数网格
rf_params = {
'n_estimators': [50, 100, 200],
'max_depth': range(1, 11),
'min_samples_split': [2, 5, 10],
'max_features': ['auto', 'sqrt', 'log2']
}
# 随机森林分类器
rf_clf = RandomForestClassifier()
# 进行参数搜索
rf_grid_search = GridSearchCV(rf_clf, rf_params, cv=5, scoring='accuracy')
rf_grid_search.fit(X_train, y_train)
# 打印最佳参数及其准确率
best_rf_params = rf_grid_search.best_params_
print(f"Best Random Forest parameters: {best_rf_params}")
rf_best_model = rf_grid_search.best_estimator_
rf_accuracy = accuracy_score(y_test, rf_best_model.predict(X_test))
print(f"Random Forest Accuracy: {rf_accuracy}")
阅读全文