> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphext.com/llms.txt
> Use this file to discover all available pages before exploring further.

# filter_sample

> Randomly sample the dataset, optionally within groups (can be used to balance a dataset). 

If you request a number of rows greater than the dataframe length, it will return the original dataframe instead.

## Usage

The following examples show how the step can be used in a recipe.

<Accordion title="Examples" icon="code" defaultOpen="true">
  <Tabs>
    <Tab title="Example 1">
      This draws a sample of 12.000 random rows from the original dataset:

      ```stan theme={null}
      filter_sample(ds, {"n_samples": 12000}) -> (ds_sampled)
      ```
    </Tab>

    <Tab title="Example 2">
      In the next example we keep only a random half of the dataset:

      ```stan theme={null}
      filter_sample(ds, {"n_samples": 0.5}) -> (ds_sampled)
      ```
    </Tab>

    <Tab title="Example 3">
      And this draws a sample of 500 rows from each department identified in the original dataset (or the maximum if there are fewer than 500 rows for a particular department):

      ```stan theme={null}
      filter_sample(ds, {"n_samples": 500, "by": "department"}) -> (ds_sampled)
      ```
    </Tab>

    <Tab title="Signature">
      General syntax for using the step in a recipe. Shows the inputs and outputs the step is expected to receive and will produce respectively. For futher details see sections below.

      ```stan theme={null}
      filter_sample(ds_in: dataset, {
          "param": value,
          ...
      }) -> (ds_out: dataset)
      ```
    </Tab>
  </Tabs>
</Accordion>

## Inputs & Outputs

The following are the inputs expected by the step and the outputs it produces. These are generally
columns (`ds.first_name`), datasets (`ds` or `ds[["first_name", "last_name"]]`) or models (referenced
by name e.g. `"churn-clf"`).

<Accordion title="Inputs" icon="right-to-bracket">
  <ParamField path="ds_in" type="dataset" required>
    An input dataset to filter.
  </ParamField>
</Accordion>

<Accordion title="Outputs" icon="right-from-bracket">
  <ParamField path="ds_out" type="dataset" required>
    A new dataset containing a random sample of the original rows.
  </ParamField>
</Accordion>

## Configuration

The following parameters can be used to configure the behaviour of the step by including them in
a json object as the last "input" to the step, i.e. `step(..., {"param": "value", ...}) -> (output)`.

<Accordion title="Parameters" defaultOpen="true" icon="sliders">
  <ParamField path="n_samples" type="[number, integer]" required>
    Number of rows to sample.
    How many random rows to pick from the original dataset (without replacement). If the value is greater than 1,
    it will be interpreted as a *count* of desired rows. If it is smaller than 1, it will be interpreted as a *proportion*
    of the entire dataset.

    <Accordion title="Options">
      <Tabs>
        <Tab title="number">
          <ParamField path="{_}" type="number">
            number.

            Values must be in the following range:

            ```javascript theme={null}
            0 < {_} < 1
            ```
          </ParamField>
        </Tab>

        <Tab title="integer">
          <ParamField path="{_}" type="integer">
            integer.

            Values must be in the following range:

            ```javascript theme={null}
            1 ≤ {_} < inf
            ```
          </ParamField>
        </Tab>
      </Tabs>
    </Accordion>
  </ParamField>

  <ParamField path="by" type="string (ds_in.column)">
    Sample independently in these groups.
    If a column is specified here, the sampling will be applied separately within each group defined by the unique
    values in this column. Combining this with a count of rows to pick (rather than a proportion), allows this step
    to balance the dataset, leading to an (approximately) equal number of rows within each group.
  </ParamField>

  <ParamField path="seed" type="[number, null]">
    A value used to initialize the random number generator, making it deterministic (reproducible).
  </ParamField>
</Accordion>
