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

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sklearn使用FeatureHasher处理字符串特征: AttributeError: ‘str‘ object has no attribute ‘items‘相关的知识,希望对你有一定的参考价值。

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默认为字典形式;

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

解决

# input_type='string'

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

完整错误日志

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-198-d11645c06978> in <module>
      4 D = ['data','preprocessing','feature','model','fine-tuning','deploy']
      5 df = pd.Series(D)
----> 6 f = h.transform(df)
      7 f.toarray()

D:\\anaconda\\lib\\site-packages\\sklearn\\feature_extraction\\_hash.py in transform(self, raw_X)
    158         indices, indptr, values = \\
    159             _hashing_transform(raw_X, self.n_features, self.dtype,
--> 160                                self.alternate_sign, seed=0)
    161         n_samples = indptr.shape[0] - 1
    162 

sklearn\\feature_extraction\\_hashing_fast.pyx in sklearn.feature_extraction._hashing_fast.transform()

D:\\anaconda\\lib\\site-packages\\sklearn\\feature_extraction\\_hash.py in <genexpr>(.0)
    153         raw_X = iter(raw_X)
    154         if self.input_type == "dict":
--> 155             raw_X = (_iteritems(d) for d in raw_X)
    156         elif self.input_type == "string":
    157             raw_X = (((f, 1) for f in x) for x in raw_X)

D:\\anaconda\\lib\\site-packages\\sklearn\\feature_extraction\\_hash.py in _iteritems(d)
     23 def _iteritems(d):
     24     """Like d.iteritems, but accepts any collections.Mapping."""
---> 25     return d.iteritems() if hasattr(d, "iteritems") else d.items()
     26 
     27 

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

参考:sklearn

参考:FeatureHasher

参考:How to use sklearn FeatureHasher?

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

以上是关于sklearn使用FeatureHasher处理字符串特征: AttributeError: ‘str‘ object has no attribute ‘items‘的主要内容,如果未能解决你的问题,请参考以下文章

sklearn FeatureHasher中的哈希技巧

特征抽取: sklearn.feature_extraction.FeatureHasher

了解 FeatureHasher、碰撞和向量大小的权衡

FeatureHasher使用方法详解

FeatureHasher使用方法详解

如何解决此 ValueError:仅接受 2 个非关键字参数 sklearn python