我正在研究一个机器学习问题,我的数据集中有很多邮政编码(~8k 唯一值)。因此,我决定将这些值散列到更小的特征空间中,而不是使用 OHE 之类的东西。
我遇到的问题是我的哈希中唯一行的比例非常小(20%),这基本上意味着根据我的理解,我有很多重复/冲突。尽管我将哈希表中的特征增加到约 200 个,但我从未获得超过 20% 的唯一值。这对我来说没有意义,因为随着散列中的列数量不断增加,应该有可能出现更多独特的组合
我使用以下代码使用 scikit 对邮政编码进行哈希处理,并根据最后一个数组中的唯一值计算冲突:
from sklearn.feature_extraction import FeatureHasher
D = pd.unique(Daten["PLZ"])
print("Zipcode Data:", D,"\nZipcode Shape:", D.shape)
h = FeatureHasher(n_features=2**5, input_type="string")
f = h.transform(D)
f = f.toarray()
print("Feature Array:\n",f ,"\nFeature Shape:", f.shape)
unq = np.unique(f, axis=0)
print("Unique values:\n",unq,"\nUnique Shape:",unq.shape)
print("Percentage of unique values in hash array:",unq.shape[0]/f.shape[0]*100)
对于我收到的输出:
Zipcode Data: ['86916' '01445' '37671' ... '82387' '83565' '83550']
Zipcode Shape: (8158,)
Feature Array:
[[ 2. 1. 0. ... 0. 0. 0.]
[ 0. -1. 0. ... 0. 0. 0.]
[ 1. 0. 0. ... 0. 0. 0.]
...
[ 0. 0. 0. ... 0. 0. 0.]
[ 1. 0. 0. ... 0. 0. 0.]
[ 0. -1. 0. ... 0. 0. 0.]]
Feature Shape: (8158, 32)
Unique values:
[[ 0. -3. 0. ... 0. 0. 0.]
[ 0. -2. 0. ... 0. 0. 0.]
[ 0. -2. 0. ... 0. 0. 0.]
...
[ 4. 0. 0. ... 0. 0. 0.]
[ 4. 0. 0. ... 0. 0. 0.]
[ 4. 0. 0. ... 0. 0. 0.]]
Unique Shape: (1707, 32)
Percentage of unique values in hash array: 20.9242461387595
非常感谢任何帮助和见解。
最佳答案
转换后的数据中的第一个 2
应该是一条线索。我想您还会发现许多列全为零。
Each sample must be iterable...
因此,哈希器将邮政编码 '86916'
视为元素 8
、6
的集合 、 9
、1
、6
,并且您只得到十个非零列(第一列大概是 6
,出现两次,如开头所述)。您应该能够通过将输入 reshape 为二维来纠正此问题。
关于machine-learning - 在机器学习中使用 Scikit 对邮政编码进行特征哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67283777/