L iddlmZddlmZddlZddlZddlZddlZddl m Z ddl m Z ddl mZ d dZ d dZ dd ZGd d e Zy)) annotations)KeysViewN) BasePruner)StudyDirection) TrialStatectjt|jj t }|t jk(rtj|Stj|S)Ndtype) npasarraylistintermediate_valuesvaluesfloatrMAXIMIZEnanmaxnanmin)trial directionrs `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/pruners/_percentile.py(_get_best_intermediate_result_over_stepsrsTZZU66==?@ NFN+++yy  99V cpt|dk(r td|Dcgc] }||jvs|j|"}}t||krtjS|t j k(rd|z }ttjtj|t|Scc}w)NrzNo trials have been completed.dr ) len ValueErrorrmathnanrrrr nanpercentilearray)completed_trialsrstep percentile n_min_trialstrs r/_get_percentile_intermediate_result_over_trialsr&s !9::.>()I^I^A^d# ,.xxN+++:%   HH( 6   s B3B3cl|z |z|z|z}|dk\sJtjfd|d}||kS)Nrc ||kDr|k7r|S|S)N)second_last_stepsr"s rz,_is_first_in_interval_step..>s-=)=!t)AQar) functoolsreduce)r"intermediate_stepsn_warmup_stepsinterval_stepsnearest_lower_pruning_stepr*s` r_is_first_in_interval_stepr44sb ~ "(")+9": & ** *!''a  8 88rcDeZdZdZ ddd ddZd dZy) PercentilePruneraI Pruner to keep the specified percentile of the trials. Prune if the best intermediate value is in the bottom percentile among trials at the same step. Example: .. testcode:: import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import SGDClassifier from sklearn.model_selection import train_test_split import optuna X, y = load_iris(return_X_y=True) X_train, X_valid, y_train, y_valid = train_test_split(X, y) classes = np.unique(y) def objective(trial): alpha = trial.suggest_float("alpha", 0.0, 1.0) clf = SGDClassifier(alpha=alpha) n_train_iter = 100 for step in range(n_train_iter): clf.partial_fit(X_train, y_train, classes=classes) intermediate_value = clf.score(X_valid, y_valid) trial.report(intermediate_value, step) if trial.should_prune(): raise optuna.TrialPruned() return clf.score(X_valid, y_valid) study = optuna.create_study( direction="maximize", pruner=optuna.pruners.PercentilePruner( 25.0, n_startup_trials=5, n_warmup_steps=30, interval_steps=10 ), ) study.optimize(objective, n_trials=20) Args: percentile: Percentile which must be between 0 and 100 inclusive (e.g., When given 25.0, top of 25th percentile trials are kept). n_startup_trials: Pruning is disabled until the given number of trials finish in the same study. n_warmup_steps: Pruning is disabled until the trial exceeds the given number of step. Note that this feature assumes that ``step`` starts at zero. interval_steps: Interval in number of steps between the pruning checks, offset by the warmup steps. If no value has been reported at the time of a pruning check, that particular check will be postponed until a value is reported. Value must be at least 1. n_min_trials: Minimum number of reported trial results at a step to judge whether to prune. If the number of reported intermediate values from all trials at the current step is less than ``n_min_trials``, the trial will not be pruned. This can be used to ensure that a minimum number of trials are run to completion without being pruned. )r$c"d|cxkrdksntd|d|dkrtd|d|dkrtd|d|dkrtd |d|dkrtd |d||_||_||_||_||_y) NgrzCPercentile must be between 0 and 100 inclusive, but got percentile=.rzFNumber of startup trials cannot be negative, but got n_startup_trials=zBNumber of warmup steps cannot be negative, but got n_warmup_steps=r7zBPruning interval steps must be at least 1, but got interval_steps=zFNumber of trials for pruning must be at least 1, but got n_min_trials=)r _percentile_n_startup_trials_n_warmup_steps_interval_steps _n_min_trials)selfr#n_startup_trialsr1r2r$s r__init__zPercentilePruner.__init__sj'C'V:-WXY  a YHXGZZ[\  A UnEVVWX  A UnEVVWX  ! YL?Z[\ &!1--)rc4|jdtjf}t|}|dk(ry||jkry|j }|y|j }||kryt||jj||jsy|j}t||}tj|ryt||||j |j"} tj| ry|t$j&k(r|| kS|| kDS)NF)deepcopystatesrT) get_trialsrCOMPLETErr; last_stepr<r4rkeysr=rrrisnanr&r:r>rr) r?studyrr!n_trialsr"r1rbest_intermediate_resultps rprunezPercentilePruner.prunes ++UJDWDWCY+Z'( q= d,, , <-- . ) %++002NDDXDX OO #KES\#] ::. / ; it/?/?ASAS  ::a= // /+a/ /'!++rN)rr7) r#rr@intr1rPr2rPr$rPreturnNone)rJz'optuna.study.Study'r'optuna.trial.FrozenTrial'rQbool)__name__ __module__ __qualname____doc__rArNr)rrr6r6Fsb?H!" "*"*"*"* "*  "*"* "*H$,rr6)rrSrrrQr) r!z list['optuna.trial.FrozenTrial']rrr"rPr#rr$rPrQr) r"rPr0z KeysView[int]r1rPr2rPrQrT) __future__rcollections.abcrr.rnumpyr optunaoptuna.prunersroptuna.study._study_directionroptuna.trial._staterrr&r4r6r)rrr`s"$  %8* %2@ 6      89 9#09BE9WZ9 9$J,zJ,r