如何在Python中以k折叠为单位创建实际的数据框

时间:2019-12-10 13:18:39

标签: python-3.x pandas machine-learning scikit-learn text-classification

在我从sklearn Stratifiednfold返回的索引中,如何从每个折叠创建一个对应的数据框?

    import javafx.application.Application;
    import javafx.geometry.BoundingBox;
    import javafx.geometry.Point2D;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;

    public class DragAndDropApp2 extends Application {

        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {

            Label label = new Label("Drag me!");
            label.setStyle("-fx-background-color: red;");

            StackPane stackPane = new StackPane(label);
            Scene scene = new Scene(stackPane, 400, 400);
            stage.setScene(scene);
            stage.show();

            label.setOnMouseReleased(event -> {
                BoundingBox bounds = new BoundingBox(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight());
                if (!bounds.contains(new Point2D(event.getScreenX(), event.getScreenY()))) {
                    Scene newScene = new Scene(new StackPane(new Label("New window")), 400., 400.);
                    Stage newStage = new Stage();
                    newStage.setScene(newScene);
                    newStage.setX(event.getScreenX());
                    newStage.setY(event.getScreenY());
                    newStage.show();
                }
            });
        }
    }

打印出带有索引的列表。如何将这些映射回我的原始数据框?

我需要它们,因为我想在对它运行texclassification模型之前将增强数据添加到trainingdata中。

1 个答案:

答案 0 :(得分:1)

您可以使用以下索引列表来过滤原始数据框:

df = pd.DataFrame({'foo': ['a', 'b', 'c', 'd', 'e']})
indices = [0, 2, 4]
df = df[df.index.isin(indices)]

输出:

  foo
0   a
2   c
4   e