gpt4 book ai didi

machine-learning - 在机器学习中使用 Scikit 对邮政编码进行特征散列

转载 作者:行者123 更新时间:2023-12-04 07:44:10 24 4
gpt4 key购买 nike

我正在研究一个机器学习问题,我的数据集中有很多邮政编码(~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在转换后的数据中应该是一个线索。我想您还会发现许多列都是全零的。
来自 the documentation ,

Each sample must be iterable...


所以哈希器正在处理邮政编码 '86916'作为元素的集合 8 , 6 , 9 , 1 , 6 ,并且您只会得到十个非零列(第一列可能是 6 ,它出现了两次,如开头所述)。您应该能够通过将输入 reshape 为二维来纠正此问题。

关于machine-learning - 在机器学习中使用 Scikit 对邮政编码进行特征散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67283777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com