Q&A
機械学習に置ける学習データが不均衡データのため、RandomUnderSamplerでアンダーサンプリングを実行したいのですが、
以下の様にエラーが吐き出され、原因がつかめません。
0. Xはpandas.Dataframe。 カラムはint, float, objectがあり、どのタイプのカラムにもNaNが存在する。
- Xに何も処理を行わずRandomUnderSamplerを実行した場合は問題なく実行できる。
- objectカラムのカテゴリカルデータをsklearn.LabelEncoderで整数に変換したXにRandomUnderSamplerを実行すると下記エラーが出る。
- X[col].fillna(-999, inplace=True)などでNaNを除去するとRandomUnderSamplerを実行できる。
- 自分で新たに作成した特徴量が入ると再び下記エラーが吐き出される(NaNは除去済み)。
最初は問題なくRandomUnderSamplerが実行できるのに、
カテゴリカルデータを整数値にした途端にエラーに。
エラーメッセージではfloatの値がおかしいとありますが、float型のカラムには
処理を加えていないため、解せません。
大変恐縮ですが、どなたか解決策お分かりになる方おられましたら
何卒よろしくお願いします。
python
1from imblearn.under_sampling import RandomUnderSampler 2 3sampler = RandomUnderSampler(random_state=42) 4# downsampling 5X_resampled, y_resampled = sampler.fit_resample(X, y) 6 7--------------------------------------------------------------------------- 8ValueError Traceback (most recent call last) 9<ipython-input-12-0aa21c497588> in <module> 10 3 sampler = RandomUnderSampler(random_state=42) 11 4 # downsampling 12----> 5 X_resampled, y_resampled = sampler.fit_resample(X, y) 13 14/opt/conda/lib/python3.6/site-packages/imblearn/base.py in fit_resample(self, X, y) 15 77 16 78 check_classification_targets(y) 17---> 79 X, y, binarize_y = self._check_X_y(X, y) 18 80 19 81 self.sampling_strategy_ = check_sampling_strategy( 20 21/opt/conda/lib/python3.6/site-packages/imblearn/under_sampling/_prototype_selection/_random_under_sampler.py in _check_X_y(X, y) 22 99 def _check_X_y(X, y): 23 100 y, binarize_y = check_target_type(y, indicate_one_vs_all=True) 24--> 101 X = check_array(X, accept_sparse=['csr', 'csc'], dtype=None) 25 102 y = check_array(y, accept_sparse=['csr', 'csc'], dtype=None, 26 103 ensure_2d=False) 27 28/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 29 540 if force_all_finite: 30 541 _assert_all_finite(array, 31--> 542 allow_nan=force_all_finite == 'allow-nan') 32 543 33 544 if ensure_min_samples > 0: 34 35/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in _assert_all_finite(X, allow_nan) 36 54 not allow_nan and not np.isfinite(X).all()): 37 55 type_err = 'infinity' if allow_nan else 'NaN, infinity' 38---> 56 raise ValueError(msg_err.format(type_err, X.dtype)) 39 57 # for object dtype data, we only check for NaNs (GH-13254) 40 58 elif X.dtype == np.dtype('object') and not allow_nan: 41 42ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). 43
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/08/08 12:20