sklearn中出现不一致的“'float'object is not iterable”错误

2024-03-29 11:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道有很多问题都是同一个问题,但没有一个能解决我的问题。我在amazonsagemaker中使用Jupyter笔记本,我想对一些特性使用散列技巧。我还不能用简单的数据做一个可复制的例子,但这里有一个我拥有的数据屏幕: enter image description here

所以我用了:

from sklearn.feature_extraction import FeatureHasher
h = FeatureHasher(n_features=10,input_type="string")
df['country_iso_code'] = h.transform(df['country_iso_code'])
h = FeatureHasher(n_features=10,input_type="string")
df['origen_tarjeta_country_iso'] = h.transform(df['origen_tarjeta_country_iso'])

第一个转换工作,但第二个没有,我得到的'浮'对象是不可编辑的错误。我已经检查了两列的类型,它们都是对象,而且我还检查了两列中只有字符串。我尝试用很少的样本在Spyder中复制代码,它可以工作:

import pandas as pd
from sklearn.feature_extraction import FeatureHasher

df = pd.DataFrame({'ES':'ES','UK':'UK'},index=[0,1])
h = FeatureHasher(n_features=10,input_type="string")
df['UK'] = h.transform(df['UK'])
h = FeatureHasher(n_features=10,input_type="string")
df['ES'] = h.transform(df['ES'])

Tags: 数据fromimportdfinputstringestype