Underfitting and Overfitting

1---The depth of the tree

In "Measure Your Model Validation", we have learned how to measure model validation and how to calculate it.In this chapter, we're going to make two things clear: Underfitting and Overfitting
In the previous chapter "Improving the Decision Tree", I introduced two types of decision trees, but there are many more types. We only need to know that the most important option determines the depth of the tree.

2---Overfitting

In practice, it is not uncommon for a tree on the top floor to have 10 leaves and a leaf with 10 branches. In order for the model to predict the most accurate house price, we tend to divide the houses in a dataset into many leaves, but at the same time, each leaf corresponds to fewer and fewer houses (after all, every time we make a branch, it is a 2 squared). Yes, you did it,you predicted the most accurate house price  in the training dataset.🤣🤣🤣

This is overfitting, which matches the training data almost perfectly, but it may not be reliable when predicting new data.

3---Underfitting

It can be understood as an invalid model. Just take out the leaf mentioned above👆🏻👆🏻 as the top layer of the decision tree,😂 and the result will be obvious: the prediction results have little to do with most houses, this model does not grasp the important differences and patterns in the training data set, 😅and the results will not be very good wherever the model is placed.

Now that the concepts are clearly distinguished, back to the validity of the model, how to find a balance between overfitting and not fitting?🥲

4---"max_leaf_nodes"

1.We can use a utility function to help compare MAE scores from different values for max_leaf_nodes.

2.We can use a for-loop to compare the accuracy of models built with different values for max_leaf_nodes.

from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecisionTreeRegressor

def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(train_X, train_y)
    preds_val = model.predict(val_X)
    mae = mean_absolute_error(val_y, preds_val)
    return(mae)
# Data Loading Code Runs At This Point
import pandas as pd
    
# Load data
melbourne_file_path = '/Users/mac/Desktop/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path) 
# Filter rows with missing values
filtered_melbourne_data = melbourne_data.dropna(axis=0)
# Choose target and features
y = filtered_melbourne_data.Price
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea', 
                        'YearBuilt', 'Lattitude', 'Longtitude']
X = filtered_melbourne_data[melbourne_features]

from sklearn.model_selection import train_test_split

# split data into training and validation data, for both features and target
train_X, val_X, train_y, val_y = train_test_split(X, y,random_state = 0)
# compare MAE with differing values of max_leaf_nodes
for max_leaf_nodes in [5, 50, 500, 5000]:
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" %(max_leaf_nodes, my_mae))
runcell(0, '/Users/mac/Desktop/untitled6.py')
Max leaf nodes: 5  		 Mean Absolute Error:  347380
Max leaf nodes: 50  		 Mean Absolute Error:  258171
Max leaf nodes: 500  		 Mean Absolute Error:  243495
Max leaf nodes: 5000  		 Mean Absolute Error:  254983

5---Analysis&Conclusion

Of the options listed, 500 is the optimal number of leaves.

Overfitting: Capturing spurious patterns that won't recur in the future, leading to less accurate predictions.

Underfitting: Failing to capture relevant patterns, again leading to less accurate predictions. 

We use validation data, which isn't used in model training, to measure a candidate model's accuracy. This lets us try many candidate models and keep the best one.

  • 53
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 过拟合和欠拟合是机器学习中常见的问题。过拟合指模型在训练集上表现很好,但在测试集上表现较差,即模型过于复杂,过度拟合了训练数据,导致泛化能力不足。欠拟合则指模型在训练集和测试集上表现都较差,即模型过于简单,无法捕捉数据的复杂性和规律。为了解决这些问题,需要对模型进行调整和优化,以达到更好的泛化能力和预测准确性。 ### 回答2: 在机器学习中,过拟合(overfitting)和欠拟合(underfitting)是两个非常重要的概念。通俗地来说,过拟合指的是模型过于“敏感”地拟合数据,把噪声和偶然性也当作规律进行训练,导致在测试集上表现不佳;而欠拟合则表示模型过于简单,没能完全拟合训练集,导致在训练集和测试集(甚至未知的数据)上的预测效果都不理想。下面分别从原因、表现和如何解决这两个问题阐述。 一、原因 (1)过拟合 过拟合出现的原因一般是模型对训练数据过于敏感,太过注重细节,把数据中本应不具有泛化能力的噪声学进去了,导致模型在未知数据上效果大打折扣。具体来说,造成过拟合的因素有: · 训练集样本量不够:如果样本数据较少,模型可能抓不到数据的本质规律,从而把一些随机性当作了规律。 · 模型复杂度过高:如果模型过于复杂,将大量无用特征学进去,很可能导致过拟合问题。一些常见的复杂模型,如决策树、支持向量机、神经网络等。 · 迭代次数太多:若模型训练次数太多,就会导致模型过于关注训练数据,而失去对未知数据的泛化能力。 (2)欠拟合 欠拟合问题一般是因为模型不具备足够的学习能力,不能很好地拟合数据,导致预测效果不佳。从技术角度分析,造成欠拟合的原因有如下几个方面: · 训练集数据量不足:与过拟合相反,训练集数据量太少,可能会使模型难以理解数据中的规律,从而没能很好地学习到特征。 · 模型复杂度不够:如果模型比较简单,很可能没能很好地学习到训练集中的关系,导致欠拟合问题。 · 非线性问题过于简单:在处理非线性问题时,如果模型只是采用线性拟合的方法,就难以拟合训练集。 二、表现 (1)过拟合 过拟合的模型通常在训练集上表现突出,但在验证集及测试集上的表现较差,通常表现为: · 训练集误差和验证集误差之间差异明显,可能是训练误差低至0,但验证集误差依旧很高; · 模型表现过于复杂,对于Case的预测准确度很高,但对于未知数据的预测表现不佳; · 模型在训练数据中产生极大波动,对于训练集中微小的变化都作出反应; (2)欠拟合 欠拟合的模型表现比较显然,可能表现如下: · 训练误差和验证误差各自都很高; · 模型表现过于简单,无法从训练集中学到足够的规律; · 对于Case的预测准确度不高,且对于未知数据的预测表现不佳。 三、如何解决 (1)过拟合 在解决过拟合的问题时,有一些常见的方法,如下: · 交叉验证:通过重复采用数据集中的不同子集,来训练和测试模型,使模型变得更可靠,从而减少过拟合的风险。 · 增加数据量:如前文所述,训练数据集不足是导致过拟合的一个重要原因,因此增加数据量的方式是一种有效减少过拟合的手段。 · 简化模型:通过降低模型复杂度,如减少层数,删除某些特征等,避免把噪声当做规律进行训练,从而提高泛化能力。 (2)欠拟合 在解决欠拟合的问题时,通常采取以下方式: · 重新设计特征:特征工程是机器学习中非常重要的一环,通过重构特征,提高模型的表达能力,能够更好地利用数据的潜在规律。 · 增加数据量:在欠拟合情况下,通常是由于数据量不足导致的,增加数据量做法同过拟合的解决方案。 · 使用更加复杂的模型:如果模型过于简单,以至于无法发现数据的更复杂的规律,那么就需要重新考虑模型的构建,使用更加复杂的模型,如深度神经网络等。 ### 回答3: Overfitting(过拟合)是指模型过于复杂,试图完全匹配训练数据集,导致在新的数据上表现不佳。这种情况下,模型能够记住训练数据集中的每个细节,包括误差和噪声,从而无法推广到新数据。过拟合通常发生在模型太复杂或参数太多的情况下。 在机器学习中,我们使用各种技术来减少过拟合,例如交叉验证、正则化和减少特征数量。这些技术都有助于建立更好的模型并使其更能够适应新数据。 Underfitting(欠拟合)是指模型过于简单,无法很好地拟合训练数据集或新数据。这种情况下,模型的表现能力有限,无法捕获数据中的复杂关系。欠拟合通常发生在模型太简单或参数太少的情况下。 为了解决欠拟合问题,我们可以尝试增加模型的复杂度,增加特征数量或添加更多的隐层。这可以帮助模型更好地捕获数据中的复杂关系。但是,需要注意的是,过度增加模型的复杂度可能会导致过拟合。 因此,我们需要找到一个平衡点,使模型能够在训练数据集和新数据上都表现良好。这需要我们在训练过程中细心观察模型的表现,并使用适当的技术来解决过拟合或欠拟合问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值