## Lineage


The record of where every column came from, and what happened to it.


Usage

``` python
Lineage()
```


A recipe can change a data frame a lot: it adds columns (dummy indicators, polynomial terms), removes others (the categorical a dummy step consumed), and changes dtypes in place (an integer column becomes float after normalizing). After a few steps the output no longer looks much like the input, and column names alone do not say why a column exists. A [Lineage](Lineage.md#mlfeatures.Lineage) keeps that history so a fitted recipe can answer questions such as "which step produced `Home_rent`?", "which original columns does it depend on?" and "what dtype did `Age` have before it was normalized?". The same record backs [summary()](Recipe.summary.md#mlfeatures.Recipe.summary) on a fitted recipe and the [has_source](Selectors.md#mlfeatures.Selectors.has_source) and [has_ancestor](Selectors.md#mlfeatures.Selectors.has_ancestor) selectors, which pick columns by where they came from rather than by name.

It is stored as a graph with one node per column (a [ColumnNode](ColumnNode.md#mlfeatures.ColumnNode)). Each produced column points at its **parents**, the columns it was built from, so following those links back through the steps gives its **ancestors**: every column it ultimately descends from. A column that was in the input data has no parents and a `source` of `"original"`; a column added by a step has the step's `id` as its `source`.

Columns come in two kinds:

- **Live** columns are the ones currently in the data frame, in column order. These are what `lineage["name"]`, `"name" in lineage`, `len(lineage)`, `columns` and `dtypes` describe.
- **Removed** columns were dropped by a step. They are kept (see [removed](Lineage.removed.md#mlfeatures.Lineage.removed)) instead of being forgotten, because a live column's history often runs *through* one. A dummy indicator's parent is the categorical it was encoded from; if that categorical were forgotten when the dummy step dropped it, the indicator could no longer be traced to the original data. [parents](Lineage.parents.md#mlfeatures.Lineage.parents) and [ancestors](Lineage.ancestors.md#mlfeatures.Lineage.ancestors) therefore resolve removed columns too, and asking about a column that never existed raises `KeyError`.

The recipe builds the lineage and updates it after each step, so it is read rather than constructed: get it from `FittedRecipe.lineage`.


## Examples

Here a small pipeline imputes missing values, rescales the numerics, and dummy-encodes the categoricals, composed step by step with `.add()`:


``` python
recipe = (
    Recipe(target="Status")
    .add(StepImputeMean(s.numeric_features()))
    .add(StepNormalize(s.numeric_features()))
    .add(StepDummy(s.nominal_features()))
)
fitted = recipe.fit(credit)
lineage = fitted.lineage
lineage.summary()
```


shape: (23, 5)

| name            | dtype   | roles     | source     | sparse |
|-----------------|---------|-----------|------------|--------|
| str             | str     | str       | str        | bool   |
| "Status"        | "str"   | "target"  | "original" | false  |
| "Seniority"     | "float" | "feature" | "original" | false  |
| "Time"          | "float" | "feature" | "original" | false  |
| "Age"           | "float" | "feature" | "original" | false  |
| "Expenses"      | "float" | "feature" | "original" | false  |
| …               | …       | …         | …          | …      |
| "Marital_widow" | "float" | "feature" | "dummy"    | false  |
| "Records_yes"   | "int"   | "feature" | "dummy"    | false  |
| "Job_freelance" | "float" | "feature" | "dummy"    | false  |
| "Job_others"    | "float" | "feature" | "dummy"    | false  |
| "Job_partime"   | "float" | "feature" | "dummy"    | false  |


`Age` stayed in place through the numeric steps, so its `source` is still `"original"`, but `dtype_history` records the integer to float cast that [StepNormalize](StepNormalize.md#mlfeatures.StepNormalize) applied:


``` python
lineage["Age"].source, lineage["Age"].dtype_history
```


    ('original', [Int64, Float64])


`Home` was expanded into indicators and dropped. [columns_from_source](Lineage.columns_from_source.md#mlfeatures.Lineage.columns_from_source) grabs everything a step produced:


``` python
lineage.columns_from_source("dummy")
```


    ['Home_other',
     'Home_owner',
     'Home_parents',
     'Home_priv',
     'Home_rent',
     'Marital_married',
     'Marital_separated',
     'Marital_single',
     'Marital_widow',
     'Records_yes',
     'Job_freelance',
     'Job_others',
     'Job_partime']


[ancestors](Lineage.ancestors.md#mlfeatures.Lineage.ancestors) traces an indicator back through the now-removed `Home` column:


``` python
lineage.ancestors("Home_rent")
```


    {'Home'}
