Utils Reference
Model persistence (save_model, load_model), the label-count check run before fitting (check_label_floor), and the synthetic dataset helper
used throughout the tutorials (make_donor_dataset).
philanthropy.utils
Generic helpers: model persistence.
save_model(model, path, *, features=None, target=None)
Persist model to path as a PhilanthroPy bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
fitted estimator
|
Any PhilanthroPy or scikit-learn estimator. |
required |
path
|
str or Path
|
Output path ( |
required |
features
|
list of str
|
Ordered feature-column names the model was trained on. |
None
|
target
|
str
|
Name of the target column. |
None
|
Returns:
| Type | Description |
|---|---|
path
|
The |
Source code in philanthropy/utils/_persistence.py
load_model(path)
Load a bundle written by :func:save_model.
Warns (does not raise) when the stored PhilanthroPy or scikit-learn version differs from the running environment, since an estimator un-pickled under a different scikit-learn can silently misbehave.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Bundle path. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The bundle: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is not a PhilanthroPy bundle. |
Source code in philanthropy/utils/_persistence.py
trailing_slope_features(df, donor_ids, cutoff, *, date_col, value_col, agg, ks=(3, 5), prefix, donor_col='donor_id', period_months=12, data_start=None)
Trailing-period OLS slope and relative slope of one base series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Long-format rows (gifts or activities) with |
required |
donor_ids
|
Index
|
The population to return a row for. |
required |
cutoff
|
Timestamp
|
The as-of date bin 0 (the most recent period) ends at. |
required |
date_col
|
str
|
Column names in |
required |
value_col
|
str
|
Column names in |
required |
agg
|
str
|
Aggregation per period bin ( |
required |
ks
|
sequence of int
|
Trailing window lengths, in periods. |
(3, 5)
|
prefix
|
str
|
Output columns are |
required |
period_months
|
int
|
Width of one period, in months. The default is one year; a future caller on a biennial cadence (for example a two-year survey wave) passes 24. |
12
|
data_start
|
Timestamp
|
The earliest date this series can be considered observed from (for
example a frozen training-set minimum). A period whose window ends
before this date is not "observed" -- it lies entirely before the
data begins, so a 0 there is unknown, not measured -- and the slope
over a window with fewer than 2 observed periods is |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Indexed by |
Source code in philanthropy/utils/_momentum.py
check_label_floor(y, task)
Say whether a file has enough labels to fit a model for task.
Counts the rarer of the two classes in y (lapsed vs retained for
lapse, major donors vs everyone else for major gift) and compares it to
LABEL_FLOORS[task]. Below the floor, at least one training draw in
ten scored below the simple rule on held-out data (lapse), or most did
(major gift), so the rule is the safer choice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
array-like of shape (n_samples,)
|
Binary training labels. |
required |
task
|
(lapse, major_gift)
|
Which floor to apply. |
"lapse"
|
Returns:
| Type | Description |
|---|---|
str
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> from philanthropy.utils import check_label_floor
>>> check_label_floor([1] * 150 + [0] * 850, "lapse")
'run'
>>> check_label_floor([1] * 150 + [0] * 850, "major_gift")
'not enough labels'