I want to structure my code cleanly for my ML application, there are many moving parts.
The main problem is a binary classification task.
There is raw data ( and I mean really just raw and dirty, freshly collected ) that can be updated with a function or totally re-fetched but this is heavy and only for when a problem occured. All the data is stored in a MongoDB database running locally (for now).
Then there are processing functions to clean the data, remove unwanted information.
After that, thematical feature datasets are computed (they are all related by an id which corresponds to a single initial line).
The training splits the data into training, validation and test set. The training dataset is augmented (some lines are duplicated and slightly modified with a business-related rationale behind).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
There are submodels of different groups : a given group of submodel trains on an associated feature set and within a group there are different types of models (logistic, tree based, etc).
They produce a probability on the training set with cross_val_predict, on the validation set after being fit on training set and on the test set after being fit on the train+validation joined dataset.
For each individual model, I do a randomized hyperparameter search. I need to save the predictions of all models on each dataset (so that I can dig into the training before of any one model if needed). After search comes selection : for each (group, type) of model, I average best models (cut-off point is decided based on number of search iterations).
My main model is a stacking of the submodels to a final tree based model (CatBoost in this case). The data includes initial features and the predictions of the submodels (cross_val_predicted probabilities).
What code architecture do you suggest for this application ?
Assistant
Here’s a high-level package layout and module breakdown that should keep your codebase clean, modular and easy to extend. I’ll follow it up with a few pointers on config, orchestration and tracking.
class FeatureGroup(ABC):
@abstractmethod
def compute(self, df_clean: pd.DataFrame) -> pd.DataFrame: ...
group_a.py / group_b.py: implement .compute() to return id + features…
manager.py:
python
class FeatureManager:
def __init__(self, groups: List[FeatureGroup]):
self.groups = groups
def build(self, df_clean):
df_list = [g.compute(df_clean) for g in self.groups]
return functools.reduce(lambda a,b: a.merge(b, on='id'), df_list)
4. src/my_ml_app/modeling
base.py: wrap sklearn/CatBoost to unify interface
submodels.py: define your logistic, tree, etc., inheriting BaseModel
tuner.py: random-search with cross_val_predict for train, then fit+predict on val/test
persistence.py: save model, and CSVs of predictions per split
stacking.py: once submodels are selected, merge their CV‐predictions + orig features → CatBoost