Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Snorkel team Snorkel PandasSFApplier Apply

From Leeroopedia
Revision as of 13:51, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Snorkel_team_Snorkel_PandasSFApplier_Apply.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Data_Slicing, Robustness
Last Updated 2026-02-14 20:00 GMT

Overview

Concrete tool for applying slicing functions to a Pandas DataFrame to produce a named slice matrix, provided by the Snorkel library.

Description

PandasSFApplier extends PandasLFApplier with _use_recarray = True, so that the output is a NumPy record array with named columns matching each slicing function. This is a thin wrapper (7 lines) that reuses the full LF applier infrastructure.

Usage

Import this class when you have defined slicing functions and need to generate the slice matrix for slice-aware training or evaluation.

Code Reference

Source Location

  • Repository: snorkel
  • File: snorkel/slicing/apply/core.py
  • Lines: L13-20

Signature

class PandasSFApplier(PandasLFApplier):
    """SF applier for a Pandas DataFrame.
    Inherits PandasLFApplier with _use_recarray = True.
    """

    def apply(
        self,
        df: pd.DataFrame,
        progress_bar: bool = True,
        fault_tolerant: bool = False,
        return_meta: bool = False,
    ) -> np.recarray:
        """
        Apply slicing functions to DataFrame.

        Args:
            df: Input DataFrame.
            progress_bar: Show progress.
        Returns:
            np.recarray with named columns per SF, shape [n_examples, n_sfs].
        """

Import

from snorkel.slicing import PandasSFApplier

I/O Contract

Inputs

Name Type Required Description
sfs List[SlicingFunction] Yes Slicing functions (passed to constructor as lfs)
df pd.DataFrame Yes DataFrame of data points

Outputs

Name Type Description
S np.recarray Slice matrix [n_examples, n_sfs] with named columns; values 0 or 1

Usage Examples

Apply Slicing Functions

import pandas as pd
from snorkel.slicing import PandasSFApplier, slicing_function

@slicing_function()
def sf_short(x):
    return 1 if len(x.text.split()) < 5 else 0

@slicing_function()
def sf_has_link(x):
    return 1 if "http" in x.text else 0

sfs = [sf_short, sf_has_link]
applier = PandasSFApplier(sfs)

df = pd.DataFrame({"text": ["hi", "check http://example.com", "a long message here today"]})
S = applier.apply(df)

print(S.dtype.names)  # ('sf_short', 'sf_has_link')
print(S["sf_short"])   # [1, 0, 0]
print(S["sf_has_link"]) # [0, 1, 0]

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment