機械学習用のPythonでデータプリプロセスを初めて導入しました。私はいくつかのデータを前処理しようとしています。データは、いくつかの空とNaNエントリを持つ多くのカテゴリ変数で構成されています。私は、ffilメソッドを使用して空またはNaNスペースを塗りつぶし、次にSklearn LabelEncoderを使用してラベルエンコードを行い、後でワンホットエンコーディングを行います。私は2つの異なるコードセグメントを持っています。 最初のコードセクションを:TypeError:余分な操作を追加すると 'str'と 'float'のインスタンス間で '>'がサポートされません
encoder = LabelEncoder()
for cols in train.keys():
if(cols.startswith('cat')):
train[cols].fillna(method='ffill', inplace=True)
train[cols].fillna(method='bfill', inplace=True)
if train[cols].dtype == 'object':
train[cols] = encoder.fit_transform(train[cols])
train = pd.get_dummies(data=train, columns=[cols])
2番目のコードセクション:
を最初の1は、ラベルエンコーディングをやっても何のエラーを与えていないが、いくつかaddintionalプロセスを有する第2のケースでは、私は上記のエラーを取得していますどこ少し単純ですencoder = LabelEncoder()
best_fit = SelectKBest(score_func=chi2, k=10)
for cols in train.keys():
if(cols.startswith('cat')):
train[cols].fillna(method='ffill', inplace=True)
train[cols].fillna(method='bfill', inplace=True)
if train[cols].dtype == 'object':
train[cols] = encoder.fit_transform(train[cols])
train_temp = pd.get_dummies(data=train, columns=[cols])
temp_df = train_temp[list(set(train_temp.keys())-set(train.keys()))]
fit_temp = best_fit.fit(temp_df, target)
features_temp = fit_temp.transform(temp_df)
train = train.drop([cols], axis=1)
train = pd.concat([train, pd.DataFrame(features_temp)], axis = 1, join='outer')
トレースバックログ:
Traceback (most recent call last):
File "<ipython-input-1-4efe4593ba69>", line 37, in <module>
train[cols] = encoder.fit_transform(train[cols])
File "C:\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 112, in fit_transform
self.classes_, y = np.unique(y, return_inverse=True)
File "C:\Continuum\anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 211, in unique
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: '>' not supported between instances of 'str' and 'float'
解決される問題: は、内部エラーのように見えます。マシンを再起動してスクリプトを再実行すると、問題は解決しました。
あなたが質問のタイトルに入れて、エラーメッセージについて尋ねている場合は、問題の完全なトレースバックが含まれ、また、それが参照するあなたが投稿したソース行のかを示してください。 – Duncan
@Duncanがログを追加しました。修正をありがとう。 –