Datasets Reference
philanthropy.datasets
Synthetic data generators and real reference datasets for donor analytics.
load_ciob_fundraising()
Load the NYC CIOB "Official Fundraising by City Agencies" registry.
A real open-government dataset: every not-for-profit that a New York City
agency reported soliciting donations for, disclosed under the Conflicts of
Interest Board's (CIOB) mandate. It is an affiliation registry, one row
per (year, agency, nonprofit) link, not donor-level giving data.
It carries no gift amounts, donor records, or engagement labels, so it does
not support the RFM / propensity modelling in the rest of this library;
use :func:generate_synthetic_donor_data for that. It is included for
honest, reproducible analysis of agency ↔ nonprofit fundraising
relationships (e.g. most-solicited nonprofits, agency fundraising breadth).
Returns:
| Type | Description |
|---|---|
pandas.DataFrame of shape (2336, 3)
|
Columns:
|
Notes
Source: NYC Open Data, dataset basd-2jwn (Conflicts of Interest Board),
https://data.cityofnewyork.us/City-Government/Official-Fundraising-by-City-Agencies/basd-2jwn.
Distributed under the NYC Open Data Terms of Use (free public use). The CSV
is vendored with the package, so this loader needs no network access.
Source code in philanthropy/datasets/_ciob.py
generate_synthetic_donor_data(n_samples=1000, random_state=None)
Generate a realistic synthetic donor DataFrame for modelling and testing.
The returned dataset simulates a hospital's major-gifts prospect pool. Features are correlated in a domain-meaningful way:
- Donors with more
years_activeand higherevent_attendance_counthave a monotonically increasing probability of being labelled as a major donor (is_major_donor = 1). total_gift_amountis log-normally distributed and positively correlated withis_major_donor.last_gift_dateis sampled uniformly across the past five calendar years, with major donors skewed toward more recent activity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of synthetic donor records to generate. |
1000
|
random_state
|
int or None
|
Seed for the NumPy random-number generator. Pass an integer to
obtain a reproducible dataset; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
pd.DataFrame of shape (n_samples, 5)
|
A DataFrame with the following columns:
|
Examples:
>>> from philanthropy.datasets import generate_synthetic_donor_data
>>> df = generate_synthetic_donor_data(n_samples=500, random_state=42)
>>> df.shape
(500, 5)
>>> df.dtypes["is_major_donor"]
dtype('int64')
>>> bool(df["is_major_donor"].isin([0, 1]).all())
True
Notes
A latent giving capacity drives everything. It is a linear function of
years_active, event_attendance_count and an unobserved wealth term,
on a log-dollar scale. total_gift_amount is then drawn as a noisy
realisation of that capacity, and is_major_donor as a soft threshold on
it at :data:MAJOR_GIFT_CAPACITY. last_gift_date follows engagement.
The ordering matters. Capacity is a confounder that causes both the
giving history and the label, so total_gift_amount is a legitimate
predictor: informative, and limited by how well giving reveals capacity.
Before version 0.7.0 the label was drawn first and total_gift_amount
was drawn conditional on the label. That inverted the domain's causal
arrow, and it was measurable: a model given total_gift_amount beat the
Bayes rate of the generator's own process by roughly 19 ROC-AUC points,
which no model can legitimately do. Using cumulative lifetime giving to
predict "is a major donor" is also the classic fundraising leakage this
library exists to prevent, so the reference dataset was teaching the
anti-pattern. last_gift_date was a second target-derived feature for
the same reason.
The label remains statistically learnable and is not trivially predictable: held out on 4,000 rows, the documented feature set reaches ROC-AUC 0.814 and accuracy 0.759 against a Bayes accuracy ceiling of 0.806 given latent capacity. Sitting below that ceiling is the point.
The function never raises an error for valid inputs. Passing
n_samples=0 returns an empty DataFrame with the correct column
schema.
Source code in philanthropy/datasets/_generator.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
make_donor_dataset(n_donors=200, fiscal_year_start=7, start_year=2018, end_year=2024, lapse_rate=0.25, major_gift_threshold=10000.0, random_state=42)
Generate a synthetic, seeded gift-level donor dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_donors
|
int
|
Number of synthetic donors to generate. |
200
|
fiscal_year_start
|
int
|
Reserved. Accepted for signature stability but not currently used when generating gift dates. |
7
|
start_year
|
int
|
Earliest calendar year used when sampling gift dates. |
2018
|
end_year
|
int
|
Latest calendar year used when sampling gift dates. |
2024
|
lapse_rate
|
float
|
Reserved. Accepted for signature stability but not currently used when generating gift dates. |
0.25
|
major_gift_threshold
|
float
|
Gift amount (inclusive) that marks a row as a major gift. |
10_000.0
|
random_state
|
int or None
|
Seed for the NumPy random-number generator. Pass an integer to
obtain a reproducible dataset; |
42
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
A gift-level DataFrame, not a donor-level DataFrame: each donor
contributes 1-5 gift rows, so
|
Examples:
>>> from philanthropy.datasets import make_donor_dataset
>>> df = make_donor_dataset(n_donors=5, random_state=0)
>>> df.shape
(15, 5)
>>> list(df.columns)
['donor_id', 'gift_date', 'gift_amount', 'appeal_code', 'is_major_gift']
>>> df['donor_id'].nunique()
5
Source code in philanthropy/datasets/_generator.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
fetch_kdd98_donors(*, data_home=None, download_if_missing=True)
Fetch the KDD Cup 1998 direct-mail donor learning set.
A real donor-level dataset: 95,412 individuals who gave at least once
between June 1995 and June 1996, one row per donor, with a full
per-promotion mail and response history (ADATE_2..ADATE_24,
RDATE_2..RDATE_24, RAMNT_2..RAMNT_24) plus the outcome of
the 1997 mailing being predicted (TARGET_B, TARGET_D). That date
history is what makes as-of feature construction testable on it rather
than only on synthetic data; see :func:generate_synthetic_donor_data for
the synthetic equivalent used elsewhere in this library. Most date columns
are encoded YYMM (year, month) rather than as a datetime dtype.
This is a read-only public research dataset, not your data. Nothing
about your own donors, gifts, or environment is ever sent anywhere; the
only network traffic this function makes is fetching the dataset file
itself, once, to a local cache. It is never called automatically: no
other function in this library imports it or calls it during fit or
transform.
Under the dataset's terms of use, teaching or training material that uses it must not name the sponsoring organisation; cite it only as "KDD Cup 1998". This docstring follows that condition, and so should anything you write based on it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_home
|
str
|
Directory to cache the downloaded archive in. Defaults to the
|
None
|
download_if_missing
|
bool
|
If the archive is not already cached, download it. If False and the
archive is not cached, raise |
True
|
Returns:
| Type | Description |
|---|---|
pandas.DataFrame of shape (95412, 481)
|
One row per donor, columns as documented in the data dictionary below. Column dtypes are pandas' own inference over the raw CSV; this function does not recode or impute any of them. |
Raises:
| Type | Description |
|---|---|
OSError
|
If the archive is not cached and |
Notes
Source: the UCI KDD Archive,
https://kdd.ics.uci.edu/databases/kddcup98/kddcup98.html. Field-by-field
documentation: cup98dic.txt at the same location. Distributed for
general research and educational use under the terms stated on that page,
including the sponsor-naming restriction noted above and a request to
notify the dataset's contacts of any published results.
The archive is not vendored with this package (its terms require an
unmodified, individually-fetched copy); this function downloads it to a
local cache on first use, the way sklearn.datasets.fetch_* functions
do, and every later call reads the cached copy.
Source code in philanthropy/datasets/_kdd98.py
make_donor_panel(n_donors=3000, n_years=7, start_fiscal_year=2018, include_encounters=False, random_state=None)
Generate a seeded multi-year donor panel with gift-level rows.
Unlike :func:generate_synthetic_donor_data, which returns one
already-aggregated row per donor, this returns the raw tables a
fundraising shop actually exports: a gift log, a donor table, and
optionally a clinical-encounter table. Aggregating them is left to the
caller, on purpose, because when you aggregate is the whole subject of
:doc:the leakage tutorial </tutorials/avoiding_temporal_data_leakage>.
Fiscal years run 1 July to 30 June and are labelled by the calendar year in which they end, so fiscal year 2019 spans 2018-07-01 to 2019-06-30. A donor gives at most once per fiscal year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_donors
|
int
|
Number of donors in the panel. Every donor appears in |
3000
|
n_years
|
int
|
Number of consecutive fiscal years. Deriving a "gave in the following year" label costs the last one, so the default yields six usable panel years. |
7
|
start_fiscal_year
|
int
|
Label of the first fiscal year. |
2018
|
include_encounters
|
bool
|
Also return an |
False
|
random_state
|
int or None
|
Seed for the NumPy generator. Pass an integer for a reproducible
panel; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
panel |
dict of str to pandas.DataFrame
|
|
Notes
There is no label column, deliberately. A label is a claim about a point in time, and shipping one pre-computed would hand every user the exact mistake this package exists to prevent. Derive it, as of the year you are scoring:
from philanthropy.datasets import make_donor_panel panel = make_donor_panel(n_donors=200, random_state=0) gifts = panel["gifts"] gave = set(zip(gifts["donor_id"], gifts["fiscal_year"])) label = int((7, 2020) in gave) # did donor 7 give in FY2020?
Every number here is invented. The panel establishes mechanisms and shapes,
never magnitudes you should quote for your own program; for magnitudes
measured on a real donor file, see
:doc:/explanation/real_data_replication.
Examples:
>>> from philanthropy.datasets import make_donor_panel
>>> panel = make_donor_panel(n_donors=500, random_state=42)
>>> sorted(panel)
['donors', 'gifts']
>>> list(panel["gifts"].columns)
['donor_id', 'gift_date', 'gift_amount', 'fiscal_year', 'appeal']
>>> int(panel["gifts"]["fiscal_year"].min()), int(panel["gifts"]["fiscal_year"].max())
(2018, 2024)
>>> len(panel["donors"])
500
Straight into RFM features, which the cross-sectional generator cannot do:
>>> from philanthropy.preprocessing import RFMTransformer
>>> rfm = RFMTransformer().fit_transform(panel["gifts"])
>>> list(rfm.columns)
['donor_id', 'recency', 'frequency', 'monetary']
With encounters, for the grateful-patient path:
>>> panel = make_donor_panel(
... n_donors=500, include_encounters=True, random_state=42
... )
>>> list(panel["encounters"].columns)
['donor_id', 'admit_date', 'discharge_date', 'service_line']
Source code in philanthropy/datasets/_panel.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |