IndexError Traceback (most recent call last) Cell In[125], line 16 13 predicted_class = np.argmax(class_probabilities) 15 # 予測確率を特徴量として追加 —> 16 X_test_with_nb_pred_proba[f’nb_pred_proba_{best_model.classes_[i]}’] = class_probabilities File /home/share/temp/venv/lib/python3.8/site-packages/scipy/sparse/_index.py:97, in IndexMixin.__setitem__(self, key, x) 96 def __setitem__(self, key, x): —> 97 row, col = self._validate_indices(key) 99 if isinstance(row, INT_TYPES) and isinstance(col, INT_TYPES): 100 x = np.asarray(x, dtype=self.dtype) File /home/share/temp/venv/lib/python3.8/site-packages/scipy/sparse/_index.py:159, in IndexMixin._validate_indices(self, key) 157 row += M 158 elif not isinstance(row, slice): –> 159 row = self._asindices(row, M) 161 if isintlike(col): 162 col = int(col) File /home/share/temp/venv/lib/python3.8/site-packages/scipy/sparse/_index.py:183, in IndexMixin._asindices(self, idx, length) 180 raise IndexError(‘invalid index’) from e 182 if x.ndim not in (1, 2): –> 183 raise IndexError(‘Index dimension must be 1 or 2’) 185 if x.size == 0: 186 return x IndexError: Index dimension must be 1 or 2

このエラーは、X_test_with_nb_pred_proba のインデックス指定が間違っている可能性があります。X_test_with_nb_pred_proba はスパース行列(sparse matrix)である可能性がありますが、スパース行列へのインデックス指定は特定の方法で行う必要があります。

もし X_test_with_nb_pred_proba がスパース行列である場合、行列の要素に直接インデックスを指定することはできません。スパース行列の要素にアクセスするためには、行と列のインデックスを指定して要素を取得する必要があります。

例えば、行 i と列 j の要素にアクセスする場合、X_test_with_nb_pred_proba[i, j] のように指定します。エラーメッセージにあるように、インデックスの次元は1または2でなければなりません。このエラーを修正するためには、正しいインデックス指定方法を使って X_test_with_nb_pred_proba にアクセスする必要があります。

未分類

Posted by ぼっち