cleverly.SplitPlan#
- class cleverly.SplitPlan(assignments: Sequence[Sequence[int]] | Sequence[ndarray[tuple[Any, ...], dtype[int64]]] | ndarray[tuple[Any, ...], dtype[int64]], source_fingerprint: str | None = None, provenance: tuple[FoldOrigin, ...] | None = None)[source]#
Reusable cross-fitting assignments for every repeat.
A fit accepts a plan only when the plan records how
random_partition()drew each repeat.split_planandfrom_folds()write that record. Before any learner runs,verify()draws each repeat again and refuses labels that differ. A plan built from labels alone stays constructible, and every fit refuses it.- Parameters:
- assignmentssequence
ofsequenceofint,orndarray Fold labels in repeat-major order, one label per row in each repeat. Any nested sequence is accepted, and a two-dimensional integer array is read as one row per repeat. The value is copied into nested tuples, so the plan does not change when the source does.
- source_fingerprint
strorNone, default=None The data fingerprint the labels were realised on, as
data_fingerprintrecords it.split_planfills it in, andvalidate()then refuses data that fingerprint differently.Noneis a plan bound to no data, which is whatunbound()returns.- provenance
tupleofFoldOriginorNone, default=None One generator record per repeat, in repeat order.
Nonefor labels with no record: a hand-built plan or a stratified split. A fit refuses a plan whose provenance isNone.
- assignmentssequence
- Attributes:
See also
cleverly.learners.CrossFitPlanPolicy that generates folds from data.
cleverly.learners.FoldsMutable-array materialization of one repeat.
cleverly.learners.random_partitionThe generator a plan must record.
Examples
>>> from cleverly import SplitPlan >>> from cleverly.learners import random_partition >>> plan = SplitPlan.from_folds([random_partition(6, 3, seed=0)]) >>> plan.assignments ((2, 1, 0, 1, 2, 0),) >>> plan.provenance[0].seed 0 >>> plan.verify(n=6)
Methods#
- classmethod SplitPlan.from_folds(folds: Iterable[Folds], *, source_fingerprint: str | None = None) SplitPlan[source]#
Copy repeat assignments, and their generator records, from realized folds.
- Parameters:
- Returns:
SplitPlanImmutable copies of the assignments.
provenanceholds each repeat’sFolds.originwhen every repeat has one, and isNoneotherwise.
Examples
>>> from cleverly import SplitPlan >>> from cleverly.learners import random_partition >>> plan = SplitPlan.from_folds([random_partition(4, 2, seed=s) for s in (1, 2)]) >>> plan.n_repeats, [origin.seed for origin in plan.provenance] (2, [1, 2])
- SplitPlan.to_folds() tuple[Folds, ...][source]#
Return fresh mutable-array fold objects for every repeat.
Each fold object carries its repeat’s
FoldOriginwhen the plan records one, so a fit on this plan returns a plan with the same record.- Returns:
tupleofFoldsIndependent materializations in repeat order.
- SplitPlan.unbound() SplitPlan[source]#
Return this plan bound to no data, with its generator record kept.
A refit on the same rows with one column replaced changes the data fingerprint and moves no row. The unbound plan serves that refit, and a caller who means to reuse the labels on other rows asks for it by name.
verify()still draws every repeat again, so an unbound plan cannot carry labels the record does not produce.- Returns:
SplitPlanThe same assignments and
provenance, withsource_fingerprintset toNone.
Examples
>>> from cleverly import SplitPlan >>> from cleverly.learners import random_partition >>> bound = SplitPlan.from_folds([random_partition(4, 2, seed=3)], source_fingerprint="ab") >>> free = bound.unbound() >>> free.source_fingerprint is None, free.provenance == bound.provenance (True, True)
- SplitPlan.validate(*, n: int, cluster: ndarray[tuple[Any, ...], dtype[int64]] | None = None, treatment: ndarray[tuple[Any, ...], dtype[float64]] | None = None, stratify: ndarray[tuple[Any, ...], dtype[float64]] | None = None, source_fingerprint: str | None = None) tuple[Folds, ...][source]#
Validate assignments against the data and return fresh folds.
- Parameters:
- n
int Number of data rows.
- cluster
ndarray,optional Cluster code for each row.
- treatment
ndarray,optional Categorical treatment code for each row.
- stratify
ndarray,optional Requested stratification code for each row. Named as in
make_folds(), which balances on the same vector;stratais the survey design rolestratacarries.- source_fingerprint
strorNone, default=None Fingerprint of the data being validated. Compared with
source_fingerprintwhen both are present.
- n
- Returns:
tupleofFoldsFresh fold realizations in repeat order.
- SplitPlan.verify(*, n: int, cluster: ndarray[tuple[Any, ...], dtype[int64]] | None = None) None[source]#
Refuse labels that the recorded generator does not produce on these rows.
Each repeat is drawn again with
random_partition(), from its recorded fold count and seed, and compared label for label. The recorded scheme must be"grouped"when the data declare clusters and"vfold"when they do not. The draw readsnand the cluster labels only, so a plan that passes holds labels the recorded fold count and seed produce.What this rules out is an assignment no draw of this package made: a hand-built one, a stratified one, and one edited after the draw. What it cannot rule out is the declaration itself. The fold count and the seed come from the caller, so a caller who searched for a seed gets a plan this method accepts. A fit records that declaration and has nothing local to audit it against.
result.split_planof an earlier fit is the source this contract is written for.- Parameters:
- Raises:
DataErrorIf the plan has no generator record, if the row count differs, or if a repeat names another generator, the other scheme, or labels its record does not produce. The message names the repeat.
Examples
>>> from cleverly import SplitPlan >>> from cleverly.learners import random_partition >>> SplitPlan.from_folds([random_partition(6, 2, seed=5)]).verify(n=6)
Attributes#
- SplitPlan.fingerprint#
Return the provenance-compatible fingerprint of every repeat.
- SplitPlan.n#
Return the number of rows in each repeat.
- SplitPlan.n_folds#
Return the number of folds in each repeat.
- SplitPlan.n_repeats#
Return the number of repeats.
- SplitPlan.provenance: tuple[FoldOrigin, ...] | None = None#