【发布时间】:2021-04-09 22:12:48
【问题描述】:
不浪费时间,直奔问题。 我实际上是在 Python 中使用 sklearn.SimpleImputer 来计算我的 DataSet。 但是我的 DataSet 包含一些带有整数的列和一些带有其他字母点的列。所以,我使用 Median 来填充空白空间,我只想为我的特定列使用整数,而不是整个 DataSet。 我试过这个:
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy="median")
imputer.fit(students['age'], ['sex'], ['failures'])
我只想对这些只有整数值而不是所有数据集的列进行插补,因为所有数据集都包含带有 alphbets 数据点的列,其 Median 不能被采用。
从上面的代码中,我得到了这个错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2894 try:
-> 2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('age', 'sex', 'failures')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-26-8961e0ce249f> in <module>
2 from sklearn.impute import SimpleImputer
3 imputer = SimpleImputer(strategy="median")
----> 4 imputer.fit(students['age', 'sex', 'failures'])
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2900 if self.columns.nlevels > 1:
2901 return self._getitem_multilevel(key)
-> 2902 indexer = self.columns.get_loc(key)
2903 if is_integer(indexer):
2904 indexer = [indexer]
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899 if tolerance is not None:
KeyError: ('age', 'sex', 'failures')
数据的链接是https://archive.ics.uci.edu/ml/machine-learning-databases/00320/
谢谢!希望您理解问题所在,我已尽力解释。
【问题讨论】:
-
你需要通过
numpy.ndarray而不是pandas series or column -
嗨 ashraful16,请再描述一下。
标签: python pandas dataframe machine-learning scikit-learn