第四天-模型选择

1.错误类型

  • 过拟合
  • 欠拟合

2.模型复杂度图表



3.交叉验证集
用语选择模型



4.K折交叉验证

一个非常有用的循环利用数据的方法
在K折交叉验证中,将数据分为K个包



如上图所示,这里K = 4,然后我们将模型培训K次



每次将不同的包用作测试集,剩下的作为训练集,然后求结果的平均值,得到最终模型。
from sklearn.model_selection import KFold
kf = KFold(12,3,shuffle = True) #参数为数据大小和测试集的大小,shuffle = True 表示随机
for train_indices, test_indices in kf:
    print train_indices, test_indicies

建议随机初始化数据,以消除任何可能的偏差。

学习曲线

通过学习曲线检测过拟合和欠拟合
将使用三个模型来训练下面的圆形数据集

  • 决策树模型
  • 逻辑回归模型
  • 支持向量机模型

其中一个模型会过拟合,一个欠拟合,还有一个正常。首先,我们将编写代码为每个模型绘制学习曲线,最后我们将查看这些学习曲线,判断每个模型对应哪个曲线
首先,请记住三个模型的学习曲线外观如下所示:


网格搜索

在sklearn 中的网格搜索
在 sklearn 中的网格搜索非常简单。 我们将用一个例子来说明一下。 假设我们想要训练支持向量机,并且我们想在以下参数之间做出决定:

  • kernel:poly或rbf。
  • C:0.1,1 或 10。
    具体步骤如下所示:
  1. 导入 GridSearchCV
from sklearn.model_selection import GridSearchCV

2.选择参数
现在我们来选择我们想要选择的参数,并形成一个字典。 在这本字典中,键 (keys) 将是参数的名称,值 (values) 将是每个参数可能值的列表。

parameters = {'kernel':['poly', 'rbf'],'C':[0.1, 1, 10]}

3.创建一个评分机制(scorer)
我们需要确认将使用什么指标来为每个候选模型评分。 这里,我们将使用 F1 分数。

from sklearn.metrics import make_scorer
from sklearn.metrics import f1_score
scorer = make_scorer(f1_score)
  1. 使用参数 (parameter) 和评分机制 (scorer) 创建一个 GridSearch 对象。 使用此对象与数据保持一致 (fit the data)
# Create the object.
grid_obj = GridSearchCV(clf, parameters, scoring=scorer)
# Fit the data
grid_fit = grid_obj.fit(X, y)

5.获得最佳估算器 (estimator)

best_clf = grid_fit.best_estimator_
例子

使用网格搜索来完善模型
1.首先,定义一些参数来执行网格搜索。 我们建议使用max_depth, min_samples_leaf, 和 min_samples_split。

2.使用f1_score,为模型制作记分器。

3.使用参数和记分器,在分类器上执行网格搜索。

4.将数据拟合到新的分类器中。

5.绘制模型并找到 f1_score。

6.如果模型不太好,请尝试更改参数的范围并再次拟合。

from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV

clf = DecisionTreeClassifier(random_state=42)

# TODO: Create the parameters list you wish to tune.
parameters = {'max_depth':[2,4,6,8,10],'min_samples_leaf':[2,4,6,8,10], 'min_samples_split':[2,4,6,8,10]}

# TODO: Make an fbeta_score scoring object.
scorer = make_scorer(f1_score)

# TODO: Perform grid search on the classifier using 'scorer' as the scoring method.
grid_obj = GridSearchCV(clf, parameters, scoring=scorer)

# TODO: Fit the grid search object to the training data and find the optimal parameters.
grid_fit = grid_obj.fit(X_train, y_train)

# Get the estimator.
best_clf = grid_fit.best_estimator_

# Fit the new model.
best_clf.fit(X_train, y_train)

# Make predictions using the new model.
best_train_predictions = best_clf.predict(X_train)
best_test_predictions = best_clf.predict(X_test)

# Calculate the f1_score of the new model.
print('The training F1 Score is', f1_score(best_train_predictions, y_train))
print('The testing F1 Score is', f1_score(best_test_predictions, y_test))

# Plot the new model.
plot_model(X, y, best_clf)

# Let's also explore what parameters ended up being used in the new model.
best_clf
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 博客上看到一篇优秀的翻译文章。文章地址:http://blog.csdn.net/heyongluoyao8/ar...
    _CelesteHuang_阅读 10,733评论 0 25
  • 机器学习工程师纳米学位 模型评价与验证 项目 : 预测波士顿房价 第一步. 导入数据 在这个项目中,你将利用马萨诸...
    代号027阅读 9,255评论 0 1
  • 本内容为Udacity课程波士顿房价预测项目,欢迎阅读,有错的地方请留言。仅参考不建议作为其他用途。 优达学城毕业...
    MrMiaow阅读 14,869评论 1 18
  • 1 “一定救救他啊”,慕儿关切地向医生说着。 “乌子,ID9865002,我记住你了” “我们会尽力,请家属签一下...
    十二时辰阅读 2,622评论 0 1
  • 2017年春,我们初中同学群刚刚建立,很多同学都很高兴,纷纷在群中聊微信,有发文字的,有发语音的,有发链接的...
    保卫中华阅读 1,760评论 0 0

友情链接更多精彩内容