gridSearch做参数调优的练习 将K近邻 svm 决策树 randomforest adaboost模型的优化全都放进来
代码实现参考了这篇文章 https://blog.csdn.net/weixin_41171061/article/details/83859856
比较各种组合下分类效果最好的一个方案
用鸢尾花数据
#!/usr/bin/python # -*- coding:utf-8 -*- from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier from sklearn.model_selection import GridSearchCV from sklearn.metrics import accuracy_score from sklearn.metrics import auc import numpy as np import matplotlib as mpl import pandas as pd seed = 1231 np.random.seed(seed) names = ['KNearesNeighbors','SVC','Decision Tree', 'Random Forest','AdaBoostClassifier'] classifiers = [KNeighborsClassifier(),SVC(kernel='rbf'),DecisionTreeClassifier(), RandomForestClassifier(),AdaBoostClassifier(base_estimator=DecisionTreeClassifier())] parameter_knn = {'n_neighbors':[3,5]} parameter_svc={'C': np.logspace(-2, 2, 10), 'gamma': np.logspace(-2, 2, 10)} parameter_dtc = {'max_features': ['auto', 'sqrt', 'log2', None], 'max_depth': range(3, 100, 2)} param