実現したいこと
pythonのライブラリであるNGboostをつかって簡単な回帰問題が解けるようになる
発生している問題・分からないこと
import numpy as np
from ngboost import NGBRegressor
from ngboost.distns import Normal
例:学習データ
X_train = np.random.randn(100,10) # 100サンプル、10特徴量
y_train = np.random.randn(100)
print(X_train.shape)
print(y_train.shape)
モデルの作成と学習
モデル作成
model = NGBRegressor(
n_estimators=200, # 決定木の数
learning_rate=0.01, # 学習率
#max_depth=5, # 決定木の最大深さ
Dist=Normal, # 出力分布(ここではNormal分布)
random_state=42, # 再現性のための乱数シード
verbose=True # 学習過程を表示
)
model.fit(X_train, y_train)
予測
X_test = np.random.randn(20, 10) # テストデータ 20サンプル
y_pred = model.predict(X_test)
以上のランダムデータでNGboostを使った予測メソッドまで使おうとしたのですが、データの配列に関するエラーが出たのですが、修正方法がわかりません。
エラーメッセージ
error
1(100, 10) 2(100,) 3Traceback (most recent call last): 4 File "c:\Users\UchinoYuta\Downloads\NGboost_プログラム\test.py", line 61, in <module> 5 model.fit(X_train, y_train) 6 ~~~~~~~~~^^^^^^^^^^^^^^^^^^ 7 File "C:\Python\Python313\Lib\site-packages\ngboost\ngboost.py", line 275, in fit 8 grads = D.grad(Y_batch, natural=self.natural_gradient) 9 File "C:\Python\Python313\Lib\site-packages\ngboost\scores.py", line 12, in grad 10 grad = np.linalg.solve(metric, grad) 11 File "C:\Python\Python313\Lib\site-packages\numpy\linalg\_linalg.py", line 410, in solve 12 r = gufunc(a, b, signature=signature) 13ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 100 is different from 2)
該当のソースコード
import numpy as np from ngboost import NGBRegressor from ngboost.distns import Normal # 例:学習データ X_train = np.random.randn(100,10) # 100サンプル、10特徴量 y_train = np.random.randn(100) print(X_train.shape) print(y_train.shape) # モデルの作成と学習 # モデル作成 model = NGBRegressor( n_estimators=200, # 決定木の数 learning_rate=0.01, # 学習率 #max_depth=5, # 決定木の最大深さ Dist=Normal, # 出力分布(ここではNormal分布) random_state=42, # 再現性のための乱数シード verbose=True # 学習過程を表示 ) model.fit(X_train, y_train) # 予測 X_test = np.random.randn(20, 10) # テストデータ 20サンプル y_pred = model.predict(X_test) のmodel.fit(X_train, y_train)の部分でエラー
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
chatgptにエラー修正を頼んだところ、配列y_trainの次元が2次元になっている可能性があるので1次元にしてくださいという指示があったので、試したところダメでした。なお、次元はもともと1次元になってました。ライブラリのバージョンはngboost 0.3.12で最新だと思います。
補足
そもそも使っているライブラリがよくないなどあれば教えていただきたいです。最終的にはNGboostをPV発電量予測に利用したいと考えています。

回答1件
あなたの回答
tips
プレビュー