如何使用 GridSearchCV 测试回归交叉验证中的过度拟合?

如何解决如何使用 GridSearchCV 测试回归交叉验证中的过度拟合?

我正在运行一组连续变量和一个连续目标的回归模型。这是我的代码

function sayhello(){
   console.log(Parent.props);     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}

我想了解这个模型是否过度拟合。我读到合并交叉验证是检查这一点的有效方法

您可以看到我已将 cv 合并到上面的代码中。但是,我完全坚持下一步。有人可以向我演示将采用 cv 信息的代码,并生成我打算分析过度拟合的图或一组统计数据吗?我知道在 SO 上有一些这样的问题(例如 herehere),但我不明白其中任何一个如何具体转化为我的情况,因为在这两个例子中,他们只是初始化一个模型并拟合它,而我的结合了 gridsearchcv

解决方法

您当然可以调整控制随机选择的特征数量的超参数,以从引导数据中生长每棵树。通常,您通过 k 折交叉验证来完成此操作;选择最小化测试样本预测误差的调整参数。此外,种植更大的森林会提高预测的准确性,但通常在种植数百棵树后收益会递减。

试试这个示例代码。

from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(random_state = 42)
from pprint import pprint # Look at parameters used by our current forest

print(rf.get_params())

结果:

{'bootstrap': True,'ccp_alpha': 0.0,'criterion': 'mse','max_depth': None,'max_features': 'auto','max_leaf_nodes': None,'max_samples': None,'min_impurity_decrease': 0.0,'min_impurity_split': None,'min_samples_leaf': 1,'min_samples_split': 2,'min_weight_fraction_leaf': 0.0,'n_estimators': 100,'n_jobs': None,'oob_score': False,'random_state': None,'verbose': 0,'warm_start': False}

还有...

import numpy as np
from sklearn.model_selection import RandomizedSearchCV # Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 200,stop = 2000,num = 10)]
# Number of features to consider at every split
max_features = ['auto','sqrt']
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10,110,num = 11)]
max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2,5,10]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1,2,4]
# Method of selecting samples for training each tree
bootstrap = [True,False]# Create the random grid
random_grid = {'n_estimators': n_estimators,'max_features': max_features,'max_depth': max_depth,'min_samples_split': min_samples_split,'min_samples_leaf': min_samples_leaf,'bootstrap': bootstrap}
pprint(random_grid)

结果:

{'bootstrap': [True,False],'max_depth': [10,20,30,40,50,60,70,80,90,100,None],'max_features': ['auto','sqrt'],'min_samples_leaf': [1,4],'min_samples_split': [2,10],'n_estimators': [200,400,600,800,1000,1200,1400,1600,1800,2000]}

查看此链接了解更多信息。

https://towardsdatascience.com/optimizing-hyperparameters-in-random-forest-classification-ec7741f9d3f6

这是一些用于交叉验证的示例代码。

# import random search,random forest,iris data,and distributions
from sklearn.model_selection import cross_validate
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier

# get iris data
iris = datasets.load_iris()
X = iris.data
y = iris.target


model = RandomForestClassifier(random_state=1)
cv = cross_validate(model,X,y,cv=5)
print(cv)
print(cv['test_score'])
print(cv['test_score'].mean())

结果:

{'fit_time': array([0.18350697,0.14461398,0.14261866,0.13116884,0.15478826]),'score_time': array([0.01496148,0.00997281,0.00897574,0.00797844,0.01396227]),'test_score': array([0.96666667,0.96666667,0.93333333,1.        ])}
[0.96666667 0.96666667 0.93333333 0.96666667 1.        ]
0.9666666666666668

交叉验证的内部工作:

Shuffle the dataset in order to remove any kind of order
Split the data into K number of folds. K= 5 or 10 will work for most of the cases
Now keep one fold for testing and remaining all the folds for training
Train(fit) the model on train set and test(evaluate) it on test set and note down the results for that split
Now repeat this process for all the folds,every time choosing separate fold as test data
So for every iteration our model gets trained and tested on different sets of data
At the end sum up the scores from each split and get the mean score

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?