Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120280962
optuna_objectives.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 3, 05:47
Size
2 KB
Mime Type
text/x-python
Expires
Sat, Jul 5, 05:47 (1 d, 15 h)
Engine
blob
Format
Raw Data
Handle
27166160
Attached To
R13271 Optical_Trapping_ML
optuna_objectives.py
View Options
# optuna_objectives.py
import
optuna
from
xgboost
import
XGBClassifier
import
numpy
as
np
#objective function for optuna optimization for both gram and bacteria classification
class
ObjectiveXGB
:
def
__init__
(
self
,
X_train
,
X_test
,
y_train
,
y_test
,
X_train_bacteria
,
X_test_bacteria
,
y_train_bacteria
,
y_test_bacteria
):
self
.
X_train
=
X_train
self
.
X_test
=
X_test
self
.
y_train
=
y_train
self
.
y_test
=
y_test
self
.
X_train_bacteria
=
X_train_bacteria
self
.
X_test_bacteria
=
X_test_bacteria
self
.
y_train_bacteria
=
y_train_bacteria
self
.
y_test_bacteria
=
y_test_bacteria
def
objective_xgb_gram
(
self
,
trial
):
param
=
{
'n_estimators'
:
trial
.
suggest_int
(
'n_estimators'
,
100
,
1000
),
'max_depth'
:
trial
.
suggest_int
(
'max_depth'
,
3
,
10
),
'learning_rate'
:
trial
.
suggest_float
(
'learning_rate'
,
0.01
,
0.3
,
log
=
True
),
'subsample'
:
trial
.
suggest_float
(
'subsample'
,
0.5
,
1.0
),
'colsample_bytree'
:
trial
.
suggest_float
(
'colsample_bytree'
,
0.5
,
1.0
),
'gamma'
:
trial
.
suggest_float
(
'gamma'
,
1e-8
,
10.0
,
log
=
True
),
'reg_alpha'
:
trial
.
suggest_float
(
'reg_alpha'
,
1e-8
,
10.0
,
log
=
True
),
'reg_lambda'
:
trial
.
suggest_float
(
'reg_lambda'
,
1e-8
,
10.0
,
log
=
True
),
'min_child_weight'
:
trial
.
suggest_int
(
'min_child_weight'
,
1
,
10
),
}
xgb_gram
=
XGBClassifier
(
**
param
,
use_label_encoder
=
False
,
eval_metric
=
'logloss'
)
xgb_gram
.
fit
(
self
.
X_train
,
self
.
y_train
,
eval_set
=
[(
self
.
X_test
,
self
.
y_test
)],
early_stopping_rounds
=
10
,
verbose
=
False
)
preds
=
xgb_gram
.
predict
(
self
.
X_test
)
accuracy
=
np
.
mean
(
preds
==
self
.
y_test
)
return
accuracy
def
objective_xgb_bacteria
(
self
,
trial
):
param
=
{
'objective'
:
'multi:softmax'
,
'num_class'
:
len
(
np
.
unique
(
self
.
y_train_bacteria
)),
'n_estimators'
:
trial
.
suggest_int
(
'n_estimators'
,
100
,
1000
),
'max_depth'
:
trial
.
suggest_int
(
'max_depth'
,
3
,
10
),
'learning_rate'
:
trial
.
suggest_float
(
'learning_rate'
,
0.01
,
0.3
,
log
=
True
),
'subsample'
:
trial
.
suggest_float
(
'subsample'
,
0.5
,
1.0
),
'colsample_bytree'
:
trial
.
suggest_float
(
'colsample_bytree'
,
0.5
,
1.0
),
'gamma'
:
trial
.
suggest_float
(
'gamma'
,
1e-8
,
10.0
,
log
=
True
),
'reg_alpha'
:
trial
.
suggest_float
(
'reg_alpha'
,
1e-8
,
10.0
,
log
=
True
),
'reg_lambda'
:
trial
.
suggest_float
(
'reg_lambda'
,
1e-8
,
10.0
,
log
=
True
),
'min_child_weight'
:
trial
.
suggest_int
(
'min_child_weight'
,
1
,
10
),
}
xgb_bacteria
=
XGBClassifier
(
**
param
,
use_label_encoder
=
False
,
eval_metric
=
'mlogloss'
)
xgb_bacteria
.
fit
(
self
.
X_train_bacteria
,
self
.
y_train_bacteria
,
eval_set
=
[(
self
.
X_test_bacteria
,
self
.
y_test_bacteria
)],
early_stopping_rounds
=
10
,
verbose
=
False
)
preds
=
xgb_bacteria
.
predict
(
self
.
X_test_bacteria
)
accuracy
=
np
.
mean
(
preds
==
self
.
y_test_bacteria
)
return
accuracy
Event Timeline
Log In to Comment