-
Notifications
You must be signed in to change notification settings - Fork 13
Example sklearn #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kingjr
wants to merge
11
commits into
main
Choose a base branch
from
example_sklearn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b5dd69e
example sklearn
kingjr 53aa337
Update example_sklearn.py
jrapin 79c3897
Merge branch 'main' into example_sklearn
jrapin 7912ba6
Add packages for examples in docs
jrapin 642aeb0
Update .github/workflows/test-type-lint.yaml
jrapin a2cfc58
Merge branch 'test/add-packages-for-docs' into example_sklearn
jrapin c82c084
Merge branch 'test/add-packages-for-docs' into example_sklearn
jrapin cd60efe
Update docs/infra/example_sklearn.py
jrapin c319346
Update docs/infra/example_sklearn.py
jrapin 1777399
Update docs/infra/example_sklearn.py
jrapin febd522
Merge branch 'main' into example_sklearn
jrapin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| A minimalist example with sklearn to show how to develop and explore a model with exca. | ||
| """ | ||
| import typing as tp | ||
| import numpy as np | ||
| import pydantic | ||
| import sys | ||
| import exca | ||
| from sklearn.datasets import make_regression | ||
| from sklearn.model_selection import train_test_split | ||
| from sklearn.linear_model import Ridge | ||
| from sklearn.metrics import mean_squared_error | ||
|
|
||
|
|
||
| class Dataset(pydantic.BaseModel): | ||
| n_samples: int = 100 | ||
| noise: float = 0.1 | ||
| random_state: int = 42 | ||
| test_size: float = 0.2 | ||
| model_config = pydantic.ConfigDict(extra="forbid") | ||
|
|
||
| def get(self) -> tp.Tuple[np.ndarray]: | ||
| # Generate synthetic data | ||
| X, y = make_regression( | ||
| n_samples=self.n_samples, | ||
| noise=self.noise, | ||
| random_state=self.random_state | ||
| ) | ||
| # Split into training and testing datasets | ||
| X_train, X_test, y_train, y_test = train_test_split( | ||
| X, y, | ||
| test_size=self.test_size, | ||
| random_state=self.random_state | ||
| ) | ||
| return X_train, X_test, y_train, y_test | ||
|
|
||
|
|
||
| class Model(pydantic.BaseModel): | ||
| data: Dataset = Dataset() | ||
| alpha: float = 1.0 | ||
| max_iter: int = 1000 | ||
| infra: exca.TaskInfra = exca.TaskInfra(folder='.cache/') | ||
|
|
||
| @infra.apply | ||
| def score(self): | ||
| # Get data | ||
| X_train, X_test, y_train, y_test = self.data.get() | ||
|
|
||
| # Train a Ridge regression model | ||
| print('Fit...') | ||
| model = Ridge(alpha=self.alpha, max_iter=self.max_iter) | ||
| model.fit(X_train, y_train) | ||
|
|
||
| # Evaluate | ||
| print('Score...') | ||
| y_pred = model.predict(X_test) | ||
| mse = mean_squared_error(y_test, y_pred) | ||
| return mse | ||
|
|
||
|
|
||
| def args_to_nested_dict(args: list[str]) -> tp.Dict[str, tp.Any]: | ||
| """ | ||
| Parses a list of Bash-style arguments (e.g., --key=value) into a nested dict. | ||
| """ | ||
| nested_dict = {} | ||
| for arg in args: | ||
| # Split argument into key and value | ||
| key, value = arg.lstrip("--").split("=", 1) | ||
| # Convert flat key into a nested dictionary | ||
| keys = key.split(".") | ||
| current_level = nested_dict | ||
| for k in keys[:-1]: | ||
| current_level = current_level.setdefault(k, {}) | ||
| current_level[keys[-1]] = value | ||
| return nested_dict | ||
|
jrapin marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Validate config | ||
| config = args_to_nested_dict(sys.argv[1:]) | ||
|
jrapin marked this conversation as resolved.
Outdated
|
||
| model = Model(**config) | ||
| print(model.infra.config) | ||
|
|
||
| # Score | ||
| mse = model.score() | ||
| print(mse) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.