前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用实例告诉你什么叫“过拟合”与“欠拟合”

用实例告诉你什么叫“过拟合”与“欠拟合”

作者头像
AI研习社
发布2019-11-15 15:35:20
1.5K0
发布2019-11-15 15:35:20
举报
文章被收录于专栏:AI研习社AI研习社

原标题 | Let’s Underfit and Overfit a Machine Learning Model

作 者 | Chris

翻 译 | 软件导刊

审 校 | 鸢尾、唐里

注:敬请点击文末【阅读原文】访问文中相关链接,PC查看体验更佳。

有位同事最近用术语“欠拟合”来指代命名实体识别(NER)模型,该模型缺少应该标记的实体。

我得纠正一下。这实际上并不是欠拟合,但是我明白为何有人会这么想。

那么,对于这个问题而言,什么是不合适的,或者是过度拟合的呢?

让我们训练一些欠缺数据并拟合过度的模型!

我们将从使用sklearn的“ make_classification”功能生成数据集开始。 每个数据点将具有2个要素(因此易于绘制)和一个标签。

代码语言:javascript
复制
from sklearn.datasets import make_classification
# We didn't need to display all params but I like to see defaults
# I've edited some of these
X,y = make_classification(
    n_samples=30, 
    n_features=2, 
    n_informative=2,
    n_redundant=0,
    n_repeated=0, 
    n_classes=2, 
    n_clusters_per_class=2, 
    weights=None, 
    flip_y=0.01, 
    class_sep=1.0, 
    hypercube=True, 
    shift=0.0, 
    scale=1.0, 
    shuffle=True, 
    random_state=None
)
# Split examples by class (positive/negative) to give diff colors
pos_feat0 = []
pos_feat1 = []
neg_feat0 = []
neg_feat1 = []
for idx,klass in enumerate(y):
    if klass == 1:
        pos_feat0.append(X[idx][0])
        pos_feat1.append(X[idx][1])
    else:
        neg_feat0.append(X[idx][0])
        neg_feat1.append(X[idx][1])
        
# And plot them
import matplotlib.pyplot as plt
plt.scatter(pos_feat0,pos_feat1, c='blue')
plt.scatter(neg_feat0,neg_feat1, c='red'))

大功告成。我们得到数据了。

现在,我们将介绍欠拟合和过拟合的定义,然后有目的地选择将数据欠拟合和过拟合的算法。

欠拟合

根据维基百科:

当统计模型无法充分捕获数据的基础结构时,就会发生欠拟合。

翻译:模型在数据中无法找到一个可靠的模式。这并不意味着没有模式。 只是该模型找不到正确的模式。

代码语言:javascript
复制
from sklearn.linear_model import SGDClassifier
model = SGDClassifier()
model.fit(X, y)
# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)
# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)
# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)
# adds the points from our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')
plt.show()

该模型并不擅长绘制决策边界,它无法利用特征来确定示例的类型。

过拟合

根据维基百科释义:

过于紧密或完全对应于特定数据集的分析结果,由此可能无法拟合其它数据或可靠地预测未来的观察结果。

翻译:该模型学习输入的示例,但不能推广到其它示例。

代码语言:javascript
复制
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=4)
model.fit(X, y)
# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)
# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)
# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)
# adds the points in our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')
plt.show()

漂亮。又一个糟糕的模型。它在目标示例的周围绘制了边界,但是其发现的模式毫无意义,很可能在新的示例。

让我们拟合数据、寻找乐趣吧!

代码语言:javascript
复制
odel import LinearRegression,LogisticRegression
model = LogisticRegression()model.fit(X, y)# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)# adds the points in our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')plt.title(''Underfitting')plt.show()'Underfitting')plt.show()
代码语言:javascript
复制
虽然不完美,但比前两种方法好多了。

现在你看到了。欠拟合,过拟合,还有计划拟合。

我们有意选择了一个简单的双特征数据集,因此你可以在图表上看到决策边界。

具有成千上万个特征的真实示例需要采用更数字化的方法来衡量欠拟合和过拟合,这就是下次我要讲到的了~

via https://towardsdatascience.com/lets-underfit-and-overfit-a-machine-learning-model-26e1aebca233

原文链接 https://www.yanxishe.com/TextTranslation/2236?from=wechatpost111403

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI研习社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 欠拟合
  • 过拟合
  • 让我们拟合数据、寻找乐趣吧!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档