当前位置:   article > 正文

sklearn使用FeatureHasher处理字符串特征: AttributeError: ‘str‘ object has no attribute ‘items‘_sklearn featurehasher

sklearn featurehasher

sklearn使用FeatureHasher处理字符串特征: AttributeError: 'str' object has no attribute 'items'

目录

sklearn使用FeatureHasher处理字符串特征: AttributeError: 'str' object has no attribute 'items'

问题

解决

完整错误日志


问题

# 因为没有指定input_type参数

input_type{“dict”, “pair”, “string”}, default=”dict”

# input_type默认为字典形式;

  1. from sklearn.feature_extraction import FeatureHasher
  2. h = FeatureHasher(n_features=10)
  3. # h = FeatureHasher(n_features=3,input_type='string')
  4. D = ['data','preprocessing','feature','model','fine-tuning','deploy']
  5. df = pd.Series(D)
  6. f = h.transform(df)
  7. f.toarray()

解决

# input_type='string'

  1. from sklearn.feature_extraction import FeatureHasher
  2. h = FeatureHasher(n_features=3,input_type='string')
  3. D = ['data','preprocessing','feature','model','fine-tuning','deploy']
  4. df = pd.Series(D)
  5. f = h.transform(df)
  6. f.toarray()
  1. array([[-1., 1., 2.],
  2. [-6., 5., -2.],
  3. [-1., 4., 2.],
  4. [ 0., 4., 1.],
  5. [-6., 1., 0.],
  6. [-1., 4., 1.]])

完整错误日志

  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. <ipython-input-198-d11645c06978> in <module>
  4. 4 D = ['data','preprocessing','feature','model','fine-tuning','deploy']
  5. 5 df = pd.Series(D)
  6. ----> 6 f = h.transform(df)
  7. 7 f.toarray()
  8. D:\anaconda\lib\site-packages\sklearn\feature_extraction\_hash.py in transform(self, raw_X)
  9. 158 indices, indptr, values = \
  10. 159 _hashing_transform(raw_X, self.n_features, self.dtype,
  11. --> 160 self.alternate_sign, seed=0)
  12. 161 n_samples = indptr.shape[0] - 1
  13. 162
  14. sklearn\feature_extraction\_hashing_fast.pyx in sklearn.feature_extraction._hashing_fast.transform()
  15. D:\anaconda\lib\site-packages\sklearn\feature_extraction\_hash.py in <genexpr>(.0)
  16. 153 raw_X = iter(raw_X)
  17. 154 if self.input_type == "dict":
  18. --> 155 raw_X = (_iteritems(d) for d in raw_X)
  19. 156 elif self.input_type == "string":
  20. 157 raw_X = (((f, 1) for f in x) for x in raw_X)
  21. D:\anaconda\lib\site-packages\sklearn\feature_extraction\_hash.py in _iteritems(d)
  22. 23 def _iteritems(d):
  23. 24 """Like d.iteritems, but accepts any collections.Mapping."""
  24. ---> 25 return d.iteritems() if hasattr(d, "iteritems") else d.items()
  25. 26
  26. 27
  27. AttributeError: 'str' object has no attribute 'items'

参考:sklearn

参考:FeatureHasher

参考:How to use sklearn FeatureHasher?

参考:AttributeError: 'str' object has no attribute 'items'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/988761
推荐阅读
相关标签
  

闽ICP备14008679号