在 Python 中使用 factorize() 后如何获取原始值?

How to get original values after using factorize() in Python?

我是一名初学者,尝试使用训练和测试数据集在 Python 中使用随机森林创建预测模型。 train["ALLOW/BLOCK"] 可以从 4 个期望值(所有字符串)中取 1 个。 test["ALLOW/BLOCK"] 是需要预测的。

y,_ = pd.factorize(train["ALLOW/BLOCK"])

y
Out[293]: array([0, 1, 0, ..., 1, 0, 2], dtype=int64)

我使用 predict 进行预测。

clf.predict(test[features])

clf.predict(test[features])[0:10]
Out[294]: array([0, 0, 0, 0, 0, 2, 2, 0, 0, 0], dtype=int64)

如何获取原始值而不是数字值?以下代码实际上是在比较实际值和预测值吗?

z,_= pd.factorize(test["AUDIT/BLOCK"])

z==clf.predict(test[features])
Out[296]: array([ True, False, False, ..., False, False, False], dtype=bool) 

首先需要将pd.factorize返回的label保存如下:

y, label = pd.factorize(train["ALLOW/BLOCK"])

然后在你得到数值预测后,你可以通过label[pred]:

提取相应的标签
pred = clf.predict(test[features])
pred_label = label[pred]

pred_label 包含具有原始值的预测。


不,您不应该重新分解测试预测,因为标签很可能会有所不同。考虑以下示例:

pd.factorize(['a', 'b', 'c'])
# (array([0, 1, 2]), array(['a', 'b', 'c'], dtype=object))

pd.factorize(['c', 'a', 'b'])
# (array([0, 1, 2]), array(['c', 'a', 'b'], dtype=object))

所以标签取决于元素的顺序。