在机器学习中使用 Scikit 对邮政编码进行哈希处理
Feature Hashing of zip codes with Scikit in machine learning
我正在处理一个机器学习问题,我的数据集中有很多邮政编码(~8k 唯一值)。因此我决定将这些值散列成一个更小的特征 space 而不是使用像 OHE 这样的东西。
我遇到的问题是我的散列中唯一行的百分比非常小 (20%),这基本上意味着根据我的理解,我有很多 duplicates/collisions。即使我将哈希 table 中的特征增加到 ~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
,它出现了两次,如开头所述)。您应该能够通过将输入重塑为二维来纠正此问题。
我正在处理一个机器学习问题,我的数据集中有很多邮政编码(~8k 唯一值)。因此我决定将这些值散列成一个更小的特征 space 而不是使用像 OHE 这样的东西。
我遇到的问题是我的散列中唯一行的百分比非常小 (20%),这基本上意味着根据我的理解,我有很多 duplicates/collisions。即使我将哈希 table 中的特征增加到 ~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
,它出现了两次,如开头所述)。您应该能够通过将输入重塑为二维来纠正此问题。