rheon.polars.ModelBank¶
- class rheon.polars.ModelBank(specs: Iterable[dict[str, Any]], *, threads: int | None = None)[source]¶
Bases:
objectA set of models fitted together over the same rows, fed one chunk at a time.
Use a model bank when you feed the rows yourself, from frames you hold or a query you hand it to read in chunks, and when you want to read, save or serve what it has learned between chunks. To run the same bank inside a Polars query, use
lf.rheon.fit_predict(specs), and to run it outside Python, from a file to a file, use therheoncommand line.Parameters
specsA list of the specs the
rheon.specbuilders make, at least one, with distinct names. The bank keeps one model for each spec and each group of the spec’sgroupcolumn, and adds one output column per spec to each chunk it learns.threadsHow many threads the bank’s work runs on.
None, the default, is the process’s shared threads, whose numberRHEON_MAX_THREADSsets when they are first used (unset or0: one thread per core). An integernisnthreads, which every bank given the samenshares and which end with the last of those banks, unless the process’s shared threads already numbern, in which case the bank runs on those. The output and the saved state are the same to the bit at any count (see Notes). The count is not saved with the state, soload()takes its own. A spec’sgram_threadscaps the threads it uses to learn each row into its Gram, andmarginal’sshardssplits its pair work across the bank’s threads. Neither adds a thread.
Raises
ValueErrorWhen:
specsis empty, two specs share a name, or an item is not a spec (the message names the field);a spec’s settings are refused, as its builder would refuse them;
threadsis below 1 (None, not 0, gives the shared threads: one per core unlessRHEON_MAX_THREADSis set), or above 1024 (or the machine’s core count, where that is larger), refused before any thread starts;from the bank’s first call that runs, with
threads=None:RHEON_MAX_THREADSis not a whole number, or is past the same bound.
WrongTypeErrorspecsis one spec, a string or bytes, in place of a list of specs. It is both aTypeErrorand aValueError.TypeErrorspecsis not iterable, orthreadsis not an integer, a bool included.RuntimeErrorFrom any method: the bank is in use on another thread (see Notes).
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = rp.ModelBank([spec]) for chunk in df.iter_slices(100): # chunks in stream order out = bank.fit_predict(chunk) # the chunk, with the output column "ridge" added coefs = bank.coef() # what it has learned, read without the rows bank.save("bank.state") # a state file, to resume from on any OS print(bank)
ModelBank(['ridge'], groups=1, rows_fed=400)
See also
fit_predict(),fit_predict_batches()andfit(), which learn rows.predict(), which predicts rows from the state and learns nothing.coef(),gram(),marginal(),last_row(),summary()anddescribe(), which read what the bank has learned without the rows.lf.rheon.fit_predict, the same bank inside a query.
Notes
A bank feeds each chunk to its models in stream order. Every prediction is made before the model learns the row it is for. Memory holds the models’ state and one chunk, however long the stream, and the numbers do not depend on how the stream is cut into chunks.
The output and the state do not depend on
threads: each piece of work is the same arithmetic whichever thread runs it, and the work is divided by the data, not by the number of threads.A bank is one ordered stream, so two threads cannot use it at once. A method that finds the bank in use on another thread raises
RuntimeError, so that two calls never interleave their rows. Without the check they could, sincefit_predict()releases Python’s global interpreter lock while it works.predict()learns nothing, so any number ofpredictcalls may run at once. Only a call that learns, in flight, refuses them, and they refuse it.
Methods and properties
The specs this bank runs, with every field filled in. |
|
The |
|
The rows fed to the bank so far, over every chunk and group. |
|
|
The groups the bank holds a model for, one row for each spec and group. |
|
Remove the models of these groups, in every spec or in one. |
|
The rows of a frame or query that the bank has not learned. |
|
Learn a frame's rows, and return them with each spec's output. |
|
Predict a frame's rows from the bank as it stands, learning nothing. |
Learn a frame's rows, and return each spec's output as an Arrow array. |
|
|
Predict a frame's rows without learning, and return each spec's output as an Arrow array. |
|
Learn a stream one chunk at a time, and yield each chunk with each spec's output. |
|
Learn every row of a query, a frame or an iterator of frames, and keep only the state. |
|
Return the coefficients of every fit the bank holds, one row per coefficient. |
|
Return each model's output on the last row it did not skip, one row per spec and group. |
|
Return what each model has been fed, one row per spec and group. |
|
Return statistics of each input column each model was fed, one row per column. |
|
Return the Gram behind a spec's fit, per group and instance, as numpy arrays. |
|
Return the feature and target pairs a |
|
Return what an |
|
Return the groups that have closed and not yet been read, oldest first, as one frame. |
|
Return findings about the data each model was fed, one row per problem, the worst first. |
|
Return how many of each model's solves needed help, one row per spec and group. |
Each spec's name, mapped to the names of its output column's fields, in order. |
|
|
Write the bank's state to a file that |
The bytes |
|
|
Write the bank's state as JSON, which can be read without this library. |
|
Load a bank from a state file |
|
Load a bank from the bytes |