Skip to content
Log inGet startedHugging FaceGitHub

Tabular prediction

Imputation

impute fills empty cells with their most likely values, learned from the other columns. This fills the 177 missing ages in the Titanic passenger list:

import pandas as pd
from yhatlabs import Client
titanic = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
titanic = titanic.drop(columns=["PassengerId", "Name", "Ticket", "Cabin"])
print("missing ages before:", titanic["Age"].isna().sum())
yh = Client()
filled = yh.impute(titanic, columns=["Age"])
print("missing ages after:", filled["Age"].isna().sum())
print(filled[filled.attrs["filled"]["Age"]][["Pclass", "Sex", "SibSp", "Age"]].head(3).round(1).to_string(index=False))
Output
missing ages before: 177
missing ages after: 0
Pclass Sex SibSp Age
3 male 0 29.4
2 male 0 33.4
3 female 0 20.5

Parameters: Client.impute

  • For each column with gaps, the model learns that column from the other columns on the rows where it is present, and predicts it for the rest: one prediction call per column.
  • Numeric columns with many distinct values are treated as regression; the rest as classification.
  • columns=[...] limits it to some columns. filled.attrs["filled"] marks every filled cell.
  • A column with fewer than 10 observed values, or a single distinct value, is left as it is, with a warning.

For gaps inside a time series, use fill_gaps instead: it fills from the series’ own history.