【问题标题】:Store Decision Tree Code / Algorithm存储决策树代码/算法
【发布时间】:2015-05-11 10:52:33
【问题描述】:

我计划使用机器学习(尤其是监督学习,例如决策树)来完成一项任务。最终代码将在没有 scikit learn 或其他外部库的助教电脑上运行。

因此,我需要从头开始编写决策树分类器之类的东西,或者在本地使用外部库并存储最终算法。

总结一下:给定一组带标签的训练数据,如何在python代码中存储最终算法,以后不依赖外部库运行最终算法?

例如,决策树可以分解为一系列 if/then 语句,我想生成这些 if/then 语句并存储它们,以便它可以在除了 python 之外没有安装任何东西的计算机上运行。

关于如何实现这一目标的最佳建议是什么。如果这是在错误的论坛,请告知。

【问题讨论】:

  • 我认为这是正确的论坛,因为您的问题主要涉及用语言 (Python) 实现机器学习算法。
  • 我不确定问题是什么。是“如何封装我的依赖项,以便代码可以在另一台计算机上运行而无需安装任何东西(除了 python)?”?
  • 是的,基本上我想要一系列用于决策树算法的 if/then 语句(例如)。

标签: python machine-learning supervised-learning


【解决方案1】:

可以使用Python随机森林包sklearn.ensemble如下:

# Import the random forest package
from sklearn.ensemble import RandomForestClassifier 

# create a random forest object with 100 trees
forest = RandomForestClassifier(n_estimators = 100)

predictors = [[0, 0], [1, 1]]
response = [0, 1]

# fit the model to the training data
forest = forest.fit(predictors, response)

# you can reuse the forest model you built to make predictions
# on other data sets
test_data = [[0, 1], [1, 0]]
output = forest.predict(test_data)

请注意,我在此处导入了 RandomForestClassifier,但如果您想在回归模式下运行随机森林,您也可以使用 RandomForestRegressor

【讨论】:

    猜你喜欢
    • 2011-03-22
    • 2013-05-19
    • 2014-03-14
    • 2011-05-06
    • 2018-12-12
    • 2018-12-20
    • 1970-01-01
    • 2016-02-23
    • 2018-08-14
    相关资源
    最近更新 更多