Par*_*rat 6 python prediction pandas random-forest
我是一个初学者,尝试使用 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)
Run Code Online (Sandbox Code Playgroud)
我用于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)
Run Code Online (Sandbox Code Playgroud)
如何获得原始值而不是数字值?以下代码实际上是在比较实际值和预测值吗?
z,_= pd.factorize(test["AUDIT/BLOCK"])
z==clf.predict(test[features])
Out[296]: array([ True, False, False, ..., False, False, False], dtype=bool)
Run Code Online (Sandbox Code Playgroud)
首先,您需要按如下方式保存label返回的pd.factorize内容:
y, label = pd.factorize(train["ALLOW/BLOCK"])
Run Code Online (Sandbox Code Playgroud)
然后在获得数字预测后,您可以通过label[pred]以下方式提取相应的标签:
pred = clf.predict(test[features])
pred_label = label[pred]
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
所以标签取决于元素的顺序。
| 归档时间: |
|
| 查看次数: |
2785 次 |
| 最近记录: |