预编译 Python 脚本并将其包含在另一个 Python 程序中

Precompile Python Script and Include it in another Python Program

我一直在 Python 中使用决策树分类器进行学习算法。

from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier()
clf.fit(train, train_label)
predicted_label = clf.predict(test)

决策树分类器接受来自大型文本文件的训练标签。 我想 运行 该程序而无需再次执行培训过程。我将如何在 Python 中完成?我将如何包含预编译的学习模型并将其用于另一个程序中的测试?预编译的python文件有什么关系吗?

训练模型后,您可以保存模型以备将来使用,避免训练过程。

import pickle
model.fit(X,y)
saved_model = pickle.dump(model,open('saved_model.sav', 'wb'))#save your model
.
.
.

model = pickle.loads(open('saved_model.sav', 'rb'))#get your model from saved model file
model.predict(X[0:1])#use without training