————————————————————————— AxisError Traceback (most recent call last) Cell In[142], line 112 110 print(classification_report(y_test, y_pred_rf)) 111 # AUCの計算 –> 112 auc_rf = roc_auc_score(y_test, y_pred_rf, multi_class=’ovr’) # 多クラスの場合は’multi_class’を指定 113 print(“Random Forest AUC:”, auc_rf) File /home/share/temp/venv/lib/python3.8/site-packages/sklearn/utils/_param_validation.py:214, in validate_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs) 208 try: 209 with config_context( 210 skip_parameter_validation=( 211 prefer_skip_nested_validation or global_skip_validation 212 ) 213 ): –> 214 return func(*args, **kwargs) 215 except InvalidParameterError as e: 216 # When the function is just a wrapper around an estimator, we allow 217 # the function to delegate validation to the estimator, but we replace 218 # the name of the estimator by the name of the function in the error 219 # message to avoid confusion. 220 msg = re.sub( 221 r”parameter of w+ must be”, 222 f”parameter of {func.__qualname__} must be”, 223 str(e), … 47 def _sum(a, axis=None, dtype=None, out=None, keepdims=False, 48 initial=_NoValue, where=True): —> 49 return umr_sum(a, axis, dtype, out, keepdims, initial, where) AxisError: axis 1 is out of bounds for array of dimension 1 Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…

エラーが発生しました。roc_auc_score関数に渡すy_pred_rfの次元が正しくない可能性があります。このエラーは、y_pred_rfが1次元の配列であり、指定された軸(axis 1)がその配列の範囲外であることを示しています。

roc_auc_score関数に渡すy_pred_rfは、各サンプルに対するクラスの予測確率が必要ですが、現在はクラスの予測ではなく、単純なクラスの予測値を含んでいます。したがって、まずRandomForestClassifierpredict_probaメソッドを使用して予測確率を取得する必要があります。

以下は、修正したコードです。

python
from sklearn.metrics import roc_auc_score # テストデータでの予測確率を取得 y_proba_rf = rf_classifier.predict_proba(X_rf) # AUCの計算 auc_rf = roc_auc_score(y_test, y_proba_rf, multi_class='ovr') # 多クラスの場合は'multi_class'を指定 print("Random Forest AUC:", auc_rf)

この修正により、各サンプルに対するクラスの予測確率を取得し、それを使用してAUCを計算します。

未分類

Posted by ぼっち