【问题标题】:Predicting from SciKitLearn RandomForestClassification with Categorical Data从 SciKit Learn Random Forest Classification with Categorical Data 进行预测
【发布时间】:2023-03-21 01:31:01
【问题描述】:

我使用 SkLearn 使用 10 个不同的文本特征和 10000 个训练集创建了一个 RandomForestClassification 模型。然后,我对模型 (76mb) 进行了腌制,希望将其用于预测。

但是,为了生成随机森林,我使用了 LabelEncoder 和 OneHotEncoder 以获得分类/字符串数据的最佳结果。

现在,我想调出腌制模型并获得对 1 个实例的分类预测。但是,我不确定如何在不加载整个训练和测试数据集 CSV 的情况下对 1 个实例上的文本进行编码 再次完成整个编码过程。

每次加载 csv 文件似乎很费力。我希望它每小时运行 1000 倍,所以我觉得它不合适。

有没有办法在给定泡菜或其他变量/设置的情况下快速编码 1 行数据?编码是否总是需要所有数据?

如果需要加载所有训练数据来对单行进行编码,则最好将文本数据自己编码到数据库中,其中每个特征都分配给一个表,并使用数字 id 和 UNIQUE 键自动递增文本/分类字段,然后将此 id 传递给 RandomForestClassification?显然我需要重新调整和腌制这个新模型,但是我会确切地知道新行的(编码)数字表示,并简单地请求对这些值进行预测。

我很可能遗漏了某个功能或误解了 SkLearn 或 Python,我在 3 天前才开始使用这两个功能。请原谅我的幼稚。

【问题讨论】:

  • 你应该腌制你的 LabelEncoder 对象...

标签: python machine-learning scikit-learn random-forest text-classification


【解决方案1】:

使用Pickle,您应该保存您的标签和一个热编码器。然后,您可以每次阅读并轻松转换新实例。例如,

import cPickle as pickle
from sklearn.externals import joblib
from sklearn import preprocessing

le = preprocessing.LabelEncoder()
train_x = [0,1,2,6,'true','false']
le.fit_transform(train_x)

# Save your encoding
joblib.dump(le, '/path/to/save/model')
# OR
pickle.dump(le, open( '/path/to/model', "wb" ) )

# Load those encodings
le = joblib.load('/path/to/save/model') 
# OR
le = pickle.load( open( '/path/to/model', "rb" ) )

# Then use as normal
new_x = [0,0,0,2,2,2,'false']
le.transform(new_x)
# array([0, 0, 0, 1, 1, 1, 3])

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 2014-06-27
    • 2014-08-04
    • 2013-05-31
    • 2016-04-01
    • 2015-08-01
    • 2018-01-14
    • 2015-12-19
    • 2018-01-09
    相关资源
    最近更新 更多