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

标签 machine-learning scikit-learn hash feature-extraction

我正在研究一个机器学习问题,我的数据集中有很多邮政编码(~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' 视为元素 86集合916,并且您只得到十个非零列(第一列大概是 6 ,出现两次,如开头所述)。您应该能够通过将输入 reshape 为二维来纠正此问题。

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

相关文章:

machine-learning - LibSVM - 具有不平衡数据的多类分类

python - 如何在多变量/3D 中实现核密度估计

python - “numpy.ndarray”对象没有属性 'columns'

hash - 当 SHA-512 更安全时,为什么要使用 SHA1 来散列 secret ?

python - 使用 DNNRegressor 的损失函数是什么?

python - ValueError : Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, 发现 ndim=2。收到的完整形状 : [None, 2584]

python-3.x - Keras 方法 'predict' 和 'predict_generator' 具有不同的结果

scikit-learn - 拟合模型时更改默认 RandomForestClassifier 的 "score"函数?

algorithm - 分词算法

php - 为什么我的哈希值总是全 0?