交叉验证随机分割训练数据到指定数量的折叠。为了防止数据泄漏,相同的数据出现在多个折叠可以使用组。scikit-learn支持组K-fold交叉验证确保折叠是独特的和非重叠。
在火花可以使用spark-sklearn库,它分配优化的scikit-learn模型利用这种方法。这个例子的曲调scikit-learn随机森林模型的组k-fold方法引发的grp变量:
%从sklearn python。整体进口RandomForestClassifier spark_sklearn从sklearn进口GridSearchCV。model_selection进口GroupKFold param_grid = {" max_depth”:[8、12、没有],“max_features”: 1、3、10],“min_samples_split”: 1、3、10],“min_samples_leaf”: 1、3、10],“引导”:真的,假的,“标准”:“基尼”、“熵”,“n_estimators”: [20、40、80]} group_kfold = GroupKFold (n_splits = 3) g = GridSearchCV (sc,估计量= RandomForestClassifier (random_state = 42), param_grid = param_grid简历= group_kfold) gs。适合(X1, y1, grp)