Skip to content

Commit 86ba2a4

Browse files
committed
Add Initial Example for Comet.
1 parent 2fea3d4 commit 86ba2a4

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

.github/workflows/comet.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: comet
2+
3+
on:
4+
schedule:
5+
- cron: '0 15 * * *'
6+
pull_request:
7+
paths:
8+
- 'comet/**'
9+
- '.github/workflows/comet.yml'
10+
11+
jobs:
12+
examples:
13+
if: (github.event_name == 'schedule' && github.repository == 'optuna/optuna-examples') || (github.event_name != 'schedule')
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: setup-python${{ matrix.python-version }}
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install (Python)
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install --progress-bar off -U setuptools
29+
pip install git+https://github.com/optuna/optuna.git
30+
python -c 'import optuna'
31+
pip install git+https://github.com/optuna/optuna-integration.git
32+
python -c 'import optuna_integration'
33+
34+
pip install -r comet/requirements.txt
35+
- name: Run examples
36+
run: |
37+
python comet/comet_callback.py
38+
env:
39+
OMP_NUM_THREADS: 1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ In addition, integration modules are available for the following libraries, prov
111111
* [Pruning with Tensorflow Integration Module](./tensorflow/tensorflow_estimator_integration.py)
112112
* [Pruning with XGBoost Integration Module](./xgboost/xgboost_integration.py)
113113
* [Pruning with XGBoost Integration Module (Cross Validation Version)](./xgboost/xgboost_cv_integration.py)
114+
* [Pruning with Comet Integration Module](./comet/comet_callback.py)
114115
</details>
115116

116117
<details open>

comet/comet_callback.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import comet_ml
3+
import optuna
4+
5+
comet_ml.login(project_name="comet-sklearn-example")
6+
7+
import numpy as np
8+
from sklearn.datasets import load_breast_cancer
9+
from sklearn.model_selection import train_test_split
10+
from sklearn.ensemble import RandomForestClassifier
11+
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix
12+
13+
random_state = 42
14+
def evaluate(y_test, y_pred):
15+
return {
16+
"f1": f1_score(y_test, y_pred),
17+
"precision": precision_score(y_test, y_pred),
18+
"recall": recall_score(y_test, y_pred),
19+
}
20+
experiment = comet_ml.start()
21+
cancer = load_breast_cancer()
22+
23+
X_train, X_test, y_train, y_test = train_test_split(
24+
cancer.data, cancer.target, stratify=cancer.target, random_state=random_state
25+
)
26+
27+
clf = RandomForestClassifier()
28+
clf.fit(X_train, y_train)
29+
y_train_pred = clf.predict(X_train)
30+
with experiment.train():
31+
metrics = evaluate(y_train, y_train_pred)
32+
experiment.log_metrics(metrics)
33+
34+
y_test_pred = clf.predict(X_test)
35+
36+
with experiment.test():
37+
metrics = evaluate(y_test, y_test_pred)
38+
experiment.log_metrics(metrics)
39+
40+
experiment.end()

comet/requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)