2

我有下面的一段代码,我正在尝试使用一个热编码器。但是我得到了errorValueError:一个系列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

 from sklearn.preprocessing import LabelEncoder, OneHotEncoder
 import pandas as pd

 target=train_features_df['y']
 train_features_df=train_features_df.drop(['y'], axis=1)

 # Categorical boolean mask this is done to find all categorical dfeature
 categorical_feature_mask = train_features_df.dtypes==object
 # filter categorical columns using mask and turn it into a list
 categorical_cols = train_features_df.columns[categorical_feature_mask].tolist()

 # instantiate labelencoder object
 le = LabelEncoder()
 # apply le on categorical feature columns
 train_features_df[categorical_cols] = train_features_df[categorical_cols].apply(lambda col: 
 le.fit_transform(col))
 train_features_df[categorical_cols].head(10)

 # instantiate OneHotEncoder
 ohe = OneHotEncoder(categories = categorical_feature_mask, sparse=False ) 
 # categorical_features = boolean mask for categorical columns
 # sparse = False output an array not sparse matrix

 # apply OneHotEncoder on categorical feature columns
 ohe.fit_transform(train_features_df)

我在最后一行收到此错误“ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。在线的ohe.fit_transform(train_features_df)

要求的完整回溯消息如下:-

   ---------------------------------------------------------------------------
   ValueError                                Traceback (most recent call last)
   <ipython-input-12-72e45bd93f15> in <module>
        23 
        24 # apply OneHotEncoder on categorical feature columns
   ---> 25 ohe.fit_transform(train_features_df)
        26 #train_encoded_df=pd.DataFrame(data = ohe.fit_transform(train_features_df)) # It returns an numpy array
   
   ~\Anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py in fit_transform(self, X, y)
       408         """
       409         self._validate_keywords()
   --> 410         return super().fit_transform(X, y)
       411 
       412     def transform(self, X):
   
   ~\Anaconda3\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params)
       688         if y is None:
       689             # fit method of arity 1 (unsupervised transformation)
   --> 690             return self.fit(X, **fit_params).transform(X)
       691         else:
       692             # fit method of arity 2 (supervised transformation)
   
   ~\Anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py in fit(self, X, y)
       383         """
       384         self._validate_keywords()
   --> 385         self._fit(X, handle_unknown=self.handle_unknown)
       386         self.drop_idx_ = self._compute_drop_idx()
       387         return self
   
   ~\Anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py in _fit(self, X, handle_unknown)
        74         X_list, n_samples, n_features = self._check_X(X)
        75 
   ---> 76         if self.categories != 'auto':
        77             if len(self.categories) != n_features:
        78                 raise ValueError("Shape mismatch: if categories is an array,"
   
   ~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
      1477     def __nonzero__(self):
      1478         raise ValueError(
   -> 1479             f"The truth value of a {type(self).__name__} is ambiguous. "
      1480             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
      1481         )
   
   ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

​</p>

4

1 回答 1

2

不可战胜,

该错误是由于您传入的categories参数是编码器函数不期望的。如果您只想使用选择来选择分类列,请执行以下操作:

ohe = OneHotEncoder(categories = 'auto', sparse=False ) 
selection = train_features_df[train_features_df.columns[categorical_feature_mask]]
encoded = ohe.fit_transform(selection)

然后将编码结果与非分类列合并

如果您想使用categories参数来传递类别值 - 使用此处的示例

更优雅的是使用 Pandas 函数进行 one-hot 编码:

pd.get_dummies(data=train_features_df, columns=train_features_df.columns[categorical_feature_mask])
于 2020-08-01T04:11:33.343 回答