1
2

ChatGPTとPythonで学ぶ PyCaret

Last updated at Posted at 2024-03-06

おはこんばんちわ。今日も引き続きChatGPT先生をお迎えして、「ChatGPTとPythonで学ぶ PyCaret」というテーマで雑談したいと思います。それではChatGPT先生、よろしくお願いします。


assistant:

おはようございます、またはこんばんは。今日もよろしくお願いします。PyCaretについて話すのは楽しみですね。では、どこから始めましょうか?


user:

まず、PyCaretで何ができるか、できるだけ詳しく網羅的に教えてください。


assistant:

PyCaretは、Python用のオープンソースの機械学習ライブラリです。簡単なコードで各種の機械学習タスクを高速に実行できます。特に、予測モデリングのよく行われる一連のステップを自動化したり、ワンラインのコードで行ったりできます。このライブラリが優れている点はその使いやすさにあります。

以下に、PyCaretで可能な主な機能を詳しく説明します:

  1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

  2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

  3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

  4. モデル選択とハイパーパラメータチューニング:たくさんの学習器(アルゴリズム)を簡単に試し、それぞれのパフォーマンスを比較できます。さらに、選んだ学習器の最適なパラメータを探すためのハイパーパラメータチューニングもサポートされています。

  5. アンサンブル学習:スタッキングやブレンディングといったアンサンブル学習手法もサポートしており、パフォーマンスを追求するために複数のモデルを組み合わせることが可能です。

  6. モデル解釈:各種のグラフィカルなツール(特徴量の重要度、相関マップ、シャプレイ値など)を使ってモデルの結果を解釈することができます。

  7. デプロイ: 構築したモデルをクラウド情報パネルに簡単にデプロイできます。

以上のように、PyCaretは機械学習の前処理からモデル構築、評価、デプロイまでの一連の工程を効率的に支援します。これらの機能により、PyCaretはデータサイエンスのプロジェクトを高速化し、効率的に行うのに役立ちます。


user:

ありがとうございます。それでは、ステップバイステップでデモンストレーションしていきましょう。まずは、

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

これを実行する具体的なコードを書いてください。


assistant:

まず最初にPyCaretをインストールしましょう。未インストールの場合、以下のコマンドでインストールすることができます。

!pip install pycaret

PyCaretの初期設定では、データセット内の欠損値は自動的に除去され、カテゴリー変数は自動的にエンコーディングされます。さらに、全ての数値特徴量は、モデルのパフォーマンスを向上させるために尺度が揃えられます。

ここでは、PyCaretが提供する内蔵データセット、特にdiamondデータセットの価格を予測するタスクについて見ていきましょう。

# 必要なライブラリのインポート
from pycaret.regression import *

# データセットの読み込みとセッションの初期設定
df = get_data('diamond')
exp_reg = setup(data = df, target = 'Price', session_id=123)

上記のsetup関数は、PyCaretの機械学習実験名の開始を示します。この関数は、データセット内の特徴量の欠損値の除去、カテゴリー変数のエンコーディング、特徴量の尺度変換などを行います。

なお、セッションの設定の詳細については、Prompt messageが表示されて、それぞれのデータの前処理の設定を確認できます。これらの設定はデフォルトで適応されていますが、setup関数の引数でカスタマイズすることも可能です。


user:

ありがとうございます。上記コードをベースに、修正した全コードを書いてください。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 5
      2 from pycaret.regression import *
      4 # データセットの読み込みとセッションの初期設定
----> 5 df = get_data('diamond')
      6 exp_reg = setup(data = df, target = 'Price', session_id=123)

NameError: name 'get_data' is not defined

assistant:

すみません、そのエラーは'get_data'関数がPyCaretの特定のモジュール、例えば'pycaret.datasets'に属しているため発生しています。したがって、'get_data'関数を使用する前に'pycaret.datasets'をインポートする必要があります。以下のように修正します:

# 必要なライブラリのインポート
from pycaret.datasets import get_data
from pycaret.regression import setup

# データセットの読み込みとセッションの初期設定
df = get_data('diamond')
exp_reg = setup(data = df, target = 'Price', session_id=123)

上記のコードはpycaret内蔵の'diamond'データセットを使用します。'get_data'関数はデータセット名を引数に取り、対応するデータセットを読み込みます。読み込んだデータセットと目的変数(この場合は'Price')を'setup'関数に渡して、データの前処理とセッションの設定を行います。


user:

ありがとうございます。エラーなく実行できて、2つの表が表示されました。

Carat Weight Cut Color Clarity Polish Symmetry Report Price
0 1.10 Ideal H SI1 VG EX GIA 5169
1 0.83 Ideal H VS1 ID ID AGSL 3470
2 0.85 Ideal H SI1 EX EX GIA 3183
3 0.91 Ideal E SI1 VG VG GIA 4370
4 0.83 Ideal G SI1 EX EX GIA 3171
  Description Value
0 Session id 123
1 Target Price
2 Target type Regression
3 Original data shape (6000, 8)
4 Transformed data shape (6000, 29)
5 Transformed train set shape (4200, 29)
6 Transformed test set shape (1800, 29)
7 Numeric features 1
8 Categorical features 6
9 Preprocess True
10 Imputation type simple
11 Numeric imputation mean
12 Categorical imputation mode
13 Maximum one-hot encoding 25
14 Encoding method None
15 Fold Generator KFold
16 Fold Number 10
17 CPU Jobs -1
18 Use GPU False
19 Log Experiment False
20 Experiment Name reg-default-name
21 USI 3007

assistant:

それは良かったです。その2つの表は、PyCaretのsetup関数が行ったデータの前処理と分析の情報を提供しています。

最初の表は、元のデータセットと変換後のデータセットの概要を示しています。各フィールドのデータ型、欠損値の有無、特異値の数などを一覧でご覧いただけます。変換後のデータセットは、欠損値を持つフィールドがなく、カテゴリ変数が数値に変換されていることがわかります。

2番目の表は設定セッションの情報を示しており、PyCaretが自動的に適用する一連の前処理ステップの概要を提供しています。特徴エンジニアリング、特徴選択、欠損値の処理、アウトライアの処理などのステップが表示されます。

次に進みましょうか?それとも、これらのテーブルやその他の情報について質問はありますか?


user:

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

の部分は以上で終了という認識で正しいでしょうか?

もしそれで正しいなら、次は

2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

の具体的なコードを書いてください。


assistant:

はい、あなたの認識は正確です。PyCaretのsetup関数はデータの前処理作業を自動的に処理します。

それでは、特徴量エンジニアリングのステップに進みましょう。PyCaretは特徴量の生成や相互作用、基底変換をするためのパラメータをsetup関数の中で設定できます。

ここでは、特徴量の交互作用(相互作用のための新たな特徴量を生成)と多項式特徴(既存の特徴量の多項式を生成)について見ていきます。

具体的なコードは以下の通りです。

exp_reg101 = setup(data = df, target = 'Price', polynomial_features = True, feature_interaction=True,  feature_ratio=True, session_id=123)

このコードでは、polynomial_features=Trueに設定することで多項式の特徴量が作成され、feature_interaction=Trueに設定することで特徴量間のすべての2次の相互作用が導入されます。また、feature_ratio=Trueに設定することで特徴量間の比率が生成されます。

なお、上記の設定は計算量を増加させるため、大規模なデータセットに対してこれらのパラメータを設定する時は注意が必要です。特に、polynomial_featuresパラメータは特徴量の数に対して指数関数的に増加するためです。


user:

ありがとうございます。上記コードをベースに、修正した全コードを書いてください。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 exp_reg101 = setup(data = df, target = 'Price', polynomial_features = True, feature_interaction=True,  feature_ratio=True, session_id=123)

TypeError: setup() got an unexpected keyword argument 'feature_interaction'

assistant:

すみません、私の間違いです。PyCaretのsetup関数はfeature_interactionsパラメータをサポートしますが、feature_interactionではありません。そして、多項式特徴(polynomial_features)と特徴の比率(feature_ratio)も確かにサポートしています。しかし、これらのオプションはかなりの計算量を必要としますので、大きなデータセットでは処理に時間がかかる可能性があります。

以下のコードで設定と実行を行います。

exp_reg101 = setup(data = df, target = 'Price', polynomial_features = True, feature_interactions=True,  feature_ratio=True, session_id=123)

この設定で、特徴量エンジニアリング(多項式特徴、2次の特徴間相互作用、特徴比率)を自動的に行います。 setup 関数は再び情報のまとめを表示しますが、今回は新たに生成された特徴量も含まれています。

どうぞお試しください。なにか問題や疑問があればお知らせください。


user:

すみません、さっきのと違いがわかりません。エラーも同じように見えます。

exp_reg101 = setup(data = df, target = 'Price', polynomial_features = True, feature_interactions=True,  feature_ratio=True, session_id=123)

assistant:

大変申し訳ありません、feature_interactionsfeature_ratioのパラメータはPyCaretのsetup関数で直接設定することはできません。私の誤解でした。

PyCaretは元の特徴量から新しい特徴量を自動的に生成することはできますが、これらはさまざまなパラメータと戦略を組み合わせて行うため、デフォルトでは行われません。たとえば、多項式の特徴量はpolynomial_features=Trueパラメータで生成できます。

また、特徴量の相互作用や比率などの特徴量のエンジニアリングは手作業で行う必要があります。

申し訳ありませんが、私の説明が誤りでした。以下の例で多項式の特徴量の生成を行いましょう。

exp_reg101 = setup(data = df, target = 'Price', polynomial_features = True, session_id=123)

これは、特徴量の一次と二次の多項式を自動的に生成します。


user:

ありがとうございます。今度こそエラーなく実行できて、1つの表が表示されました。

  Description Value
0 Session id 123
1 Target Price
2 Target type Regression
3 Original data shape (6000, 8)
4 Transformed data shape (6000, 435)
5 Transformed train set shape (4200, 435)
6 Transformed test set shape (1800, 435)
7 Numeric features 1
8 Categorical features 6
9 Preprocess True
10 Imputation type simple
11 Numeric imputation mean
12 Categorical imputation mode
13 Maximum one-hot encoding 25
14 Encoding method None
15 Polynomial features True
16 Polynomial degree 2
17 Fold Generator KFold
18 Fold Number 10
19 CPU Jobs -1
20 Use GPU False
21 Log Experiment False
22 Experiment Name reg-default-name
23 USI ede6

assistant:

それは良かったです。表示された表では、このセッションで作成された多項式特徴量などの詳細情報を見ることができます。

上記のsetup関数では、polynomial_features = Trueパラメータを使用して、元の特徴量から新しい多項式特徴量を生成しています。これらの新しい特徴量は、特徴量とターゲット間の非線形関係を捉え、モデルの予測精度を改善する可能性があります。

特徴量エンジニアリングはデータ分析や機械学習モデル構築の重要なステップであり、PyCaretはこのプロセスを容易にするためのいくつかのツールを提供しています。しかし、特徴量間の相互作用やレシオなど、より高度な特徴量エンジニアリングの手法は明示的に指定と調整が必要となります。

次に進む前に、もしご質問があればお気軽にどうぞ。


user:

ありがとうございます。それではひとまず、ここまでのコードの復習です。

# 必要なライブラリのインポート
from pycaret.datasets import get_data
from pycaret.regression import setup

# データセットの読み込みとセッションの初期設定
df = get_data("diamond")

# データの前処理と、特徴量エンジニアリング
exp_reg101 = setup(data=df, target="Price", polynomial_features=True, session_id=123)

ここまでで、

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

までは終了したという認識で正しいでしょうか?

もし正しいなら、次は

3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

の具体的なコードを書いてください。


assistant:

はい、あなたの理解は完全に正確です。前処理と特徴量エンジニアリングのステップが完了しました。

それでは、特徴量選択について進んでいきましょう。PyCaretは、自動的に特徴量選択を行うためのメソッドも提供しています。

setup関数にはremove_multicollinearityパラメータが存在し、これをTrueに設定することで、多重共線性(特徴間の強い相関)を削減するための特徴量の削減が可能となります。具体的なコードは以下となります。

exp_reg102 = setup(data = df, target = 'Price', polynomial_features = True, remove_multicollinearity = True, multicollinearity_threshold = 0.9, session_id=123)

今回は、remove_multicollinearity = Trueとすることで多重共線性のある特徴量を除去しています。また、multicollinearity_threshold = 0.9とすることで、相関係数が0.9以上の特徴量を除去しています。

ただし、特徴量を除去するとモデルの性能に影響を与える可能性があるため、パラメータの設定には注意が必要です。


user:

ありがとうございます。エラーなく実行できて、1つの表が表示されました。

  Description Value
0 Session id 123
1 Target Price
2 Target type Regression
3 Original data shape (6000, 8)
4 Transformed data shape (6000, 341)
5 Transformed train set shape (4200, 341)
6 Transformed test set shape (1800, 341)
7 Numeric features 1
8 Categorical features 6
9 Preprocess True
10 Imputation type simple
11 Numeric imputation mean
12 Categorical imputation mode
13 Maximum one-hot encoding 25
14 Encoding method None
15 Polynomial features True
16 Polynomial degree 2
17 Remove multicollinearity True
18 Multicollinearity threshold 0.900000
19 Fold Generator KFold
20 Fold Number 10
21 CPU Jobs -1
22 Use GPU False
23 Log Experiment False
24 Experiment Name reg-default-name
25 USI 9b6d

assistant:

それは素晴らしいです!

その表はsetup関数の実行結果をまとめて表示しています。今回の場合、多重共線性のある特徴量を削除する設定(remove_multicollinearity = True)を行ったので、その結果込みで情報が表示されているはずです。

多重共線性のある特徴量とは相互に強い相関のある特徴量のことを指し、これらは相互依存性を引き起こし、モデルの結果を不安定にする可能性があります。そのため、多重共線性のある特徴量を予め除外できるのは非常に便利です。

次にどのステップを進めますか?それとも、この段階で何か質問はありますか?


user:

ここまでで、

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

までは終了したという認識で正しいでしょうか?

もし正しいなら、次は

4. モデル選択とハイパーパラメータチューニング:たくさんの学習器(アルゴリズム)を簡単に試し、それぞれのパフォーマンスを比較できます。さらに、選んだ学習器の最適なパラメータを探すためのハイパーパラメータチューニングもサポートされています。

の具体的なコードを書いてください。


assistant:

はい、あなたの理解は完全に正確です。データの前処理、特徴量エンジニアリング、特徴量選択のステップが完了しました。

それでは次に、モデル選択とハイパーパラメータのチューニングについて進めてまいりましょう。

まずは、どのアルゴリズムがこの特定のデータセットに最も適しているかを見つけるために、いくつかの異なるアルゴリズムを比較します。以下のコードでは、PyCaretのデフォルトの学習器のリストを使用しています。

from pycaret.regression import compare_models

# モデルの比較
best_model = compare_models()
  Model MAE MSE RMSE R2 RMSLE MAPE TT (Sec)
gbr Gradient Boosting Regressor 806.1529 2131760.7492 1439.4524 0.9797 0.0932 0.0719 0.5850
et Extra Trees Regressor 730.9932 2455423.3020 1512.2386 0.9773 0.0796 0.0585 1.3080
rf Random Forest Regressor 743.4197 2616700.2359 1571.5210 0.9756 0.0815 0.0588 1.1090
lightgbm Light Gradient Boosting Machine 743.9563 2976173.6317 1653.0651 0.9727 0.0802 0.0576 0.8250
ridge Ridge Regression 1049.9623 3044281.2954 1735.4017 0.9706 0.1643 0.1049 0.3460
br Bayesian Ridge 1054.7660 3058279.1431 1739.8587 0.9704 0.1660 0.1058 0.3570
llar Lasso Least Angle Regression 1043.6765 3117559.6595 1754.8621 0.9700 0.1636 0.1030 0.3510
lasso Lasso Regression 1043.5657 3116900.3214 1754.6715 0.9700 0.1637 0.1030 0.4120
huber Huber Regressor 964.1827 3695366.1467 1889.1860 0.9651 0.1154 0.0816 0.4970
omp Orthogonal Matching Pursuit 1174.5834 3731480.5164 1914.9481 0.9642 0.1798 0.1231 0.3440
dt Decision Tree Regressor 953.3839 3777925.4246 1919.8410 0.9641 0.1054 0.0758 0.3710
par Passive Aggressive Regressor 979.3975 5011567.2323 2193.0275 0.9531 0.1054 0.0770 0.5260
en Elastic Net 1992.0482 17040983.4896 4071.7231 0.8391 0.2090 0.1781 0.3350
ada AdaBoost Regressor 3556.0908 18887982.1593 4341.0229 0.8151 0.4436 0.4832 0.6150
knn K Neighbors Regressor 2344.5751 19444335.0872 4372.6712 0.8140 0.2873 0.2008 0.3460
dummy Dummy Regressor 7359.1861 103457199.1884 10147.6696 -0.0014 0.7592 0.8943 0.3290
lr Linear Regression 1793303343840.9614 3295042394615579171004350464.0000 31917407603376.1680 -29648943270717980672.0000 0.9382 264022971.9004 0.5330
lar Least Angle Regression 2541615562811197001284753187481321472.0000 17436081430465764076102845399704820999362623751943140372068894871389669949440.0000 52085205977413771674102613152798605312.0000 -133303592218696783519916596345752305159523816191473789881077628665856.0000 51.6687 37689891608461168747219882344448.0000 0.3500

compare_models()関数は、全てのモデルを訓練し、指定された評価指標(デフォルトではR2スコア)でモデルを評価します。最終的には最良のモデル(この場合は最も高いR2スコアを持つモデル)を返します。

次に、ハイパーパラメータのチューニングに進みます。以下のコードは、自動的にハイパーパラメータのチューニングを行い、最良のモデルを返します。

from pycaret.regression import tune_model

# ハイパーパラメータのチューニング
tuned_model = tune_model(best_model)
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 685.2108 1854325.6961 1361.7363 0.9833 0.0757 0.0551
1 652.2107 1403835.0277 1184.8354 0.9830 0.0709 0.0540
2 700.5663 2485452.7027 1576.5319 0.9778 0.0761 0.0530
3 772.1582 2368131.6733 1538.8735 0.9789 0.0781 0.0554
4 640.7234 1479360.5636 1216.2897 0.9855 0.0698 0.0510
5 651.4593 1459825.9317 1208.2326 0.9832 0.0795 0.0558
6 772.5056 4443204.6851 2107.8911 0.9666 0.0804 0.0576
7 677.2541 1779809.7596 1334.0951 0.9824 0.0756 0.0525
8 649.0868 1333866.8874 1154.9316 0.9871 0.0729 0.0558
9 600.9084 1238296.3456 1112.7876 0.9861 0.0637 0.0493
Mean 680.2084 1984610.9273 1379.6205 0.9814 0.0743 0.0540
Std 52.7714 913781.3086 285.0584 0.0057 0.0048 0.0024
Fitting 10 folds for each of 10 candidates, totalling 100 fits

setup関数で指定した事前処理のパイプラインを通じて、tune_model()関数はハイパーパラメータの異なる組み合わせを試し、最良の結果を出す組み合わせを返します。

以上でモデル選択とハイパーパラメータのチューニングのステップは終了となります。必要に応じてどのモデルを選択し、どのようにチューニングするかは自由に決定できるため、PyCaretの真価が発揮されるステップでもあります。


user:

ありがとうございます。r2が最高のモデルとしてgbrが選択され、ハイパラチューニングが行われ、結果の表が表示されたようです。
ここで行われたハイパラチューニングは、グリッドサーチでしょうか?


assistant:

はい、その通りです。PyCaretの tune_model関数はデフォルトでランダムグリッドサーチの手法を採用しています。ランダムグリッドサーチは、全てのパラメータの組み合わせを試すのは時間がかかるため、ランダムに一部の組み合わせだけを試す方法です。

PyCaretでは、以前のバージョンではグリッドサーチが利用されていましたが、時間効率の観点からランダムサーチに変更になったようです。

ただし、手動でハイパーパラメータのリストを指定し、custom_grid引数を介して各モデルのカスタムグリッドを渡せば、グリッドサーチを実施することも可能です。

PyCaretのハイパーパラメータチューニングは、時間とリソースを最適に使用しながら最良のモデルを探すための強力なツールと言えるでしょう。


user:

ふたつ質問があります。ここまでで、

# 必要なライブラリのインポート
from pycaret.datasets import get_data
from pycaret.regression import setup

# データセットの読み込みとセッションの初期設定
df = get_data("diamond")

# データの前処理
exp_reg = setup(data = df, target = 'Price', session_id=123)

# データの前処理と、特徴量エンジニアリング
exp_reg101 = setup(data=df, target="Price", polynomial_features=True, session_id=123)

# データの前処理と、特徴量エンジニアリングと、特徴量選択
exp_reg102 = setup(data = df, target = 'Price', polynomial_features = True, remove_multicollinearity = True, multicollinearity_threshold = 0.9, session_id=123)

というコードと、

from pycaret.regression import compare_models
from pycaret.regression import tune_model

# モデルの比較
best_model = compare_models()

# ハイパーパラメータのチューニング
tuned_model = tune_model(best_model)

というコードを実行しました。

質問1:exp_reg や exp_reg101 や exp_reg102 などの変数が、compare_models や tune_model などの関数に渡されていないように見えますが、どのように渡されているのでしょうか?

質問2:上記コード中の特徴量選択は、モデルの学習前に行われているようです。モデルの学習時には、特徴量選択は行わないのでしょうか?


assistant:

素晴らしい質問です!

質問1について:
setup() 関数は内部的に PyCaret の実行環境の状態を保持しています。これにより、PyCaret はデータの前処理と変換の各ステップを追跡し、結果を維持します。つまり、戻り値であるexp_reg101exp_reg102を実際に関数に渡す必要はありません。compare_models()tune_model()setup()で設定された環境を自動的に使用します。これは"暗黙的な"ステート管理の一種であり、グローバルステートを利用しています。

質問2について:
はい、その通りです。PyCaret では、特徴量選択は前処理ステップの一部として実行され、モデルの学習前に行われます。これは一般的なワークフローで、特定のアルゴリズムにフィットさせる前にデータをクリーニングし、変換し、適切な特徴量を選択するという流れです。

ただし、これは絶対的なルールではありません。特に機械学習の一部の分野(例えば、特徴量の重要性を見るための学習といった)では、学習後の特徴量選択が行われることもあります。これはアルゴリズムや問題設定によります。

つまり、ここまでの各ステップは基本的なデータサイエンスのワークフローをカバーしていますが、一部の特定のタスクや問題設定によっては、異なるアプローチを採ることがあり得ます。


user:

ありがとうございます。ここまで、

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

4. モデル選択とハイパーパラメータチューニング:たくさんの学習器(アルゴリズム)を簡単に試し、それぞれのパフォーマンスを比較できます。さらに、選んだ学習器の最適なパラメータを探すためのハイパーパラメータチューニングもサポートされています。

まではできたので、続いて

5. アンサンブル学習:スタッキングやブレンディングといったアンサンブル学習手法もサポートしており、パフォーマンスを追求するために複数のモデルを組み合わせることが可能です。

を実行する具体的コードを書いてください。


assistant:

それでは、アンサンブル学習を進めていきましょう。

まず、アンサンブル学習の一種であるバギングを行うために、以下のようなコードを使用します:

from pycaret.regression import ensemble_model

# バギング
bagged_model = ensemble_model(tuned_model)

ensemble_model()関数は、指定されたモデルのバギングアンサンブルを作成します。このバギングアンサンブルは、オリジナルのモデルとは異なるサブセットのデータで訓練され、予測の平均化によってオーバーフィットのリスクを減らします。

  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 705.3173 2422310.9009 1556.3775 0.9782 0.0739 0.0534
1 632.8424 1424101.0305 1193.3570 0.9827 0.0688 0.0510
2 699.5979 3115066.9942 1764.9552 0.9722 0.0736 0.0515
3 752.4200 2495313.4634 1579.6561 0.9778 0.0776 0.0537
4 639.6600 1393871.9299 1180.6235 0.9863 0.0696 0.0518
5 667.1550 1529670.7865 1236.7986 0.9824 0.0772 0.0548
6 758.0054 5544679.6546 2354.7143 0.9583 0.0787 0.0542
7 695.5597 2128104.5675 1458.8024 0.9790 0.0751 0.0510
8 637.7826 1460069.7489 1208.3335 0.9859 0.0692 0.0525
9 586.3581 1293575.3845 1137.3546 0.9855 0.0625 0.0475
Mean 677.4698 2280676.4461 1467.0973 0.9788 0.0726 0.0521
Std 52.0576 1231716.2469 358.1927 0.0080 0.0048 0.0020

次に、ブースティングアンサンブルを作成します:

# ブースティング
boosted_model = ensemble_model(tuned_model, method = 'Boosting')

method = 'Boosting'とすることで、ブースティング型のアンサンブル学習となります。ブースティングは、逐次的にモデルを訓練し、前のモデルで誤分類されたデータの重要度を高めることで性能を改善します。

  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 714.5889 2225267.2661 1491.7330 0.9800 0.0758 0.0552
1 643.7416 1583911.0257 1258.5353 0.9808 0.0698 0.0516
2 743.4538 3260292.2895 1805.6279 0.9709 0.0739 0.0531
3 747.8496 2376095.0718 1541.4587 0.9788 0.0797 0.0551
4 652.3098 1601527.9877 1265.5149 0.9843 0.0713 0.0528
5 641.2039 1363070.1144 1167.5059 0.9843 0.0711 0.0525
6 798.2027 6220045.6767 2494.0019 0.9532 0.0826 0.0565
7 695.6483 1959753.1228 1399.9118 0.9807 0.0753 0.0511
8 626.3409 1371957.1545 1171.3057 0.9868 0.0709 0.0535
9 616.7654 1304569.2779 1142.1774 0.9853 0.0648 0.0504
Mean 688.0105 2326648.8987 1473.7773 0.9785 0.0735 0.0532
Std 58.1944 1419140.2734 393.2295 0.0094 0.0049 0.0019

最後に、複数のモデルを組み合わせるスタッキングも可能です:

from pycaret.regression import blend_models, stack_models

# モデルのブレンディング
blended = blend_models(estimator_list = [tuned_model, bagged_model, boosted_model])

# モデルのスタッキング
stacked = stack_models(estimator_list = [bagged_model, boosted_model], meta_model = tuned_model)

blend_models()関数は、予測値の平均化により複数のモデルを組み合わせます。これにより、特定のモデルの過剰適合を抑制し、様々なモデルの良い部分を引き出すことができます。

また、stack_models()関数は、「メタモデル」と呼ばれる2次モデルを使用して、複数の基本モデルの出力を組み合わせます。これにより、モデル間の相互作用を捉えることができます。

これらを組み合わせれば、さまざまな手法でアンサンブル学習を構築することができます。


user:

ありがとうございます。上記2つのコード(バギングとブースティング)はエラーなく計算終了しました。

3つ目のコードは次のようなエラーが出ました。

Initiated	. . . . . . . . . . . . . . . . . .	09:39:37
Status	. . . . . . . . . . . . . . . . . .	Finalizing Model
Estimator	. . . . . . . . . . . . . . . . . .	Voting Regressor
Processing:  83%
 5/6 [02:54<00:34, 34.89s/it]
---------------------------------------------------------------------------
_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/joblib/externals/loky/process_executor.py", line 463, in _process_worker
    r = call_item()
        ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/externals/loky/process_executor.py", line 291, in __call__
    return self.fn(*self.args, **self.kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/parallel.py", line 589, in __call__
    return [func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/parallel.py", line 589, in <listcomp>
    return [func(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/parallel.py", line 129, in __call__
    return self.function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/ensemble/_base.py", line 36, in _fit_single_estimator
    estimator.fit(X, y)
  File "/usr/local/lib/python3.11/site-packages/sklearn/base.py", line 1474, in wrapper
    return fit_method(estimator, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/ensemble/_bagging.py", line 326, in fit
    X, y = self._validate_data(
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/base.py", line 650, in _validate_data
    X, y = check_X_y(X, y, **check_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1263, in check_X_y
    X = check_array(
        ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1097, in check_array
    array.flags.writeable = True
    ^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot set WRITEABLE flag to True of this array
"""

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[13], line 4
      1 from pycaret.regression import blend_models, stack_models
      3 # モデルのブレンディング
----> 4 blended = blend_models(estimator_list = [tuned_model, bagged_model, boosted_model])
      6 # モデルのスタッキング
      7 stacked = stack_models(estimator_list = [bagged_model, boosted_model], meta_model = tuned_model)

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:1415, in blend_models(estimator_list, fold, round, choose_better, optimize, weights, fit_kwargs, groups, verbose, return_train_score)
   1326 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
   1327 def blend_models(
   1328     estimator_list: list,
   (...)
   1337     return_train_score: bool = False,
   1338 ):
   1339     """
   1340     This function trains a Voting Regressor for select models passed in the
   1341     ``estimator_list`` param. The output of this function is a score grid with
   (...)
   1412 
   1413     """
-> 1415     return _CURRENT_EXPERIMENT.blend_models(
   1416         estimator_list=estimator_list,
   1417         fold=fold,
   1418         round=round,
   1419         choose_better=choose_better,
   1420         optimize=optimize,
   1421         weights=weights,
   1422         fit_kwargs=fit_kwargs,
   1423         groups=groups,
   1424         verbose=verbose,
   1425         return_train_score=return_train_score,
   1426     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:1715, in RegressionExperiment.blend_models(self, estimator_list, fold, round, choose_better, optimize, weights, fit_kwargs, groups, verbose, return_train_score)
   1626 def blend_models(
   1627     self,
   1628     estimator_list: list,
   (...)
   1637     return_train_score: bool = False,
   1638 ):
   1639     """
   1640     This function trains a Voting Regressor for select models passed in the
   1641     ``estimator_list`` param. The output of this function is a score grid with
   (...)
   1712 
   1713     """
-> 1715     return super().blend_models(
   1716         estimator_list=estimator_list,
   1717         fold=fold,
   1718         round=round,
   1719         choose_better=choose_better,
   1720         optimize=optimize,
   1721         method="auto",
   1722         weights=weights,
   1723         fit_kwargs=fit_kwargs,
   1724         groups=groups,
   1725         verbose=verbose,
   1726         return_train_score=return_train_score,
   1727     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:3486, in _SupervisedExperiment.blend_models(self, estimator_list, fold, round, choose_better, optimize, method, weights, fit_kwargs, groups, probability_threshold, verbose, return_train_score)
   3481 display.move_progress()
   3483 self.logger.info(
   3484     "SubProcess create_model() called =================================="
   3485 )
-> 3486 model, model_fit_time = self._create_model(
   3487     estimator=model,
   3488     system=False,
   3489     display=display,
   3490     fold=fold,
   3491     round=round,
   3492     fit_kwargs=fit_kwargs,
   3493     groups=groups,
   3494     probability_threshold=probability_threshold,
   3495     return_train_score=return_train_score,
   3496 )
   3498 model_results = self.pull()
   3499 self.logger.info(
   3500     "SubProcess create_model() end =================================="
   3501 )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1533, in _SupervisedExperiment._create_model(self, estimator, fold, round, cross_validation, predict, fit_kwargs, groups, refit, probability_threshold, experiment_custom_tags, verbose, system, add_to_model_list, X_train_data, y_train_data, metrics, display, model_only, return_train_score, error_score, **kwargs)
   1530         return model, model_fit_time
   1531     return model
-> 1533 model, model_fit_time, model_results, _ = self._create_model_with_cv(
   1534     model=model,
   1535     data_X=data_X,
   1536     data_y=data_y,
   1537     fit_kwargs=fit_kwargs,
   1538     round=round,
   1539     cv=cv,
   1540     groups=groups,
   1541     metrics=metrics,
   1542     refit=refit,
   1543     system=system,
   1544     display=display,
   1545     error_score=error_score,
   1546     return_train_score=return_train_score,
   1547 )
   1549 # end runtime
   1550 runtime_end = time.time()

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1223, in _SupervisedExperiment._create_model_with_cv(self, model, data_X, data_y, fit_kwargs, round, cv, groups, metrics, refit, system, display, error_score, return_train_score)
   1221 self.logger.info("Finalizing model")
   1222 with redirect_output(self.logger):
-> 1223     pipeline_with_model.fit(data_X, data_y, **fit_kwargs)
   1224     model_fit_end = time.time()
   1226 # calculating metrics on predictions of complete train dataset

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pipeline.py:276, in Pipeline.fit(self, X, y, **params)
    274 if self._final_estimator != "passthrough":
    275     last_step_params = routed_params[self.steps[-1][0]]
--> 276     fitted_estimator = self._memory_fit(
    277         clone(self.steps[-1][1]), X, y, **last_step_params["fit"]
    278     )
    279     # Hacky way to make sure that the state of the estimator
    280     # loaded from cache is carried over to the estimator
    281     # in steps
    282     _copy_estimator_state(fitted_estimator, self.steps[-1][1])

File /usr/local/lib/python3.11/site-packages/joblib/memory.py:353, in NotMemorizedFunc.__call__(self, *args, **kwargs)
    352 def __call__(self, *args, **kwargs):
--> 353     return self.func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pipeline.py:69, in _fit_one(transformer, X, y, message, params)
     67         if "y" in signature(transformer.fit).parameters:
     68             args.append(y)
---> 69         transformer.fit(*args)
     70 return transformer

File /usr/local/lib/python3.11/site-packages/sklearn/base.py:1474, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1467     estimator._validate_params()
   1469 with config_context(
   1470     skip_parameter_validation=(
   1471         prefer_skip_nested_validation or global_skip_validation
   1472     )
   1473 ):
-> 1474     return fit_method(estimator, *args, **kwargs)

File /usr/local/lib/python3.11/site-packages/sklearn/ensemble/_voting.py:623, in VotingRegressor.fit(self, X, y, sample_weight)
    621 _raise_for_unsupported_routing(self, "fit", sample_weight=sample_weight)
    622 y = column_or_1d(y, warn=True)
--> 623 return super().fit(X, y, sample_weight)

File /usr/local/lib/python3.11/site-packages/sklearn/ensemble/_voting.py:89, in _BaseVoting.fit(self, X, y, sample_weight)
     83 if self.weights is not None and len(self.weights) != len(self.estimators):
     84     raise ValueError(
     85         "Number of `estimators` and weights must be equal; got"
     86         f" {len(self.weights)} weights, {len(self.estimators)} estimators"
     87     )
---> 89 self.estimators_ = Parallel(n_jobs=self.n_jobs)(
     90     delayed(_fit_single_estimator)(
     91         clone(clf),
     92         X,
     93         y,
     94         sample_weight=sample_weight,
     95         message_clsname="Voting",
     96         message=self._log_message(names[idx], idx + 1, len(clfs)),
     97     )
     98     for idx, clf in enumerate(clfs)
     99     if clf != "drop"
    100 )
    102 self.named_estimators_ = Bunch()
    104 # Uses 'drop' as placeholder for dropped estimators

File /usr/local/lib/python3.11/site-packages/sklearn/utils/parallel.py:67, in Parallel.__call__(self, iterable)
     62 config = get_config()
     63 iterable_with_config = (
     64     (_with_config(delayed_func, config), args, kwargs)
     65     for delayed_func, args, kwargs in iterable
     66 )
---> 67 return super().__call__(iterable_with_config)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1952, in Parallel.__call__(self, iterable)
   1946 # The first item from the output is blank, but it makes the interpreter
   1947 # progress until it enters the Try/Except block of the generator and
   1948 # reach the first `yield` statement. This starts the aynchronous
   1949 # dispatch of the tasks to the workers.
   1950 next(output)
-> 1952 return output if self.return_generator else list(output)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1595, in Parallel._get_outputs(self, iterator, pre_dispatch)
   1592     yield
   1594     with self._backend.retrieval_context():
-> 1595         yield from self._retrieve()
   1597 except GeneratorExit:
   1598     # The generator has been garbage collected before being fully
   1599     # consumed. This aborts the remaining tasks if possible and warn
   1600     # the user if necessary.
   1601     self._exception = True

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1699, in Parallel._retrieve(self)
   1692 while self._wait_retrieval():
   1693 
   1694     # If the callback thread of a worker has signaled that its task
   1695     # triggered an exception, or if the retrieval loop has raised an
   1696     # exception (e.g. `GeneratorExit`), exit the loop and surface the
   1697     # worker traceback.
   1698     if self._aborting:
-> 1699         self._raise_error_fast()
   1700         break
   1702     # If the next job is not ready for retrieval yet, we just wait for
   1703     # async callbacks to progress.

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1734, in Parallel._raise_error_fast(self)
   1730 # If this error job exists, immediatly raise the error by
   1731 # calling get_result. This job might not exists if abort has been
   1732 # called directly or if the generator is gc'ed.
   1733 if error_job is not None:
-> 1734     error_job.get_result(self.timeout)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:736, in BatchCompletionCallBack.get_result(self, timeout)
    730 backend = self.parallel._backend
    732 if backend.supports_retrieve_callback:
    733     # We assume that the result has already been retrieved by the
    734     # callback thread, and is stored internally. It's just waiting to
    735     # be returned.
--> 736     return self._return_or_raise()
    738 # For other backends, the main thread needs to run the retrieval step.
    739 try:

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:754, in BatchCompletionCallBack._return_or_raise(self)
    752 try:
    753     if self.status == TASK_ERROR:
--> 754         raise self._result
    755     return self._result
    756 finally:

ValueError: cannot set WRITEABLE flag to True of this array

assistant:

問題のエラーはここにあります。

File "/usr/local/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1097, in check_array
    array.flags.writeable = True
    ^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot set WRITEABLE flag to True of this array

これは、PyCaretがNumpy配列に対してWRITEABLEフラグをTrueに設定しようとしたときに発生します。これは、一般的には配列がメモリ上のある位置に参照している場合、またはNumpyが配列のビューを作成していて、元のデータが変更できないように保証している場合に発生します。

この問題を解決するための一般的な方法の一つは、配列のコピーを作成することです。以下にその例を示します。

import numpy as np

# sample array
a = np.array([1,2,3])

# making the array read-only
a.flags.writeable = False

# create a new array b as a copy of a
b = np.copy(a)

# now you can modify the array b
b[0] = 5

print(b)  # prints: array([5, 2, 3])

上記のコードのように、Numpyのcopy関数を使用すると、新しい配列が作成され、それに対して変更を加えることができます。

しかし、PyCaretの中でこのエラーが発生する具体的な原因と対処法を特定するには、データとコード全体を見る必要があります。上記の方法を適用するには、PyCaretのソースコードを修正するか、直接Numpy配列を扱う必要があるかもしれません。

このエラーの原因や解決策について追加情報が必要であれば、使用している具体的なデータとコードを提供してください。また、この問題がPyCaretのバグである場合は、PyCaretの開発者にフィードバックを提供するのも適切かもしれません。


user:

スタッキングの方も同じようなエラーが得らーれました。

Initiated	. . . . . . . . . . . . . . . . . .	09:52:04
Status	. . . . . . . . . . . . . . . . . .	Finalizing Model
Estimator	. . . . . . . . . . . . . . . . . .	Stacking Regressor
Processing:  83%
 5/6 [14:08<02:49, 169.67s/it]
---------------------------------------------------------------------------
_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/joblib/externals/loky/process_executor.py", line 463, in _process_worker
    r = call_item()
        ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/externals/loky/process_executor.py", line 291, in __call__
    return self.fn(*self.args, **self.kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/parallel.py", line 589, in __call__
    return [func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/joblib/parallel.py", line 589, in <listcomp>
    return [func(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/parallel.py", line 129, in __call__
    return self.function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/ensemble/_base.py", line 36, in _fit_single_estimator
    estimator.fit(X, y)
  File "/usr/local/lib/python3.11/site-packages/sklearn/base.py", line 1474, in wrapper
    return fit_method(estimator, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/ensemble/_bagging.py", line 326, in fit
    X, y = self._validate_data(
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/base.py", line 650, in _validate_data
    X, y = check_X_y(X, y, **check_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1263, in check_X_y
    X = check_array(
        ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1097, in check_array
    array.flags.writeable = True
    ^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot set WRITEABLE flag to True of this array
"""

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[14], line 1
----> 1 stacked = stack_models(estimator_list = [bagged_model, boosted_model], meta_model = tuned_model)

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:1530, in stack_models(estimator_list, meta_model, meta_model_fold, fold, round, restack, choose_better, optimize, fit_kwargs, groups, verbose, return_train_score)
   1429 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
   1430 def stack_models(
   1431     estimator_list: list,
   (...)
   1442     return_train_score: bool = False,
   1443 ):
   1444     """
   1445     This function trains a meta model over select estimators passed in
   1446     the ``estimator_list`` parameter. The output of this function is a
   (...)
   1527 
   1528     """
-> 1530     return _CURRENT_EXPERIMENT.stack_models(
   1531         estimator_list=estimator_list,
   1532         meta_model=meta_model,
   1533         meta_model_fold=meta_model_fold,
   1534         fold=fold,
   1535         round=round,
   1536         restack=restack,
   1537         choose_better=choose_better,
   1538         optimize=optimize,
   1539         fit_kwargs=fit_kwargs,
   1540         groups=groups,
   1541         verbose=verbose,
   1542         return_train_score=return_train_score,
   1543     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:1830, in RegressionExperiment.stack_models(self, estimator_list, meta_model, meta_model_fold, fold, round, restack, choose_better, optimize, fit_kwargs, groups, verbose, return_train_score)
   1729 def stack_models(
   1730     self,
   1731     estimator_list: list,
   (...)
   1742     return_train_score: bool = False,
   1743 ):
   1744     """
   1745     This function trains a meta model over select estimators passed in
   1746     the ``estimator_list`` parameter. The output of this function is a
   (...)
   1827 
   1828     """
-> 1830     return super().stack_models(
   1831         estimator_list=estimator_list,
   1832         meta_model=meta_model,
   1833         meta_model_fold=meta_model_fold,
   1834         fold=fold,
   1835         round=round,
   1836         method="auto",
   1837         restack=restack,
   1838         choose_better=choose_better,
   1839         optimize=optimize,
   1840         fit_kwargs=fit_kwargs,
   1841         groups=groups,
   1842         verbose=verbose,
   1843         return_train_score=return_train_score,
   1844     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:3868, in _SupervisedExperiment.stack_models(self, estimator_list, meta_model, meta_model_fold, fold, round, method, restack, choose_better, optimize, fit_kwargs, groups, probability_threshold, verbose, return_train_score)
   3863 display.move_progress()
   3865 self.logger.info(
   3866     "SubProcess create_model() called =================================="
   3867 )
-> 3868 model, model_fit_time = self._create_model(
   3869     estimator=model,
   3870     system=False,
   3871     display=display,
   3872     fold=fold,
   3873     round=round,
   3874     fit_kwargs=fit_kwargs,
   3875     groups=groups,
   3876     probability_threshold=probability_threshold,
   3877     return_train_score=return_train_score,
   3878 )
   3879 model_results = self.pull()
   3880 self.logger.info(
   3881     "SubProcess create_model() end =================================="
   3882 )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1533, in _SupervisedExperiment._create_model(self, estimator, fold, round, cross_validation, predict, fit_kwargs, groups, refit, probability_threshold, experiment_custom_tags, verbose, system, add_to_model_list, X_train_data, y_train_data, metrics, display, model_only, return_train_score, error_score, **kwargs)
   1530         return model, model_fit_time
   1531     return model
-> 1533 model, model_fit_time, model_results, _ = self._create_model_with_cv(
   1534     model=model,
   1535     data_X=data_X,
   1536     data_y=data_y,
   1537     fit_kwargs=fit_kwargs,
   1538     round=round,
   1539     cv=cv,
   1540     groups=groups,
   1541     metrics=metrics,
   1542     refit=refit,
   1543     system=system,
   1544     display=display,
   1545     error_score=error_score,
   1546     return_train_score=return_train_score,
   1547 )
   1549 # end runtime
   1550 runtime_end = time.time()

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1223, in _SupervisedExperiment._create_model_with_cv(self, model, data_X, data_y, fit_kwargs, round, cv, groups, metrics, refit, system, display, error_score, return_train_score)
   1221 self.logger.info("Finalizing model")
   1222 with redirect_output(self.logger):
-> 1223     pipeline_with_model.fit(data_X, data_y, **fit_kwargs)
   1224     model_fit_end = time.time()
   1226 # calculating metrics on predictions of complete train dataset

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pipeline.py:276, in Pipeline.fit(self, X, y, **params)
    274 if self._final_estimator != "passthrough":
    275     last_step_params = routed_params[self.steps[-1][0]]
--> 276     fitted_estimator = self._memory_fit(
    277         clone(self.steps[-1][1]), X, y, **last_step_params["fit"]
    278     )
    279     # Hacky way to make sure that the state of the estimator
    280     # loaded from cache is carried over to the estimator
    281     # in steps
    282     _copy_estimator_state(fitted_estimator, self.steps[-1][1])

File /usr/local/lib/python3.11/site-packages/joblib/memory.py:353, in NotMemorizedFunc.__call__(self, *args, **kwargs)
    352 def __call__(self, *args, **kwargs):
--> 353     return self.func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pipeline.py:69, in _fit_one(transformer, X, y, message, params)
     67         if "y" in signature(transformer.fit).parameters:
     68             args.append(y)
---> 69         transformer.fit(*args)
     70 return transformer

File /usr/local/lib/python3.11/site-packages/sklearn/ensemble/_stacking.py:968, in StackingRegressor.fit(self, X, y, sample_weight)
    966 _raise_for_unsupported_routing(self, "fit", sample_weight=sample_weight)
    967 y = column_or_1d(y, warn=True)
--> 968 return super().fit(X, y, sample_weight)

File /usr/local/lib/python3.11/site-packages/sklearn/base.py:1474, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1467     estimator._validate_params()
   1469 with config_context(
   1470     skip_parameter_validation=(
   1471         prefer_skip_nested_validation or global_skip_validation
   1472     )
   1473 ):
-> 1474     return fit_method(estimator, *args, **kwargs)

File /usr/local/lib/python3.11/site-packages/sklearn/ensemble/_stacking.py:216, in _BaseStacking.fit(self, X, y, sample_weight)
    211             self.estimators_.append(estimator)
    212 else:
    213     # Fit the base estimators on the whole training data. Those
    214     # base estimators will be used in transform, predict, and
    215     # predict_proba. They are exposed publicly.
--> 216     self.estimators_ = Parallel(n_jobs=self.n_jobs)(
    217         delayed(_fit_single_estimator)(clone(est), X, y, sample_weight)
    218         for est in all_estimators
    219         if est != "drop"
    220     )
    222 self.named_estimators_ = Bunch()
    223 est_fitted_idx = 0

File /usr/local/lib/python3.11/site-packages/sklearn/utils/parallel.py:67, in Parallel.__call__(self, iterable)
     62 config = get_config()
     63 iterable_with_config = (
     64     (_with_config(delayed_func, config), args, kwargs)
     65     for delayed_func, args, kwargs in iterable
     66 )
---> 67 return super().__call__(iterable_with_config)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1952, in Parallel.__call__(self, iterable)
   1946 # The first item from the output is blank, but it makes the interpreter
   1947 # progress until it enters the Try/Except block of the generator and
   1948 # reach the first `yield` statement. This starts the aynchronous
   1949 # dispatch of the tasks to the workers.
   1950 next(output)
-> 1952 return output if self.return_generator else list(output)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1595, in Parallel._get_outputs(self, iterator, pre_dispatch)
   1592     yield
   1594     with self._backend.retrieval_context():
-> 1595         yield from self._retrieve()
   1597 except GeneratorExit:
   1598     # The generator has been garbage collected before being fully
   1599     # consumed. This aborts the remaining tasks if possible and warn
   1600     # the user if necessary.
   1601     self._exception = True

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1699, in Parallel._retrieve(self)
   1692 while self._wait_retrieval():
   1693 
   1694     # If the callback thread of a worker has signaled that its task
   1695     # triggered an exception, or if the retrieval loop has raised an
   1696     # exception (e.g. `GeneratorExit`), exit the loop and surface the
   1697     # worker traceback.
   1698     if self._aborting:
-> 1699         self._raise_error_fast()
   1700         break
   1702     # If the next job is not ready for retrieval yet, we just wait for
   1703     # async callbacks to progress.

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:1734, in Parallel._raise_error_fast(self)
   1730 # If this error job exists, immediatly raise the error by
   1731 # calling get_result. This job might not exists if abort has been
   1732 # called directly or if the generator is gc'ed.
   1733 if error_job is not None:
-> 1734     error_job.get_result(self.timeout)

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:736, in BatchCompletionCallBack.get_result(self, timeout)
    730 backend = self.parallel._backend
    732 if backend.supports_retrieve_callback:
    733     # We assume that the result has already been retrieved by the
    734     # callback thread, and is stored internally. It's just waiting to
    735     # be returned.
--> 736     return self._return_or_raise()
    738 # For other backends, the main thread needs to run the retrieval step.
    739 try:

File /usr/local/lib/python3.11/site-packages/joblib/parallel.py:754, in BatchCompletionCallBack._return_or_raise(self)
    752 try:
    753     if self.status == TASK_ERROR:
--> 754         raise self._result
    755     return self._result
    756 finally:

ValueError: cannot set WRITEABLE flag to True of this array

assistant:

エラーメッセージを見ると、"ValueError: cannot set WRITEABLE flag to True of this array"というエラーが表示されています。これは、numpy配列を変更しようとしていますが、それが許可されていない場合に発生します。

これが起こる一般的な原因は、numpy配列がメモリ上の特定の場所に規制があるときです。例えば、その配列の一部を別の変数に割り当てた場合、その部分配列は元のデータを参照しているため、変更不能(書き込み不可)になります。

この問題の具体的な原因と解決策を特定するためには、コード全体を見ることが必要です。例外が発生した部分前後のコード、特にエラーが発生した配列を通っている部分が関連している可能性があります。

エラーが発生したのは、SklearnのStackingRegressor.fitメソッド内で、fitを行うために使用されている_fit_single_estimator関数内のcheck_array関数で、この関数が配列のWRITEABLEフラグをTrueに設定しようとしています。

いくつか可能性が考えられますが、スレッドセーフでない操作をマルチスレッド環境で行っていたり、一部の配列がメモリを共有していて、その一部が書き込み不可になっている等が考えられます。

また、このエラーはNumpyのバージョンによっても発生することがあります。Numpyのバージョンをダウングレードすることで解決することもあります。その場合、以下のように実行してNumpyのバージョンを戻すことができます:

pip install numpy==1.20.3

ただし、全体のコードや環境を確認しないと確実な解決策は出せませんので、その点ご了承ください。


user:

そうですか。それは残念ですね。では、とりあえず今回はスタッキングの利用は諦めることにします。

ここまでのコードに関して4つ質問があります。

質問1:今回、compare_models で R2 値が最高を記録した gbr をハイパーパラメーターチューニングしました。ですが、他のモデルも、ハイパーパラメーターチューニングすれば gbr を超える可能性があると思うのですが、その可能性は無視するのでしょうか?

質問2:今回、tune_model でハイパーパラメーターが最適化された gbr を用いてアンサンブルしました。ここでも、他のモデルを用いたアンサンブルの可能性は無視するのでしょうか?

質問3:スタッキングにおける「メタモデル」とは何でしょうか?詳しく教えてください。

質問4:今回、スタッキングモデルでは、どれも gbr によるモデルを用いました。gbr以外のモデルも組み合わせるほうがスタッキングの長所が出ると思うのですが、どう思われますか?


assistant:

<質問1について>

たしかに、他のモデルでもハイパーパラメーターチューニングを行えば、その性能は改善する可能性があります。しかし、ハイパーパラメーターチューニングは計算リソース(時間やメモリ)を必要としますので、計算コストや時間の制限により、最初に比較した結果が良かったモデルに対してのみチューニングを行うというアプローチが一般的です。ただし、ある程度性能が良さそうなモデルについては、一定数チューニングを試みることもあります。

<質問2について>

他のモデルを用いたアンサンブルの可能性を無視するのかどうかは、具体的な状況によります。他のモデルを用いたアンサンブルが有効に働くかどうかは、問題設定やデータ、アンサンブルを構成するモデルの特性等に依存します。そのため、具体的に試してみないと結論を出すことは難しいです。

<質問3について>

スタッキングにおける「メタモデル」は、一言で言えば、ベースモデルの予測結果から最終的な予測結果を導くモデルです。スタッキングでは、まず各ベースモデルが個々に学習を行い、それぞれの予測結果を出力します(レベル0)。次に、これらの予測結果が新たな特徴量となって、メタモデルがそれを元に学習して最終的な予測結果を生成します(レベル1)。このようにステージを重ねるやり方が「スタッキング」です。

<質問4について>

確かに、異なるタイプのモデルを組み合わせることは、スタッキングの長所を引き立たせる一方で、モデルの多様性を取り入れることが可能です。つまり、よりロバストなモデルを構築することができます。ただし、逆に予測の揺らぎ(バリアンス)が大きくなる可能性もあるため、注意が必要です。

以上、質問に対する僕の考えを述べてみました。どの手法を選ぶべきかは、具体的なタスクやデータ、目的によるというのが結論になると思います。


user:

ご回答ありがとうございました。質問3について、さらに質問があります。

スタッキングにおける「メタモデル」は、一言で言えば、ベースモデルの予測結果から最終的な予測結果を導くモデルです。スタッキングでは、まず各ベースモデルが個々に学習を行い、それぞれの予測結果を出力します(レベル0)。次に、これらの予測結果が新たな特徴量となって、メタモデルがそれを元に学習して最終的な予測結果を生成します(レベル1)。このようにステージを重ねるやり方が「スタッキング」です。

とのことですが、それは私の理解とも一致しています。ところが、ご提案いただいたコードは

# モデルのスタッキング
stacked = stack_models(estimator_list = [bagged_model, boosted_model], meta_model = tuned_model)

となっています。この中で、bagged_model, boosted_model, tuned_model はどれも、df = get_data('diamond') で得たデータを入力とする gbr モデルだと認識しております。上記のメタモデルは、[bagged_model, boosted_model] の2つのモデルの出力を入力とするモデルでなくてはいけないのではないでしょうか?


assistant:

ご質問いただきまして、ありがとうございます。スタッキングを行うプロセスについて理解が深まってきたようで嬉しいです。

pycaretのstack_models関数では、estimator_listパラメータで指定したモデルがレベル0(ベースモデル)として訓練され、meta_modelパラメータで指定したモデルがレベル1(メタモデル)として訓練されます。

この場合、bagged_modelboosted_modelが各々データを用いて予測を行い(レベル0)、その予測結果がmeta_modelであるtuned_modelの入力となります(レベル1)。したがって、stack_models関数は内部でレベル0のモデルの出力を新たな特徴量としてレベル1のモデルに渡すようになっています。

したがって、tuned_modelは原則として、bagged_modelboosted_modelの出力を入力とするモデルとなります。ただしstack_models関数のmethodパラメータがpredict_probapredictに設定されている場合には、元の特徴量もメタモデルの入力に用いられます。

ですので、ご認識のとおり、ここでmeta_modelとしてセットされているtuned_modelは、そのままtuned_modelを適用するのではなく、bagged_modelboosted_modelの出力を入力として使用するように設計されています。

以上、お答えになりましたでしょうか。何か不明点がございましたら、どうぞ遠慮なくお知らせくださいませ。


user:

いえ、まだ不明点があります。それならば、meta_model = tuned_model ではなくて、meta_modelとして普通の gbr モデルや、他の回帰モデルを用いるべきではないでしょうか?


assistant:

スタッキングでは、メタモデルとして使用するモデルも、ベースモデル(レベル0のモデル)と同様に重要となります。メタモデルはベースモデルの結果を最終的な予測結果に変換しますので、その予測精度に大きな影響を与えます。

したがって、あらかじめ予測精度が良いことがわかっているtuned_modelをメタモデルとして使用するというアプローチは、決して間違ってはいません。大切なのは、tuned_modelがどのようにチューニングされているか、ということです。

あらかじめチューニングされたモデルを使うことになった場合、そのモデルは新たに与えられた特徴量(ここでは他のモデルの予測結果)に対してうまく対応できるようにチューニングされていなければなりません。それができない場合、そのモデルはメタモデルとしての役割を果たせません。

ただし、tuned_modelが新たに与えられた特徴量に適応できる範囲でチューニングされていたら、メタモデルとして有効と考えられます。

私が示したコードはあくまで一例で、他のモデルをメタモデルとして用いることももちろん可能です。精度向上のためには、メタモデルとしてどのモデルを選ぶべきか試行錯誤することも一つの方法です。

確かに、もっと広範な視点から考えて、普通のgbrモデルや他の回帰モデルを試すのも良い選択だと思います。スタッキングの考え方を捉えるためには、いろいろ試してみることが大切です。

以上、お答えになりましたでしょうか?どういったところが気になるのか、もしご意見やご質問がございましたらお知らせいただけますと幸いです。


user:

ベースモデルの入力と、メタモデルの入力は、次元数が異なりますよね。ベースモデルの入力は、df = get_data('diamond') のデータの次元数で、メタモデルの入力は2次元です。正しいですよね?

それが正しいとするならば、df = get_data('diamond') のデータの次元数に従ってチューニングされた tuned_model が、メタモデルに対しても最適化されているとする根拠が分かりません。


assistant:

おっしゃる通りで、ベースモデルとメタモデルとで、それぞれに供給されるデータの特徴量(次元数)は異なります。すなわち、ベースモデルは元の入力データに対して学習を行い、一方メタモデルはベースモデルの出力(予測結果)に対して学習を行います。メタモデルの入力データの次元数はベースモデルの数となります。

したがって、ベースモデルの学習で用いた与えられたデータセットに対してチューニングを行っているモデル(ここではtuned_model)が、メタモデルとして使用する際の新たなデータ(ベースモデルの出力)に対して同じように最適とは必ずしも言えません。

おっしゃる通り、この点について注意する必要があります。モデルをチューニングする際、新たな特徴量についての適応性も考慮しなければならないためです。

そのため、メタモデルにはチューニングされていないものを用いる、またはチューニングする際にはベースモデルの出力を用いるといった対策が必要となります。

当初の説明で省略してしまった点で混乱を招いてしまい、大変申し訳ありませんでした。そして、ご丁寧に質問いただき、ありがとうございます。どんな質問も大歓迎ですので、引き続き遠慮なくご質問いただけますと幸いです。


user:

ありがとうございます。ここまでで、

1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

4. モデル選択とハイパーパラメータチューニング:たくさんの学習器(アルゴリズム)を簡単に試し、それぞれのパフォーマンスを比較できます。さらに、選んだ学習器の最適なパラメータを探すためのハイパーパラメータチューニングもサポートされています。

5. アンサンブル学習:スタッキングやブレンディングといったアンサンブル学習手法もサポートしており、パフォーマンスを追求するために複数のモデルを組み合わせることが可能です。

まで実行できたと思いますので、続いて

6. モデル解釈:各種のグラフィカルなツール(特徴量の重要度、相関マップ、シャプレイ値など)を使ってモデルの結果を解釈することができます。

について、具体的なコードを書いていただけますか?


assistant:

はい、もちろんです。モデル解釈には様々な手法がありますが、ここでは特徴量の重要度、相関マップ、シャプレイ値を見る方法を示しますね。

まずは特徴量の重要度からです。例えば、以下のようにして、特徴量の重要度をプロットすることができます。

# 特徴量の重要度をプロット(あらかじめ学習したモデルが必要です。)
plot_model(tuned_model, plot='feature')

次に、相関マップ(相関行列のヒートマップ)を見る方法です。以下のようにしてプロットできます。

# 相関マップをプロット
plot_model(tuned_model, plot='correlation')

最後にシャプレイ値についてですが、これにはinterpret_model関数を使用します。

# シャプレイ値をプロット
interpret_model(tuned_model)

以上のように、PyCaretでは様々なモデル解釈の手法を容易に試すことができます。必要なモデル解釈の手法があれば、それに対応する関数やパラメータを指定してプロットしてみてください。

なお、これらの関数を実行する前にモデルの学習が必要な点、ご了承くださいませ。それぞれの関数plot_modelinterpret_modelには学習済みのモデルのインスタンスを引数として渡す必要があります。

以上がモデル解釈に関する具体的なコードとなります。何か他にご質問がございましたら、どうぞお知らせくださいませ。


user:

output_15_1.png

ありがとうございます。まず第1のコード(特徴量の重要度)はエラーなく動きました。この特徴量の重要度は、どのように計算されたのか詳しく説明していただけますか?


assistant:

はい、もちろんです。特徴量の重要度は、多くの場合、以下のような方法で計算されます。

  1. Permutation Feature Importance(置換特徴量重要度): この手法では、まずモデルを学習し、その性能を評価します(例えば、予測の精度やF1スコアなどで)。次に、一つの特徴量を選び、その特徴量の値をランダムに並び替えます(つまり、その特徴量が元のデータとは無関係になるようにします)。この状態で再度モデルの性能を評価します。元の評価指標と並び替え後の評価指標との差が大きいほどその特徴量は重要なものとされます。

  2. Tree-based Feature Importance(木構造ベースの特徴量重要度): 決定木やランダムフォレスト、勾配ブースティングなどの木構造を用いたモデルでは、モデルの学習過程をもとに特徴量の重要度を計算できます。各特徴量がどれだけ目的変数の分散を説明できているか(つまり、データをどれだけうまく分割できているか)を評価し、その結果を特徴量の重要度とします。

上記のtuned_modelが木構造ベースのモデルであれば、おそらく2番目の手法が使われていると思われます。

どちらの手法でも、特徴量の重要度が高いほどその特徴量がモデルの予測に大きく寄与していると解釈できます。

以上が特徴量の重要度についての説明になります。わかりにくい点や他にご質問があれば、どうぞお知らせくださいませ。


user:

ありがとうございます。第2のコード(相関マップ)では、次のようなエラーになりました。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 2
      1 # 相関マップをプロット
----> 2 plot_model(tuned_model, plot='correlation')

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:1646, in plot_model(estimator, plot, scale, save, fold, fit_kwargs, plot_kwargs, groups, verbose, display_format)
   1546 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
   1547 def plot_model(
   1548     estimator,
   (...)
   1557     display_format: Optional[str] = None,
   1558 ) -> Optional[str]:
   1559     """
   1560     This function analyzes the performance of a trained model on holdout set.
   1561     It may require re-training the model in certain cases.
   (...)
   1643 
   1644     """
-> 1646     return _CURRENT_EXPERIMENT.plot_model(
   1647         estimator=estimator,
   1648         plot=plot,
   1649         scale=scale,
   1650         save=save,
   1651         fold=fold,
   1652         fit_kwargs=fit_kwargs,
   1653         plot_kwargs=plot_kwargs,
   1654         groups=groups,
   1655         verbose=verbose,
   1656         display_format=display_format,
   1657     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:1946, in RegressionExperiment.plot_model(self, estimator, plot, scale, save, fold, fit_kwargs, plot_kwargs, groups, verbose, display_format)
   1846 def plot_model(
   1847     self,
   1848     estimator,
   (...)
   1857     display_format: Optional[str] = None,
   1858 ) -> Optional[str]:
   1859     """
   1860     This function analyzes the performance of a trained model on holdout set.
   1861     It may require re-training the model in certain cases.
   (...)
   1943 
   1944     """
-> 1946     return super().plot_model(
   1947         estimator=estimator,
   1948         plot=plot,
   1949         scale=scale,
   1950         save=save,
   1951         fold=fold,
   1952         fit_kwargs=fit_kwargs,
   1953         plot_kwargs=plot_kwargs,
   1954         groups=groups,
   1955         verbose=verbose,
   1956         display_format=display_format,
   1957     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/tabular_experiment.py:2045, in _TabularExperiment.plot_model(self, estimator, plot, scale, save, fold, fit_kwargs, plot_kwargs, groups, feature_name, label, verbose, display_format)
   1933 def plot_model(
   1934     self,
   1935     estimator,
   (...)
   1946     display_format: Optional[str] = None,
   1947 ) -> Optional[str]:
   1948     """
   1949     This function takes a trained model object and returns a plot based on the
   1950     test / hold-out set. The process may require the model to be re-trained in
   (...)
   2043 
   2044     """
-> 2045     return self._plot_model(
   2046         estimator=estimator,
   2047         plot=plot,
   2048         scale=scale,
   2049         save=save,
   2050         fold=fold,
   2051         fit_kwargs=fit_kwargs,
   2052         plot_kwargs=plot_kwargs,
   2053         groups=groups,
   2054         feature_name=feature_name,
   2055         label=label,
   2056         verbose=verbose,
   2057         display_format=display_format,
   2058     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/tabular_experiment.py:411, in _TabularExperiment._plot_model(self, estimator, plot, scale, save, fold, fit_kwargs, plot_kwargs, groups, feature_name, label, verbose, system, display, display_format)
    406     raise ValueError(
    407         f"Estimator {estimator} does not have the required fit() method."
    408     )
    410 if plot not in self._available_plots:
--> 411     raise ValueError(
    412         "Plot Not Available. Please see docstring for list of available Plots."
    413     )
    415 # checking display_format parameter
    416 self.plot_model_check_display_format_(display_format=display_format)

ValueError: Plot Not Available. Please see docstring for list of available Plots.

assistant:

すみません、そのエラーは私が誤った情報をお伝えしたために起こりました。PyCaretのplot_modelでは、correlationという種類のプロットは提供されていません。

相関行列のプロットは、Pythonのseabornやmatplotlibライブラリを使うことで作成することができますので、その方法を以下に示します。

import seaborn as sns
import matplotlib.pyplot as plt

# データの相関行列を作成
corr_matrix = df.corr()

# ヒートマップをプロット
plt.figure(figsize=(12, 12))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.show()

上記のコードでは、まずpandasのcorr()関数を使ってデータの相関行列を計算しています。次に、seabornのheatmap関数を使ってこの相関行列をヒートマップとしてプロットしています。

もし特定のモデルに基づく特徴量の相関を見たい場合、一部のモデル(例えば、ランダムフォレストや勾配ブースティングなど)は特徴量の相互影響度を見ることができますが、具体的な方法は使用しているモデルやライブラリに依存します。

お手数おかけして申し訳ありませんが、ご参考になれば幸いです。他にも何かご質問があればお気軽にどうぞ。


user:

ありがとうございます。上記コードをベースに、エラーを修正した全コードを書いてください。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[18], line 5
      2 import matplotlib.pyplot as plt
      4 # データの相関行列を作成
----> 5 corr_matrix = df.corr()
      7 # ヒートマップをプロット
      8 plt.figure(figsize=(12, 12))

File ~/.local/lib/python3.11/site-packages/pandas/core/frame.py:10704, in DataFrame.corr(self, method, min_periods, numeric_only)
  10702 cols = data.columns
  10703 idx = cols.copy()
> 10704 mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
  10706 if method == "pearson":
  10707     correl = libalgos.nancorr(mat, minp=min_periods)

File ~/.local/lib/python3.11/site-packages/pandas/core/frame.py:1889, in DataFrame.to_numpy(self, dtype, copy, na_value)
   1887 if dtype is not None:
   1888     dtype = np.dtype(dtype)
-> 1889 result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
   1890 if result.dtype is not dtype:
   1891     result = np.array(result, dtype=dtype, copy=False)

File ~/.local/lib/python3.11/site-packages/pandas/core/internals/managers.py:1656, in BlockManager.as_array(self, dtype, copy, na_value)
   1654         arr.flags.writeable = False
   1655 else:
-> 1656     arr = self._interleave(dtype=dtype, na_value=na_value)
   1657     # The underlying data was copied within _interleave, so no need
   1658     # to further copy if copy=True or setting na_value
   1660 if na_value is lib.no_default:

File ~/.local/lib/python3.11/site-packages/pandas/core/internals/managers.py:1715, in BlockManager._interleave(self, dtype, na_value)
   1713     else:
   1714         arr = blk.get_values(dtype)
-> 1715     result[rl.indexer] = arr
   1716     itemmask[rl.indexer] = 1
   1718 if not itemmask.all():

ValueError: could not convert string to float: 'Ideal'

assistant:

エラーメッセージを見ると、文字列の特徴量('Ideal')を浮動小数点数に変換できないという問題が起きています。相関行列は数値型の特徴量間のみ計算できるため、該当するカテゴリ型の特徴量を除外するか数値に変換する必要があります。

以下に、数値型の特徴量だけを用いて相関行列を計算しプロットするサンプルコードを提供します。

import seaborn as sns
import matplotlib.pyplot as plt

# 数値型の特徴量だけを選び出す
df_numeric = df.select_dtypes(include=['float64', 'int64'])

# データの相関行列を作成
corr_matrix = df_numeric.corr()

# ヒートマップをプロット
plt.figure(figsize=(12, 12))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.show()

このコードでは、select_dtypes関数を用いて数値型('float64'と'int64')の特徴量のみを含む新しいデータフレームdf_numericを作成しています。その上で、このデータフレームに対して相関行列を計算し、ヒートマップをプロットしています。

ただし、上記コードはデータセットの全特徴量(df)を対象に相関行列を作成しようとしています。dfが実際にモデル学習に利用したデータかどうか、また、既に前処理(例えば、欠損値処理やカテゴリ変数のエンコーディング)が完了しているかどうかによりますが、適宜修正してお使いください。


user:

ありがとうございました。動作しました。

output_19_0.png

続いて、第3のコード(シャプレイ値)について、次のエラーが得られました。

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[17], line 2
      1 # シャプレイ値をプロット
----> 2 interpret_model(tuned_model)

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:1824, in interpret_model(estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   1727 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
   1728 def interpret_model(
   1729     estimator,
   (...)
   1737     **kwargs,
   1738 ):
   1739     """
   1740     This function takes a trained model object and returns an interpretation plot
   1741     based on the test / hold-out set.
   (...)
   1821 
   1822     """
-> 1824     return _CURRENT_EXPERIMENT.interpret_model(
   1825         estimator=estimator,
   1826         plot=plot,
   1827         feature=feature,
   1828         observation=observation,
   1829         use_train_data=use_train_data,
   1830         X_new_sample=X_new_sample,
   1831         y_new_sample=y_new_sample,
   1832         save=save,
   1833         **kwargs,
   1834     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:2122, in RegressionExperiment.interpret_model(self, estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   2025 def interpret_model(
   2026     self,
   2027     estimator,
   (...)
   2035     **kwargs,
   2036 ):
   2037     """
   2038     This function takes a trained model object and returns an interpretation plot
   2039     based on the test / hold-out set.
   (...)
   2119 
   2120     """
-> 2122     return super().interpret_model(
   2123         estimator=estimator,
   2124         plot=plot,
   2125         feature=feature,
   2126         observation=observation,
   2127         use_train_data=use_train_data,
   2128         X_new_sample=X_new_sample,
   2129         y_new_sample=y_new_sample,
   2130         save=save,
   2131         **kwargs,
   2132     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:4051, in _SupervisedExperiment.interpret_model(self, estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   4049 # checking if shap available
   4050 if plot in ["summary", "correlation", "reason"]:
-> 4051     _check_soft_dependencies("shap", extra="analysis", severity="error")
   4052     import shap
   4054 # checking if pdpbox is available

File ~/.local/lib/python3.11/site-packages/pycaret/utils/_dependencies.py:152, in _check_soft_dependencies(package, severity, extra, install_name)
    150 if severity == "error":
    151     logger.exception(f"{msg}")
--> 152     raise ModuleNotFoundError(msg)
    153 elif severity == "warning":
    154     logger.warning(f"{msg}")

ModuleNotFoundError: 
'shap' is a soft dependency and not included in the pycaret installation. Please run: `pip install shap` to install.
Alternately, you can install this by running `pip install pycaret[analysis]`

assistant:

エラーメッセージによると、「shap」というモジュールが見つからないという問題が発生しています。

PyCaretのinterpret_model関数でシャプレイ値に基づく結果を描画するためには、'shap'パッケージが必要です。そのため、先にこのパッケージをインストールする必要があります。

以下のコマンドで'shap'パッケージをインストールできます。

!pip install shap

その後、以下のコマンドでモデルのインタープリテーションを行うことができます。

# シャプレイ値をプロット
interpret_model(tuned_model)

なお、'shap'パッケージのインストールは一度だけ行えば十分です。次回以降の実行時には、このパッケージを再度インストールする必要はありません。


user:

ありがとうございます。次のエラーになりました。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 2
      1 # シャプレイ値をプロット
----> 2 interpret_model(tuned_model)

NameError: name 'interpret_model' is not defined

assistant:

このエラーメッセージは、interpret_modelという名前の関数が定義されていない、またはインポートされていないために出ています。

interpret_modelはPyCaretの関数なので、まずPyCaretをインポートする必要があります。次に、PyCaretの中から対象となるモジュール(回帰、分類など)をインポートし、その中にあるinterpret_model関数を使います。

もし、回帰問題を解いている場合は、以下のようにpycaret.regressionからinterpret_model関数をインポートしてください。

from pycaret.regression import interpret_model

分類の場合は、pycaret.classificationからinterpret_model関数をインポートします。

from pycaret.classification import interpret_model

それ以外のタスクの場合は、適切なPyCaretのモジュールを選んでインポートしてください。

そして、以下のようにモデルのインタープリテーションを行います。

interpret_model(tuned_model)

このinterpret_model関数はPyCaretが提供する関数で、引数として学習済みのモデルを受け取り、そのモデルの解釈を行います。具体的には、モデルの特徴量重要度やパーシャル依存プロットなどを作成します。

ただし、各モデルのインタープリテーション方法はモデルによって異なるため、各モデルのドキュメンテーションを参照することをおすすめします。


user:

ありがとうございます。次のエラーになりました。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 2
      1 # シャプレイ値をプロット
----> 2 interpret_model(tuned_model)

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:1824, in interpret_model(estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   1727 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
   1728 def interpret_model(
   1729     estimator,
   (...)
   1737     **kwargs,
   1738 ):
   1739     """
   1740     This function takes a trained model object and returns an interpretation plot
   1741     based on the test / hold-out set.
   (...)
   1821 
   1822     """
-> 1824     return _CURRENT_EXPERIMENT.interpret_model(
   1825         estimator=estimator,
   1826         plot=plot,
   1827         feature=feature,
   1828         observation=observation,
   1829         use_train_data=use_train_data,
   1830         X_new_sample=X_new_sample,
   1831         y_new_sample=y_new_sample,
   1832         save=save,
   1833         **kwargs,
   1834     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:2122, in RegressionExperiment.interpret_model(self, estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   2025 def interpret_model(
   2026     self,
   2027     estimator,
   (...)
   2035     **kwargs,
   2036 ):
   2037     """
   2038     This function takes a trained model object and returns an interpretation plot
   2039     based on the test / hold-out set.
   (...)
   2119 
   2120     """
-> 2122     return super().interpret_model(
   2123         estimator=estimator,
   2124         plot=plot,
   2125         feature=feature,
   2126         observation=observation,
   2127         use_train_data=use_train_data,
   2128         X_new_sample=X_new_sample,
   2129         y_new_sample=y_new_sample,
   2130         save=save,
   2131         **kwargs,
   2132     )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:4083, in _SupervisedExperiment.interpret_model(self, estimator, plot, feature, observation, use_train_data, X_new_sample, y_new_sample, save, **kwargs)
   4078 shap_models_ids = set(shap_models.keys())
   4080 if plot in ["summary", "correlation", "reason"] and (
   4081     model_id not in shap_models_ids
   4082 ):
-> 4083     raise TypeError(
   4084         f"This function only supports tree based models for binary classification: {', '.join(shap_models_ids)}."
   4085     )
   4087 # plot type
   4088 allowed_types = ["summary", "correlation", "reason", "pdp", "msa", "pfi"]

TypeError: This function only supports tree based models for binary classification: et, dt, rf, lightgbm.

assistant:

エラーメッセージを見ると、interpret_model関数がサポートしているのは特定の木ベースのモデル('et'、'dt'、'rf'、'lightgbm')だけであるとのことです。

つまり、PyCaretのinterpret_model関数にエラーが発生したのは、あなたが使用したinterpret_model(tuned_model)tuned_modelが上記の木ベースのモデルではなかったからです。

解決策としては、setup関数で作成し、Hyperoptimizationを行ったモデルが上記のいずれか('et'、'dt'、'rf'、'lightgbm')である必要があります。

例えば、ランダムフォレストのモデルを作成しインタープリテーションする場合は以下のようになります:

from pycaret.regression import *

# データセットの設定
exp_reg = setup(data, target = 'Column_name')

# ランダムフォレストのモデル作成
model = create_model('rf') 

# モデルのチューニング
tuned_rf = tune_model(model)

# モデルのインタープリテーション
interpret_model(tuned_rf)

なお、interpret_model 関数は、 木ベースモデル('et'、'dt'、'rf'、'lightgbm')の解釈だけでなく、他の多くのモデルに対しても Partial Dependence Plot (PDP) や MFI などを通じて解釈することが可能です。これらを表示したい場合は、関数に plot = 'pdp' パラメータを渡してみてください。

interpret_model(tuned_rf, plot = 'pdp')

user:

おかしいですね。今回の tuned_model は gbr (Gradient Boosting Regressor) なので木ベースモデルのはずですが、サポートされてないんですね。


assistant:

はい、ご理解の通り、Gradient Boosting Regressor (GBR) も木ベースのモデルの一つです。しかし、現在のところ、PyCaretのinterpret_model関数は特定の木ベースのモデル('et'、'dt'、'rf'、'lightgbm')のみをシャプレイ値の解釈に対応しています。

GBRモデルの解釈を行いたい場合、SHAPライブラリを直接使用してモデルの解釈を行うことが可能です。以下にサンプルコードを提供します。

import shap

# モデルの学習
model = tuned_model.estimator
model.fit(X_train, y_train)

# SHAP値を計算
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)

# SHAP値をプロット
shap.summary_plot(shap_values, X_train)

このコードではまず、PyCaretの学習済みモデル(tuned_model)から実際のモデルインスタンスを取り出し(tuned_model.estimator)、そのモデルを訓練データで学習させます。次に、shap.TreeExplainerを使用してSHAP値を計算し、shap.summary_plot関数でSHAP値をプロットします。

なお、上記のコードではX_trainy_trainを使用していますが、これはそれぞれ訓練データの特徴量とターゲットを表しています。これらの変数はご自身のデータセットに合わせて適切に定義してください。また、SHAPの詳細な使用方法については、公式ドキュメンテーション(https://shap.readthedocs.io/)を参照してください。


user:

ちょっと話は戻りますが、PyCaretでランダムフォレストだけ学習するコードは次のように改変したら実行できました。

from pycaret.regression import *

# データセットの設定
df = get_data("diamond")
exp_reg102 = setup(
    data=df,
    target="Price",
    polynomial_features=True,
    remove_multicollinearity=True,
    multicollinearity_threshold=0.9,
    session_id=123,
)

# ランダムフォレストのモデル作成
model = create_model('rf') 

# モデルのチューニング
tuned_rf = tune_model(model)

# モデルのインタープリテーション
interpret_model(tuned_rf)
Carat Weight Cut Color Clarity Polish Symmetry Report Price
0 1.10 Ideal H SI1 VG EX GIA 5169
1 0.83 Ideal H VS1 ID ID AGSL 3470
2 0.85 Ideal H SI1 EX EX GIA 3183
3 0.91 Ideal E SI1 VG VG GIA 4370
4 0.83 Ideal G SI1 EX EX GIA 3171
  Description Value
0 Session id 123
1 Target Price
2 Target type Regression
3 Original data shape (6000, 8)
4 Transformed data shape (6000, 341)
5 Transformed train set shape (4200, 341)
6 Transformed test set shape (1800, 341)
7 Numeric features 1
8 Categorical features 6
9 Preprocess True
10 Imputation type simple
11 Numeric imputation mean
12 Categorical imputation mode
13 Maximum one-hot encoding 25
14 Encoding method None
15 Polynomial features True
16 Polynomial degree 2
17 Remove multicollinearity True
18 Multicollinearity threshold 0.900000
19 Fold Generator KFold
20 Fold Number 10
21 CPU Jobs -1
22 Use GPU False
23 Log Experiment False
24 Experiment Name reg-default-name
25 USI 4855
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 804.5913 3337737.7394 1826.9477 0.9699 0.0888 0.0622
1 699.3456 1831446.2887 1353.3094 0.9778 0.0756 0.0576
2 816.8831 3652787.4937 1911.2267 0.9674 0.0834 0.0617
3 824.6551 2653613.0294 1628.9914 0.9763 0.0855 0.0602
4 641.6505 1411067.2848 1187.8835 0.9861 0.0803 0.0538
5 668.5710 1460613.2268 1208.5583 0.9832 0.0771 0.0567
6 856.3470 6031359.6356 2455.8827 0.9546 0.0916 0.0655
7 771.4154 2530398.6134 1590.7227 0.9750 0.0796 0.0572
8 686.5251 1540154.8770 1241.0298 0.9851 0.0799 0.0592
9 664.2131 1717824.1705 1310.6579 0.9807 0.0730 0.0544
Mean 743.4197 2616700.2359 1571.5210 0.9756 0.0815 0.0588
Std 75.2907 1365127.1393 383.4344 0.0091 0.0056 0.0035
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 977.5783 4953657.1517 2225.6813 0.9554 0.1096 0.0786
1 912.9531 2708706.5497 1645.8149 0.9671 0.1009 0.0757
2 950.8265 5149827.2276 2269.3231 0.9540 0.1013 0.0745
3 1083.5463 3955522.7655 1988.8496 0.9647 0.1095 0.0803
4 875.9000 2181526.2545 1476.9991 0.9786 0.1017 0.0743
5 839.6355 1982097.1919 1407.8697 0.9771 0.0973 0.0749
6 1069.1776 6734443.1137 2595.0806 0.9494 0.1089 0.0814
7 1020.6591 4281765.7913 2069.2428 0.9577 0.1042 0.0751
8 922.2250 2604694.2623 1613.9065 0.9749 0.1074 0.0820
9 890.1696 2774257.6590 1665.6103 0.9688 0.0957 0.0729
Mean 954.2671 3732649.7967 1895.8378 0.9648 0.1037 0.0770
Std 78.1054 1468703.2478 372.0872 0.0098 0.0048 0.0031
Fitting 10 folds for each of 10 candidates, totalling 100 fits
Original model was better than the tuned model, hence it will be returned. NOTE: The display metrics are for the tuned model (not the original one).

output_27_9.png

このコードでも、df や exp_reg102 がどのようにランダムフォレストに渡されるのか明示されていませんね。個人的には、そこが非常に分かりにくいと思います。


assistant:

はい、理解します。これこそがPyCaretの特徴的なところであり、一方で混乱を招く原因でもあります。

PyCaretは内部で「グローバル環境」のようなものを作り、そこに設定や学習済みモデルを保存します。このため、setupcreate_modelで返される値を特に指定せずとも、後続の処理(tune_modelinterpret_modelなど)でそれらを参照できるようになっています。

これにより、コードが簡潔になりますが、一方で内部の動きがブラックボックス化されるデメリットもあります。また、Jupyter notebookのような環境以外で使うときや、複数のモデルやデータセットを扱うときには注意が必要です。

しかしPyCaretでも、モデルや設定を変数に代入し、それらを引数として明示的に渡すことも可能です。例えば以下のようなコードも動作します:

# データセットの設定
experiment1 = setup(
    data = df, 
    target = 'Price', 
    session_id=42)

# ランダムフォレストのモデル作成
model = create_model('rf', experiment_name=experiment1) 

# モデルのチューニング
tuned_rf = tune_model(model, experiment_name=experiment1)

# モデルのインタープリテーション
interpret_model(tuned_rf, experiment_name=experiment1)

これで、PyCaretの内部状態を意識せずに複数のエクスペリメントを同時に行うことも可能になります。ただし、create_modeltune_modelexperiemnt_name引数を渡すAPIは現在のところ公式ドキュメンテーションにはないため、将来的に変更される可能性もあります。公式ドキュメンテーションやGitHubのリポジトリ等で最新の情報を確認することをお勧めします。


user:

おお、すごい。よくご存知ですね。

と思ったらエラーになりました。今は使えないのかもしれません。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 8
      2 experiment1 = setup(
      3     data = df, 
      4     target = 'Price', 
      5     session_id=42)
      7 # ランダムフォレストのモデル作成
----> 8 model = create_model('rf', experiment_name=experiment1) 
     10 # モデルのチューニング
     11 tuned_rf = tune_model(model, experiment_name=experiment1)

File ~/.local/lib/python3.11/site-packages/pycaret/utils/generic.py:964, in check_if_global_is_not_none.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    962     if globals_d[name] is None:
    963         raise ValueError(message)
--> 964 return func(*args, **kwargs)

File ~/.local/lib/python3.11/site-packages/pycaret/regression/functional.py:990, in create_model(estimator, fold, round, cross_validation, fit_kwargs, groups, experiment_custom_tags, engine, verbose, return_train_score, **kwargs)
    862 @check_if_global_is_not_none(globals(), _CURRENT_EXPERIMENT_DECORATOR_DICT)
    863 def create_model(
    864     estimator: Union[str, Any],
   (...)
    874     **kwargs,
    875 ):
    876     """
    877     This function trains and evaluates the performance of a given estimator
    878     using cross validation. The output of this function is a score grid with
   (...)
    987 
    988     """
--> 990     return _CURRENT_EXPERIMENT.create_model(
    991         estimator=estimator,
    992         fold=fold,
    993         round=round,
    994         cross_validation=cross_validation,
    995         fit_kwargs=fit_kwargs,
    996         groups=groups,
    997         experiment_custom_tags=experiment_custom_tags,
    998         engine=engine,
    999         verbose=verbose,
   1000         return_train_score=return_train_score,
   1001         **kwargs,
   1002     )

File ~/.local/lib/python3.11/site-packages/pycaret/regression/oop.py:1285, in RegressionExperiment.create_model(self, estimator, fold, round, cross_validation, fit_kwargs, groups, experiment_custom_tags, engine, verbose, return_train_score, **kwargs)
   1282     self._set_engine(estimator=estimator, engine=engine, severity="error")
   1284 try:
-> 1285     return_values = super().create_model(
   1286         estimator=estimator,
   1287         fold=fold,
   1288         round=round,
   1289         cross_validation=cross_validation,
   1290         fit_kwargs=fit_kwargs,
   1291         groups=groups,
   1292         experiment_custom_tags=experiment_custom_tags,
   1293         verbose=verbose,
   1294         return_train_score=return_train_score,
   1295         **kwargs,
   1296     )
   1297 finally:
   1298     if engine is not None:
   1299         # Reset the models back to the default engines

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1766, in _SupervisedExperiment.create_model(self, estimator, fold, round, cross_validation, predict, fit_kwargs, groups, refit, probability_threshold, experiment_custom_tags, verbose, return_train_score, **kwargs)
   1754 # TODO improve error message
   1755 assert not any(
   1756     x
   1757     in (
   (...)
   1764     for x in kwargs
   1765 )
-> 1766 return self._create_model(
   1767     estimator=estimator,
   1768     fold=fold,
   1769     round=round,
   1770     cross_validation=cross_validation,
   1771     predict=predict,
   1772     fit_kwargs=fit_kwargs,
   1773     groups=groups,
   1774     refit=refit,
   1775     probability_threshold=probability_threshold,
   1776     experiment_custom_tags=experiment_custom_tags,
   1777     verbose=verbose,
   1778     return_train_score=return_train_score,
   1779     **kwargs,
   1780 )

File ~/.local/lib/python3.11/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py:1462, in _SupervisedExperiment._create_model(self, estimator, fold, round, cross_validation, predict, fit_kwargs, groups, refit, probability_threshold, experiment_custom_tags, verbose, system, add_to_model_list, X_train_data, y_train_data, metrics, display, model_only, return_train_score, error_score, **kwargs)
   1460     model_args = model_definition.args
   1461     model_args = {**model_args, **kwargs}
-> 1462     model = model_definition.class_def(**model_args)
   1463     full_name = model_definition.name
   1464 else:

TypeError: RandomForestRegressor.__init__() got an unexpected keyword argument 'experiment_name'

assistant:

申し訳ありません。create_modeltune_modelexperiment_nameという引数は存在しないようです。PyCaretは現状、一度に1つのエクスペリメント(setup関数の呼び出し)しかサポートしていないため、複数のエクスペリメントを同時に行うことは難しいです。

一方で、複数のモデルを扱うことは可能で、各モデルを別の変数に代入すれば混乱することなく扱うことができます。例えば次のようなコードが考えられます:

# データセットの設定
exp_reg = setup(data = df, target = 'Price')

# ランダムフォレストのモデル作成
rf = create_model('rf') 

# ランダムフォレストのチューニング
tuned_rf = tune_model(rf)

# ランダムフォレストのインタープリテーション
interpret_model(tuned_rf)

# 勾配ブースティングのモデル作成
gbr = create_model('gbr') 

# 勾配ブースティングのチューニング
tuned_gbr = tune_model(gbr)

# 勾配ブースティングのインタープリテーション(SHAP値は使えないが、他のプロットは作成可能)
interpret_model(tuned_gbr, plot = 'feature') 

このように、別々のモデルを別々の変数に保存しておけば、それぞれ独立に操作することができます。ただし、setupの呼び出しは1つのノートブックまたはスクリプトで1回だけであるべきという制限はあります。

  Description Value
0 Session id 8968
1 Target Price
2 Target type Regression
3 Original data shape (6000, 8)
4 Transformed data shape (6000, 29)
5 Transformed train set shape (4200, 29)
6 Transformed test set shape (1800, 29)
7 Numeric features 1
8 Categorical features 6
9 Preprocess True
10 Imputation type simple
11 Numeric imputation mean
12 Categorical imputation mode
13 Maximum one-hot encoding 25
14 Encoding method None
15 Fold Generator KFold
16 Fold Number 10
17 CPU Jobs -1
18 Use GPU False
19 Log Experiment False
20 Experiment Name reg-default-name
21 USI 4bea
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 619.5924 1283882.6541 1133.0855 0.9856 0.0727 0.0544
1 632.3076 1407673.6571 1186.4542 0.9793 0.0713 0.0524
2 785.9663 3339615.5324 1827.4615 0.9645 0.0814 0.0608
3 798.5456 2599478.8681 1612.2899 0.9713 0.0861 0.0628
4 746.7974 2075590.4082 1440.6909 0.9809 0.0769 0.0571
5 809.7579 4303273.7201 2074.4333 0.9618 0.0792 0.0582
6 743.8083 2017718.4099 1420.4642 0.9778 0.0814 0.0606
7 767.4904 2091810.7124 1446.3093 0.9796 0.0773 0.0585
8 845.9517 2638936.4506 1624.4804 0.9776 0.0824 0.0604
9 716.0066 1875038.7125 1369.3205 0.9831 0.0764 0.0578
Mean 746.6224 2363301.9125 1513.4990 0.9762 0.0785 0.0583
Std 69.7722 862619.1767 269.4861 0.0074 0.0043 0.0030
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 1001.4635 4203922.8921 2050.3470 0.9529 0.1140 0.0847
1 967.4285 2779409.0430 1667.1560 0.9592 0.1106 0.0828
2 1155.2282 5901536.5231 2429.3078 0.9373 0.1186 0.0902
3 1054.5275 3887512.9896 1971.6777 0.9571 0.1252 0.0855
4 1188.6948 5366111.6298 2316.4869 0.9507 0.1235 0.0904
5 1281.2930 9551738.4845 3090.5887 0.9152 0.1297 0.0920
6 1049.1893 4499400.8801 2121.1791 0.9506 0.1199 0.0849
7 1037.3902 3304529.9039 1817.8366 0.9678 0.1131 0.0842
8 1302.1539 7431534.2162 2726.0840 0.9369 0.1357 0.0920
9 1295.0971 9916351.6388 3149.0239 0.9107 0.1396 0.0955
Mean 1133.2466 5684204.8201 2333.9688 0.9438 0.1230 0.0882
Std 121.4553 2387346.7996 486.6154 0.0178 0.0092 0.0041
Fitting 10 folds for each of 10 candidates, totalling 100 fits
Original model was better than the tuned model, hence it will be returned. NOTE: The display metrics are for the tuned model (not the original one).

output_29_8.png

  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 827.5468 1998958.2424 1413.8452 0.9776 0.1027 0.0775
1 803.6802 1944207.2919 1394.3483 0.9715 0.0963 0.0720
2 997.2005 3901144.2659 1975.1315 0.9586 0.1056 0.0796
3 916.0360 2384006.6255 1544.0229 0.9737 0.1050 0.0810
4 979.0954 3463708.1321 1861.1040 0.9682 0.1087 0.0825
5 941.9748 5205195.5722 2281.4898 0.9538 0.0965 0.0708
6 788.6009 1877037.8782 1370.0503 0.9794 0.0943 0.0708
7 898.5028 3048802.5930 1746.0821 0.9703 0.0961 0.0758
8 1006.5477 3988881.4138 1997.2184 0.9662 0.0962 0.0736
9 882.0054 3022900.6836 1738.6491 0.9728 0.1046 0.0784
Mean 904.1191 3083484.2699 1732.1942 0.9692 0.1006 0.0762
Std 74.9457 1027538.0621 288.0758 0.0076 0.0049 0.0041
  MAE MSE RMSE R2 RMSLE MAPE
Fold            
0 666.7819 1413833.5474 1189.0473 0.9842 0.0767 0.0570
1 740.0818 2098378.3140 1448.5780 0.9692 0.0785 0.0571
2 874.4908 2811490.5349 1676.7500 0.9701 0.0830 0.0633
3 788.9210 2223039.3210 1490.9860 0.9755 0.0828 0.0614
4 828.2061 2675233.8554 1635.6142 0.9754 0.0781 0.0574
5 867.5114 5659477.2522 2378.9656 0.9498 0.0825 0.0584
6 775.6896 2260189.7450 1503.3927 0.9752 0.0808 0.0597
7 775.3884 2374232.7884 1540.8546 0.9768 0.0758 0.0571
8 875.6668 3313364.5127 1820.2650 0.9719 0.0790 0.0584
9 640.6530 1422680.1866 1192.7616 0.9872 0.0717 0.0545
Mean 783.3391 2625192.0057 1587.7215 0.9735 0.0789 0.0585
Std 78.6766 1151805.9863 323.0053 0.0096 0.0034 0.0024
Fitting 10 folds for each of 10 candidates, totalling 100 fits

user:

なるほど。最後の interpret_model(tuned_gbr, plot = 'feature') だけはエラー終了しましたが、それ以外は実行できました。


assistant:

interpret_model(tuned_gbr, plot = 'feature')がエラーになった原因としては、勾配ブースティングモデル ('gbr') の機能重要度プロットに関する問題が考えられます。plot='feature'とすると、モデルの特徴量の重要度を描画するという意味になるのですが、'gbr'がこれに対応していないか、PyCaretに用意されていないかと考えられます。

代替としては、モデル自体に特徴量の重要度を算出するメソッドが多く用意されているため、直接それを利用します。以下にサンプルコードを記載します。

import matplotlib.pyplot as plt
import numpy as np

# 特徴量重要度の取得
importances = tuned_gbr.feature_importances_

# ソートとプロットの準備
indices = np.argsort(importances)
features = df.columns  # 特徴量名が入っている配列を指定

# プロット
plt.figure(figsize=(8, 8))
plt.title('Feature Importances')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [features[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()

output_30_1.png

なおこのコードでは、tuned_gbr.feature_importances_でモデルの特徴量重要度を取得し、それを降順にソートしてプロットしています。特徴量名を取得するためにfeaturesという変数を用意していますが、これはあなたのデータフレームの列名を指定してください(私の例ではdf.columnsとしました)。また、dfは訓練したモデルの特徴量に対応するデータフレームです。これを適切に置き換えてください。


user:

ありがとうございます。個人的には、PyCaret の長所は、さまざまな種類のモデルを一気に試せるところにあると思うので、どのモデルが最適なのか目星がついたら、そこから先(さらなるファインチューニングや可視化など)はPyCaretに拘る必要ないと思ってます。


assistant:

その考え方はとても合理的で、たしかにPyCaretの強みの一つは、短時間で多数のモデルを試し、初期のハイパーパラメーターチューニングが可能であることです。これにより、データに対する最適なモデルの候補を素早く見つけ出すことができます。

その後のプロセス、例えば、詳細なハイパーパラメーターチューニングや進んだ特徴エンジニアリング、モデルの解釈といった部分では、Scikit-learnやXGBoost、LightGBM、CatBoostといったライブラリを直接使用し、より細かい設定や操作を行うことが一般的です。

したがって、PyCaretを素早く多数のモデルを試すツールとして位置づけ、より高度な操作には他のツールを使うというのは、実践的で効率的なアプローチと言えるでしょう。


user:

とはいえ、私も知らなかったことがたくさんありましたので、色々とありがとうございます。ここまで、

  1. データの前処理: パイプラインの一部として、欠損値の処理、カテゴリ変数の処理、特徴量の尺度変換など、あらかじめ定義されたデータ前処理タスクを自動で行うことができます。

  2. 特徴量エンジニアリング:PyCaretは、特徴量の生成や変換、特徴量の相互作用、多項式特徴量の作成などのタスクを容易に行うことができます。

  3. 特徴量選択:不要な特徴量の自動除去や重要な特徴量の選択といったタスクもサポートしています。

  4. モデル選択とハイパーパラメータチューニング:たくさんの学習器(アルゴリズム)を簡単に試し、それぞれのパフォーマンスを比較できます。さらに、選んだ学習器の最適なパラメータを探すためのハイパーパラメータチューニングもサポートされています。

  5. アンサンブル学習:スタッキングやブレンディングといったアンサンブル学習手法もサポートしており、パフォーマンスを追求するために複数のモデルを組み合わせることが可能です。

  6. モデル解釈:各種のグラフィカルなツール(特徴量の重要度、相関マップ、シャプレイ値など)を使ってモデルの結果を解釈することができます。

まで教えていただきましたが、最後に

  1. デプロイ: 構築したモデルをクラウド情報パネルに簡単にデプロイできます。

について教えていただけますか?


assistant:

はい、もちろんです。

PyCaretでは、構築した機械学習モデルをクラウドに簡単にデプロイする機能を提供しています。具体的には、AWS S3バケットやMicrosoft AzureのBlob Storageにモデルを保存し、その後、それを使用して新たなデータに対する予測を行うことができます。

モデルの保存と読み込みは下記のようなコマンドで行えます:

# モデルの保存
save_model(tuned_rf, 'rf_model')

# 保存したモデルの読み込み
loaded_rf = load_model('rf_model')

# 読み込んだモデルで新たなデータに対する予測を行う
predictions = predict_model(loaded_rf, data = unseen_data)

ここでunseen_dataは予測を行いたい新たなデータです。この新たなデータは、学習時と同じカラムを持つ必要があります。

なお、save_modelおよびload_modelのパスはローカルパスであり、クラウドデプロイを実現するにはそれぞれのクラウド上にあらかじめモデルをアップロードし、必要なときにダウンロードして利用する、といった工程が必要になります。

ただし、現在はPyCaretの直接的なクラウドデプロイ機能は開発中で、その詳細な情報は公式のGitHubレポジトリ等で最新の情報を確認していただくのが良いと思います。なお、PyCaretの機能ではないですが、Pythonを通じてAWSやAzure、GCPといったクラウドのAPIを操作することで、モデルのアップロードやダウンロードをプログラムから行うことが可能です。


user:

何が保存されたの確認したら、pickleファイルとlogファイルですね。


assistant:

はい、正確には、save_model関数はモデルをpickle形式(.pkl)で保存します。このpickleファイルには学習済みのモデルの情報およびトランスフォーメーションパイプラインが含まれています。

load_model関数を使うことで、保存したモデルを簡単に読み込み、新たなデータに対する予測を行うことができます。これは例えば、APIを通じて予測結果を提供したり、大量のデータに対するバッチ処理に用いることができます。

それと一緒に保存される.logファイルには、モデルの学習やパラメータチューニング等、一連の処理に関する詳細なログが記録されています。これらを用いることで、再現性の確保やエラー検証、モデルの挙動の理解などに役立てることができます。


user:

一通り、PyCaret について学ぶことができました。ありがとうございました!


assistant:

どういたしまして!お手伝いできてうれしいです。

多くのデータ分析や機械学習の作業を自動化し、効率を上げることができるPyCaretについて学べたとのことでとても良かったです。PyCaretは直感的な使いやすさと、広範囲の機能を提供しているため、さまざまなデータサイエンスプロジェクトに有用です。

これからもプロジェクトに活かせるよう、定期的に最新の情報をチェックしてみてください。PyCaretの公式ドキュメンテーションやGitHubリポジトリ、さらにデータサイエンス関連の掲示板やブログ記事などは素晴らしいリソースになります。

何か他の質問がありましたら、遠慮なくお問い合わせください。さらなる成功を祈っております!


user:

今回の議論を受けて、PyCaret のテーマソングを作詞してください。


assistant:

それは面白い企画ですね!詩的な自由を許していただけると仮定して、以下のように考えてみました。

(Verse 1)
コードを書くスキル、そんじょそこらに溢れていて、
でも深い洞察へと導くものは何だろう?
目の前に広がるデータの海、その深淵を照らすは何か?
それはPyCaret、君の新たな登竜門。

(Chorus)
PyCaret光を放つ、データ分析の道を照らす。
前処理から学習、チューニングまで、全てを網羅。
深淵の闇を払い、洞察へと導く光源になるだろう。

(Verse 2)
データのフロウが必ずしもスムーズでないとき。
時には途方に暮れ、何をすべきか分からなくなる。
PyCaretが仲間となり、君を支えるだろう。
一緒に道を切り開き、混乱を前進へと変えよう。

(Chorus)
PyCaret、君の信頼の矢、問題を解決へと導く。
膨大な選択肢から最適な道を見つけ出し、
手間から解放し、思考の時間を増やすだろう。

(Bridge)
さあ、広大な知識の海を旅するがいい。
ともに、見るべき洞察を求めて、その先へ。

(Chorus)
PyCaret、君のリーダー、理解への道を示す。
周到な計画と機能性を組み合わせ、
遥かなる洞察の追求を可能に。来たれ、明るい未来へ。

(END)
一緒に、PyCaretの力で世界を変えよう!

注: 実際のメロディは存在しないため、自由に旋律を想像していただければと思います。


1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2