【发布时间】:2021-07-20 20:07:51
【问题描述】:
我正在研究一个机器学习问题,我的数据集中有很多邮政编码(约 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
非常感谢任何帮助和见解。
【问题讨论】:
标签: machine-learning scikit-learn hash feature-extraction