存储决策树代码/算法

时间:2015-03-10 04:41:07

标签: python machine-learning supervised-learning

我正在计划使用机器学习(特别是监督学习,例如决策树)。最终的代码将在没有scikit学习或其他外部库的教学助手的电脑上运行。

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

总结:当给出一组标记的训练数据时,如何将最终算法存储在python代码中,而不依赖外部库来运行最终算法?

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用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

相关问题