我有训练数据,我随机分成两部分:
我使用train_train拟合glm(glmnet)模型,然后使用train_cv进行交叉验证.
我的问题是train_train和train_cv的不同随机分割返回不同的交叉验证结果(使用曲线下面积,"AUC"评估):
AUC = 0.6381583第一次
AUC = 0.6164524第二次
有没有办法运行多个交叉验证,而不重复代码?
我使用 scikit-learn 在 python 中创建了一个自定义模型,我想使用交叉验证。
模型的类定义如下:
class MultiLabelEnsemble:
''' MultiLabelEnsemble(predictorInstance, balance=False)
Like OneVsRestClassifier: Wrapping class to train multiple models when
several objectives are given as target values. Its predictor may be an ensemble.
This class can be used to create a one-vs-rest classifier from multiple 0/1 labels
to treat a multi-label problem or to create a one-vs-rest classifier from
a categorical target variable.
Arguments:
predictorInstance -- A predictor instance is passed as argument (be careful, you must instantiate
the predictor …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用交叉验证来使用 Sklearn 测试我的分类器。
我有 3 个班级,总共 50 个样本。
以下按预期运行,这大概是进行 5 折交叉验证。
result = cross_validation.cross_val_score(classifier, X, y, cv=5)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 cv=50 折叠进行留一法,所以我执行以下操作,
result = cross_validation.cross_val_score(classifier, X, y, cv=50)
Run Code Online (Sandbox Code Playgroud)
然而,令人惊讶的是,它给出了以下错误:
/Library/Python/2.7/site-packages/sklearn/cross_validation.py:413: Warning: The least populated class in y has only 5 members, which is too few. The minimum number of labels for any class cannot be less than n_folds=50.
% (min_labels, self.n_folds)), Warning)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py:55: RuntimeWarning: Mean of empty slice.
warnings.warn("Mean of …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个简单的文本分类器运行一些有监督的实验,但是K Fold在Sklearn. 我得到的错误是:“您的系统已用完应用程序内存”,但我的数据集只有 ~245K 行 x ~81K 列。大,当然,但不是很大。该程序永远不会终止,而是“挂起”,直到我手动关闭终端应用程序。我让它像这样运行了大约 30 分钟,没有任何进展。
我还编写了print语句以查看代码在交叉验证 for 循环中的哪个位置卡住了。看起来好像生成了训练集和测试集的索引,但代码永远不会使用这些索引来分割特征和标签的实际训练和测试集。我在运行 10.9.5 的 Macbook Pro 上运行它。我已经运行此关闭除终端应用程序之外的所有其他应用程序,但没有成功。有没有其他人遇到过这个问题,或者这可能是我的机器特有的问题?
编辑:我已经用 10 倍和 5 倍交叉验证运行了这个,并且每次都遇到相同的问题。
我一直在使用http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.cross_val_score.html
为了交叉验证逻辑回归分类器。我得到的结果是:
[ 0.78571429 0.64285714 0.85714286 0.71428571
0.78571429 0.64285714 0.84615385 0.53846154
0.76923077 0.66666667]
Run Code Online (Sandbox Code Playgroud)
我的主要问题是如何找到哪个集合/折叠使我的分类器得分最大化并产生 0.857。
后续问题:用这组训练我的分类器是一个好习惯吗?
先感谢您。
我的问题与插入符号中提出的问题非常相似 :结合 createResample 和 groupKFold
唯一的区别:我需要在分组后创建分层折叠(也重复 10 次)而不是自举重采样(据我所知没有分层),以便将它与插入符号的 trainControl 一起使用。以下代码使用 10 倍重复的 CV,但我无法包含基于“ID” ( df$ID)的数据分组。
# creating indices
cv.10.folds <- createMultiFolds(rf_label, k = 10, times = 10)
# creating folds
ctrl.10fold <- trainControl(method = "repeatedcv", number = 10, repeats = 10, index = cv.10.folds)
# train
rf.ctrl10 <- train(rf_train, y = rf_label, method = "rf", tuneLength = 6,
ntree = 1000, trControl = ctrl.10fold, importance = TRUE)
Run Code Online (Sandbox Code Playgroud)
这是我的实际问题:我的数据包含许多组,每个组由 20 个实例组成,具有相同的“ID”。因此,当使用 10 倍 CV 重复 10 次时,我在训练中得到了一组实例,在验证集中得到了一些实例。我想避免这种情况,但总的来说,我需要对预测值 ( …
我使用 Caret 包中的随机森林方法对我的数据进行了交叉验证,R 表示最终模型是使用 mtry=34 构建的,这是否意味着在最终随机森林(由交叉验证产生)中只有 34 个参数变量在我的数据集中用于在树中分裂?
> output
Random Forest
375 samples
592 predictors
2 classes: 'alzheimer', 'control'
No pre-processing
Resampling: Cross-Validated (3 fold)
Summary of sample sizes: 250, 250, 250
Resampling results across tuning parameters:
mtry Accuracy Kappa
2 0.6826667 0.3565541
34 0.7600000 0.5194246
591 0.7173333 0.4343563
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 34.
Run Code Online (Sandbox Code Playgroud) 我想用 H2O 构建一个 GBM 模型。我的数据集不平衡,所以我使用 balance_classes 参数。对于网格搜索(参数调整),我想使用 5 折交叉验证。我想知道在这种情况下 H2O 如何处理类平衡。只会重新平衡训练折叠吗?我想确保测试折叠没有重新平衡。
我想知道如何计算sklearn中LogisticRegressionCV的最终模型(即决策边界).所以说我有一些Xdata和ylabels这样的话
Xdata # shape of this is (n_samples,n_features)
ylabels # shape of this is (n_samples,), and it is binary
Run Code Online (Sandbox Code Playgroud)
现在我跑了
from sklearn.linear_model import LogisticRegressionCV
clf = LogisticRegressionCV(Cs=[1.0],cv=5)
clf.fit(Xdata,ylabels)
Run Code Online (Sandbox Code Playgroud)
这只是查看一个正则化参数和CV中的5倍.因此clf.scores_将是一个带有一个键的字典,其值为具有形状的数组(n_folds,1).通过这五个折叠,您可以更好地了解模型的执行方式.
但是,我对你得到的东西很困惑clf.coef_(我假设参数clf.coef_是用的clf.predict).我认为可能有以下几种选择:
clf.coef_来自在所有数据上训练模型clf.coef_来自最佳得分折叠clf.coef_以某种方式在折叠中平均.我想这是一个重复的问题,但对于我的生活,我无法在网上,sklearn文档或LogisticRegressionCV的源代码中找到简单的答案.我找到的一些相关帖子是:
当我cross_validation从sklearn以下导入时:
from sklearn import cross_validation
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'cross_validation' from 'sklearn' (/root/anaconda3/lib/python3.7/site-packages/sklearn/__init__.py)
Run Code Online (Sandbox Code Playgroud) cross-validation ×10
python ×6
scikit-learn ×6
r ×3
r-caret ×2
gbm ×1
h2o ×1
memory ×1
numpy ×1
python-2.7 ×1
typeerror ×1