...

/

An Incremental Strategy for Partitioning

An Incremental Strategy for Partitioning

Learn about the incremental strategy for partitioning along with the abstract and concrete methods in Python.

We'll cover the following...

Overview

We have an alternative to splitting a single list after it’s been built. Instead of extending the list class to provide two sub-lists, we can reframe the problem slightly. Let’s define a subclass of SamplePartition that makes a random choice between testing and training on each SampleDict object that is presented via initialization, or the append() or extend() methods.

Abstract methods

Here’s an abstraction that summarizes our thinking on this. We’ll have three methods for building a list, and two properties that will provide the training and testing sets, as below. We don’t inherit from list because we’re not providing any other list-like features, not even __len__(). The class has only five methods, as shown:

Press + to interact
class DealingPartition(abc.ABC):
@abc.abstractmethod
def __init__(
self,
items,
*,
training_subset: tuple[int, int] = (8, 10),
) -> None:
...
@abc.abstractmethod
def extend(self, items) -> None:
...
@abc.abstractmethod
def append(self, item) -> None:
...
@property
@abc.abstractmethod
def training(self) -> list[TrainingKnownSample]:
...
@property
@abc.abstractmethod
def testing(self) -> list[TestingKnownSample]:
...

This definition has no concrete implementations. It provides five placeholders where methods can be defined to implement the necessary dealing algorithm. We’ve changed the definition of the ...