One Hot Encoding 和 pandas.categorical.code 有什么区别
What is difference between One Hot Encoding and pandas.categorical.code
我正在处理一些问题,有如下疑问:
在数据集中有一个文本列具有以下唯一值:
array(['1 bath', 'na', '1 shared bath', '1.5 baths', '1 private bath',
'2 baths', '1.5 shared baths', '3 baths', 'Half-bath',
'2 shared baths', '2.5 baths', '0 shared baths', '0 baths',
'5 baths', 'Private half-bath', 'Shared half-bath', '4.5 baths',
'5.5 baths', '2.5 shared baths', '3.5 baths', '15.5 baths',
'6 baths', '4 baths', '3 shared baths', '4 shared baths',
'3.5 shared baths', '6 shared baths', '6.5 shared baths',
'6.5 baths', '4.5 shared baths', '7.5 baths', '5.5 shared baths',
'7 baths', '8 shared baths', '5 shared baths', '8 baths',
'10 baths', '7 shared baths'], dtype=object)
如果我使用 Count Vectorize 将它们转换为一种热编码,
vectorizer = CountVectorizer()
vectorizer.fit(X_train[colname].values)
我收到以下错误:
AttributeError: 'float' object has no attribute 'lower'
请告知错误原因。
我可以用 :
代替它吗
pd.Categorical(_DF_LISTING_EDA.bathrooms_text).codes
One hot编码和pd.categorical.code有什么区别?
谢谢
阿米特·莫迪
- CountVectorizer不是One hot encoding
- Pandas Categorical不是One hot encoding
如果你想要使用 pandas 的一种热编码,你可以这样做:
pandas.get_dummies(X_train[colname])[0]
我正在处理一些问题,有如下疑问:
在数据集中有一个文本列具有以下唯一值:
array(['1 bath', 'na', '1 shared bath', '1.5 baths', '1 private bath',
'2 baths', '1.5 shared baths', '3 baths', 'Half-bath',
'2 shared baths', '2.5 baths', '0 shared baths', '0 baths',
'5 baths', 'Private half-bath', 'Shared half-bath', '4.5 baths',
'5.5 baths', '2.5 shared baths', '3.5 baths', '15.5 baths',
'6 baths', '4 baths', '3 shared baths', '4 shared baths',
'3.5 shared baths', '6 shared baths', '6.5 shared baths',
'6.5 baths', '4.5 shared baths', '7.5 baths', '5.5 shared baths',
'7 baths', '8 shared baths', '5 shared baths', '8 baths',
'10 baths', '7 shared baths'], dtype=object)
如果我使用 Count Vectorize 将它们转换为一种热编码,
vectorizer = CountVectorizer()
vectorizer.fit(X_train[colname].values)
我收到以下错误:
AttributeError: 'float' object has no attribute 'lower'
请告知错误原因。
我可以用 :
代替它吗pd.Categorical(_DF_LISTING_EDA.bathrooms_text).codes
One hot编码和pd.categorical.code有什么区别?
谢谢 阿米特·莫迪
- CountVectorizer不是One hot encoding
- Pandas Categorical不是One hot encoding
如果你想要使用 pandas 的一种热编码,你可以这样做:
pandas.get_dummies(X_train[colname])[0]