标签: cross-validation

在Keras“ImageDataGenerator”中,“validation_split”参数是一种K折交叉验证吗?

我正在尝试对 Keras 模型进行 K 折交叉验证(使用 ImageDataGenerator 和 flow_from_directory 用于训练和验证数据),我想知道“ImageDataGenerator”中的参数“validation_split”是否有效

    test_datagen = ImageDataGenerator(
    rescale=1. / 255,
    rotation_range = 180,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    brightness_range = (0.8, 1.2),
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
    vertical_flip = True,
    validation_split = 0.1

)

train_datagen = ImageDataGenerator(
    rotation_range = 180,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    brightness_range = (0.8, 1.2),
    rescale = 1. / 255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
    vertical_flip = True,
    validation_split = 0.1 …
Run Code Online (Sandbox Code Playgroud)

python cross-validation keras

2
推荐指数
1
解决办法
7443
查看次数

如何加快cross_val_score的速度?

我想使用 MNIST 数据集评估 SGDClassifier sklearn.model_selection.cross_val_score。3折我花了大约6分钟。我怎样才能使用完整的系统功率来加速这个过程(我的意思是使用从CPU到显卡等的所有东西)顺便说一句,我监控CPU的使用情况,它只使用了54%的功率。

from sklearn.datasets import fetch_openml
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_val_score

mnist = fetch_openml('mnist_784')
X, y = mnist['data'], mnist['target']
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train)

cross_val_score(sgd_clf, X_train, y_train, cv=3, scoring='accuracy')
Run Code Online (Sandbox Code Playgroud)

python python-3.x scikit-learn cross-validation

2
推荐指数
1
解决办法
3275
查看次数

过采样:SMOTE 参数“比率”

jupyter笔记本出现错误:

init () 收到意外的关键字参数“ratio”

我的代码:

smote = SMOTE(ratio = 'minority', random_state=10)

也尝试使用: smote = SMOTE(ratio = 0.5, random_state=10) 但它给了我同样的错误消息。

怎么解决这个问题呢?谢谢。

machine-learning cross-validation oversampling imblearn imbalanced-data

2
推荐指数
1
解决办法
5293
查看次数

GroupSplitShuffle 和 GroupKFolds 之间的区别

正如标题所说,我想知道sklearnGroupKFoldGroupShuffleSplit.

两者都针对具有组 ID 的数据进行训练-测试分割,因此组不会在分割中分离。我检查了每个函数的一个训练/测试集,它们看起来都做了很好的分层,但如果有人可以确认所有分割都这样做,那就太好了。

我对两者进行了 10 次分割测试:

gss = GroupShuffleSplit(n_splits=10, train_size=0.8, random_state=42)

 

for train_idx, test_idx in gss.split(X,y,groups):

    print("train:", train_idx, "test:", test_idx)

train: [ 1  2  3  4  5 11 12 13 14 15 16 17 19 20] test: [ 0  6  7  8  9 10 18]

train: [ 1  2  3  4  5  6  7  8  9 10 12 13 14 18 19 20] test: [ 0 11 15 16 17]

train: [ 0  1  3 …
Run Code Online (Sandbox Code Playgroud)

python split scikit-learn cross-validation k-fold

2
推荐指数
1
解决办法
2570
查看次数

使用交叉验证来计算特异性

我想使用交叉验证来计算特异性。我找到了计算准确度、f1 分数和精度的代码。但我找不到具体的。例如,f1-score 的代码如下:

cross_val_score(SVC, X, y, scoring="f1", cv = 7)
Run Code Online (Sandbox Code Playgroud)

或者对于精度来说是这样的:

cross_val_score(SVC, X, y, scoring="precision", cv = 7)
Run Code Online (Sandbox Code Playgroud)

machine-learning scikit-learn cross-validation make-scorer

2
推荐指数
1
解决办法
3241
查看次数

libsvm中的Holdout与K折叠交叉验证

我正在使用libsvm进行分类任务.我有10倍交叉验证,其中F1得分为0.80.但是,当我将训练数据集分成两部分时(一部分用于训练,另一部分用于测试,我将其称为保持测试集),F1分数降至0.65.分割比例为.8至.2.

那么,我的问题是,在进行k折交叉验证与保持测试之间是否有任何显着差异?这两种技术中的哪一种会产生一种概括良好的模型?在这两种情况下,我的数据集都会缩放.

machine-learning libsvm cross-validation

1
推荐指数
1
解决办法
2520
查看次数

NaNs突然出现在sklearn KFolds身上

我正在尝试对我的数据集运行交叉验证.数据似乎很干净,但是当我尝试运行它时,我的一些数据被NaN取代.我不知道为什么.有没有人见过这个?

y, X = np.ravel(df_test['labels']), df_test[['variation', 'length', 'tempo']]
X_train, X_test, y_train, y_test = cv.train_test_split(X,y,test_size=.30, random_state=4444)
Run Code Online (Sandbox Code Playgroud)

这是我在KFolds之前看到的X数据: variation length tempo 0 0.005144 1183.148118 135.999178 1 0.002595 720.165442 117.453835 2 0.008146 397.500952 112.347147 3 0.005367 1109.819501 172.265625 4 0.001631 509.931973 135.999178 5 0.001620 560.365714 151.999081 6 0.002513 763.377778 107.666016 7 0.009262 502.083628 99.384014 8 0.000610 500.017052 143.554688 9 0.000733 269.001723 117.453835

我的Y数据看起来像这样: array([ True, False, False, True, True, True, True, False, True, False], dtype=bool)

现在当我尝试做十字架时:

kf = KFold(X_train.shape[0], n_folds=4, shuffle=True)

for train_index, …
Run Code Online (Sandbox Code Playgroud)

python machine-learning scikit-learn cross-validation

1
推荐指数
1
解决办法
1740
查看次数

GridSearchCV.best_score_意味着当得分设置为'准确度'和CV时

我正在尝试找到应用于众所周知的威斯康星癌症数据集(569个样本,31个特征+目标)的乳腺癌样本分类的最佳模型神经网络模型.我正在使用sklearn 0.18.1.到目前为止我还没有使用Normalization.当我解决这个问题时,我会添加它.

# some init code omitted
X_train, X_test, y_train, y_test = train_test_split(X, y)
Run Code Online (Sandbox Code Playgroud)

为GridSearchCV定义params NN params

tuned_params = [{'solver': ['sgd'], 'learning_rate': ['constant'], "learning_rate_init" : [0.001, 0.01, 0.05, 0.1]},
                {"learning_rate_init" : [0.001, 0.01, 0.05, 0.1]}]
Run Code Online (Sandbox Code Playgroud)

CV方法和模型

cv_method = KFold(n_splits=4, shuffle=True)
model = MLPClassifier()
Run Code Online (Sandbox Code Playgroud)

应用网格

grid = GridSearchCV(estimator=model, param_grid=tuned_params, cv=cv_method, scoring='accuracy')
grid.fit(X_train, y_train)
y_pred = grid.predict(X_test)
Run Code Online (Sandbox Code Playgroud)

如果我跑:

print(grid.best_score_)
print(accuracy_score(y_test, y_pred))
Run Code Online (Sandbox Code Playgroud)

结果为0.7464788732390.902097902098

根据文档"best_score_:float,best_estimator得分左侧数据".我认为在运行8种不同配置的那些中获得的最佳准确度是在tuned_pa​​rams中指定的次数,由KFold指定的次数,在左边的数据中由KFold指定.我对吗?

还有一个问题.有没有一种方法可以找到在train_test_split中使用的最佳测试数据大小,默认为0.25?

非常感谢

参考

python pandas scikit-learn cross-validation grid-search

1
推荐指数
1
解决办法
6960
查看次数

如何从交叉验证中产生混淆矩阵?

我是R和机器学习的新手,我正在使用2个类的数据.我正在尝试进行交叉验证,但是当我尝试制作模型的混淆矩阵时,我得到一个错误,即所有参数必须具有相同的长度.我无法理解为什么我输入的内容长度不一样.任何正确方向的帮助将不胜感激.

library(MASS)
xCV = x[sample(nrow(x)),]

folds <- cut(seq(1,nrow(xCV)),breaks=10,labels=FALSE)

for(i in 1:10){

  testIndexes = which(folds==i,arr.ind=TRUE)
  testData = xCV[testIndexes, ]
  trainData = xCV[-testIndexes, ]

}
ldamodel = lda(class ~ ., trainData)
lda.predCV = predict(model)

conf.LDA.CV=table(trainData$class, lda.predCV$class)
print(conf.LDA.CV)
Run Code Online (Sandbox Code Playgroud)

r machine-learning lda cross-validation

1
推荐指数
1
解决办法
2162
查看次数

配合使用带有管道和GridSearch的cross_val_score嵌套的交叉验证

我正在使用scikit,正在尝试调整XGBoost。我尝试使用嵌套的交叉验证,通过管道对训练折叠进行重新缩放(以避免数据泄漏和过度拟合),并与GridSearchCV并行进行参数调整,并与cross_val_score并行获得roc_auc得分。

from imblearn.pipeline import Pipeline 
from sklearn.model_selection import RepeatedKFold 
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier


std_scaling = StandardScaler() 
algo = XGBClassifier()

steps = [('std_scaling', StandardScaler()), ('algo', XGBClassifier())]

pipeline = Pipeline(steps)

parameters = {'algo__min_child_weight': [1, 2],
              'algo__subsample': [0.6, 0.9],
              'algo__max_depth': [4, 6],
              'algo__gamma': [0.1, 0.2],
              'algo__learning_rate': [0.05, 0.5, 0.3]}

cv1 = RepeatedKFold(n_splits=2, n_repeats = 5, random_state = 15)

clf_auc = GridSearchCV(pipeline, cv = cv1, param_grid = parameters, scoring = 'roc_auc', n_jobs=-1, return_train_score=False)

cv1 = RepeatedKFold(n_splits=2, …
Run Code Online (Sandbox Code Playgroud)

pipeline nested scikit-learn cross-validation grid-search

1
推荐指数
1
解决办法
1105
查看次数