drawdata

repository·main·Indexed 23 days ago

https://github.com/koaning/drawdata

A Python library providing interactive widgets for drawing datasets directly within notebooks such as Jupyter, marimo, VSCode, and Colab. It includes ScatterWidget and BarWidget to manually create training data for machine learning, with support for exporting data as lists of dictionaries, Pandas DataFrames, Polars DataFrames, or X and y formats compatible with scikit-learn.

Tokens
910
Snippets
4
Records
10
Agent score
32%

What's inside drawdata

  1. Use the ScatterWidget to draw data

    main

    To start drawing a dataset immediately in a notebook, import and instantiate the ScatterWidget. Displaying the widget object in a cell will render the interactive drawing interface.

    from drawdata import ScatterWidget
    
    widget = ScatterWidget()
    widget
  2. Retrieve drawn data as different formats

    main

    Once you have finished drawing data using a widget, you can access the resulting dataset through several properties depending on your preferred data structure:

    • widget.data: Returns the data as a list of dictionaries.
    • widget.data_as_pandas: Returns the data as a Pandas DataFrame.
    • widget.data_as_polars: Returns the data as a Polars DataFrame.
    # Get the drawn data as a list of dictionaries
    widget.data
    
    # Get the drawn data as a dataframe
    widget.data_as_pandas
    widget.data_as_polars
  3. Get data formatted for scikit-learn

    main

    The widget.data_as_X_y property provides data pre-formatted for machine learning tasks.

    • Classification: If you used multiple colors in the widget, X will contain the features and y will contain the class labels.
    • Regression: If you used only one color, X will contain the features and y will contain the values from the y-axis.
    X, y = widget.data_as_X_y
  4. Use BarWidget for bar drawing

    main

    The BarWidget is an interactive widget used to draw data for bar charts. It synchronizes data via the data traitlet.

    Configurable properties:

    • y_min: Minimum value for the y-axis (float).
    • y_max: Maximum value for the y-axis (float).
    • n_bins: Number of bins (int).
    • width: Widget width (int).
    • height: Widget height (int).
    • collection_names: A list of names for the data collections (list of strings).
  5. Convert ScatterWidget data to Pandas or Polars DataFrames

    main

    You can easily convert the raw drawn data from a ScatterWidget into structured dataframes using the following properties:

    • data_as_pandas: Returns a pandas.DataFrame constructed from the drawn data.
    • data_as_polars: Returns a polars.DataFrame constructed from the drawn data.
  6. Access drawn data from ScatterWidget as X and y

    main

    The data_as_X_y property of ScatterWidget extracts the drawn points into a format suitable for machine learning tasks.

    • Regression mode: If all drawn points have the same color (only 1 unique color), it returns X (a 2D array of x coordinates) and y (a 1D array of y coordinates).
    • Classification mode: If multiple colors are present, it returns X (a 2D array of [x, y] coordinates) and colors (a list of the color values assigned to each point).
  7. Use ScatterWidget for scatter drawing

    main

    The ScatterWidget is an interactive widget used to draw data points on a scatter plot. It synchronizes drawn data back to Python via the data traitlet. You can configure the visual properties like brushsize, width, height, and the number of classes (n_classes).

    Constraints:

    • n_classes must be an integer between 1 and 4.