网格搜索法是指定参数值的一种穷举搜索方法,通过将估计函数的参数通过交叉验证的方法进行优化来得到最优的学习算法。
即,将各个参数可能的取值进行排列组合,列出所有可能的组合结果生成“网格”。然后将各组合用于SVM训练,并使用交叉验证对表现进行评估。在拟合函数尝试了所有的参数组合后,返回一个合适的分类器,自动调整至最佳参数组合,可以通过clf.best_params_获得参数值
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_/article/details/
交叉验证与网格搜索
交叉验证与网格搜索是机器学习中的两个非常重要且基本的概念,但是这两个概念在刚入门的时候并不是非常容易理解与掌握,自己开始学习的时候,对这两个概念理解的并不到位,现在写一篇关于交叉验证与网格搜索的文章,将这两个基本的概念做一下梳理。
网格搜索
网格搜索(Grid Search)名字非常大气,但是用简答的话来说就是你手动的给出一个模型中你想要改动的所用的参数,程序自动的帮你使用穷举法来将所用的参数都运行一遍。决策树中我们常常将最大树深作为需要调节的参数;AdaBoost中将弱分类器的数量作为需要调节的参数。
评分方法
为了确定搜索参数,也就是手动设定的调节的变量的值中,那个是最好的,这时就需要使用一个比较理想的评分方式(这个评分方式是根据实际情况来确定的可能是accuracy、f1-score、f-beta、pricise、recall等)
交叉验证
有了好的评分方式,但是只用一次的结果就能说明某组的参数组合比另外的参数组合好吗?这显然是不严谨的,上小学的时候老师就告诉我们要求平均��。所以就有了交叉验证这一概念。下面以K折交叉验证为例介绍这一概念。
- 首先进行数据分割
将原始数据集分为训练集和测试集。如下图以8:2的方式分割:
训练集使用来训练模型,测试集使用来测试模型的准确率。
注意:绝对不能使用测试集来训练数据,这相当于考试的时候先让你把考试的答案背过了,又让你参加考试。 - 数据验真
在k折交叉验证方法中其中K-1份作为训练数据,剩下的一份作为验真数据:
这个过程一共需要进行K次,将最后K次使用实现选择好的评分方式的评分求平均返回,然后找出最大的一个评分对用的参数组合。这也就完成了交叉验证这一过程。
举例 下面使用一个简单的例子(预测年收入是否大于5万美元)来进行说明网格搜索与交叉验证的使用。 数据集来自[UCI机器学习知识库](https://archive.ics.uci.edu/ml/datasets/Census+Income)。
import numpy as np import pandas as pd from IPython.display import display from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split from sklearn.metrics import make_scorer, fbeta_score, accuracy_score from sklearn.model_selection import GridSearchCV, KFold %matplotlib inline data = pd.read_csv("census.csv") # 将数据切分成特征和标签 income_raw = data['income'] features_raw = data.drop('income', axis=1) # 显示部分数据 # display(features_raw.head(n=1)) # 因为原始数据中的,capital-gain 和 capital-loss的倾斜度非常高,所以要是用对数转换。 skewed = ['capital-gain', 'capital-loss'] features_raw[skewed] = data[skewed].apply(lambda x: np.log(x + 1)) # 归一化数字特征,是为了保证所有的特征均被平等的对待 scaler = MinMaxScaler() numerical = ['age', 'education-num', 'capital-gain', 'capital-loss', 'hours-per-week'] features_raw[numerical] = scaler.fit_transform(data[numerical]) # display(features_raw.head(n=1)) # 独热编码,将非数字的形式转化为数字 features = pd.get_dummies(features_raw) income = income_raw.replace(['>50K', ['<=50K']], [1, 0]) # 切分数据集 X_train, X_test, y_train, y_test = train_test_split(features, income, test_size=0.2, random_state=0) # Adaboost from sklearn.ensemble import AdaBoostClassifier clf_Ada = AdaBoostClassifier(random_state=0) # 决策树 from sklearn.tree import DecisionTreeClassifier clf_Tree = DecisionTreeClassifier(random_state=0) # KNN from sklearn.neighbors import KNeighborsClassifier clf_KNN = KNeighborsClassifier() # SVM from sklearn.svm import SVC clf_svm = SVC(random_state=0) # Logistic from sklearn.linear_model import LogisticRegression clf_log = LogisticRegression(random_state=0) # 随机森林 from sklearn.ensemble import RandomForestClassifier clf_forest = RandomForestClassifier(random_state=0) # GBDT from sklearn.ensemble import GradientBoostingClassifier clf_gbdt = GradientBoostingClassifier(random_state=0) # GaussianNB from sklearn.naive_bayes import GaussianNB clf_NB = GaussianNB() scorer = make_scorer(accuracy_score) # 参数调优 kfold = KFold(n_splits=10) # 决策树 parameter_tree = {'max_depth': xrange(1, 10)} grid = GridSearchCV(clf_Tree, parameter_tree, scorer, cv=kfold) grid = grid.fit(X_train, y_train) print "best score: {}".format(grid.best_score_) display(pd.DataFrame(grid.cv_results_).T)
best score: 0.4.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|---|
| mean_fit_time | 0.0 | 0.0 | 0.0 | 0. | 0. | 0. | 0.17124 | 0. | 0. |
| mean_score_time | 0.00 | 0.00 | 0.00 | 0.0026047 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| mean_test_score | 0.75114 | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| mean_train_score | 0.75114 | 0.82421 | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| param_max_depth | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| params | {u’max_depth’: 1} | {u’max_depth’: 2} | {u’max_depth’: 3} | {u’max_depth’: 4} | {u’max_depth’: 5} | {u’max_depth’: 6} | {u’max_depth’: 7} | {u’max_depth’: 8} | {u’max_depth’: 9} |
| rank_test_score | 9 | 8 | 7 | 6 | 5 | 3 | 4 | 2 | 1 |
| split0_test_score | 0. | 0.8267 | 0. | 0. | 0. | 0. | 0. | 0.86042 | 0. |
| split0_train_score | 0. | 0. | 0. | 0.83943 | 0. | 0. | 0. | 0. | 0. |
| split1_test_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split1_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split2_test_score | 0. | 0. | 0. | 0.84052 | 0.8466 | 0. | 0. | 0. | 0. |
| split2_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0.86397 |
| split3_test_score | 0.73162 | 0. | 0. | 0. | 0.8466 | 0. | 0. | 0. | 0. |
| split3_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0.85537 | 0. | 0. |
| split4_test_score | 0. | 0. | 0.83361 | 0. | 0.83969 | 0. | 0. | 0.85047 | 0. |
| split4_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split5_test_score | 0. | 0. | 0. | 0. | 0. | 0. | 0.85738 | 0. | 0. |
| split5_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split6_test_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split6_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split7_test_score | 0. | 0. | 0. | 0. | 0. | 0.85264 | 0. | 0. | 0.85264 |
| split7_train_score | 0.75129 | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| split8_test_score | 0. | 0. | 0. | 0.83854 | 0.84407 | 0. | 0. | 0. | 0. |
| split8_train_score | 0. | 0. | 0. | 0.84008 | 0. | 0. | 0. | 0. | 0. |
| split9_test_score | 0. | 0. | 0. | 0. | 0.84877 | 0. | 0.85264 | 0. | 0. |
| split9_train_score | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| std_fit_time | 0.0 | 0.00 | 0.00 | 0.00 | 0.0053195 | 0.0 | 0.00 | 0.00 | 0.0 |
| std_score_time | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |
| std_test_score | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| std_train_score | 0.000 | 0.000 | 0.000 | 0.00 | 0.00 | 0.000 | 0.00 | 0.00 | 0.000 |
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176836.html原文链接:https://javaforall.net
