👏

Optunaに入門してみた

に公開

今回はOptunaを利用してパラメータ最適化に入門してみたので共有します。

Optunaとは?

OptunaとはPreferred Networksによって開発されているパラメータ最適化ライブラリになります。例えばディープラーニングで様々なハイパーパラメータの組み合わせを効率的に探索して最適モデルを検討するような使い方ができます。

主な特徴としては以下があるようです。

  • Eager search spaces: Pythonの条件文やループ、構文を使用した自動的なハイパーパラメータ探索
  • State-of-the-art algorithm: 大規模な空間を効率的に探索し、見込みのない試行を削減することで迅速結果を得ることが可能
  • Easy parallelization: コード変更なしに複数のスレッドまたはプロセスにわたってハイパーパラメータ検索を並列化することが可能

ハイパーパラメータを最適化したい時に手動でグリッドサーチなどを組み必要がなくなるので、とても重宝されるようなライブラリとなっています。

https://optuna.org/#key_features

実際に使ってみる!

今回は公式で提供されているQuickstartをはじめとして実際に利用してみました。

環境構築

uvを利用して環境を構築します。

uv init optuna_quickstart -p 3.12
cd optuna_quickstart
uv add optuna

関数最適化の実装

それではまずはQuickstartで提供されている関数最適化をさせてみましょう。まずは提供されているサンプルを眺めます。

simple_optimization.py
import optuna

def objective(trial):
    x = trial.suggest_float('x', -10, 10)
    return (x - 2) ** 2

study = optuna.create_study()
study.optimize(objective, n_trials=100)

print(f"{study.best_params=}")

まずはimport optunaでoptunaをインポートした後に、最適化したい関数を定義します。以下のように定義しており、(x-2)**2においてxが[-10, 10]の範囲の中で一番値が小さくなる時の値を求めるというものです(厳密解はx=2になります)。関数最適化の目的関数としてobjective関数を定義しており、以下のように定義しています。x = trial.suggest_float(...)とすることで、[-10, 10]の範囲でxの値を提案させています。

def objective(trial):
    x = trial.suggest_float('x', -10, 10)
    return (x - 2) ** 2

次に探索させるためにcreate_studyを呼び出し、objective関数を与えることで探索を開始します。

study = optuna.create_study()
study.optimize(objective, n_trials=100)

実際にこのコードを実行すると以下のような結果になりました。

uv run simple_optimization.py
最適化結果
[I 2025-08-14 22:00:50,874] A new study created in memory with name: no-name-8fbf2345-5d03-4c04-ba71-57b69d99a08f
[I 2025-08-14 22:00:50,881] Trial 0 finished with value: 2.312576872031178 and parameters: {'x': 3.5207159077326633}. Best is trial 0 with value: 2.312576872031178.
[I 2025-08-14 22:00:50,881] Trial 1 finished with value: 63.50692414705047 and parameters: {'x': -5.969123173037952}. Best is trial 0 with value: 2.312576872031178.
[I 2025-08-14 22:00:50,882] Trial 2 finished with value: 21.6145000851478 and parameters: {'x': 6.649139714522225}. Best is trial 0 with value: 2.312576872031178.
[I 2025-08-14 22:00:50,882] Trial 3 finished with value: 86.50281492925826 and parameters: {'x': -7.300688949172436}. Best is trial 0 with value: 2.312576872031178.
[I 2025-08-14 22:00:50,882] Trial 4 finished with value: 3.7366687953208526 and parameters: {'x': 0.06695349375115844}. Best is trial 0 with value: 2.312576872031178.
[I 2025-08-14 22:00:50,882] Trial 5 finished with value: 1.3453061503342088 and parameters: {'x': 3.159873333745633}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,882] Trial 6 finished with value: 19.305140386385883 and parameters: {'x': -2.3937615304413007}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,882] Trial 7 finished with value: 1.84385875547529 and parameters: {'x': 3.3578876078215347}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,882] Trial 8 finished with value: 15.84042256307917 and parameters: {'x': 5.980002834556675}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,882] Trial 9 finished with value: 8.568238910364599 and parameters: {'x': -0.9271554298268136}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,884] Trial 10 finished with value: 30.42729500268125 and parameters: {'x': 7.516094180004657}. Best is trial 5 with value: 1.3453061503342088.
[I 2025-08-14 22:00:50,885] Trial 11 finished with value: 0.5834419928702536 and parameters: {'x': 2.7638337468783725}. Best is trial 11 with value: 0.5834419928702536.
[I 2025-08-14 22:00:50,887] Trial 12 finished with value: 0.009029049560867416 and parameters: {'x': 2.095021311087921}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,888] Trial 13 finished with value: 31.956811725725743 and parameters: {'x': -3.653035620418975}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,889] Trial 14 finished with value: 0.5394325504554285 and parameters: {'x': 1.265539279160942}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,890] Trial 15 finished with value: 55.60130004999197 and parameters: {'x': 9.45662792755492}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,892] Trial 16 finished with value: 1.780421273531159 and parameters: {'x': 0.6656757239969142}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,893] Trial 17 finished with value: 38.704823374418076 and parameters: {'x': -4.221320066868291}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,894] Trial 18 finished with value: 118.8968905928895 and parameters: {'x': -8.90398507853388}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,896] Trial 19 finished with value: 0.4198641352413811 and parameters: {'x': 1.352030760574716}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,897] Trial 20 finished with value: 10.689615093689309 and parameters: {'x': 5.269497682166071}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,898] Trial 21 finished with value: 6.1893614602272065 and parameters: {'x': -0.48784273221343466}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,899] Trial 22 finished with value: 2.501096580044942 and parameters: {'x': 0.41851443887560524}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,901] Trial 23 finished with value: 0.08742875404459825 and parameters: {'x': 2.2956835369860795}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,902] Trial 24 finished with value: 7.041213297819989 and parameters: {'x': 4.653528461844717}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,903] Trial 25 finished with value: 0.026757722779392364 and parameters: {'x': 2.1635778798596936}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,904] Trial 26 finished with value: 34.980771788956986 and parameters: {'x': 7.914454479405263}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,906] Trial 27 finished with value: 14.256409675718345 and parameters: {'x': -1.7757661044771225}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,907] Trial 28 finished with value: 6.644903849240427 and parameters: {'x': 4.577771101017394}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,908] Trial 29 finished with value: 0.02148170495898396 and parameters: {'x': 2.1465663841369635}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,910] Trial 30 finished with value: 4.794916925822032 and parameters: {'x': 4.18972987508095}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,911] Trial 31 finished with value: 0.09404397945609558 and parameters: {'x': 2.306665908532552}. Best is trial 12 with value: 0.009029049560867416.
[I 2025-08-14 22:00:50,912] Trial 32 finished with value: 0.0002421479968589939 and parameters: {'x': 1.9844388947417289}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,914] Trial 33 finished with value: 0.05780998132068064 and parameters: {'x': 1.7595629368822672}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,915] Trial 34 finished with value: 9.9197124275032 and parameters: {'x': -1.1495574970943456}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,916] Trial 35 finished with value: 4.64417970209952 and parameters: {'x': 4.1550358934596705}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,918] Trial 36 finished with value: 19.329930039045507 and parameters: {'x': -2.3965816311135986}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,919] Trial 37 finished with value: 19.37902249173842 and parameters: {'x': 6.402161116058614}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,920] Trial 38 finished with value: 3.0055291043786143 and parameters: {'x': 0.2663538122273581}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,921] Trial 39 finished with value: 1.353054412268282 and parameters: {'x': 3.1632086709908425}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,923] Trial 40 finished with value: 31.1182822712259 and parameters: {'x': -3.5783763113674842}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,924] Trial 41 finished with value: 0.10881301504044154 and parameters: {'x': 1.6701318217220074}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,925] Trial 42 finished with value: 0.05292103813660811 and parameters: {'x': 1.7699542694666817}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,927] Trial 43 finished with value: 1.1673559691418267 and parameters: {'x': 3.0804424876604153}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,928] Trial 44 finished with value: 0.8556329899983469 and parameters: {'x': 1.0749956810920573}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,929] Trial 45 finished with value: 12.040806893930494 and parameters: {'x': 5.469986584113906}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,931] Trial 46 finished with value: 0.11529912582370865 and parameters: {'x': 2.339557249699824}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,932] Trial 47 finished with value: 8.17470801936226 and parameters: {'x': -0.8591446307177713}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,933] Trial 48 finished with value: 3.0027991987410645 and parameters: {'x': 3.732858678236937}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,935] Trial 49 finished with value: 4.70558224415188 and parameters: {'x': -0.16923540542558002}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,936] Trial 50 finished with value: 2.068578988485147 and parameters: {'x': 3.4382555365737852}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,937] Trial 51 finished with value: 0.09564342306354695 and parameters: {'x': 1.690737291185072}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,938] Trial 52 finished with value: 0.0010800781412165044 and parameters: {'x': 2.0328645423095546}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,940] Trial 53 finished with value: 1.806772789630075 and parameters: {'x': 0.6558375136799588}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,941] Trial 54 finished with value: 0.20026939825126053 and parameters: {'x': 2.4475146905424006}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,942] Trial 55 finished with value: 0.7621241168637104 and parameters: {'x': 1.1270027967606595}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,943] Trial 56 finished with value: 10.264713659981046 and parameters: {'x': 5.20385918229579}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,945] Trial 57 finished with value: 66.1140687544225 and parameters: {'x': -6.13105582039765}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,946] Trial 58 finished with value: 4.132588098201745 and parameters: {'x': -0.032876803498368475}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,948] Trial 59 finished with value: 0.4173745796310476 and parameters: {'x': 2.64604533868069}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,949] Trial 60 finished with value: 2.8800636496900047 and parameters: {'x': 3.6970750277138618}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,950] Trial 61 finished with value: 0.03531920501278924 and parameters: {'x': 1.8120659556844763}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,952] Trial 62 finished with value: 0.08721923076351829 and parameters: {'x': 1.704670978799038}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,953] Trial 63 finished with value: 1.7507225620166327 and parameters: {'x': 0.6768512698805804}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,954] Trial 64 finished with value: 14.262331534067204 and parameters: {'x': -1.7765502160129163}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,956] Trial 65 finished with value: 0.005099686942883397 and parameters: {'x': 2.071412092413564}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,957] Trial 66 finished with value: 1.0752125423682866 and parameters: {'x': 3.03692455963213}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,958] Trial 67 finished with value: 5.819498865360643 and parameters: {'x': 4.412363750631451}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,960] Trial 68 finished with value: 0.040808021372843784 and parameters: {'x': 2.2020099536479423}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,961] Trial 69 finished with value: 1.2594019954562146 and parameters: {'x': 0.8777691879759251}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,962] Trial 70 finished with value: 143.87990425817338 and parameters: {'x': -9.994994966992415}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,964] Trial 71 finished with value: 0.0801098452833413 and parameters: {'x': 2.2830368267263843}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,965] Trial 72 finished with value: 2.6090314867902293 and parameters: {'x': 3.6152496670144307}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,967] Trial 73 finished with value: 6.053427733319593 and parameters: {'x': -0.46037146246650984}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,968] Trial 74 finished with value: 0.00024438501509174286 and parameters: {'x': 1.9843671814732038}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,970] Trial 75 finished with value: 0.40416951895047887 and parameters: {'x': 1.3642567193037123}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,971] Trial 76 finished with value: 0.5681877579182683 and parameters: {'x': 2.7537823014095437}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,972] Trial 77 finished with value: 2.954189586593597 and parameters: {'x': 0.28122439318170533}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,974] Trial 78 finished with value: 7.664250659878693 and parameters: {'x': 4.76843830703859}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,975] Trial 79 finished with value: 4.322670257480812 and parameters: {'x': 4.0791032339643}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,976] Trial 80 finished with value: 0.0004615752145915378 and parameters: {'x': 1.978515698415086}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,978] Trial 81 finished with value: 0.020978011290683968 and parameters: {'x': 1.8551621206635365}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,979] Trial 82 finished with value: 0.7568668683728677 and parameters: {'x': 2.8699809586266056}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,981] Trial 83 finished with value: 0.3945529421226439 and parameters: {'x': 1.371865506342277}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,982] Trial 84 finished with value: 0.07984226354925239 and parameters: {'x': 2.282563733605805}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,983] Trial 85 finished with value: 1.830793815201683 and parameters: {'x': 3.3530682965769625}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,985] Trial 86 finished with value: 1.5315664677956844 and parameters: {'x': 0.7624352672301604}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,986] Trial 87 finished with value: 0.0014204987610343463 and parameters: {'x': 2.037689504653608}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,988] Trial 88 finished with value: 0.5764058422267354 and parameters: {'x': 1.2407860365965762}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,989] Trial 89 finished with value: 9.267336230825894 and parameters: {'x': -1.0442299898046294}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,990] Trial 90 finished with value: 3.4346230596388585 and parameters: {'x': 0.14672639374568908}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,992] Trial 91 finished with value: 0.0008805579923226407 and parameters: {'x': 1.9703258025833446}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,993] Trial 92 finished with value: 0.0015516325352048005 and parameters: {'x': 1.9606092328685414}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,994] Trial 93 finished with value: 0.006887209027399195 and parameters: {'x': 1.9170107896928812}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,996] Trial 94 finished with value: 0.4681164599353647 and parameters: {'x': 2.684190368198329}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,997] Trial 95 finished with value: 5.699551405807817 and parameters: {'x': -0.3873733276988367}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:50,998] Trial 96 finished with value: 1.2008154563306712 and parameters: {'x': 3.095817254988564}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:51,000] Trial 97 finished with value: 0.38867514334413295 and parameters: {'x': 1.3765618367920256}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:51,001] Trial 98 finished with value: 4.169366656879209 and parameters: {'x': 4.041902705047233}. Best is trial 32 with value: 0.0002421479968589939.
[I 2025-08-14 22:00:51,003] Trial 99 finished with value: 2.6210854769498817 and parameters: {'x': 0.3810233241488987}. Best is trial 32 with value: 0.0002421479968589939.
study.best_params={'x': 1.9844388947417289}

結果を見ると、トライアルごとに異なるxの値が提案されており、提案された全ての値の中で最も最適な値としてx=1.9844388947417289が与えられました。厳密会ではないですが概ね2にちかい値になっていますし、根拠がない中で提案させている状況下でこれだけ近似解が出ました。

scikit-learnで分類モデルを開発する

それでは二つ目にscikit-learnを利用してirisデータセットの分類をさせてみようと思います。ソースコードは以下になります(こちらを参考にしました)。

iris_classification.py
import optuna
import sklearn


def objective(trial):
    classifier_name = trial.suggest_categorical("classifier", ["SVC", "RandomForest"])
    if classifier_name == "SVC":
        svc_c = trial.suggest_float("svc_c", 1e-10, 1e10, log=True)
        classifier_obj = sklearn.svm.SVC(C=svc_c)
    else:
        rf_max_depth = trial.suggest_int("rf_max_depth", 2, 5)
        n_estimators = trial.suggest_int("n_estimators", 3, 10)
        classifier_obj = sklearn.ensemble.RandomForestClassifier(max_depth=rf_max_depth, n_estimators=n_estimators)

    iris = sklearn.datasets.load_iris()
    X_train, X_val, y_train, y_val = sklearn.model_selection.train_test_split(iris.data, iris.target, random_state=0)

    classifier_obj.fit(X_train, y_train)
    y_pred = classifier_obj.predict(X_val)

    accuracy = sklearn.metrics.accuracy_score(y_val, y_pred)

    return accuracy


study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=50)
print(f"{study.best_params=}")

今回の実行では、SVCとRandomForest二つの分類モデルを対象にして、各イテレーションでランダムにモデルを選択肢、そのパラメータもランダムに選ばせています。また、最適化の目的関数としては精度指標(accuracy)採用しました。accuracyは大きいほどいいので、optuna.create_study(direction="maximise")として値が大きくなる方がいいということを指定しました。

こちらのコードを実行すると以下のような結果になりました。

uv run iris_classification.py
最適化結果
[I 2025-08-14 22:19:50,458] A new study created in memory with name: no-name-b98c1aab-14ad-4f28-bdea-797f4d5c3464
[I 2025-08-14 22:19:50,617] Trial 0 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 92052444.28482218}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,714] Trial 1 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 10}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,717] Trial 2 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 2, 'n_estimators': 5}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,721] Trial 3 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 7}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,722] Trial 4 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 514448316.7985294}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,725] Trial 5 finished with value: 0.9210526315789473 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 2, 'n_estimators': 5}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,726] Trial 6 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 21072.865617179417}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,730] Trial 7 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 6}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,732] Trial 8 finished with value: 0.23684210526315788 and parameters: {'classifier': 'SVC', 'svc_c': 0.00029175954233941805}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,733] Trial 9 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 2720365.0018320535}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,736] Trial 10 finished with value: 0.23684210526315788 and parameters: {'classifier': 'SVC', 'svc_c': 7.464820641338084e-08}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,744] Trial 11 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 5, 'n_estimators': 10}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,747] Trial 12 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 2.032882791654662}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,755] Trial 13 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 5, 'n_estimators': 10}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,763] Trial 14 finished with value: 0.8947368421052632 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 3, 'n_estimators': 8}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,765] Trial 15 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 88.60991138627037}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,768] Trial 16 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 4626380614.480345}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,773] Trial 17 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 3, 'n_estimators': 3}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,776] Trial 18 finished with value: 0.23684210526315788 and parameters: {'classifier': 'SVC', 'svc_c': 0.00018771782585821906}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,784] Trial 19 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 9}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,791] Trial 20 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 5, 'n_estimators': 8}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,798] Trial 21 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 7}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,804] Trial 22 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 3}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,812] Trial 23 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 3, 'n_estimators': 8}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,819] Trial 24 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 6}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,827] Trial 25 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 3, 'n_estimators': 9}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,829] Trial 26 finished with value: 0.23684210526315788 and parameters: {'classifier': 'SVC', 'svc_c': 1.2607888235279859e-10}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,836] Trial 27 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 5, 'n_estimators': 4}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,838] Trial 28 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 28077.207950983717}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,845] Trial 29 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 7}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,854] Trial 30 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 3, 'n_estimators': 9}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,856] Trial 31 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 147682995.4381497}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,859] Trial 32 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 1747327.1187008733}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,861] Trial 33 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 5995376382.71223}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,864] Trial 34 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 5436.605811619656}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,866] Trial 35 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 24182870.789171927}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,869] Trial 36 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 53.27716256536845}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,871] Trial 37 finished with value: 0.23684210526315788 and parameters: {'classifier': 'SVC', 'svc_c': 0.014054220136388301}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,880] Trial 38 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 5, 'n_estimators': 10}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,882] Trial 39 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 414157284.4619685}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,889] Trial 40 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 5}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,891] Trial 41 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 66027.51775596257}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,894] Trial 42 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 790511.7578451082}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 s22:19:50,896] Trial 43 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 931.1071578619841}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,899] Trial 44 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 32626638.286355812}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,902] Trial 45 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 295501.81799669255}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,904] Trial 46 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 5745846647.934128}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,912] Trial 47 finished with value: 0.9736842105263158 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 2, 'n_estimators': 7}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,915] Trial 48 finished with value: 0.9736842105263158 and parameters: {'classifier': 'SVC', 'svc_c': 50317705.25308862}. Best is trial 0 with value: 0.9736842105263158.
[I 2025-08-14 22:19:50,923] Trial 49 finished with value: 0.9473684210526315 and parameters: {'classifier': 'RandomForest', 'rf_max_depth': 4, 'n_estimators': 9}. Best is trial 0 with value: 0.9736842105263158.
study.best_params={'classifier': 'SVC', 'svc_c': 92052444.28482218}

実験結果から、最高の精度はSVCを利用した時の0.97368程度の精度であったようです。なおこのレベルの簡単なタスクであればいろんな手法で同程度のスコアは出せますが、その場合一番最初に作成されたベストモデルがデフォルトで記録されるようです。

まとめ

今回はOptunaを使ってみました。今まで実際に使ったことはなかったのですが、とても簡単にパラメータ最適化をできるので、今後MLモデルを開発する時は使っていこうと思います。次の紹介記事ではPyTorchとともに利用してみようと思います。

Discussion