Simple and crude understanding and implementation of machine learning linear regression (VIII): underfitting and over-fitting causes and solutions, regularization, dimensional disaster

Linear Regression

learning target

  • Ownership in the implementation process of linear regression
  • Application LinearRegression or SGDRegressor achieve regression prediction
  • We know the assessment criteria and formulas regression algorithm
  • Overfitting know the causes and solutions underfitting
  • We know principle ridge regression and linear regression differences
  • Application Ridge achieve regression prediction
  • Application joblib achieve saving and loading models

2.8 underfitting and overfitting

Here Insert Picture Description

Definition 1

  • Over-fitting: a hypothesis on the training data to obtain a better fit than other assumptions, but can not fit the data very well on the test data set , then consider this assumption appeared over-fitting phenomenon. (Model too complicated)
  • Underfitting: a hypothesis can not get a better fit on the training data, and can not fit the data very well on the test data set , then consider this assumption appeared underfitting phenomenon. (Model is too simple)

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-McSlpvfW-1583245257515) (../ images /% E6% AC% A0% E6% 8B% 9F% E5 % 90% 88% E8% BF% 87% E6% 8B% 9F% E5% 90% 88% E5% 9B% BE% E7% A4% BA.png)]

So what causes complex model? When linear regression model will be training and learning becomes complicated here corresponding data of the previous two relationships linear regression to say, the non-linear relationship, that is, there is something a lot of useless features or characteristics of reality with the target value the relationship is not a simple linear relationship.

2 causes and solutions

  • Underfitting causes and solutions
    • The reason: to learn the characteristics of the data is too small
    • Solution:
      • ** 1) add additional features items ** Sometimes we owe the model appears to fit the time because of insufficient lead feature items, you can add other features items to a good solution. For example, "combination", "generalization", "relevance" three characteristics is an important means to add features, no matter what the scene, you can copy or imitate, always get unexpected results. In addition to the above features, "contextual features", "platform characteristics" and so on, can be used as preference feature to add.
      • 2)添加多项式特征,这个在机器学习算法里面用的很普遍,例如将线性模型通过添加二次项或者三次项使模型泛化能力更强。
  • 过拟合原因以及解决办法
    • 原因:原始特征过多,存在一些嘈杂特征, 模型过于复杂是因为模型尝试去兼顾各个测试数据点
    • 解决办法:
      • 1)重新清洗数据,导致过拟合的一个原因也有可能是数据不纯导致的,如果出现了过拟合就需要我们重新清洗数据。
      • 2)增大数据的训练量,还有一个原因就是我们用于训练的数据量太小导致的,训练数据占总数据的比例过小。
      • 3)正则化
      • 4)减少特征维度,防止维灾难

3 正则化

3.1 什么是正则化

在解决回归过拟合中,我们选择正则化。但是对于其他机器学习算法如分类算法来说也会出现这样的问题,除了一些算法本身作用之外(决策树、神经网络),我们更多的也是去自己做特征选择,包括之前说的删除、合并一些特征

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o0OJu34Z-1583245257516)(../images/%E6%A8%A1%E5%9E%8B%E5%A4%8D%E6%9D%82.png)]

如何解决?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WJcz8HCD-1583245257516)(../images/%E6%AD%A3%E5%88%99%E5%8C%96.png)]

在学习的时候,数据提供的特征有些影响模型复杂度或者这个特征的数据点异常较多,所以算法在学习的时候尽量减少这个特征的影响(甚至删除某个特征的影响),这就是正则化

注:调整时候,算法并不知道某个特征影响,而是去调整参数得出优化的结果

3.2 正则化类别

  • L2正则化
    • 作用:可以使得其中一些W的都很小,都接近于0,削弱某个特征的影响
    • 优点:越小的参数说明模型越简单,越简单的模型则越不容易产生过拟合现象
    • Ridge回归
  • L1正则化
    • 作用:可以使得其中一些W的值直接为0,删除这个特征的影响
    • LASSO回归

4 维灾难【拓展知识】

4.1 什么是维灾难

随着维度的增加,分类器性能逐步上升,到达某点之后,其性能便逐渐下降

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nllj1Etz-1583245257516)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE1.png)]

有一系列的图片,每张图片的内容可能是猫也可能是狗;我们需要构造一个分类器能够对猫、狗自动的分类。首先,要寻找到一些能够描述猫和狗的特征,这样我们的分类算法就可以利用这些特征去识别物体。猫和狗的皮毛颜色可能是一个很好的特征,考虑到红绿蓝构成图像的三基色,因此用图片三基色各自的平均值称得上方便直观。这样就有了一个简单的Fisher分类器:

If  0.5*red + 0.3*green + 0.2*blue > 0.6 : return cat;
    else return dog;

使用颜色特征可能无法得到一个足够准确的分类器,如果是这样的话,我们不妨加入一些诸如图像纹理(图像灰度值在其X、Y方向的导数dx、dy),就有5个特征(Red、Blue、Green、dx、dy)来设计我们的分类器:

也许分类器准确率依然无法达到要求,加入更多的特征,比如颜色、纹理的统计信息等等,如此下去,可能会得到上百个特征。那是不是我们的分类器性能会随着特征数量的增加而逐步提高呢?答案也许有些让人沮丧,事实上,当特征数量达到一定规模后,分类器的性能是在下降的。

随着维度(特征数量)的增加,分类器的性能却下降了

4.2 维数灾难与过拟合

我们假设猫和狗图片的数量是有限的(样本数量总是有限的),假设有10张图片,接下来我们就用这仅有的10张图片来训练我们的分类器。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CMCSiNoL-1583245257517)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE2.png)]

增加一个特征,比如绿色,这样特征维数扩展到了2维:

增加一个特征后,我们依然无法找到一条简单的直线将它们有效分类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CZJfKjqb-1583245257517)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE3.png)]

再增加一个特征,比如蓝色,扩展到3维特征空间:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0ABo77Nr-1583245257517)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE4.png)]

在3维特征空间中,我们很容易找到一个分类平面,能够在训练集上有效的将猫和狗进行分类:

在高维空间中,我们似乎能得到更优的分类器性能。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2onVb1Yq-1583245257518)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE5.png)]

从1维到3维,给我们的感觉是:维数越高,分类性能越优。然而,维数过高将导致一定的问题:在一维特征空间下,我们假设一个维度的宽度为5个单位,这样样本密度为10/5=2;在2维特征空间下,10个样本所分布的空间大小25,这样样本密度为10/25=0.4;在3维特征空间下,10个样本分布的空间大小为125,样本密度就为10/125=0.08.

如果继续增加特征数量,随着维度的增加,样本将变得越来越稀疏,在这种情况下,也更容易找到一个超平面将目标分开。然而,如果我们将高维空间向低维空间投影,高维空间隐藏的问题将会显现出来:

过多的特征导致的过拟合现象:训练集上表现良好,但是对新数据缺乏泛化能力。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tNu5UOH0-1583245257518)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE7.png)]

高维空间训练形成的线性分类器,相当于在低维空间的一个复杂的非线性分类器,这种分类器过多的强调了训练集的准确率甚至于对一些错误/异常的数据也进行了学习,而正确的数据却无法覆盖整个特征空间。为此,这样得到的分类器在对新数据进行预测时将会出现错误。这种现象称之为过拟合,同时也是维灾难的直接体现。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jJMnqE8Z-1583245257519)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE8.png)]

简单的线性分类器在训练数据上的表现不如非线性分类器,但由于线性分类器的学习过程中对噪声没有对非线性分类器敏感,因此对新数据具备更优的泛化能力。换句话说,通过使用更少的特征,避免了维数灾难的发生(也即避免了高维情况下的过拟合)

由于高维而带来的数据稀疏性问题:假设有一个特征,它的取值范围D在0到1之间均匀分布,并且对狗和猫来说其值都是唯一的,我们现在利用这个特征来设计分类器。如果我们的训练数据覆盖了取值范围的20%(e.g 0到0.2),那么所使用的训练数据就占总样本量的20%。上升到二维情况下,覆盖二维特征空间20%的面积,则需要在每个维度上取得45%的取值范围。在三维情况下,要覆盖特征空间20%的体积,则需要在每个维度上取得58%的取值范围…在维度接近一定程度时,要取得同样的训练样本数量,则几乎要在每个维度上取得接近100%的取值范围,或者增加总样本数量,但样本数量也总是有限的。

如果一直增加特征维数,由于样本分布越来越稀疏,如果要避免过拟合的出现,就不得不持续增加样本数量。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lV4XKYrk-1583245257519)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE9.png)]

数据在高维空间的中心比在边缘区域具备更大的稀疏性,数据更倾向于分布在空间的边缘区域:

不属于单位圆的训练样本比搜索空间的中心更接近搜索空间的角点。这些样本很难分类,因为它们的特征值差别很大(例如,单位正方形的对角的样本)。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WGBqrMYq-1583245257520)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE10.png)]

一个有趣的问题是,当我们增加特征空间的维度时,圆(超球面)的体积如何相对于正方形(超立方体)的体积发生变化。尺寸d的单位超立方体的体积总是1 ^ d = 1.尺寸d和半径0.5的内切超球体的体积可以计算为:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qFqmdGux-1583245257520)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE11.png)]

在高维空间中,大多数训练数据驻留在定义特征空间的超立方体的角落中。如前所述,特征空间角落中的实例比围绕超球体质心的实例难以分类。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cGQIVFlN-1583245257521)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE12.png)]

在高维空间中,大多数训练数据驻留在定义特征空间的超立方体的角落中。如前所述,特征空间角落中的实例比围绕超球体质心的实例难以分类:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FtZZqzHS-1583245257521)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE13.png)]

an 8D hypercube which has 2^8 = 256 corners

事实证明,许多事物在高维空间中表现得非常不同。 例如,如果你选择一个单位平方(1×1平方)的随机点,它将只有大约0.4%的机会位于小于0.001的边界(换句话说,随机点将沿任何维度“极端”这是非常不可能的)。 但是在一个10000维单位超立方体(1×1×1立方体,有1万个1)中,这个概率大于99.999999%。 高维超立方体中的大部分点都非常靠近边界。更难区分的是:如果你在一个单位正方形中随机抽取两个点,这两个点之间的距离平均约为0.52。如果在单位三维立方体中选取两个随机点,则平均距离将大致为0.66。但是在一个100万维的超立方体中随机抽取两点呢?那么平均距离将是大约408.25(大约1,000,000 / 6)!

非常违反直觉:当两个点位于相同的单位超立方体内时,两点如何分离?这个事实意味着高维数据集有可能非常稀疏:大多数训练实例可能彼此远离。当然,这也意味着一个新实例可能离任何训练实例都很远,这使得预测的可信度表现得比在低维度数据中要来的差。训练集的维度越多,过度拟合的风险就越大

In theory, the curse of dimensionality of a solution might be to increase the size of the training set of training examples in order to achieve a sufficient density. Unfortunately, in practice, to achieve a given density required number of training examples with the number of dimensions of exponential growth. If only 100 features (much less than MNIST problem), then the training examples in order to make the average of less than 0.1, require more training examples may be observed using an atomic universe, assuming they are evenly distributed in all dimensions.

For the 8-dimensional hypercube, about 98% of the data set 256 at its corners. As a result, when the dimension of the feature space reaches infinity, from the sample point to centroid distance difference between the minimum and maximum and the minimum Euclidean distance to zero than itself only:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bQ2Ed2CQ-1583245257522)(../images/%E7%BB%B4%E7%81%BE%E9%9A%BE14.png)]

Distance measurement measured begins to lose its validity in the high dimensional space, since the distance measurement depends on the classification, the classification and therefore is generally easier in a lower-dimensional space, wherein the object of interest is used to describe features less.

If the theory of an infinite number of training samples available, the dimensions of the curse does not apply, we can simply use an unlimited number of features to get the perfect classification. The smaller the size of the training data, the less you should use the function. If the N training samples 1D feature space is sufficient to cover the size of a unit section, then the N ^ 2 samples required to cover the 2D feature space having the same density, and requires the 3D feature space N ^ 3 samples. In other words, the number of training examples needed as the number of dimensions used exponential growth .

Published 596 original articles · won praise 790 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104642960