使用带交叉验证的网络搜索对鸢尾花数据进行分类Python代码
时间: 2024-10-20 08:08:08 浏览: 46
在Python中,我们可以使用sklearn库中的`GridSearchCV`结合`RandomForestClassifier`对鸢尾花数据进行分类,并使用交叉验证提高模型性能。以下是一个简单的示例:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestClassifier
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集(比如80%的数据用于训练,20%用于测试)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建随机森林分类器
rfc = RandomForestClassifier()
# 定义超参数网格搜索
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10]
}
# 实例化网格搜索并指定模型和参数网格
grid_search = GridSearchCV(rfc, param_grid, cv=5) # 交叉验证次数设置为5折
# 训练模型
grid_search.fit(X_train, y_train)
# 打印最佳参数组合
print("Best parameters found: ", grid_search.best_params_)
# 使用最佳参数进行预测
best_rfc = grid_search.best_estimator_
predictions = best_rfc.predict(X_test)
# 评估模型性能
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy with best parameters: {accuracy}")
阅读全文
相关推荐


















